Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / daoimpl / DescriptiveScopeDaoImpl.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.hibernate.criterion.Restrictions;
33 import org.openecomp.policy.pap.xacml.rest.HibernateSession;
34 import org.openecomp.policy.rest.dao.DescriptiveScopeDao;
35 import org.openecomp.policy.rest.jpa.DescriptiveScope;
36 import org.openecomp.policy.rest.jpa.PolicyVersion;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Service;
39
40 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
41
42 @Service("DescriptiveScopeDao")
43 public class DescriptiveScopeDaoImpl implements DescriptiveScopeDao{
44         private static final Log logger = LogFactory.getLog(DescriptiveScopeDaoImpl.class);
45         @Autowired
46         SessionFactory sessionfactory;
47         
48         public SessionFactory getSessionfactory() {
49                 return sessionfactory;
50         }
51
52         public void setSessionfactory(SessionFactory sessionfactory) {
53                 this.sessionfactory = sessionfactory;
54         }
55
56         @SuppressWarnings("unchecked")
57         @Override
58         public List<DescriptiveScope> getDescriptiveScope() {
59                 Session session = HibernateSession.getSessionFactory();
60                 Transaction tx = session.beginTransaction();
61         List<DescriptiveScope> descriptiveScopeData = null;
62         try {
63                 Criteria cr = session.createCriteria(DescriptiveScope.class);
64                 descriptiveScopeData = cr.list();
65                         tx.commit();
66                 } catch (Exception e) {
67                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying DescriptiveScope Table"+e); 
68                 }finally{
69                         try{
70                                 session.close();
71                         }catch(Exception e1){
72                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
73                         }
74                 }
75                 return descriptiveScopeData;
76         }
77
78         @Override
79         public void Save(DescriptiveScope descriptiveScope) {
80                 Session session = HibernateSession.getSessionFactory();
81                 Transaction tx = session.beginTransaction();
82                 try {
83                         session.persist(descriptiveScope);
84                         tx.commit();    
85                 }catch(Exception e){
86                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving DescriptiveScope 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                 
95         }
96
97         @Override
98         public void delete(DescriptiveScope descriptiveScope) {
99                 Session session = HibernateSession.getSessionFactory();
100                 Transaction tx = session.beginTransaction();
101                 try {
102                         session.delete(descriptiveScope);
103                         tx.commit();    
104                 }catch(Exception e){
105                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting DescriptiveScope 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 update(DescriptiveScope descriptiveScope) {
118                 Session session = HibernateSession.getSessionFactory();
119                 Transaction tx = session.beginTransaction();
120                 try {
121                         session.update(descriptiveScope);
122                         tx.commit();    
123                 }catch(Exception e){
124                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating DescriptiveScope 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         @SuppressWarnings("unchecked")
136         @Override
137         public List<String> getDescriptiveScopeDataByName() {
138                 Session session = HibernateSession.getSessionFactory();
139                 Transaction tx = session.beginTransaction();    
140         List<String> data = new ArrayList<String>();  
141         try {
142                 Criteria cr = session.createCriteria(DescriptiveScope.class);
143             List<DescriptiveScope> descriptiveScopeData = cr.list();
144             for(int i = 0; i < descriptiveScopeData.size(); i++){
145                  data.add(descriptiveScopeData.get(i).getScopeName());
146            }
147                         tx.commit();
148                 } catch (Exception e) {
149                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying DescriptiveScope Table"+e); 
150                 }finally{
151                         try{
152                                 session.close();
153                         }catch(Exception e1){
154                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
155                         }
156                 }
157                 return data;
158         }
159
160         @Override
161         public DescriptiveScope getDescriptiveScopeById(String name) {
162                 Session session = HibernateSession.getSessionFactory();
163                 Transaction tx = session.beginTransaction();
164                 DescriptiveScope data = null;
165                 try {
166                         Criteria cr = session.createCriteria(DescriptiveScope.class);
167                         cr.add(Restrictions.eq("scopename",name));
168                         data = (DescriptiveScope) cr.list().get(0);
169                         tx.commit();
170                 } catch (Exception e) {
171                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying DescriptiveScope Table"+e); 
172                 }finally{
173                         try{
174                                 session.close();
175                         }catch(Exception e1){
176                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
177                         }
178                 }
179                 return data;
180         }
181
182 }