nexus site path corrected
[portal.git] / ecomp-portal-BE / src / main / java / org / openecomp / portalapp / portal / service / PortalAdminServiceImpl.java
1 /*-
2  * ================================================================================
3  * eCOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.service;
21
22 import java.util.List;
23
24 import javax.annotation.PostConstruct;
25 import javax.servlet.http.HttpServletResponse;
26
27 import org.hibernate.Session;
28 import org.hibernate.SessionFactory;
29 import org.hibernate.Transaction;
30 import org.openecomp.portalapp.portal.domain.EPUser;
31 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
32 import org.openecomp.portalapp.portal.transport.FieldsValidator;
33 import org.openecomp.portalapp.portal.transport.PortalAdmin;
34 import org.openecomp.portalapp.portal.transport.PortalAdminUserRole;
35 import org.openecomp.portalapp.portal.utils.EPSystemProperties;
36 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
37 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
38 import org.openecomp.portalsdk.core.service.DataAccessService;
39 import org.openecomp.portalsdk.core.util.SystemProperties;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.context.annotation.EnableAspectJAutoProxy;
42 import org.springframework.stereotype.Service;
43
44 @Service("portalAdminService")
45 @org.springframework.context.annotation.Configuration
46 @EnableAspectJAutoProxy
47 @EPMetricsLog
48 public class PortalAdminServiceImpl implements PortalAdminService {
49         
50         private String SYS_ADMIN_ROLE_ID = "1";
51         private String ECOMP_APP_ID = "1";
52
53         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PortalAdminServiceImpl.class);
54         
55         @Autowired
56         private SessionFactory sessionFactory;
57         @Autowired
58         private DataAccessService dataAccessService;
59         @Autowired
60         SearchService searchService;
61
62         @PostConstruct
63         private void init() {
64                 SYS_ADMIN_ROLE_ID = SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID);
65                 ECOMP_APP_ID = SystemProperties.getProperty(EPSystemProperties.ECOMP_APP_ID);
66         }
67         
68         public List<PortalAdmin> getPortalAdmins() {
69                 try {
70                         String sql = "SELECT u.user_id, u.first_name, u.last_name, u.login_id " +
71                                         " FROM fn_user u, fn_user_role ur " +
72                                         " WHERE u.user_id = ur.user_id " +
73                                         " AND ur.role_id=" + SYS_ADMIN_ROLE_ID;
74                         logQuery(sql);
75         
76                         @SuppressWarnings("unchecked")
77                         List<PortalAdmin> portalAdmins = dataAccessService.executeSQLQuery(sql, PortalAdmin.class, null);
78                         logger.debug(EELFLoggerDelegate.debugLogger, portalAdmins.toString());
79                         return portalAdmins;
80         
81                 } catch (Exception e) {
82                         logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing getPortalAdmins operation, Details: " + EcompPortalUtils.getStackTrace(e));
83                         return null;
84                 }
85         }
86         
87         @SuppressWarnings("unchecked")
88         public FieldsValidator createPortalAdmin(String orgUserId) {
89                 FieldsValidator fieldsValidator = new FieldsValidator();
90                 logger.debug(EELFLoggerDelegate.debugLogger, "LR: createPortalAdmin: test 1");
91                 boolean result = false;
92                 EPUser user = null;
93                 boolean createNewUser = false;
94                 List<EPUser> localUserList = dataAccessService.getList(EPUser.class, " where org_user_id='" + orgUserId + "'",
95                                 null, null);
96                 if (localUserList.size() > 0) {
97                         user = localUserList.get(0);
98                 } else {
99                         createNewUser = true;
100                 }
101                 if (user != null || createNewUser) {
102                 Session localSession = null;
103                 Transaction transaction = null;
104                 try {
105                         localSession = sessionFactory.openSession();
106
107                         transaction = localSession.beginTransaction();
108                         if (createNewUser) {
109                                 user = this.searchService.searchUserByUserId(orgUserId);
110                                 if (user != null) {
111                                         // insert the user with active true in order to
112                                         // pass login phase.
113                                         user.setActive(true);
114                                         localSession.save(EPUser.class.getName(), user);
115                                 }
116                         }
117                         if (user != null) {
118                                 Long userId = user.getId();
119                                 PortalAdminUserRole userRole = new PortalAdminUserRole();
120                                 userRole.userId = userId;
121                                 userRole.roleId = Long.valueOf(SYS_ADMIN_ROLE_ID);
122                                 userRole.appId = Long.valueOf(ECOMP_APP_ID);
123                                 
124                                 localSession.save(PortalAdminUserRole.class.getName(), userRole);
125                         }
126 //                      logger.debug(EELFLoggerDelegate.debugLogger, "after saving menu object, new id: " + userId);
127
128                         transaction.commit();
129                         result = true;
130                 } catch (Exception e) {
131                         EcompPortalUtils.rollbackTransaction(transaction, 
132                                         "createPortalAdmin rollback, exception = " + e);
133                         logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
134                 } finally {
135                         EcompPortalUtils.closeLocalSession(localSession, "createPortalAdmin");
136                 }
137                 if (!result) {
138                         logger.debug(EELFLoggerDelegate.debugLogger, "LR: createPortalAdmin: no result. setting httpStatusCode to "
139                                         + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
140                         fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
141                 }
142                 logger.error(EELFLoggerDelegate.errorLogger, "PortalAdminServiceImpl.createPortalAdmin: bad request");
143                                 }
144                 return fieldsValidator;
145         }
146
147         public FieldsValidator deletePortalAdmin(Long userId) {
148                 FieldsValidator fieldsValidator = new FieldsValidator();
149                 logger.debug(EELFLoggerDelegate.debugLogger, "LR: deletePortalAdmin: test 1");
150                 boolean result = false;
151                 Session localSession = null;
152                 Transaction transaction = null;
153
154                 try {
155                         localSession = sessionFactory.openSession();
156                         transaction = localSession.beginTransaction();
157                         dataAccessService.deleteDomainObjects(PortalAdminUserRole.class, "user_id='" + userId + "' AND role_id='" + SYS_ADMIN_ROLE_ID + "'", null);
158                         transaction.commit();
159                         result = true;
160                 } catch (Exception e) {
161                         EcompPortalUtils.rollbackTransaction(transaction,
162                                         "deletePortalAdmin rollback, exception = " + e);
163                         logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
164                 } finally {
165                         EcompPortalUtils.closeLocalSession(localSession, "deletePortalAdmin");
166                 }
167                 if (result) {
168                 } else {
169                         logger.debug(EELFLoggerDelegate.debugLogger, "LR: deletePortalAdmin: no result. setting httpStatusCode to "
170                                         + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
171                         fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
172                 }
173                 return fieldsValidator;
174         }
175                 
176         private void logQuery(String sql) {
177                 logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
178         }
179 }