Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / daoImp / ServiceGroupDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.policy.daoImp;
22
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 //import org.apache.commons.logging.Log;
28 //import org.apache.commons.logging.LogFactory;
29 import org.hibernate.Criteria;
30 import org.hibernate.Session;
31 import org.hibernate.SessionFactory;
32 import org.hibernate.Transaction;
33 import org.openecomp.policy.rest.dao.ServiceGroupDao;
34 import org.openecomp.policy.rest.jpa.GroupServiceList;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37
38 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
39
40 import org.openecomp.policy.common.logging.flexlogger.FlexLogger; 
41 import org.openecomp.policy.common.logging.flexlogger.Logger;
42
43 @Service("ServiceGroupDao")
44 public class ServiceGroupDaoImpl implements ServiceGroupDao{
45         private static final Logger logger = FlexLogger.getLogger(ServiceGroupDaoImpl.class);
46         @Autowired
47         SessionFactory sessionfactory;
48         
49         @SuppressWarnings("unchecked")
50         @Override
51         public List<GroupServiceList> getGroupServiceListData() {
52                 Session session = sessionfactory.openSession();
53                 Transaction tx = session.beginTransaction();
54                 List<GroupServiceList> attributeData = null;
55         try {
56                 Criteria cr = session.createCriteria(GroupServiceList.class);
57             attributeData = cr.list();
58                         tx.commit();
59                 } catch (Exception e) {
60                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying GroupServiceList Table"+e); 
61                 }finally{
62                         try{
63                                 session.close();
64                         }catch(Exception e1){
65                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
66                         }
67                 }
68                 return attributeData;
69                 
70         }
71
72         @Override
73         public void Save(GroupServiceList attribute) {
74                 Session session = sessionfactory.openSession();
75                 Transaction tx = session.beginTransaction();
76                 try {
77                         session.persist(attribute);
78                         tx.commit();    
79                 }catch(Exception e){
80                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving GroupServiceList Table"+e);   
81                 }finally{
82                         try{
83                                 session.close();
84                         }catch(Exception e1){
85                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
86                         }
87                 }
88                 
89         }
90
91         @Override
92         public void delete(GroupServiceList attribute) {
93                 Session session = sessionfactory.openSession();
94                 Transaction tx = session.beginTransaction();
95                 try {
96                         session.delete(attribute);
97                         tx.commit();    
98                 }catch(Exception e){
99                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting GroupServiceList Table"+e); 
100                 }finally{
101                         try{
102                                 session.close();
103                         }catch(Exception e1){
104                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
105                         }
106                 }
107         }
108
109         @Override
110         public void update(GroupServiceList attribute) {
111                 Session session = sessionfactory.openSession();
112                 Transaction tx = session.beginTransaction();
113                 try {
114                         session.update(attribute);
115                         tx.commit();    
116                 }catch(Exception e){
117                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating GroupServiceList Table"+e); 
118                 }finally{
119                         try{
120                                 session.close();
121                         }catch(Exception e1){
122                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
123                         }
124                 }
125                 
126         }
127
128         @SuppressWarnings("unchecked")
129         @Override
130         public List<String> getGroupServiceDataByName() {
131                 Session session = sessionfactory.openSession();
132                 Transaction tx = session.beginTransaction();
133                 List<String> data = new ArrayList<String>();
134         try {
135                 Criteria cr = session.createCriteria(GroupServiceList.class);
136             List<GroupServiceList> attributeData = cr.list();      
137             for(int i = 0; i < attributeData.size(); i++){
138                  data.add(attributeData.get(i).getGroupName());
139             }
140                         tx.commit();
141                 } catch (Exception e) {
142                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying GroupServiceList Table"+e); 
143                 }finally{
144                         try{
145                                 session.close();
146                         }catch(Exception e1){
147                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
148                         }
149                 }
150                 return data;
151         }
152
153 }