af76cdc0264703c0d67f171c16dd32e6d2698b36
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / controller / UserNotificationController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.controller;
39
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44
45 import javax.servlet.http.HttpServletRequest;
46 import javax.servlet.http.HttpServletResponse;
47
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.context.annotation.EnableAspectJAutoProxy;
50 import org.springframework.web.bind.annotation.PathVariable;
51 import org.springframework.web.bind.annotation.RequestBody;
52 import org.springframework.web.bind.annotation.RequestMapping;
53 import org.springframework.web.bind.annotation.RequestMethod;
54 import org.springframework.web.bind.annotation.RequestParam;
55 import org.springframework.web.bind.annotation.RestController;
56 import org.onap.portalapp.controller.EPRestrictedBaseController;
57 import org.onap.portalapp.portal.domain.EPUser;
58 import org.onap.portalapp.portal.domain.EcompAppRole;
59 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
60 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
61 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
62 import org.onap.portalapp.portal.service.FunctionalMenuService;
63 import org.onap.portalapp.portal.service.UserNotificationService;
64 import org.onap.portalapp.portal.transport.EpNotificationItem;
65 import org.onap.portalapp.portal.transport.EpNotificationItemVO;
66 import org.onap.portalapp.portal.transport.EpRoleNotificationItem;
67 import org.onap.portalapp.portal.transport.FunctionalMenuRole;
68 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
69 import org.onap.portalapp.util.EPUserUtils;
70 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
71 import org.onap.portalsdk.core.util.SystemProperties;
72 import org.onap.portalsdk.core.web.support.UserUtils;
73
74 @RestController
75 @org.springframework.context.annotation.Configuration
76 @EnableAspectJAutoProxy
77 @EPAuditLog
78 public class UserNotificationController extends EPRestrictedBaseController {
79
80         @Autowired
81         FunctionalMenuService functionalMenuService;
82
83         @Autowired
84         UserNotificationService userNotificationService;
85
86         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserNotificationController.class);
87
88         @RequestMapping(value = {
89                         "/portalApi/getFunctionalMenuRole" }, method = RequestMethod.GET, produces = "application/json")
90         public List<FunctionalMenuRole> getMenuIdRoleId(HttpServletRequest request, HttpServletResponse response) {
91                 // EPUser user = EPUserUtils.getUserSession(request);
92                 List<FunctionalMenuRole> menuRoleList = null;
93                 menuRoleList = functionalMenuService.getFunctionalMenuRole();
94                 return menuRoleList;
95         }
96
97         @RequestMapping(value = {
98                         "/portalApi/getNotifications" }, method = RequestMethod.GET, produces = "application/json")
99         public PortalRestResponse<List<EpNotificationItem>> getNotifications(HttpServletRequest request,
100                         HttpServletResponse response) {
101                 EPUser user = EPUserUtils.getUserSession(request);
102                 PortalRestResponse<List<EpNotificationItem>> portalRestResponse = null;
103                 try {
104                         List<EpNotificationItem> notificationList = userNotificationService.getNotifications(user.getId());
105                         portalRestResponse = new PortalRestResponse<List<EpNotificationItem>>(PortalRestStatusEnum.OK, "success",
106                                         notificationList);
107                 } catch (Exception e) {
108                         logger.error(EELFLoggerDelegate.errorLogger, "getAllAppsAndContacts failed", e);
109                         portalRestResponse = new PortalRestResponse<List<EpNotificationItem>>(PortalRestStatusEnum.ERROR,
110                                         e.getMessage(), null);
111                 }
112                 return portalRestResponse;
113         }
114
115         @RequestMapping(value = {
116                         "/portalApi/getAdminNotifications" }, method = RequestMethod.GET, produces = "application/json")
117         public List<EpNotificationItemVO> getAdminNotifications(HttpServletRequest request, HttpServletResponse response) {
118                 List<EpNotificationItemVO> adminNotificationList = null;
119                 EPUser user = EPUserUtils.getUserSession(request);
120                 adminNotificationList = userNotificationService.getAdminNotificationVOS(user.getId());
121                 return adminNotificationList;
122         }
123
124         @RequestMapping(value = "/portalApi/saveNotification", method = RequestMethod.POST, produces = "application/json")
125         public PortalRestResponse<String> save(HttpServletRequest request, HttpServletResponse response,
126                         @RequestBody EpNotificationItem notificationItem) {
127
128                 if (notificationItem == null || notificationItem.getMsgHeader() == null)
129                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
130                                         "Notification Header cannot be null or empty");
131                 if (notificationItem.getEndTime().compareTo(notificationItem.getStartTime()) < 0) {
132                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
133                                         "End Time should be greater than  start time");
134                 }
135
136                 if ((notificationItem.getIsForAllRoles() == "N") && notificationItem.getRoleIds().isEmpty()) {
137                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE",
138                                         "No Roles Ids Exist for the selected Roles");
139                 }
140
141                 Long creatorId = UserUtils.getUserIdAsLong(request);
142                 notificationItem.setCreatorId(creatorId);
143
144                 // Front-end date picker does not accept a time value, so all
145                 // values are the start of the chosen day in the local time zone.
146                 // Move the end time value to the very end of the chosen day.
147                 // Avoid Calendar.getDefault() which uses the server's locale.
148                 Long endTime = notificationItem.getEndTime().getTime();
149                 endTime += (23 * 3600 + 59 * 60 + 59) * 1000;
150                 notificationItem.getEndTime().setTime(endTime);
151
152                 try {
153                         userNotificationService.saveNotification(notificationItem);
154                 } catch (Exception e) {
155                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage());
156                 }
157                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "SUCCESS", "");
158         }
159
160         @RequestMapping(value = {
161                         "/portalApi/notificationUpdateRate" }, method = RequestMethod.GET, produces = "application/json")
162         public PortalRestResponse<Map<String, String>> getNotificationUpdateRate(HttpServletRequest request) {
163                 try {
164                         String updateRate = SystemProperties.getProperty(EPCommonSystemProperties.NOTIFICATION_UPDATE_RATE);
165                         String updateDuration = SystemProperties.getProperty(EPCommonSystemProperties.NOTIFICATION_UPDATE_DURATION);
166                         Integer rateInMiliSec = Integer.valueOf(updateRate) * 1000;
167                         Integer durationInMiliSec = Integer.valueOf(updateDuration) * 1000;
168                         Map<String, String> results = new HashMap<String, String>();
169                         results.put("updateRate", String.valueOf(rateInMiliSec));
170                         results.put("updateDuration", String.valueOf(durationInMiliSec));
171                         return new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", results);
172                 } catch (Exception e) {
173                         logger.error(EELFLoggerDelegate.errorLogger, "getNotificationUpdateRate failed", e);
174                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.toString(), null);
175                 }
176         }
177
178         @RequestMapping(value = {
179                         "/portalApi/notificationRead" }, method = RequestMethod.GET, produces = "application/json")
180         public PortalRestResponse<Map<String, String>> notificationRead(
181                         @RequestParam("notificationId") String notificationID, HttpServletRequest request) {
182                 try {
183                         userNotificationService.setNotificationRead(Long.parseLong(notificationID), UserUtils.getUserId(request));
184                         return new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", null);
185                 } catch (Exception e) {
186                         logger.error(EELFLoggerDelegate.errorLogger, "notificationRead failed", e);
187                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.toString(), null);
188                 }
189         }
190
191         @RequestMapping(value = {
192                         "/portalApi/getNotificationHistory" }, method = RequestMethod.GET, produces = "application/json")
193         public List<EpNotificationItemVO> getNotificationHistory(HttpServletRequest request, HttpServletResponse response) {
194                 EPUser user = EPUserUtils.getUserSession(request);
195                 List<EpNotificationItemVO> notificationList = null;
196                 notificationList = userNotificationService.getNotificationHistoryVO(user.getId());
197                 return notificationList;
198         }
199
200         @RequestMapping(value = { "/portalApi/notificationRole/{notificationId}/roles" }, method = {
201                         RequestMethod.GET }, produces = "application/json")
202         public List<Integer> testGetRoles(HttpServletRequest request, @PathVariable("notificationId") Long notificationId) {
203                 List<EpRoleNotificationItem> NotifRoles = userNotificationService.getNotificationRoles(notificationId);
204                 ArrayList<Integer> rolesList = new ArrayList<Integer>();
205                 for (EpRoleNotificationItem notifRole : NotifRoles) {
206                         rolesList.add(notifRole.roleId);
207                 }
208                 return rolesList;
209         }
210
211         @RequestMapping(value = { "/portalApi/getNotificationAppRoles" }, method = {
212                         RequestMethod.GET }, produces = "application/json")
213         public List<EcompAppRole> getNotificationAppRoles(HttpServletRequest request, HttpServletResponse response) {
214                 List<EcompAppRole> epAppRoleList = null;
215                 try {
216                         epAppRoleList = userNotificationService.getAppRoleList();
217                 } catch (Exception e) {
218                         logger.error(EELFLoggerDelegate.errorLogger,
219                                         "Exception occurred while performing UserNofiticationController.getNotificationAppRoles. Details: ",
220                                         e);
221                 }
222                 return epAppRoleList;
223         }
224
225         @RequestMapping(value = {
226                         "/portalApi/getMessageRecipients" }, method = RequestMethod.GET, produces = "application/json")
227         public List<String> getMessageRecipients(@RequestParam("notificationId") Long notificationID) {
228                 // EPUser user = EPUserUtils.getUserSession(request);
229                 List<String> messageUserRecipients = null;
230                 messageUserRecipients = userNotificationService.getMessageRecipients(notificationID);
231                 return messageUserRecipients;
232         }
233
234 }