Merge "HealthCheckController up"
[portal.git] / portal-BE / src / main / java / org / onap / portal / controller / UserNotificationController.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
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
41 package org.onap.portal.controller;
42
43 import java.security.Principal;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50 import org.onap.portal.domain.db.ep.EpNotification;
51 import org.onap.portal.domain.db.ep.EpRoleNotification;
52 import org.onap.portal.domain.db.fn.FnMenuFunctionalRoles;
53 import org.onap.portal.domain.db.fn.FnUser;
54 import org.onap.portal.domain.dto.PortalRestResponse;
55 import org.onap.portal.domain.dto.PortalRestStatusEnum;
56 import org.onap.portal.domain.dto.ecomp.EcompAppRole;
57 import org.onap.portal.domain.dto.transport.EpNotificationItemVO;
58 import org.onap.portal.logging.aop.EPAuditLog;
59 import org.onap.portal.service.EcompAppRoleService;
60 import org.onap.portal.service.EpNotificationItemVOService;
61 import org.onap.portal.service.epNotification.EpNotificationService;
62 import org.onap.portal.service.epRoleNotification.EpRoleNotificationService;
63 import org.onap.portal.service.epUserNotification.EpUserNotificationService;
64 import org.onap.portal.service.menuFunctionalRoles.FnMenuFunctionalRolesService;
65 import org.onap.portal.service.user.FnUserService;
66 import org.onap.portal.utils.EPCommonSystemProperties;
67 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
68 import org.onap.portalsdk.core.util.SystemProperties;
69 import org.onap.portalsdk.core.web.support.UserUtils;
70 import org.springframework.beans.factory.annotation.Autowired;
71 import org.springframework.context.annotation.EnableAspectJAutoProxy;
72 import org.springframework.web.bind.annotation.PathVariable;
73 import org.springframework.web.bind.annotation.RequestBody;
74 import org.springframework.web.bind.annotation.RequestMapping;
75 import org.springframework.web.bind.annotation.RequestMethod;
76 import org.springframework.web.bind.annotation.RequestParam;
77 import org.springframework.web.bind.annotation.RestController;
78
79 @RestController
80 @EnableAspectJAutoProxy
81 @EPAuditLog
82 public class UserNotificationController {
83
84     private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(UserNotificationController.class);
85
86     private final FnMenuFunctionalRolesService functionalMenuService;
87     private final FnUserService fnUserService;
88     private final EpNotificationService userNotificationService;
89     private final EpUserNotificationService epUserNotificationService;
90     private final EpRoleNotificationService roleNotificationService;
91     private final EcompAppRoleService ecompAppRoleService;
92     private final EpNotificationItemVOService epNotificationItemVOService;
93
94     private static final String SUCCESS = "success";
95     private static final String FAILURE = "FAILURE";
96
97     @Autowired
98     public UserNotificationController(
99         final FnMenuFunctionalRolesService functionalMenuService, final FnUserService fnUserService,
100         final EpNotificationService epNotificationService,
101         final EpUserNotificationService epUserNotificationService,
102         final EpRoleNotificationService roleNotificationService,
103         final EcompAppRoleService ecompAppRoleService,
104         final EpNotificationItemVOService epNotificationItemVOService) {
105         this.functionalMenuService = functionalMenuService;
106         this.fnUserService = fnUserService;
107         this.userNotificationService = epNotificationService;
108         this.epUserNotificationService = epUserNotificationService;
109         this.roleNotificationService = roleNotificationService;
110         this.ecompAppRoleService = ecompAppRoleService;
111         this.epNotificationItemVOService = epNotificationItemVOService;
112     }
113
114     @RequestMapping(value = {
115             "/portalApi/getFunctionalMenuRole" }, method = RequestMethod.GET, produces = "application/json")
116     public List<FnMenuFunctionalRoles> getMenuIdRoleId(Principal principal, HttpServletRequest request, HttpServletResponse response) {
117         return functionalMenuService.findAll();
118     }
119
120     @RequestMapping(value = {
121             "/portalApi/getNotifications" }, method = RequestMethod.GET, produces = "application/json")
122     public PortalRestResponse<List<EpNotification>> getNotifications(Principal principal, HttpServletRequest request,
123             HttpServletResponse response) {
124         FnUser user = fnUserService.loadUserByUsername(principal.getName());
125         PortalRestResponse<List<EpNotification>> portalRestResponse = null;
126         try {
127             List<EpNotification> notificationList = userNotificationService.getNotifications(user.getId());
128             portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS,
129                     notificationList);
130         } catch (Exception e) {
131             logger.error(EELFLoggerDelegate.errorLogger, "getAllAppsAndContacts failed", e);
132             portalRestResponse = new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
133                     e.getMessage(), null);
134         }
135         return portalRestResponse;
136     }
137
138     @RequestMapping(value = {
139             "/portalApi/getAdminNotifications" }, method = RequestMethod.GET, produces = "application/json")
140     public List<EpNotificationItemVO> getAdminNotifications(Principal principal, HttpServletRequest request, HttpServletResponse response) {
141         List<EpNotificationItemVO> adminNotificationList = null;
142         FnUser user = fnUserService.loadUserByUsername(principal.getName());
143         adminNotificationList = userNotificationService.getAdminNotificationVOS(user.getId());
144         return adminNotificationList;
145     }
146
147     @RequestMapping(value = "/portalApi/saveNotification", method = RequestMethod.POST, produces = "application/json")
148     public PortalRestResponse<String> save(Principal principal, HttpServletRequest request, HttpServletResponse response,
149             @RequestBody EpNotification notificationItem) {
150         FnUser fnUser = fnUserService.loadUserByUsername(principal.getName());
151         if (notificationItem == null || notificationItem.getMsgHeader() == null)
152             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
153                     "Notification Header cannot be null or empty");
154         if (notificationItem.getEndTime().compareTo(notificationItem.getStartTime()) < 0) {
155             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
156                     "End Time should be greater than  start time");
157         }
158
159         if (("N".equals(notificationItem.getIsForAllRoles())) && notificationItem.getRoleIds().isEmpty()) {
160             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE,
161                     "No Roles Ids Exist for the selected Roles");
162         }
163
164         Long creatorId = UserUtils.getUserIdAsLong(request);
165         notificationItem.setCreatorId(fnUser);
166
167         //TODO
168         // Front-end date picker does not accept a time value, so all
169         // values are the start of the chosen day in the local time zone.
170         // Move the end time value to the very end of the chosen day.
171         // Avoid Calendar.getDefault() which uses the server's locale.
172         //Long endTime = notificationItem.getEndTime()
173         //endTime += (23 * 3600 + 59 * 60 + 59) * 1000;
174         //notificationItem.getEndTime().setTime(endTime);
175
176         try {
177             userNotificationService.saveNotification(notificationItem);
178         } catch (Exception e) {
179             logger.error(EELFLoggerDelegate.errorLogger, "saveNotification failed", e);
180             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, FAILURE, e.getMessage());
181         }
182         return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", "");
183     }
184
185     @RequestMapping(value = {
186             "/portalApi/notificationUpdateRate" }, method = RequestMethod.GET, produces = "application/json")
187     public PortalRestResponse<Map<String, String>> getNotificationUpdateRate(HttpServletRequest request) {
188         try {
189             String updateRate = SystemProperties.getProperty(EPCommonSystemProperties.NOTIFICATION_UPDATE_RATE);
190             String updateDuration = SystemProperties.getProperty(EPCommonSystemProperties.NOTIFICATION_UPDATE_DURATION);
191             Integer rateInMiliSec = Integer.parseInt(updateRate) * 1000;
192             Integer durationInMiliSec = Integer.parseInt(updateDuration) * 1000;
193             Map<String, String> results = new HashMap<>();
194             results.put("updateRate", String.valueOf(rateInMiliSec));
195             results.put("updateDuration", String.valueOf(durationInMiliSec));
196             return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, results);
197         } catch (Exception e) {
198             logger.error(EELFLoggerDelegate.errorLogger, "getNotificationUpdateRate failed", e);
199             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.toString(), null);
200         }
201     }
202
203     @RequestMapping(value = {
204             "/portalApi/notificationRead" }, method = RequestMethod.GET, produces = "application/json")
205     public PortalRestResponse<Map<String, String>> notificationRead(
206             @RequestParam("notificationId") Long notificationID, HttpServletRequest request) {
207         try {
208             epUserNotificationService.setNotificationRead(notificationID, UserUtils.getUserId(request));
209             return new PortalRestResponse<>(PortalRestStatusEnum.OK, SUCCESS, null);
210         } catch (Exception e) {
211             logger.error(EELFLoggerDelegate.errorLogger, "notificationRead failed", e);
212             return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.toString(), null);
213         }
214     }
215
216     @RequestMapping(value = {
217             "/portalApi/getNotificationHistory" }, method = RequestMethod.GET, produces = "application/json")
218     public List<EpNotificationItemVO> getNotificationHistory(Principal principal, HttpServletRequest request, HttpServletResponse response) {
219         FnUser user = fnUserService.loadUserByUsername(principal.getName());
220         return epNotificationItemVOService.getNotificationHistoryVO(user.getId());
221     }
222
223     @RequestMapping(value = { "/portalApi/notificationRole/{notificationId}/roles" }, method = {
224             RequestMethod.GET }, produces = "application/json")
225     public List<Long> testGetRoles(HttpServletRequest request, @PathVariable("notificationId") Long notificationId) {
226         List<EpRoleNotification> notifRoles = roleNotificationService.getNotificationRoles(notificationId);
227         ArrayList<Long> rolesList = new ArrayList<>();
228         for (EpRoleNotification notifRole : notifRoles) {
229             rolesList.add(notifRole.getRoleId().getId());
230         }
231         return rolesList;
232     }
233
234     @RequestMapping(value = { "/portalApi/getNotificationAppRoles" }, method = {
235             RequestMethod.GET }, produces = "application/json")
236     public List<EcompAppRole> getNotificationAppRoles(HttpServletRequest request, HttpServletResponse response) {
237         List<EcompAppRole> epAppRoleList = null;
238         try {
239             epAppRoleList = ecompAppRoleService.getAppRoleList();
240         } catch (Exception e) {
241             logger.error(EELFLoggerDelegate.errorLogger,
242                     "Exception occurred while performing UserNofiticationController.getNotificationAppRoles. Details: ",
243                     e);
244         }
245         return epAppRoleList;
246     }
247
248     @RequestMapping(value = {
249             "/portalApi/getMessageRecipients" }, method = RequestMethod.GET, produces = "application/json")
250     public List<String> getMessageRecipients(@RequestParam("notificationId") Long notificationID) {
251         return userNotificationService.getMessageRecipients(notificationID);
252     }
253
254 }