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