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