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.controller;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.context.annotation.EnableAspectJAutoProxy;
32 import org.springframework.web.bind.annotation.PathVariable;
33 import org.springframework.web.bind.annotation.RequestBody;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RequestMethod;
36 import org.springframework.web.bind.annotation.RequestParam;
37 import org.springframework.web.bind.annotation.RestController;
39 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
40 import org.openecomp.portalsdk.core.util.SystemProperties;
41 import org.openecomp.portalsdk.core.web.support.UserUtils;
42 import org.openecomp.portalapp.controller.EPRestrictedBaseController;
43 import org.openecomp.portalapp.portal.domain.EPUser;
44 import org.openecomp.portalapp.portal.domain.EcompAppRole;
45 import org.openecomp.portalapp.portal.logging.aop.EPAuditLog;
46 import org.openecomp.portalapp.portal.service.FunctionalMenuService;
47 import org.openecomp.portalapp.portal.service.UserNotificationService;
48 import org.openecomp.portalapp.portal.transport.EpNotificationItem;
49 import org.openecomp.portalapp.portal.transport.EpNotificationItemVO;
50 import org.openecomp.portalapp.portal.transport.EpRoleNotificationItem;
51 import org.openecomp.portalapp.portal.transport.FunctionalMenuRole;
52 import org.openecomp.portalapp.portal.utils.EPCommonSystemProperties;
53 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
54 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
55 import org.openecomp.portalapp.util.EPUserUtils;
58 @org.springframework.context.annotation.Configuration
59 @EnableAspectJAutoProxy
61 public class UserNotificationController extends EPRestrictedBaseController {
64 FunctionalMenuService functionalMenuService;
67 UserNotificationService userNotificationService;
69 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserNotificationController.class);
71 @RequestMapping(value = {
72 "/portalApi/getFunctionalMenuRole" }, method = RequestMethod.GET, produces = "application/json")
73 public List<FunctionalMenuRole> getMenuIdRoleId(HttpServletRequest request, HttpServletResponse response) {
74 // EPUser user = EPUserUtils.getUserSession(request);
75 List<FunctionalMenuRole> menuRoleList = null;
76 menuRoleList = functionalMenuService.getFunctionalMenuRole();
80 @RequestMapping(value = {
81 "/portalApi/getNotifications" }, method = RequestMethod.GET, produces = "application/json")
82 public PortalRestResponse<List<EpNotificationItem>> getNotifications(HttpServletRequest request,
83 HttpServletResponse response) {
84 EPUser user = EPUserUtils.getUserSession(request);
85 PortalRestResponse<List<EpNotificationItem>> portalRestResponse = null;
87 List<EpNotificationItem> notificationList = userNotificationService.getNotifications(user.getId());
88 portalRestResponse = new PortalRestResponse<List<EpNotificationItem>>(PortalRestStatusEnum.OK, "success",
90 } catch (Exception e) {
91 logger.error(EELFLoggerDelegate.errorLogger, "getAllAppsAndContacts failed", e);
92 portalRestResponse = new PortalRestResponse<List<EpNotificationItem>>(PortalRestStatusEnum.ERROR,
93 e.getMessage(), null);
95 return portalRestResponse;
98 @RequestMapping(value = {
99 "/portalApi/getAdminNotifications" }, method = RequestMethod.GET, produces = "application/json")
100 public List<EpNotificationItemVO> getAdminNotifications(HttpServletRequest request, HttpServletResponse response) {
101 List<EpNotificationItemVO> adminNotificationList = null;
102 EPUser user = EPUserUtils.getUserSession(request);
103 adminNotificationList = userNotificationService.getAdminNotificationVOS(user.getId());
104 return adminNotificationList;
107 @RequestMapping(value = "/portalApi/saveNotification", method = RequestMethod.POST, produces = "application/json")
108 public PortalRestResponse<String> save(HttpServletRequest request, HttpServletResponse response,
109 @RequestBody EpNotificationItem notificationItem) {
111 if (notificationItem == null || notificationItem.getMsgHeader() == null)
112 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
113 "Notification Header cannot be null or empty");
114 if (notificationItem.getEndTime().compareTo(notificationItem.getStartTime()) < 0) {
115 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
116 "End Time should be greater than start time");
119 if ((notificationItem.getIsForAllRoles() == "N") && notificationItem.getRoleIds().isEmpty()) {
120 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
121 "No Roles Ids Exist for the selected Roles");
124 Long creatorId = UserUtils.getUserIdAsLong(request);
125 notificationItem.setCreatorId(creatorId);
127 // Front-end date picker does not accept a time value, so all
128 // values are the start of the chosen day in the local time zone.
129 // Move the end time value to the very end of the chosen day.
130 // Avoid Calendar.getDefault() which uses the server's locale.
131 Long endTime = notificationItem.getEndTime().getTime();
132 endTime += (23 * 3600 + 59 * 60 + 59) * 1000;
133 notificationItem.getEndTime().setTime(endTime);
136 userNotificationService.saveNotification(notificationItem);
137 } catch (Exception e) {
138 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
140 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
143 @RequestMapping(value = {
144 "/portalApi/notificationUpdateRate" }, method = RequestMethod.GET, produces = "application/json")
145 public PortalRestResponse<Map<String, String>> getNotificationUpdateRate(HttpServletRequest request) {
147 String updateRate = SystemProperties.getProperty(EPCommonSystemProperties.NOTIFICATION_UPDATE_RATE);
148 String updateDuration = SystemProperties.getProperty(EPCommonSystemProperties.NOTIFICATION_UPDATE_DURATION);
149 Integer rateInMiliSec = Integer.valueOf(updateRate) * 1000;
150 Integer durationInMiliSec = Integer.valueOf(updateDuration) * 1000;
151 Map<String, String> results = new HashMap<String, String>();
152 results.put("updateRate", String.valueOf(rateInMiliSec));
153 results.put("updateDuration", String.valueOf(durationInMiliSec));
154 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", results);
155 } catch (Exception e) {
156 logger.error(EELFLoggerDelegate.errorLogger, "getNotificationUpdateRate failed", e);
157 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.toString(), null);
161 @RequestMapping(value = {
162 "/portalApi/notificationRead" }, method = RequestMethod.GET, produces = "application/json")
163 public PortalRestResponse<Map<String, String>> notificationRead(
164 @RequestParam("notificationId") String notificationID, HttpServletRequest request) {
166 userNotificationService.setNotificationRead(Long.parseLong(notificationID), UserUtils.getUserId(request));
167 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);
168 } catch (Exception e) {
169 logger.error(EELFLoggerDelegate.errorLogger, "notificationRead failed", e);
170 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.toString(), null);
174 @RequestMapping(value = {
175 "/portalApi/getNotificationHistory" }, method = RequestMethod.GET, produces = "application/json")
176 public List<EpNotificationItemVO> getNotificationHistory(HttpServletRequest request, HttpServletResponse response) {
177 EPUser user = EPUserUtils.getUserSession(request);
178 List<EpNotificationItemVO> notificationList = null;
179 notificationList = userNotificationService.getNotificationHistoryVO(user.getId());
180 return notificationList;
183 @RequestMapping(value = { "/portalApi/notificationRole/{notificationId}/roles" }, method = {
184 RequestMethod.GET }, produces = "application/json")
185 public List<Integer> testGetRoles(HttpServletRequest request, @PathVariable("notificationId") Long notificationId) {
186 List<EpRoleNotificationItem> NotifRoles = userNotificationService.getNotificationRoles(notificationId);
187 ArrayList<Integer> rolesList = new ArrayList<Integer>();
188 for (EpRoleNotificationItem notifRole : NotifRoles) {
189 rolesList.add(notifRole.roleId);
194 @RequestMapping(value = { "/portalApi/getNotificationAppRoles" }, method = {
195 RequestMethod.GET }, produces = "application/json")
196 public List<EcompAppRole> getNotificationAppRoles(HttpServletRequest request, HttpServletResponse response) {
197 List<EcompAppRole> epAppRoleList = null;
199 epAppRoleList = userNotificationService.getAppRoleList();
200 } catch (Exception e) {
201 logger.error(EELFLoggerDelegate.errorLogger,
202 "Exception occurred while performing UserNofiticationController.getNotificationAppRoles. Details: ",
205 return epAppRoleList;
208 @RequestMapping(value = {
209 "/portalApi/getMessageRecipients" }, method = RequestMethod.GET, produces = "application/json")
210 public List<String> getMessageRecipients(@RequestParam("notificationId") Long notificationID) {
211 // EPUser user = EPUserUtils.getUserSession(request);
212 List<String> messageUserRecipients = null;
213 messageUserRecipients = userNotificationService.getMessageRecipients(notificationID);
214 return messageUserRecipients;