Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / daoImp / PolicyRolesDaoImpl.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 import java.util.List;
24
25 import org.hibernate.Criteria;
26 import org.hibernate.Session;
27 import org.hibernate.SessionFactory;
28 import org.hibernate.Transaction;
29 import org.hibernate.criterion.Conjunction;
30 import org.hibernate.criterion.Disjunction;
31 import org.hibernate.criterion.Restrictions;
32 import org.openecomp.policy.dao.PolicyRolesDao;
33 import org.openecomp.policy.rest.jpa.PolicyRoles;
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 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
40 import org.openecomp.policy.common.logging.flexlogger.Logger;
41
42 @Service("PolicyRolesDao")
43 public class PolicyRolesDaoImpl implements PolicyRolesDao{
44         private static final Logger logger = FlexLogger.getLogger(PolicyRolesDaoImpl.class);
45         @Autowired
46         SessionFactory sessionfactory;
47         
48         @SuppressWarnings("unchecked")
49         @Override
50         public List<PolicyRoles> getUserRoles() {
51                 Session session = sessionfactory.openSession();
52                 Transaction tx = session.beginTransaction();
53                 List<PolicyRoles> rolesData = null;
54                 try {
55                         Criteria cr = session.createCriteria(PolicyRoles.class);                
56                         Disjunction disjunction = Restrictions.disjunction(); 
57                         Conjunction conjunction1  = Restrictions.conjunction();
58                         conjunction1.add(Restrictions.eq("role", "admin"));
59                         Conjunction conjunction2  = Restrictions.conjunction();
60                         conjunction2.add(Restrictions.eq("role", "editor"));    
61                         Conjunction conjunction3  = Restrictions.conjunction();
62                         conjunction3.add(Restrictions.eq("role", "guest"));     
63                         disjunction.add(conjunction1);
64                         disjunction.add(conjunction2);
65                         disjunction.add(conjunction3);
66                         rolesData = cr.add(disjunction).list();
67                         tx.commit();
68                 } catch (Exception e) {
69                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyRoles Table"+e);      
70                 }finally{
71                         try{
72                                 session.close();
73                         }catch(Exception e1){
74                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
75                         }
76                 }
77                 return rolesData;
78         }
79
80         @Override
81         public void save(PolicyRoles role) {
82                 Session session = sessionfactory.openSession();
83                 Transaction tx = session.beginTransaction();
84                 try {
85                         session.persist(role);
86                         tx.commit();    
87                 }catch(Exception e){
88                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving PolicyRoles Table"+e);        
89                 }finally{
90                         try{
91                                 session.close();
92                         }catch(Exception e1){
93                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
94                         }
95                 }
96                 
97         }
98
99         @Override
100         public void delete(PolicyRoles role) {
101                 Session session = sessionfactory.openSession();
102                 Transaction tx = session.beginTransaction();
103                 try {
104                         session.delete(role);
105                         tx.commit();    
106                 }catch(Exception e){
107                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting PolicyRoles Table"+e);      
108                 }finally{
109                         try{
110                                 session.close();
111                         }catch(Exception e1){
112                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
113                         }
114                 }
115                 
116         }
117
118         @Override
119         public void update(PolicyRoles role) {
120                 Session session = sessionfactory.openSession();
121                 Transaction tx = session.beginTransaction();
122                 try {
123                         session.update(role);
124                         tx.commit();    
125                 }catch(Exception e){
126                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Updating PolicyRoles Table"+e);      
127                 }finally{
128                         try{
129                                 session.close();
130                         }catch(Exception e1){
131                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
132                         }
133                 }
134                 
135         }
136
137         @SuppressWarnings("unchecked")
138         @Override
139         public List<PolicyRoles> getRoles() {
140                 Session session = sessionfactory.openSession();
141                 Transaction tx = session.beginTransaction();
142                 List<PolicyRoles> rolesData = null;
143                 try {
144                         Criteria cr = session.createCriteria(PolicyRoles.class);                
145                         rolesData = cr.list();
146                         tx.commit();
147                 } catch (Exception e) {
148                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying PolicyRoles Table"+e);      
149                 }finally{
150                         try{
151                                 session.close();
152                         }catch(Exception e1){
153                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
154                         }
155                 }
156                 return rolesData;
157         }
158
159 }