2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.portal.service;
22 import java.util.List;
24 import javax.annotation.PostConstruct;
25 import javax.servlet.http.HttpServletResponse;
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;
44 @Service("portalAdminService")
45 @org.springframework.context.annotation.Configuration
46 @EnableAspectJAutoProxy
48 public class PortalAdminServiceImpl implements PortalAdminService {
50 private String SYS_ADMIN_ROLE_ID = "1";
51 private String ECOMP_APP_ID = "1";
53 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PortalAdminServiceImpl.class);
56 private SessionFactory sessionFactory;
58 private DataAccessService dataAccessService;
60 SearchService searchService;
64 SYS_ADMIN_ROLE_ID = SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID);
65 ECOMP_APP_ID = SystemProperties.getProperty(EPSystemProperties.ECOMP_APP_ID);
68 public List<PortalAdmin> getPortalAdmins() {
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;
76 @SuppressWarnings("unchecked")
77 List<PortalAdmin> portalAdmins = dataAccessService.executeSQLQuery(sql, PortalAdmin.class, null);
78 logger.debug(EELFLoggerDelegate.debugLogger, portalAdmins.toString());
81 } catch (Exception e) {
82 logger.error(EELFLoggerDelegate.errorLogger, "Exception occurred while performing getPortalAdmins operation, Details: " + EcompPortalUtils.getStackTrace(e));
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;
93 boolean createNewUser = false;
94 List<EPUser> localUserList = dataAccessService.getList(EPUser.class, " where org_user_id='" + orgUserId + "'",
96 if (localUserList.size() > 0) {
97 user = localUserList.get(0);
101 if (user != null || createNewUser) {
102 Session localSession = null;
103 Transaction transaction = null;
105 localSession = sessionFactory.openSession();
107 transaction = localSession.beginTransaction();
109 user = this.searchService.searchUserByUserId(orgUserId);
111 // insert the user with active true in order to
113 user.setActive(true);
114 localSession.save(EPUser.class.getName(), user);
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);
124 localSession.save(PortalAdminUserRole.class.getName(), userRole);
126 // logger.debug(EELFLoggerDelegate.debugLogger, "after saving menu object, new id: " + userId);
128 transaction.commit();
130 } catch (Exception e) {
131 EcompPortalUtils.rollbackTransaction(transaction,
132 "createPortalAdmin rollback, exception = " + e);
133 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
135 EcompPortalUtils.closeLocalSession(localSession, "createPortalAdmin");
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);
142 logger.error(EELFLoggerDelegate.errorLogger, "PortalAdminServiceImpl.createPortalAdmin: bad request");
144 return fieldsValidator;
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;
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();
160 } catch (Exception e) {
161 EcompPortalUtils.rollbackTransaction(transaction,
162 "deletePortalAdmin rollback, exception = " + e);
163 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
165 EcompPortalUtils.closeLocalSession(localSession, "deletePortalAdmin");
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);
173 return fieldsValidator;
176 private void logQuery(String sql) {
177 logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);