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