[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / ExternalAppsRestfulController.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.controller;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Calendar;
25 import java.util.List;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import org.openecomp.portalapp.controller.EPRestrictedRESTfulBaseController;
31 import org.openecomp.portalapp.portal.domain.EPApp;
32 import org.openecomp.portalapp.portal.domain.EPRole;
33 import org.openecomp.portalapp.portal.domain.EPUser;
34 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
35 import org.openecomp.portalapp.portal.service.AdminRolesService;
36 import org.openecomp.portalapp.portal.service.EPLoginService;
37 import org.openecomp.portalapp.portal.service.EPRoleService;
38 import org.openecomp.portalapp.portal.service.FunctionalMenuService;
39 import org.openecomp.portalapp.portal.service.UserNotificationService;
40 import org.openecomp.portalapp.portal.transport.EpNotificationItem;
41 import org.openecomp.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
42 import org.openecomp.portalapp.portal.transport.FieldsValidator;
43 import org.openecomp.portalapp.portal.transport.FunctionalMenuItem;
44 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
45 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
46 import org.openecomp.portalapp.portal.utils.PortalConstants;
47 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
48 import org.openecomp.portalsdk.core.onboarding.crossapi.PortalAPIResponse;
49 import org.slf4j.MDC;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.context.annotation.Configuration;
52 import org.springframework.context.annotation.EnableAspectJAutoProxy;
53 import org.springframework.http.HttpStatus;
54 import org.springframework.web.bind.annotation.ExceptionHandler;
55 import org.springframework.web.bind.annotation.RequestBody;
56 import org.springframework.web.bind.annotation.RequestMapping;
57 import org.springframework.web.bind.annotation.RequestMethod;
58 import org.springframework.web.bind.annotation.ResponseBody;
59 import org.springframework.web.bind.annotation.RestController;
60
61 import io.swagger.annotations.ApiOperation;
62
63 @RestController
64 @RequestMapping(PortalConstants.REST_AUX_API)
65 @Configuration
66 @EnableAspectJAutoProxy
67 @EPAuditLog
68 public class ExternalAppsRestfulController extends EPRestrictedRESTfulBaseController {
69
70         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAppsRestfulController.class);
71
72         @Autowired
73         private FunctionalMenuService functionalMenuService;
74
75         @Autowired
76         private EPLoginService epLoginService;
77
78         @Autowired
79         private AdminRolesService adminRolesService;
80
81         @Autowired
82         private UserNotificationService userNotificationService;
83
84         @Autowired
85         private EPRoleService epRoleService;
86
87         @ApiOperation(value = "Creates a Portal user notification for roles identified in the content from an external application.", response = PortalAPIResponse.class)
88         @RequestMapping(value = { "/publishNotification" }, method = RequestMethod.POST, produces = "application/json")
89         @ResponseBody
90         public PortalAPIResponse publishNotification(HttpServletRequest request,
91                         @RequestBody EpNotificationItem notificationItem) throws Exception {
92                 String appKey = request.getHeader("uebkey");
93                 EPApp app = findEpApp(appKey);
94                 List<Long> postRoleIds = new ArrayList<Long>();
95                 for (Long roleId : notificationItem.getRoleIds()) {
96                         EPRole role = epRoleService.getRole(app.getId(), roleId);
97                         if (role != null)
98                                 postRoleIds.add(role.getId());
99                 }
100
101                 // --- recreate the user notification object with the POrtal Role Ids
102                 EpNotificationItem postItem = new EpNotificationItem();
103                 postItem.setRoleIds(postRoleIds);
104                 postItem.setIsForAllRoles("N");
105                 postItem.setIsForOnlineUsers("N");
106                 postItem.setActiveYn("Y");
107                 postItem.setPriority(notificationItem.getPriority());
108                 postItem.setMsgHeader(notificationItem.getMsgHeader());
109                 postItem.setMsgDescription(notificationItem.getMsgDescription());
110                 postItem.setStartTime(notificationItem.getStartTime());
111                 postItem.setEndTime(notificationItem.getEndTime());
112                 postItem.setCreatedDate(Calendar.getInstance().getTime());
113                 // default creator to 1 for now
114                 postItem.setCreatorId(PortalConstants.DEFAULT_NOTIFICATION_CREATOR);
115                 // ----
116
117                 try {
118                         userNotificationService.saveNotification(postItem);
119                 } catch (Exception e) {
120                         return new PortalAPIResponse(false, e.getMessage());
121                 }
122
123                 PortalAPIResponse response = new PortalAPIResponse(true, "success");
124                 return response;
125         }
126
127         private EPApp findEpApp(String uebKey) {
128                 List<?> list = null;
129                 StringBuffer criteria = new StringBuffer();
130                 criteria.append(" where ueb_key = '" + uebKey + "'");
131                 list = getDataAccessService().getList(EPApp.class, criteria.toString(), null, null);
132                 return (list == null || list.size() == 0) ? null : (EPApp) list.get(0);
133         }
134
135         @ApiOperation(value = "Gets favorite items within the functional menu for the current user.", response = FavoritesFunctionalMenuItemJson.class, responseContainer="List")
136         @RequestMapping(value = { "/getFavorites" }, method = RequestMethod.GET, produces = "application/json")
137         public List<FavoritesFunctionalMenuItemJson> getFavoritesForUser(HttpServletRequest request,
138                         HttpServletResponse response) throws Exception {
139                 String loginId = "";
140                 String userAgent = "";
141                 List<FavoritesFunctionalMenuItemJson> favorites = null;
142
143                 loginId = request.getHeader(EPCommonSystemProperties.MDC_LOGIN_ID);
144                 userAgent = MDC.get(EPCommonSystemProperties.PARTNER_NAME);
145
146                 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
147                 logger.info(EELFLoggerDelegate.errorLogger,
148                                 "getFavorites request was received from " + userAgent + " for the user " + loginId + ".");
149                 if (epUser == null || epUser.getId() == null) {
150                         logger.error(EELFLoggerDelegate.errorLogger,
151                                         "No User record found for the LoginId '" + loginId + "' in the database.");
152                         throw new Exception("Received null for Login-Id.");
153                 } else {
154                         favorites = functionalMenuService.getFavoriteItems(epUser.getId());
155                         FieldsValidator fieldsValidator = new FieldsValidator();
156                         response.setStatus(fieldsValidator.httpStatusCode.intValue());
157
158                         EcompPortalUtils.logAndSerializeObject(logger, "/getFavorites", "result = ", favorites);
159                 }
160
161                 return favorites;
162         }
163
164         @ApiOperation(value = "Gets functional menu items appropriate for the current user.", response = FunctionalMenuItem.class, responseContainer="List")
165         @RequestMapping(value = {
166                         "/functionalMenuItemsForUser" }, method = RequestMethod.GET, produces = "application/json")
167         public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(HttpServletRequest request,
168                         HttpServletResponse response) throws Exception {
169                 String loginId = "";
170                 String userAgent = "";
171                 List<FunctionalMenuItem> fnMenuItems = null;
172
173                 loginId = request.getHeader("LoginId");
174                 userAgent = MDC.get(EPCommonSystemProperties.PARTNER_NAME);
175
176                 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
177                 logger.info(EELFLoggerDelegate.errorLogger, "getFunctionalMenuItemsForUser request was received from "
178                                 + userAgent + " for the user " + loginId + ".");
179                 if (epUser == null || epUser.getId() == null) {
180                         logger.error(EELFLoggerDelegate.errorLogger,
181                                         "No User record found for the LoginId '" + loginId + "' in the database.");
182                         throw new Exception("Received null for Login-Id.");
183                 } else if (adminRolesService.isSuperAdmin(epUser)) {
184                         logger.debug(EELFLoggerDelegate.debugLogger,
185                                         "FunctionalMenuHandler: SuperUser, about to call getFunctionalMenuItems()");
186                         fnMenuItems = functionalMenuService.getFunctionalMenuItems();
187                 } else {
188                         logger.debug(EELFLoggerDelegate.debugLogger,
189                                         "getMenuItemsForAuthUser: about to call getFunctionalMenuItemsForUser()");
190                         fnMenuItems = functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId());
191                 }
192
193                 FieldsValidator fieldsValidator = new FieldsValidator();
194                 response.setStatus(fieldsValidator.httpStatusCode.intValue());
195
196                 EcompPortalUtils.logAndSerializeObject(logger, "/functionalMenuItemsForUser", "result = ", fnMenuItems);
197
198                 return fnMenuItems;
199         }
200
201         @ExceptionHandler(Exception.class)
202         protected void handleBadRequests(Exception e, HttpServletResponse response) throws IOException {
203                 logger.warn(EELFLoggerDelegate.errorLogger, "Handling bad request", e);
204                 response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());
205         }
206 }