24b698e8fdef7fc74d9e07523a386d46991b3073
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / UserNotificationController.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.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
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;
38
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;
56
57 @RestController
58 @org.springframework.context.annotation.Configuration
59 @EnableAspectJAutoProxy
60 @EPAuditLog
61 public class UserNotificationController extends EPRestrictedBaseController {
62
63         @Autowired
64         FunctionalMenuService functionalMenuService;
65
66         @Autowired
67         UserNotificationService userNotificationService;
68
69         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserNotificationController.class);
70
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();
77                 return menuRoleList;
78         }
79
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;
86                 try {
87                         List<EpNotificationItem> notificationList = userNotificationService.getNotifications(user.getId());
88                         portalRestResponse = new PortalRestResponse<List<EpNotificationItem>>(PortalRestStatusEnum.OK, "success",
89                                         notificationList);
90                 } catch (Exception e) {
91                         logger.error(EELFLoggerDelegate.errorLogger, "getAllAppsAndContacts failed", e);
92                         portalRestResponse = new PortalRestResponse<List<EpNotificationItem>>(PortalRestStatusEnum.ERROR,
93                                         e.getMessage(), null);
94                 }
95                 return portalRestResponse;
96         }
97
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;
105         }
106
107         @RequestMapping(value = "/portalApi/saveNotification", method = RequestMethod.POST, produces = "application/json")
108         public PortalRestResponse<String> save(HttpServletRequest request, HttpServletResponse response,
109                         @RequestBody EpNotificationItem notificationItem) {
110
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");
117                 }
118
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");
122                 }
123
124                 Long creatorId = UserUtils.getUserIdAsLong(request);
125                 notificationItem.setCreatorId(creatorId);
126
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);
134
135                 try {
136                         userNotificationService.saveNotification(notificationItem);
137                 } catch (Exception e) {
138                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
139                 }
140                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
141         }
142
143         @RequestMapping(value = {
144                         "/portalApi/notificationUpdateRate" }, method = RequestMethod.GET, produces = "application/json")
145         public PortalRestResponse<Map<String, String>> getNotificationUpdateRate(HttpServletRequest request) {
146                 try {
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);
158                 }
159         }
160
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) {
165                 try {
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);
171                 }
172         }
173
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;
181         }
182
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);
190                 }
191                 return rolesList;
192         }
193
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;
198                 try {
199                         epAppRoleList = userNotificationService.getAppRoleList();
200                 } catch (Exception e) {
201                         logger.error(EELFLoggerDelegate.errorLogger,
202                                         "Exception occurred while performing UserNofiticationController.getNotificationAppRoles. Details: ",
203                                         e);
204                 }
205                 return epAppRoleList;
206         }
207
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;
215         }
216
217 }