Initial OpenECOMP policy/engine commit
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / daoimpl / UserInfoDaoImpl.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.List;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.hibernate.Criteria;
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.pap.xacml.rest.HibernateSession;
33 import org.openecomp.policy.rest.dao.UserInfoDao;
34 import org.openecomp.policy.rest.jpa.UserInfo;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Service;
37
38 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
39
40
41 @Service("UserInfoDao")
42 public class UserInfoDaoImpl implements UserInfoDao{
43         private static final Log logger = LogFactory.getLog(UserInfoDaoImpl.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         @Override
56         public void save(UserInfo userInfo) {
57                 Session session = HibernateSession.getSessionFactory();
58                 Transaction tx = session.beginTransaction();
59                 try {
60                         session.persist(userInfo);
61                         tx.commit();    
62                 }catch(Exception e){
63                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Saving UserInfo Table"+e);   
64                 }finally{
65                         try{
66                                 session.close();
67                         }catch(Exception e1){
68                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
69                         }
70                 }       
71         }
72
73         @SuppressWarnings("unchecked")
74         @Override
75         public List<UserInfo> getUserInfo() {
76                 System.out.println("UserInfoDaoImpl:  getUserInfo().. getting user info before save()");
77                 Session session = HibernateSession.getSessionFactory();
78                 Transaction tx = session.beginTransaction();
79                 List<UserInfo> userData = null;
80         try {
81                 Criteria cr = session.createCriteria(UserInfo.class);
82             userData = cr.list();
83                         tx.commit();
84                 } catch (Exception e) {
85                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying UserInfo Table"+e); 
86                 }finally{
87                         try{
88                                 session.close();
89                         }catch(Exception e1){
90                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
91                         }
92                 }
93                 return userData;
94         }
95
96         @Override
97         public String getUserName(String loginid) {
98                 Session session = HibernateSession.getSessionFactory();
99                 Transaction tx = session.beginTransaction();
100                 UserInfo user = null;
101                 try {
102                         user = (UserInfo) session.get(UserInfo.class, loginid);
103                         tx.commit();
104                 } catch (Exception e) {
105                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying UserInfo 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                 return user.getUserName().toString();
114         }
115
116         @Override
117         public UserInfo getUserInfoByLoginId(String loginid) {
118                 Session session = HibernateSession.getSessionFactory();
119                 Transaction tx = session.beginTransaction();
120                 UserInfo userData = null;
121         try {
122                 Criteria cr = session.createCriteria(UserInfo.class);
123                 cr.add(Restrictions.eq("userLoginId", loginid));
124             userData = (UserInfo) cr.list().get(0);
125                         tx.commit();
126                 } catch (Exception e) {
127                         logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Querying UserInfo Table"+e); 
128                 }finally{
129                         try{
130                                 session.close();
131                         }catch(Exception e1){
132                                 logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "Error While Closing Connection/Statement"+e1);
133                         }
134                 }
135                 return userData;
136         }
137         
138 }