Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / daoImp / PolicyVersionDaoImpl.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.hibernate.Criteria;
28 import org.hibernate.Query;
29 import org.hibernate.Session;
30 import org.hibernate.SessionFactory;
31 import org.hibernate.Transaction;
32 import org.hibernate.criterion.Restrictions;
33 import org.openecomp.policy.dao.PolicyVersionDao;
34 import org.openecomp.policy.rest.jpa.PolicyVersion;
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("PolicyVersionDao")
44 public class PolicyVersionDaoImpl implements PolicyVersionDao {
45         private static final Logger logger = FlexLogger.getLogger(PolicyVersionDaoImpl.class);
46         @Autowired
47         SessionFactory sessionfactory;
48         
49         @SuppressWarnings("unchecked")
50         @Override
51         public List<PolicyVersion> getPolicyVersionData() {
52                 Session session = sessionfactory.openSession();
53                 Transaction tx = session.beginTransaction();
54                 List<PolicyVersion> versionData = null;
55         try {
56                 Criteria cr = session.createCriteria(PolicyVersion.class);
57             versionData = cr.list();
58                         tx.commit();
59                 } catch (Exception e) {
60                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyVersion 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 versionData;
69                 
70         }
71
72         @SuppressWarnings("unchecked")
73         @Override
74         public List<String> getPolicyVersionDataByName() {
75                 Session session = sessionfactory.openSession();
76                 Transaction tx = session.beginTransaction();
77                 List<String> data = new ArrayList<String>();
78         try {
79                 Criteria cr = session.createCriteria(PolicyVersion.class);
80             List<PolicyVersion> attributeData = cr.list();  
81             for(int i = 0; i < attributeData.size(); i++){
82                  data.add(attributeData.get(i).getPolicyName());
83             }
84                         tx.commit();
85                 } catch (Exception e) {
86                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyVersion 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(PolicyVersion policyVersion) {
99                 Session session = sessionfactory.openSession();
100                 Transaction tx = session.beginTransaction();
101                 try {
102                         session.persist(policyVersion);
103                         tx.commit();    
104                 }catch(Exception e){
105                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving PolicyVersion 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         @Override
116         public void delete(PolicyVersion policyVersion) {
117                 Session session = sessionfactory.openSession();
118                 Transaction tx = session.beginTransaction();
119                 try {
120                         session.delete(policyVersion);
121                         tx.commit();    
122                 }catch(Exception e){
123                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting PolicyVersion Table"+e);    
124                 }finally{
125                         try{
126                                 session.close();
127                         }catch(Exception e1){
128                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
129                         }
130                 }
131         }
132
133         @Override
134         public void update(PolicyVersion policyVersion) {
135                 Session session = sessionfactory.openSession();
136                 Transaction tx = session.beginTransaction();
137                 try {
138                         session.update(policyVersion);
139                         tx.commit();    
140                 }catch(Exception e){
141                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating PolicyVersion Table"+e);    
142                 }finally{
143                         try{
144                                 session.close();
145                         }catch(Exception e1){
146                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
147                         }
148                 }
149         }
150
151         @SuppressWarnings("unchecked")
152         @Override
153         public List<PolicyVersion> getPolicyVersionEntityByName(String policyVersion) {
154                 Session session = sessionfactory.openSession();
155                 Transaction tx = session.beginTransaction();
156                 List<PolicyVersion> data = null;
157                 try {
158                         Criteria cr = session.createCriteria(PolicyVersion.class);
159                         cr.add(Restrictions.eq("policyName",policyVersion));
160                         data = cr.list();
161                         tx.commit();
162                 } catch (Exception e) {
163                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyVersion Table"+e);    
164                 }finally{
165                         try{
166                                 session.close();
167                         }catch(Exception e1){
168                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
169                         }
170                 }
171                 return data;
172         }
173
174         @SuppressWarnings("unchecked")
175         @Override
176         public List<PolicyVersion> getActiveVersionPolicy(String query) {
177                 Session session = sessionfactory.openSession();
178                 Transaction tx = session.beginTransaction();
179                 List<PolicyVersion> data = null;
180                 try {
181                         Query hbquery = session.createQuery(query);
182                         data = hbquery.list();
183                         tx.commit();
184                 } catch (Exception e) {
185                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyVersion Table"+e);    
186                 }finally{
187                         try{
188                                 session.close();
189                         }catch(Exception e1){
190                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
191                         }
192                 }
193                 return data;
194         }
195         
196         @Override
197         public void updateQuery(String policyVersion) {
198                 Session session = sessionfactory.openSession();
199                 Transaction tx = session.beginTransaction();    
200                 try {
201                         Query hbquery = session.createQuery(policyVersion);
202                         hbquery.executeUpdate();
203                         tx.commit();
204                 } catch (Exception e) {
205                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating PolicyVersion Table"+e);    
206                 }finally{
207                         try{
208                                 session.close();
209                         }catch(Exception e1){
210                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
211                         }
212                 }
213                 
214         }
215
216 }