Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / daoImp / GroupPolicyScopeListDaoImpl.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.hibernate.criterion.Restrictions;
34 import org.openecomp.policy.rest.dao.GroupPolicyScopeListDao;
35 import org.openecomp.policy.rest.jpa.GroupPolicyScopeList;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Service;
38
39 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
40
41 import org.openecomp.policy.common.logging.flexlogger.FlexLogger; 
42 import org.openecomp.policy.common.logging.flexlogger.Logger;
43
44 @Service("GroupPolicyScopeListDao")
45 public class GroupPolicyScopeListDaoImpl implements GroupPolicyScopeListDao {
46         private static final Logger logger = FlexLogger.getLogger(GroupPolicyScopeListDaoImpl.class);
47         @Autowired
48         SessionFactory sessionfactory;
49         
50         @SuppressWarnings("unchecked")
51         @Override
52         public List<GroupPolicyScopeList> getGroupPolicyScopeListData() {
53                 Session session = sessionfactory.openSession();
54                 Transaction tx = session.beginTransaction();
55                  List<GroupPolicyScopeList> attributeData = null;
56         try {
57                 Criteria cr = session.createCriteria(GroupPolicyScopeList.class);
58             attributeData = cr.list();
59                         tx.commit();
60                 } catch (Exception e) {
61                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying GroupPolicyScopeList Table"+e);     
62                 }finally{
63                         try{
64                                 session.close();
65                         }catch(Exception e1){
66                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
67                         }
68                 }
69                 return attributeData;
70         }
71
72         @SuppressWarnings("unchecked")
73         @Override
74         public List<String> getGroupPolicyScopeListDataByName() {
75                 Session session = sessionfactory.openSession();
76                 Transaction tx = session.beginTransaction();
77                 List<String> data = new ArrayList<String>();
78                 try {
79                         Criteria cr = session.createCriteria(GroupPolicyScopeList.class);
80                         List<GroupPolicyScopeList> attributeData = cr.list();           
81                         for(int i = 0; i < attributeData.size(); i++){
82                                 data.add(attributeData.get(i).getGroupName());
83                         }
84                         tx.commit();
85                 } catch (Exception e) {
86                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying GroupPolicyScopeList Table"+e);     
87                 }finally{
88                         try{
89                                 session.close();
90                         }catch(Exception e1){
91                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
92                         }
93                 }
94                 return data;
95         }
96
97         @Override
98         public void Save(GroupPolicyScopeList attribute) {
99                 Session session = sessionfactory.openSession();
100                 Transaction tx = session.beginTransaction();
101                 try {
102                         session.persist(attribute);
103                         tx.commit();    
104                 }catch(Exception e){
105                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving GroupPolicyScopeList Table"+e);       
106                 }finally{
107                         try{
108                                 session.close();
109                         }catch(Exception e1){
110                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
111                         }
112                 }
113                 
114         }
115
116         @Override
117         public void delete(GroupPolicyScopeList attribute) {
118                 Session session = sessionfactory.openSession();
119                 Transaction tx = session.beginTransaction();
120                 try {
121                         session.delete(attribute);
122                         tx.commit();    
123                 }catch(Exception e){
124                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting Attribute Table"+e);        
125                 }finally{
126                         try{
127                                 session.close();
128                         }catch(Exception e1){
129                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
130                         }
131                 }
132                 
133         }
134
135         @Override
136         public void update(GroupPolicyScopeList attribute) {
137                 Session session = sessionfactory.openSession();
138                 Transaction tx = session.beginTransaction();
139                 try {
140                         session.update(attribute);
141                         tx.commit();    
142                 }catch(Exception e){
143                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating Attribute Table"+e);        
144                 }finally{
145                         try{
146                                 session.close();
147                         }catch(Exception e1){
148                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
149                         }
150                 }
151                 
152         }
153         
154         @SuppressWarnings("unchecked")
155         @Override
156         public List<GroupPolicyScopeList> CheckDuplicateEntry(String value) {
157                 Session session = sessionfactory.openSession();
158                 Transaction tx = session.beginTransaction();
159                 List<GroupPolicyScopeList> data = null;
160                 try {
161                         Criteria cr = session.createCriteria(GroupPolicyScopeList.class);
162                         cr.add(Restrictions.eq("name",value));
163                         data = cr.list();
164                         tx.commit();
165                 } catch (Exception e) {
166                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying GroupPolicyScopeList Table"+e);     
167                 }finally{
168                         try{
169                                 session.close();
170                         }catch(Exception e1){
171                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
172                         }
173                 }
174                 return data;
175         }
176
177 }