Fix NPE & other sonar issues
[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     private static final String SUCCESS = "success";
88     private static final String FAILURE = "FAILURE";
89
90     @RequestMapping(value = {
91             "/portalApi/getFunctionalMenuRole" }, method = RequestMethod.GET, produces = "application/json")
92     public List<FunctionalMenuRole> getMenuIdRoleId(HttpServletRequest request, HttpServletResponse response) {
93         // EPUser user = EPUserUtils.getUserSession(request);
94         List<FunctionalMenuRole> menuRoleList = null;
95         menuRoleList = functionalMenuService.getFunctionalMenuRole();
96         return menuRoleList;
97     }
98
99     @RequestMapping(value = {
100             "/portalApi/getNotifications" }, method = RequestMethod.GET, produces = "application/json")
101     public PortalRestResponse<List<EpNotificationItem>> getNotifications(HttpServletRequest request,
102             HttpServletResponse response) {
103         EPUser user = EPUserUtils.getUserSession(request);
104         PortalRestResponse<List<EpNotificationItem>> portalRestResponse = null;
105         try {
106             List<EpNotificationItem> notificationList = userNotificationService.getNotifications(user.getId());
107             portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS,
108                     notificationList);
109         } catch (Exception e) {
110             logger.error(EELFLoggerDelegate.errorLogger, "getAllAppsAndContacts failed", e);
111             portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
112                     e.getMessage(), null);
113         }
114         return portalRestResponse;
115     }
116
117     @RequestMapping(value = {
118             "/portalApi/getAdminNotifications" }, method = RequestMethod.GET, produces = "application/json")
119     public List<EpNotificationItemVO> getAdminNotifications(HttpServletRequest request, HttpServletResponse response) {
120         List<EpNotificationItemVO> adminNotificationList = null;
121         EPUser user = EPUserUtils.getUserSession(request);
122         adminNotificationList = userNotificationService.getAdminNotificationVOS(user.getId());
123         return adminNotificationList;
124     }
125
126     @RequestMapping(value = "/portalApi/saveNotification", method = RequestMethod.POST, produces = "application/json")
127     public PortalRestResponse<String> save(HttpServletRequest request, HttpServletResponse response,
128             @RequestBody EpNotificationItem notificationItem) {
129
130         if (notificationItem == null || notificationItem.getMsgHeader() == null)
131             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
132                     "Notification Header cannot be null or empty");
133         if (notificationItem.getEndTime().compareTo(notificationItem.getStartTime()) < 0) {
134             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
135                     "End Time should be greater than  start time");
136         }
137
138         if ((notificationItem.getIsForAllRoles() == "N") && notificationItem.getRoleIds().isEmpty()) {
139             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
140                     "No Roles Ids Exist for the selected Roles");
141         }
142
143         Long creatorId = UserUtils.getUserIdAsLong(request);
144         notificationItem.setCreatorId(creatorId);
145
146         // Front-end date picker does not accept a time value, so all
147         // values are the start of the chosen day in the local time zone.
148         // Move the end time value to the very end of the chosen day.
149         // Avoid Calendar.getDefault() which uses the server's locale.
150         Long endTime = notificationItem.getEndTime().getTime();
151         endTime += (23 * 3600 + 59 * 60 + 59) * 1000;
152         notificationItem.getEndTime().setTime(endTime);
153
154         try {
155             userNotificationService.saveNotification(notificationItem);
156         } catch (Exception e) {
157             logger.error(EELFLoggerDelegate.errorLogger, "saveNotification failed", e);
158             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
159         }
160         return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
161     }
162
163     @RequestMapping(value = {
164             "/portalApi/notificationUpdateRate" }, method = RequestMethod.GET, produces = "application/json")
165     public PortalRestResponse<Map<String, String>> getNotificationUpdateRate(HttpServletRequest request) {
166         try {
167             String updateRate = SystemProperties.getProperty(EPCommonSystemProperties.NOTIFICATION_UPDATE_RATE);
168             String updateDuration = SystemProperties.getProperty(EPCommonSystemProperties.NOTIFICATION_UPDATE_DURATION);
169             Integer rateInMiliSec = Integer.valueOf(updateRate) * 1000;
170             Integer durationInMiliSec = Integer.valueOf(updateDuration) * 1000;
171             Map<String, String> results = new HashMap<>();
172             results.put("updateRate", String.valueOf(rateInMiliSec));
173             results.put("updateDuration", String.valueOf(durationInMiliSec));
174             return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, results);
175         } catch (Exception e) {
176             logger.error(EELFLoggerDelegate.errorLogger, "getNotificationUpdateRate failed", e);
177             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.toString(), null);
178         }
179     }
180
181     @RequestMapping(value = {
182             "/portalApi/notificationRead" }, method = RequestMethod.GET, produces = "application/json")
183     public PortalRestResponse<Map<String, String>> notificationRead(
184             @RequestParam("notificationId") String notificationID, HttpServletRequest request) {
185         try {
186             userNotificationService.setNotificationRead(Long.parseLong(notificationID), UserUtils.getUserId(request));
187             return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, null);
188         } catch (Exception e) {
189             logger.error(EELFLoggerDelegate.errorLogger, "notificationRead failed", e);
190             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.toString(), null);
191         }
192     }
193
194     @RequestMapping(value = {
195             "/portalApi/getNotificationHistory" }, method = RequestMethod.GET, produces = "application/json")
196     public List<EpNotificationItemVO> getNotificationHistory(HttpServletRequest request, HttpServletResponse response) {
197         EPUser user = EPUserUtils.getUserSession(request);
198         List<EpNotificationItemVO> notificationList = null;
199         notificationList = userNotificationService.getNotificationHistoryVO(user.getId());
200         return notificationList;
201     }
202
203     @RequestMapping(value = { "/portalApi/notificationRole/{notificationId}/roles" }, method = {
204             RequestMethod.GET }, produces = "application/json")
205     public List<Integer> testGetRoles(HttpServletRequest request, @PathVariable("notificationId") Long notificationId) {
206         List<EpRoleNotificationItem> notifRoles = userNotificationService.getNotificationRoles(notificationId);
207         ArrayList<Integer> rolesList = new ArrayList<>();
208         for (EpRoleNotificationItem notifRole : notifRoles) {
209             rolesList.add(notifRole.roleId);
210         }
211         return rolesList;
212     }
213
214     @RequestMapping(value = { "/portalApi/getNotificationAppRoles" }, method = {
215             RequestMethod.GET }, 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     @RequestMapping(value = {
229             "/portalApi/getMessageRecipients" }, method = RequestMethod.GET, 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 }