Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / daoimpl / EnforcerPolicyDaoImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
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.pap.xacml.rest.daoimpl;
22
23 import java.util.List;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.hibernate.Criteria;
28 import org.hibernate.Session;
29 import org.hibernate.SessionFactory;
30 import org.hibernate.Transaction;
31 import org.openecomp.policy.pap.xacml.rest.HibernateSession;
32 import org.openecomp.policy.rest.dao.EnforcerPolicyDao;
33 import org.openecomp.policy.rest.jpa.EnforcingType;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Service;
36
37 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
38
39 @Service("EnforcerPolicyDao")
40 public class EnforcerPolicyDaoImpl implements EnforcerPolicyDao {
41         private static final Log logger = LogFactory.getLog(EnforcerPolicyDaoImpl.class);
42         @Autowired
43         SessionFactory sessionfactory;
44         
45         public SessionFactory getSessionfactory() {
46                 return sessionfactory;
47         }
48
49         public void setSessionfactory(SessionFactory sessionfactory) {
50                 this.sessionfactory = sessionfactory;
51         }
52         
53         @SuppressWarnings("unchecked")
54         @Override
55         public List<EnforcingType> getEnforcingTypeData() {
56                 Session session = HibernateSession.getSessionFactory();
57                 Transaction tx = session.beginTransaction();
58                 List<EnforcingType> enforcingTypeData = null;
59         try {
60                 Criteria cr = session.createCriteria(EnforcingType.class);
61             enforcingTypeData = cr.list();
62                         tx.commit();
63                 } catch (Exception e) {
64                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying EnforcingType Table"+e);    
65                 }finally{
66                         try{
67                                 session.close();
68                         }catch(Exception e1){
69                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
70                         }
71                 }
72                 return enforcingTypeData;
73                 
74         }
75
76         @Override
77         public void Save(EnforcingType enforcingType) {
78                 Session session = HibernateSession.getSessionFactory();
79                 Transaction tx = session.beginTransaction();
80                 try {
81                         session.persist(enforcingType);
82                         tx.commit();    
83                 }catch(Exception e){
84                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving EnforcingType Table"+e);      
85                 }finally{
86                         try{
87                                 session.close();
88                         }catch(Exception e1){
89                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
90                         }
91                 }
92                 
93         }
94
95         @Override
96         public void delete(EnforcingType enforcingType) {
97                 Session session = HibernateSession.getSessionFactory();
98                 Transaction tx = session.beginTransaction();
99                 try {
100                         session.delete(enforcingType);
101                         tx.commit();    
102                 }catch(Exception e){
103                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting EnforcingType Table"+e);    
104                 }finally{
105                         try{
106                                 session.close();
107                         }catch(Exception e1){
108                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
109                         }
110                 }
111         }
112
113         @Override
114         public void update(EnforcingType enforcingType) {
115                 Session session = HibernateSession.getSessionFactory();
116                 Transaction tx = session.beginTransaction();
117                 try {
118                         session.update(enforcingType);
119                         tx.commit();    
120                 }catch(Exception e){
121                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating EnforcingType Table"+e);    
122                 }finally{
123                         try{
124                                 session.close();
125                         }catch(Exception e1){
126                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
127                         }
128                 }
129                 
130         }
131
132 }