[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-common / 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.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.annotation.PostConstruct;
27 import javax.servlet.http.HttpServletResponse;
28
29 import org.hibernate.Session;
30 import org.hibernate.SessionFactory;
31 import org.hibernate.Transaction;
32 import org.openecomp.portalapp.portal.domain.EPUser;
33 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
34 import org.openecomp.portalapp.portal.transport.FieldsValidator;
35 import org.openecomp.portalapp.portal.transport.PortalAdmin;
36 import org.openecomp.portalapp.portal.transport.PortalAdminUserRole;
37 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
38 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
39 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
40 import org.openecomp.portalsdk.core.service.DataAccessService;
41 import org.openecomp.portalsdk.core.util.SystemProperties;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.context.annotation.EnableAspectJAutoProxy;
44 import org.springframework.stereotype.Service;
45
46 @Service("portalAdminService")
47 @org.springframework.context.annotation.Configuration
48 @EnableAspectJAutoProxy
49 @EPMetricsLog
50 public class PortalAdminServiceImpl implements PortalAdminService {
51
52         private String SYS_ADMIN_ROLE_ID = "1";
53         private String ECOMP_APP_ID = "1";
54
55         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(PortalAdminServiceImpl.class);
56
57         @Autowired
58         private SessionFactory sessionFactory;
59         @Autowired
60         private DataAccessService dataAccessService;
61         @Autowired
62         SearchService searchService;
63
64         @PostConstruct
65         private void init() {
66                 SYS_ADMIN_ROLE_ID = SystemProperties.getProperty(SystemProperties.SYS_ADMIN_ROLE_ID);
67                 ECOMP_APP_ID = SystemProperties.getProperty(EPCommonSystemProperties.ECOMP_APP_ID);
68         }
69
70         public List<PortalAdmin> getPortalAdmins() {
71                 try {
72                         Map<String, String> params = new HashMap<>();
73                         params.put("adminRoleId", SYS_ADMIN_ROLE_ID);
74                         @SuppressWarnings("unchecked")
75                         List<PortalAdmin> portalAdmins = (List<PortalAdmin>) dataAccessService.executeNamedQuery("getPortalAdmins",
76                                         params, null);
77                         logger.debug(EELFLoggerDelegate.debugLogger, "getPortalAdmins was successful");
78                         return portalAdmins;
79                 } catch (Exception e) {
80                         logger.error(EELFLoggerDelegate.errorLogger,
81                                         "Exception occurred while performing getPortalAdmins operation, Details: "
82                                                         + 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 orgUserId='" + orgUserId + "'",
95                                 null, null);
96                 if (localUserList.size() > 0) {
97                         user = localUserList.get(0);
98                 } else {
99                         createNewUser = true;
100                 }
101
102                 if (user != null && isLoggedInUserPortalAdmin(user.getId())) {
103                         fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_CONFLICT);
104                         logger.error(EELFLoggerDelegate.errorLogger,
105                                         "User '" + user.getOrgUserId() + "' already has PortalAdmin role assigned.");
106                 } else if (user != null || createNewUser) {
107                         Session localSession = null;
108                         Transaction transaction = null;
109                         try {
110                                 localSession = sessionFactory.openSession();
111
112                                 transaction = localSession.beginTransaction();
113                                 if (createNewUser) {
114                                         user = this.searchService.searchUserByUserId(orgUserId);
115                                         if (user != null) {
116                                                 // insert the user with active true in order to
117                                                 // pass login phase.
118                                                 user.setActive(true);
119                                                 localSession.save(EPUser.class.getName(), user);
120                                         }
121                                 }
122                                 if (user != null) {
123                                         Long userid = user.getId();
124                                         PortalAdminUserRole userRole = new PortalAdminUserRole();
125                                         userRole.userId = userid;
126                                         userRole.roleId = Long.valueOf(SYS_ADMIN_ROLE_ID);
127                                         userRole.appId = Long.valueOf(ECOMP_APP_ID);
128
129                                         localSession.save(PortalAdminUserRole.class.getName(), userRole);
130                                 }
131
132                                 transaction.commit();
133                                 result = true;
134                         } catch (Exception e) {
135                                 EcompPortalUtils.rollbackTransaction(transaction, "createPortalAdmin rollback, exception = " + e);
136                                 logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
137                         } finally {
138                                 EcompPortalUtils.closeLocalSession(localSession, "createPortalAdmin");
139                         }
140                         if (!result) {
141                                 logger.debug(EELFLoggerDelegate.debugLogger,
142                                                 "LR: createPortalAdmin: no result. setting httpStatusCode to "
143                                                                 + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
144                                 fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
145                                 logger.error(EELFLoggerDelegate.errorLogger, "PortalAdminServiceImpl.createPortalAdmin: bad request");
146                         }
147                 }
148                 return fieldsValidator;
149         }
150
151         public FieldsValidator deletePortalAdmin(Long userId) {
152                 FieldsValidator fieldsValidator = new FieldsValidator();
153                 logger.debug(EELFLoggerDelegate.debugLogger, "deletePortalAdmin: test 1");
154                 boolean result = false;
155                 Session localSession = null;
156                 Transaction transaction = null;
157
158                 try {
159                         localSession = sessionFactory.openSession();
160                         transaction = localSession.beginTransaction();
161                         dataAccessService.deleteDomainObjects(PortalAdminUserRole.class,
162                                         "user_id='" + userId + "' AND role_id='" + SYS_ADMIN_ROLE_ID + "'", null);
163                         transaction.commit();
164                         result = true;
165                 } catch (Exception e) {
166                         EcompPortalUtils.rollbackTransaction(transaction, "deletePortalAdmin rollback, exception = " + e);
167                         logger.error(EELFLoggerDelegate.errorLogger, EcompPortalUtils.getStackTrace(e));
168                 } finally {
169                         EcompPortalUtils.closeLocalSession(localSession, "deletePortalAdmin");
170                 }
171                 if (result) {
172                 } else {
173                         logger.debug(EELFLoggerDelegate.debugLogger, "deletePortalAdmin: no result. setting httpStatusCode to "
174                                         + HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
175                         fieldsValidator.httpStatusCode = new Long(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
176                 }
177                 return fieldsValidator;
178         }
179
180         private void logQuery(String sql) {
181                 logger.debug(EELFLoggerDelegate.debugLogger, "Executing query: " + sql);
182         }
183
184         private boolean isLoggedInUserPortalAdmin(Long userId) {
185                 try {
186                         String sql = "SELECT u.user_id, u.first_name, u.last_name, u.login_id "
187                                         + " FROM fn_user u, fn_user_role ur " + " WHERE u.user_id = ur.user_id " + " AND ur.user_id="
188                                         + userId + " AND ur.role_id=" + SYS_ADMIN_ROLE_ID;
189
190                         logQuery(sql);
191
192                         @SuppressWarnings("unchecked")
193                         List<PortalAdmin> portalAdmins = dataAccessService.executeSQLQuery(sql, PortalAdmin.class, null);
194                         logger.debug(EELFLoggerDelegate.debugLogger, portalAdmins.toString());
195                         if (portalAdmins == null || portalAdmins.size() <= 0) {
196                                 return false;
197                         }
198                         return true;
199
200                 } catch (Exception e) {
201                         logger.error(EELFLoggerDelegate.errorLogger,
202                                         "Exception occurred while performing isLoggedInUserPortalAdmin operation, Details: "
203                                                         + EcompPortalUtils.getStackTrace(e));
204                         return false;
205                 }
206         }
207 }