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