fixed some sonar code smells
[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.PostMapping;
55 import org.springframework.web.bind.annotation.GetMapping;
56 import org.springframework.web.bind.annotation.RequestParam;
57 import org.springframework.web.bind.annotation.RestController;
58 import org.onap.portalapp.controller.EPRestrictedBaseController;
59 import org.onap.portalapp.portal.domain.EPUser;
60 import org.onap.portalapp.portal.domain.EcompAppRole;
61 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
62 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
63 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
64 import org.onap.portalapp.portal.service.FunctionalMenuService;
65 import org.onap.portalapp.portal.service.UserNotificationService;
66 import org.onap.portalapp.portal.transport.EpNotificationItem;
67 import org.onap.portalapp.portal.transport.EpNotificationItemVO;
68 import org.onap.portalapp.portal.transport.EpRoleNotificationItem;
69 import org.onap.portalapp.portal.transport.FunctionalMenuRole;
70 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
71 import org.onap.portalapp.util.EPUserUtils;
72 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
73 import org.onap.portalsdk.core.util.SystemProperties;
74 import org.onap.portalsdk.core.web.support.UserUtils;
75
76 @RestController
77 @org.springframework.context.annotation.Configuration
78 @EnableAspectJAutoProxy
79 @EPAuditLog
80 public class UserNotificationController extends EPRestrictedBaseController {
81
82     @Autowired
83     FunctionalMenuService functionalMenuService;
84
85     @Autowired
86     UserNotificationService userNotificationService;
87
88     EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserNotificationController.class);
89     private static final String SUCCESS = "success";
90     private static final String FAILURE = "FAILURE";
91
92     @GetMapping(value = {
93             "/portalApi/getFunctionalMenuRole" }, produces = "application/json")
94     public List<FunctionalMenuRole> getMenuIdRoleId(HttpServletRequest request, HttpServletResponse response) {
95         // EPUser user = EPUserUtils.getUserSession(request);
96         List<FunctionalMenuRole> menuRoleList = null;
97         menuRoleList = functionalMenuService.getFunctionalMenuRole();
98         return menuRoleList;
99     }
100
101     @GetMapping(value = {
102             "/portalApi/getNotifications" }, produces = "application/json")
103     public PortalRestResponse<List<EpNotificationItem>> getNotifications(HttpServletRequest request,
104             HttpServletResponse response) {
105         EPUser user = EPUserUtils.getUserSession(request);
106         PortalRestResponse<List<EpNotificationItem>> portalRestResponse = null;
107         try {
108             List<EpNotificationItem> notificationList = userNotificationService.getNotifications(user.getId());
109             portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS,
110                     notificationList);
111         } catch (Exception e) {
112             logger.error(EELFLoggerDelegate.errorLogger, "getAllAppsAndContacts failed", e);
113             portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
114                     e.getMessage(), null);
115         }
116         return portalRestResponse;
117     }
118
119     @GetMapping(value = {
120             "/portalApi/getAdminNotifications" }, produces = "application/json")
121     public List<EpNotificationItemVO> getAdminNotifications(HttpServletRequest request, HttpServletResponse response) {
122         List<EpNotificationItemVO> adminNotificationList = null;
123         EPUser user = EPUserUtils.getUserSession(request);
124         adminNotificationList = userNotificationService.getAdminNotificationVOS(user.getId());
125         return adminNotificationList;
126     }
127
128     @PostMapping(value = "/portalApi/saveNotification", produces = "application/json")
129     public PortalRestResponse<String> save(HttpServletRequest request, HttpServletResponse response,
130             @RequestBody EpNotificationItem notificationItem) {
131
132         if (notificationItem == null || notificationItem.getMsgHeader() == null)
133             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
134                     "Notification Header cannot be null or empty");
135         if (notificationItem.getEndTime().compareTo(notificationItem.getStartTime()) < 0) {
136             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
137                     "End Time should be greater than  start time");
138         }
139
140         if ((notificationItem.getIsForAllRoles() == "N") && notificationItem.getRoleIds().isEmpty()) {
141             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
142                     "No Roles Ids Exist for the selected Roles");
143         }
144
145         Long creatorId = UserUtils.getUserIdAsLong(request);
146         notificationItem.setCreatorId(creatorId);
147
148         // Front-end date picker does not accept a time value, so all
149         // values are the start of the chosen day in the local time zone.
150         // Move the end time value to the very end of the chosen day.
151         // Avoid Calendar.getDefault() which uses the server's locale.
152         Long endTime = notificationItem.getEndTime().getTime();
153         endTime += (23 * 3600 + 59 * 60 + 59) * 1000;
154         notificationItem.getEndTime().setTime(endTime);
155
156         try {
157             userNotificationService.saveNotification(notificationItem);
158         } catch (Exception e) {
159             logger.error(EELFLoggerDelegate.errorLogger, "saveNotification failed", e);
160             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
161         }
162         return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
163     }
164
165     @GetMapping(value = {
166             "/portalApi/notificationUpdateRate" }, produces = "application/json")
167     public PortalRestResponse<Map<String, String>> getNotificationUpdateRate(HttpServletRequest request) {
168         try {
169             String updateRate = SystemProperties.getProperty(EPCommonSystemProperties.NOTIFICATION_UPDATE_RATE);
170             String updateDuration = SystemProperties.getProperty(EPCommonSystemProperties.NOTIFICATION_UPDATE_DURATION);
171             Integer rateInMiliSec = Integer.valueOf(updateRate) * 1000;
172             Integer durationInMiliSec = Integer.valueOf(updateDuration) * 1000;
173             Map<String, String> results = new HashMap<>();
174             results.put("updateRate", String.valueOf(rateInMiliSec));
175             results.put("updateDuration", String.valueOf(durationInMiliSec));
176             return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, results);
177         } catch (Exception e) {
178             logger.error(EELFLoggerDelegate.errorLogger, "getNotificationUpdateRate failed", e);
179             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.toString(), null);
180         }
181     }
182
183     @GetMapping(value = {
184             "/portalApi/notificationRead" }, produces = "application/json")
185     public PortalRestResponse<Map<String, String>> notificationRead(
186             @RequestParam("notificationId") String notificationID, HttpServletRequest request) {
187         try {
188             userNotificationService.setNotificationRead(Long.parseLong(notificationID), UserUtils.getUserId(request));
189             return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, null);
190         } catch (Exception e) {
191             logger.error(EELFLoggerDelegate.errorLogger, "notificationRead failed", e);
192             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.toString(), null);
193         }
194     }
195
196     @GetMapping(value = {
197             "/portalApi/getNotificationHistory" }, produces = "application/json")
198     public List<EpNotificationItemVO> getNotificationHistory(HttpServletRequest request, HttpServletResponse response) {
199         EPUser user = EPUserUtils.getUserSession(request);
200         List<EpNotificationItemVO> notificationList = null;
201         notificationList = userNotificationService.getNotificationHistoryVO(user.getId());
202         return notificationList;
203     }
204
205     @GetMapping(value = { "/portalApi/notificationRole/{notificationId}/roles" }, produces = "application/json")
206     public List<Integer> testGetRoles(HttpServletRequest request, @PathVariable("notificationId") Long notificationId) {
207         List<EpRoleNotificationItem> notifRoles = userNotificationService.getNotificationRoles(notificationId);
208         ArrayList<Integer> rolesList = new ArrayList<>();
209         for (EpRoleNotificationItem notifRole : notifRoles) {
210             rolesList.add(notifRole.roleId);
211         }
212         return rolesList;
213     }
214
215     @GetMapping(value = { "/portalApi/getNotificationAppRoles" }, produces = "application/json")
216     public List<EcompAppRole> getNotificationAppRoles(HttpServletRequest request, HttpServletResponse response) {
217         List<EcompAppRole> epAppRoleList = null;
218         try {
219             epAppRoleList = userNotificationService.getAppRoleList();
220         } catch (Exception e) {
221             logger.error(EELFLoggerDelegate.errorLogger,
222                     "Exception occurred while performing UserNofiticationController.getNotificationAppRoles. Details: ",
223                     e);
224         }
225         return epAppRoleList;
226     }
227
228     @GetMapping(value = {
229             "/portalApi/getMessageRecipients" }, produces = "application/json")
230     public List<String> getMessageRecipients(@RequestParam("notificationId") Long notificationID) {
231         // EPUser user = EPUserUtils.getUserSession(request);
232         List<String> messageUserRecipients = null;
233         messageUserRecipients = userNotificationService.getMessageRecipients(notificationID);
234         return messageUserRecipients;
235     }
236
237 }