2  * ================================================================================
\r 
   4  * ================================================================================
\r 
   5  * Copyright (C) 2017 AT&T Intellectual Property
\r 
   6  * ================================================================================
\r 
   7  * Licensed under the Apache License, Version 2.0 (the "License");
\r 
   8  * you may not use this file except in compliance with the License.
\r 
   9  * You may obtain a copy of the License at
\r 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
\r 
  13  * Unless required by applicable law or agreed to in writing, software
\r 
  14  * distributed under the License is distributed on an "AS IS" BASIS,
\r 
  15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  16  * See the License for the specific language governing permissions and
\r 
  17  * limitations under the License.
\r 
  18  * ================================================================================
\r 
  20 package org.openecomp.portalapp.portal.controller;
\r 
  22 import java.io.IOException;
\r 
  23 import java.util.ArrayList;
\r 
  24 import java.util.Calendar;
\r 
  25 import java.util.List;
\r 
  27 import javax.servlet.http.HttpServletRequest;
\r 
  28 import javax.servlet.http.HttpServletResponse;
\r 
  30 import org.openecomp.portalapp.controller.EPRestrictedRESTfulBaseController;
\r 
  31 import org.openecomp.portalapp.portal.domain.EPApp;
\r 
  32 import org.openecomp.portalapp.portal.domain.EPRole;
\r 
  33 import org.openecomp.portalapp.portal.domain.EPUser;
\r 
  34 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
\r 
  35 import org.openecomp.portalapp.portal.service.AdminRolesService;
\r 
  36 import org.openecomp.portalapp.portal.service.EPLoginService;
\r 
  37 import org.openecomp.portalapp.portal.service.EPRoleService;
\r 
  38 import org.openecomp.portalapp.portal.service.FunctionalMenuService;
\r 
  39 import org.openecomp.portalapp.portal.service.UserNotificationService;
\r 
  40 import org.openecomp.portalapp.portal.transport.EpNotificationItem;
\r 
  41 import org.openecomp.portalapp.portal.transport.FavoritesFunctionalMenuItemJson;
\r 
  42 import org.openecomp.portalapp.portal.transport.FieldsValidator;
\r 
  43 import org.openecomp.portalapp.portal.transport.FunctionalMenuItem;
\r 
  44 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
\r 
  45 import org.openecomp.portalapp.portal.utils.EcompPortalUtils;
\r 
  46 import org.openecomp.portalapp.portal.utils.PortalConstants;
\r 
  47 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r 
  48 import org.openecomp.portalsdk.core.onboarding.crossapi.PortalAPIResponse;
\r 
  49 import org.slf4j.MDC;
\r 
  50 import org.springframework.beans.factory.annotation.Autowired;
\r 
  51 import org.springframework.context.annotation.Configuration;
\r 
  52 import org.springframework.context.annotation.EnableAspectJAutoProxy;
\r 
  53 import org.springframework.http.HttpStatus;
\r 
  54 import org.springframework.web.bind.annotation.ExceptionHandler;
\r 
  55 import org.springframework.web.bind.annotation.RequestBody;
\r 
  56 import org.springframework.web.bind.annotation.RequestMapping;
\r 
  57 import org.springframework.web.bind.annotation.RequestMethod;
\r 
  58 import org.springframework.web.bind.annotation.ResponseBody;
\r 
  59 import org.springframework.web.bind.annotation.RestController;
\r 
  61 import io.swagger.annotations.ApiOperation;
\r 
  64 @RequestMapping(PortalConstants.REST_AUX_API)
\r 
  66 @EnableAspectJAutoProxy
\r 
  68 public class ExternalAppsRestfulController extends EPRestrictedRESTfulBaseController {
\r 
  70         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ExternalAppsRestfulController.class);
\r 
  73         private FunctionalMenuService functionalMenuService;
\r 
  76         private EPLoginService epLoginService;
\r 
  79         private AdminRolesService adminRolesService;
\r 
  82         private UserNotificationService userNotificationService;
\r 
  85         private EPRoleService epRoleService;
\r 
  87         @ApiOperation(value = "Creates a Portal user notification for roles identified in the content from an external application.", response = PortalAPIResponse.class)
\r 
  88         @RequestMapping(value = { "/publishNotification" }, method = RequestMethod.POST, produces = "application/json")
