Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / daoimpl / AttributeDaoImpl.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.ArrayList;
24 import java.util.List;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.hibernate.Criteria;
29 import org.hibernate.Session;
30 import org.hibernate.SessionFactory;
31 import org.hibernate.Transaction;
32 import org.openecomp.policy.pap.xacml.rest.HibernateSession;
33 import org.openecomp.policy.rest.dao.AttributeDao;
34 import org.openecomp.policy.rest.jpa.Attribute;
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 @Service("AttributeDao")
41 public class AttributeDaoImpl implements AttributeDao {
42         private static final Log logger = LogFactory.getLog(AttributeDaoImpl.class);
43         @Autowired
44         SessionFactory sessionfactory;
45         
46         public SessionFactory getSessionfactory() {
47                 return sessionfactory;
48         }
49
50         public void setSessionfactory(SessionFactory sessionfactory) {
51                 this.sessionfactory = sessionfactory;
52         }
53
54         @SuppressWarnings("unchecked")
55         @Override
56         public List<Attribute> getData() {
57                 Session session = HibernateSession.getSessionFactory();
58                 Transaction tx = session.beginTransaction();
59                 List<Attribute> attributeData = null;
60                 try {
61                         Criteria cr = session.createCriteria(Attribute.class);
62                         attributeData = cr.list();
63                         tx.commit();
64                 } catch (Exception e) {
65                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Attribute Table"+e);        
66                 }finally{
67                         try{
68                                 session.close();
69                         }catch(Exception e1){
70                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
71                         }
72                 }
73                 return attributeData;
74                 
75         }
76
77         @Override
78         public void Save(Attribute attribute) {
79                 Session session = HibernateSession.getSessionFactory();
80                 Transaction tx = session.beginTransaction();
81                 try {
82                         session.persist(attribute);
83                         tx.commit();    
84                 }catch(Exception e){
85                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving Attribute Table"+e);  
86                 }finally{
87                         try{
88                                 session.close();
89                         }catch(Exception e1){
90                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
91                         }
92                 }
93                 
94         }
95
96         @Override
97         public void delete(Attribute attribute) {
98                 Session session = HibernateSession.getSessionFactory();
99                 Transaction tx = session.beginTransaction();    
100                 try {
101                         session.delete(attribute);
102                         tx.commit();    
103                 }catch(Exception e){
104                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting Attribute Table"+e);        
105                 }finally{
106                         try{
107                                 session.close();
108                         }catch(Exception e1){
109                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
110                         }
111                 }
112         }
113
114         @Override
115         public void update(Attribute attribute) {
116                 Session session = HibernateSession.getSessionFactory();
117                 Transaction tx = session.beginTransaction();
118                 try {
119                         session.update(attribute);
120                         tx.commit();    
121                 }catch(Exception e){
122                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating Attribute Table"+e);        
123                 }finally{
124                         try{
125                                 session.close();
126                         }catch(Exception e1){
127                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
128                         }
129                 }
130         }
131
132         @SuppressWarnings("unchecked")
133         @Override
134         public List<String> getAttributeData() {
135                 Session session = HibernateSession.getSessionFactory();
136                 Transaction tx = session.beginTransaction();
137                 List<String> data = new ArrayList<String>();
138                 try {
139                         Criteria cr = session.createCriteria(Attribute.class);
140                         List<Attribute> attributeData = cr.list();
141                         for(int i = 0; i < attributeData.size(); i++){
142                                 data.add(attributeData.get(i).getXacmlId());
143                         }
144                         tx.commit();
145                 } catch (Exception e) {
146                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Attribute Table"+e);        
147                 }finally{
148                         try{
149                                 session.close();
150                         }catch(Exception e1){
151                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
152                         }
153                 }
154                 return data;
155         }
156
157 }