Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / daoImp / RolesDaoImpl.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.List;
25
26 import org.hibernate.Criteria;
27 import org.hibernate.Query;
28 import org.hibernate.Session;
29 import org.hibernate.SessionFactory;
30 import org.hibernate.Transaction;
31 import org.hibernate.criterion.Restrictions;
32 import org.openecomp.policy.dao.RolesDao;
33 import org.openecomp.policy.model.Roles;
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("RolesDao")
43 public class RolesDaoImpl  implements RolesDao{
44         private static final Logger logger = FlexLogger.getLogger(RolesDaoImpl.class);
45         @Autowired
46         SessionFactory sessionfactory;
47         
48         @Override
49         public void save(Roles role) {
50                 Session session = sessionfactory.openSession();
51                 Transaction tx = session.beginTransaction();
52                 try {
53                         session.persist(role);
54                         tx.commit();    
55                 }catch(Exception e){
56                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving Roles Table"+e);      
57                 }finally{
58                         try{
59                                 session.close();
60                         }catch(Exception e1){
61                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
62                         }
63                 }
64         }
65         
66         @Override
67         public void delete(Roles role) {
68                 Session session = sessionfactory.openSession();
69                 Transaction tx = session.beginTransaction();    
70                 try {
71                         System.out.println("delete from Roles where loginid = '"+role.getLoginId()+"'");
72                         Query q = session.createQuery(" delete from Roles where loginid = '"+role.getLoginId()+"'");
73                         q.executeUpdate();
74                         tx.commit();    
75                 }catch(Exception e){
76                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Deleting Roles Table"+e);    
77                 }finally{
78                         try{
79                                 session.close();
80                         }catch(Exception e1){
81                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
82                         }
83                 }
84         }
85
86         @SuppressWarnings("unchecked")
87         @Override
88         public List<Roles> getUserRoles(String userId) {
89                 System.out.println("User Id:"+userId);
90                 Session session = sessionfactory.openSession();
91                 Transaction tx = session.beginTransaction();
92                 List<Roles> rolesData = null;
93         try {
94                 Criteria cr = session.createCriteria(Roles.class);
95                 cr = cr.add(Restrictions.eq("loginId",userId));
96             rolesData = cr.list();
97                         tx.commit();
98                 } catch (Exception e) {
99                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying Roles Table"+e);    
100                 }finally{
101                         try{
102                                 session.close();
103                         }catch(Exception e1){
104                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
105                         }
106                 }
107                 return rolesData;
108         }
109 }