[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / UserNotificationServiceImpl.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.service;\r
21 \r
22 import java.util.Date;\r
23 import java.util.HashMap;\r
24 import java.util.HashSet;\r
25 import java.util.List;\r
26 import java.util.Map;\r
27 import java.util.Set;\r
28 \r
29 import org.hibernate.SessionFactory;\r
30 import org.openecomp.portalapp.portal.domain.EPUser;\r
31 import org.openecomp.portalapp.portal.domain.EPUserNotification;\r
32 import org.openecomp.portalapp.portal.domain.EcompAppRole;\r
33 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;\r
34 import org.openecomp.portalapp.portal.transport.EpNotificationItem;\r
35 import org.openecomp.portalapp.portal.transport.EpNotificationItemVO;\r
36 import org.openecomp.portalapp.portal.transport.EpRoleNotificationItem;\r
37 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
38 import org.openecomp.portalsdk.core.service.DataAccessService;\r
39 import org.springframework.beans.factory.annotation.Autowired;\r
40 import org.springframework.context.annotation.EnableAspectJAutoProxy;\r
41 import org.springframework.stereotype.Service;\r
42 \r
43 @Service("userNotificationService")\r
44 @org.springframework.context.annotation.Configuration\r
45 @EnableAspectJAutoProxy\r
46 @EPMetricsLog\r
47 public class UserNotificationServiceImpl implements UserNotificationService {\r
48         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FunctionalMenuServiceImpl.class);\r
49 \r
50         @Autowired\r
51         private DataAccessService dataAccessService;\r
52         @Autowired\r
53         private SessionFactory sessionFactory;\r
54 \r
55         /*\r
56          * (non-Javadoc)\r
57          * @see org.openecomp.portalapp.portal.service.UserNotificationService#getNotifications(java.lang.Long)\r
58          */\r
59         @Override\r
60         public List<EpNotificationItem> getNotifications(Long userId) {\r
61                 Map<String, String> params = new HashMap<String, String>();\r
62                 params.put("user_id", userId.toString());\r
63                 @SuppressWarnings("unchecked")\r
64                 List<EpNotificationItem> notificationList = dataAccessService.executeNamedQuery("getNotifications", params,\r
65                                 null);\r
66                 // set the roles to null for pure retrieval of all notifications\r
67                 for (EpNotificationItem item : notificationList) {\r
68                         item.setRoles(null);\r
69                 }\r
70                 return notificationList;\r
71         }\r
72 \r
73         /*\r
74          * (non-Javadoc)\r
75          * @see org.openecomp.portalapp.portal.service.UserNotificationService#getNotificationHistoryVO(java.lang.Long)\r
76          */\r
77         @Override\r
78         public List<EpNotificationItemVO> getNotificationHistoryVO(Long userId) {\r
79                 Map<String, String> params = new HashMap<String, String>();\r
80                 params.put("user_id", userId.toString());\r
81                 @SuppressWarnings("unchecked")\r
82                 List<EpNotificationItemVO> notificationList = dataAccessService.executeNamedQuery("getNotificationHistoryVO",\r
83                                 params, null);\r
84                 return notificationList;\r
85         }\r
86 \r
87         /*\r
88          * (non-Javadoc)\r
89          * @see org.openecomp.portalapp.portal.service.UserNotificationService#getAdminNotificationVOS()\r
90          */\r
91         @Override\r
92         public List<EpNotificationItemVO> getAdminNotificationVOS() {\r
93                 Map<String, String> params = new HashMap<String, String>();\r
94                 @SuppressWarnings("unchecked")\r
95                 List<EpNotificationItemVO> notificationList = dataAccessService\r
96                                 .executeNamedQuery("getAdminNotificationHistoryVO", params, null);\r
97                 return notificationList;\r
98         }\r
99 \r
100         /*\r
101          * (non-Javadoc)\r
102          * @see org.openecomp.portalapp.portal.service.UserNotificationService#getNotificationRoles(java.lang.Long)\r
103          */\r
104         @Override\r
105         public List<EpRoleNotificationItem> getNotificationRoles(Long notificationId) {\r
106                 Map<String, String> params = new HashMap<String, String>();\r
107                 params.put("notificationId", Long.toString(notificationId));\r
108                 @SuppressWarnings("unchecked")\r
109                 List<EpRoleNotificationItem> roleNotifList = dataAccessService.executeNamedQuery("getNotificationRoles", params,\r
110                                 null);\r
111                 return roleNotifList;\r
112         }\r
113 \r
114         /*\r
115          * (non-Javadoc)\r
116          * @see org.openecomp.portalapp.portal.service.UserNotificationService#getAppRoleList()\r
117          */\r
118         @SuppressWarnings("unchecked")\r
119         @Override\r
120         public List<EcompAppRole> getAppRoleList() {\r
121                 List<EcompAppRole> appRoleList = (List<EcompAppRole>) dataAccessService.executeNamedQuery("getEpNotificationAppRoles", null, null);             \r
122                 return appRoleList;             \r
123         }       \r
124         \r
125         /*\r
126          * (non-Javadoc)\r
127          * \r
128          * @see org.openecomp.portalapp.portal.service.UserNotificationService#\r
129          * setNotificationsRead(java.lang.Long, int)\r
130          */\r
131         @Override\r
132         public void setNotificationRead(Long notificationId, int userId) {\r
133                 EPUserNotification userNotification = new EPUserNotification();\r
134                 userNotification.setNotificationId(notificationId);\r
135                 userNotification.setUpdateTime(new Date());\r
136                 userNotification.setViewed("Y");\r
137                 userNotification.setUserId((long) userId);\r
138                 getDataAccessService().saveDomainObject(userNotification, null);\r
139         }\r
140 \r
141         /*\r
142          * (non-Javadoc)\r
143          * @see org.openecomp.portalapp.portal.service.UserNotificationService#saveNotification(org.openecomp.portalapp.portal.transport.EpNotificationItem)\r
144          */\r
145         @Override\r
146         public String saveNotification(EpNotificationItem notificationItem) throws Exception {\r
147 \r
148                 // gather the roles\r
149                 if (notificationItem.getRoleIds() != null && !notificationItem.getIsForAllRoles().equals("Y")) {\r
150                         if (notificationItem.getRoles() == null) {\r
151                                 Set<EpRoleNotificationItem> roleSet = new HashSet<EpRoleNotificationItem>();\r
152                                 notificationItem.setRoles(roleSet);\r
153                         }\r
154                         for (Long roleId : notificationItem.getRoleIds()) {\r
155                                 EpRoleNotificationItem roleItem = new EpRoleNotificationItem();\r
156                                 roleItem.setNotificationId(notificationItem.getNotificationId());\r
157                                 roleItem.setRoleId(roleId.intValue());\r
158                                 notificationItem.getRoles().add(roleItem);\r
159                         }\r
160                 }\r
161 \r
162                 \r
163                         // for updates fetch roles and then save\r
164                         if (notificationItem.getNotificationId() != null) {\r
165                                 EpNotificationItem updateNotificationItem = (EpNotificationItem) getDataAccessService()\r
166                                                 .getDomainObject(EpNotificationItem.class, notificationItem.getNotificationId(), null);\r
167                                 notificationItem.setRoles(updateNotificationItem.getRoles());\r
168                         }\r
169                          if(notificationItem.msgSource == null)\r
170                     {\r
171                         notificationItem.setMsgSource("EP");\r
172                     }\r
173                         getDataAccessService().saveDomainObject(notificationItem, null);\r
174                          return "" ;\r
175 \r
176                 } \r
177           @Override\r
178             public List<EPUser> getUsersByOrgIds(List<String> OrgIds) {\r
179                 Map<String, Object> params = new HashMap<String, Object>();\r
180                 params.put("OrgIds", OrgIds);\r
181                 @SuppressWarnings("unchecked")\r
182                 List<EPUser> userList = dataAccessService.executeNamedQuery("getUsersByOrgIdsNotifications", params,    null);\r
183                 return userList;\r
184             }\r
185           \r
186           @Override\r
187                         public List<String> getMessageRecipients(Long notificationId) {\r
188                                 Map<String, String> params = new HashMap<>();\r
189                                 params.put("notificationId", Long.toString(notificationId));\r
190                                 @SuppressWarnings("unchecked")\r
191                                 List<String> activeUsers = dataAccessService.executeNamedQuery("messageRecipients", params, null);\r
192                                 return activeUsers;\r
193                         }\r
194 \r
195         public DataAccessService getDataAccessService() {\r
196                 return dataAccessService;\r
197         }\r
198 \r
199         public void setDataAccessService(DataAccessService dataAccessService) {\r
200                 this.dataAccessService = dataAccessService;\r
201         }\r
202 \r
203         public SessionFactory getSessionFactory() {\r
204                 return sessionFactory;\r
205         }\r
206 \r
207         public void setSessionFactory(SessionFactory sessionFactory) {\r
208                 this.sessionFactory = sessionFactory;\r
209         }\r
210 \r
211 }\r