\r 
  90         public PortalAPIResponse publishNotification(HttpServletRequest request,
\r 
  91                         @RequestBody EpNotificationItem notificationItem) throws Exception {
\r 
  92                 String appKey = request.getHeader("uebkey");
\r 
  93                 EPApp app = findEpApp(appKey);
\r 
  94                 List<Long> postRoleIds = new ArrayList<Long>();
\r 
  95                 for (Long roleId : notificationItem.getRoleIds()) {
\r 
  96                         EPRole role = epRoleService.getRole(app.getId(), roleId);
\r 
  98                                 postRoleIds.add(role.getId());
\r 
 101                 // --- recreate the user notification object with the POrtal Role Ids
\r 
 102                 EpNotificationItem postItem = new EpNotificationItem();
\r 
 103                 postItem.setRoleIds(postRoleIds);
\r 
 104                 postItem.setIsForAllRoles("N");
\r 
 105                 postItem.setIsForOnlineUsers("N");
\r 
 106                 postItem.setActiveYn("Y");
\r 
 107                 postItem.setPriority(notificationItem.getPriority());
\r 
 108                 postItem.setMsgHeader(notificationItem.getMsgHeader());
\r 
 109                 postItem.setMsgDescription(notificationItem.getMsgDescription());
\r 
 110                 postItem.setStartTime(notificationItem.getStartTime());
\r 
 111                 postItem.setEndTime(notificationItem.getEndTime());
\r 
 112                 postItem.setCreatedDate(Calendar.getInstance().getTime());
\r 
 113                 // default creator to 1 for now
\r 
 114                 postItem.setCreatorId(PortalConstants.DEFAULT_NOTIFICATION_CREATOR);
\r 
 118                         userNotificationService.saveNotification(postItem);
\r 
 119                 } catch (Exception e) {
\r 
 120                         return new PortalAPIResponse(false, e.getMessage());
\r 
 123                 PortalAPIResponse response = new PortalAPIResponse(true, "success");
\r 
 127         private EPApp findEpApp(String uebKey) {
\r 
 128                 List<?> list = null;
\r 
 129                 StringBuffer criteria = new StringBuffer();
\r 
 130                 criteria.append(" where ueb_key = '" + uebKey + "'");
\r 
 131                 list = getDataAccessService().getList(EPApp.class, criteria.toString(), null, null);
\r 
 132                 return (list == null || list.size() == 0) ? null : (EPApp) list.get(0);
\r 
 135         @ApiOperation(value = "Gets favorite items within the functional menu for the current user.", response = FavoritesFunctionalMenuItemJson.class, responseContainer="List")
\r 
 136         @RequestMapping(value = { "/getFavorites" }, method = RequestMethod.GET, produces = "application/json")
\r 
 137         public List<FavoritesFunctionalMenuItemJson> getFavoritesForUser(HttpServletRequest request,
\r 
 138                         HttpServletResponse response) throws Exception {
\r 
 139                 String loginId = "";
\r 
 140                 String userAgent = "";
\r 
 141                 List<FavoritesFunctionalMenuItemJson> favorites = null;
\r 
 143                 loginId = request.getHeader(EPCommonSystemProperties.MDC_LOGIN_ID);
\r 
 144                 userAgent = MDC.get(EPCommonSystemProperties.PARTNER_NAME);
\r 
 146                 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
\r 
 147                 logger.info(EELFLoggerDelegate.errorLogger,
\r 
 148                                 "getFavorites request was received from " + userAgent + " for the user " + loginId + ".");
\r 
 149                 if (epUser == null || epUser.getId() == null) {
\r 
 150                         logger.error(EELFLoggerDelegate.errorLogger,
\r 
 151                                         "No User record found for the LoginId '" + loginId + "' in the database.");
\r 
 152                         throw new Exception("Received null for Login-Id.");
\r 
 154                         favorites = functionalMenuService.getFavoriteItems(epUser.getId());
\r 
 155                         FieldsValidator fieldsValidator = new FieldsValidator();
\r 
 156                         response.setStatus(fieldsValidator.httpStatusCode.intValue());
\r 
 158                         EcompPortalUtils.logAndSerializeObject(logger, "/getFavorites", "result = ", favorites);
\r 
 164         @ApiOperation(value = "Gets functional menu items appropriate for the current user.", response = FunctionalMenuItem.class, responseContainer="List")
\r 
 165         @RequestMapping(value = {
\r 
 166                         "/functionalMenuItemsForUser" }, method = RequestMethod.GET, produces = "application/json")
\r 
 167         public List<FunctionalMenuItem> getFunctionalMenuItemsForUser(HttpServletRequest request,
\r 
 168                         HttpServletResponse response) throws Exception {
\r 
 169                 String loginId = "";
\r 
 170                 String userAgent = "";
\r 
 171                 List<FunctionalMenuItem> fnMenuItems = null;
\r 
 173                 loginId = request.getHeader("LoginId");
\r 
 174                 userAgent = MDC.get(EPCommonSystemProperties.PARTNER_NAME);
\r 
 176                 EPUser epUser = epLoginService.findUserWithoutPwd(loginId);
\r 
 177                 logger.info(EELFLoggerDelegate.errorLogger, "getFunctionalMenuItemsForUser request was received from "
\r 
 178                                 + userAgent + " for the user " + loginId + ".");
\r 
 179                 if (epUser == null || epUser.getId() == null) {
\r 
 180                         logger.error(EELFLoggerDelegate.errorLogger,
\r 
 181                                         "No User record found for the LoginId '" + loginId + "' in the database.");
\r 
 182                         throw new Exception("Received null for Login-Id.");
\r 
 183                 } else if (adminRolesService.isSuperAdmin(epUser)) {
\r 
 184                         logger.debug(EELFLoggerDelegate.debugLogger,
\r 
 185                                         "FunctionalMenuHandler: SuperUser, about to call getFunctionalMenuItems()");
\r 
 186                         fnMenuItems = functionalMenuService.getFunctionalMenuItems();
\r 
 188                         logger.debug(EELFLoggerDelegate.debugLogger,
\r 
 189                                         "getMenuItemsForAuthUser: about to call getFunctionalMenuItemsForUser()");
\r 
 190                         fnMenuItems = functionalMenuService.getFunctionalMenuItemsForUser(epUser.getOrgUserId());
\r 
 193                 FieldsValidator fieldsValidator = new FieldsValidator();
\r 
 194                 response.setStatus(fieldsValidator.httpStatusCode.intValue());
\r 
 196                 EcompPortalUtils.logAndSerializeObject(logger, "/functionalMenuItemsForUser", "result = ", fnMenuItems);
\r 
 198                 return fnMenuItems;
\r 
 201         @ExceptionHandler(Exception.class)
\r 
 202         protected void handleBadRequests(Exception e, HttpServletResponse response) throws IOException {
\r 
 203                 logger.warn(EELFLoggerDelegate.errorLogger, "Handling bad request", e);
\r 
 204                 response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());
\r