Update license; improve coverage; add docs dir
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / UserNotificationServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.service;
39
40 import java.util.Date;
41 import java.util.HashMap;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Set;
46
47 import org.hibernate.SessionFactory;
48 import org.openecomp.portalapp.portal.domain.EPUser;
49 import org.openecomp.portalapp.portal.domain.EPUserNotification;
50 import org.openecomp.portalapp.portal.domain.EcompAppRole;
51 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
52 import org.openecomp.portalapp.portal.transport.EpNotificationItem;
53 import org.openecomp.portalapp.portal.transport.EpNotificationItemVO;
54 import org.openecomp.portalapp.portal.transport.EpRoleNotificationItem;
55 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.openecomp.portalsdk.core.service.DataAccessService;
57 import org.springframework.beans.factory.annotation.Autowired;
58 import org.springframework.context.annotation.EnableAspectJAutoProxy;
59 import org.springframework.stereotype.Service;
60
61 @Service("userNotificationService")
62 @org.springframework.context.annotation.Configuration
63 @EnableAspectJAutoProxy
64 @EPMetricsLog
65 public class UserNotificationServiceImpl implements UserNotificationService {
66         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FunctionalMenuServiceImpl.class);
67
68         @Autowired
69         private DataAccessService dataAccessService;
70         @Autowired
71         private SessionFactory sessionFactory;
72
73         /*
74          * (non-Javadoc)
75          * 
76          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
77          * getNotifications(java.lang.Long)
78          */
79         @Override
80         public List<EpNotificationItem> getNotifications(Long userId) {
81                 Map<String, String> params = new HashMap<String, String>();
82                 params.put("user_id", userId.toString());
83                 @SuppressWarnings("unchecked")
84                 List<EpNotificationItem> notificationList = dataAccessService.executeNamedQuery("getNotifications", params,
85                                 null);
86                 // set the roles to null for pure retrieval of all notifications
87                 for (EpNotificationItem item : notificationList) {
88                         item.setRoles(null);
89                 }
90                 return notificationList;
91         }
92
93         /*
94          * (non-Javadoc)
95          * 
96          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
97          * getNotificationHistoryVO(java.lang.Long)
98          */
99         @Override
100         public List<EpNotificationItemVO> getNotificationHistoryVO(Long userId) {
101                 Map<String, String> params = new HashMap<String, String>();
102                 params.put("user_id", userId.toString());
103                 @SuppressWarnings("unchecked")
104                 List<EpNotificationItemVO> notificationList = dataAccessService.executeNamedQuery("getNotificationHistoryVO",
105                                 params, null);
106                 return notificationList;
107         }
108
109         /*
110          * (non-Javadoc)
111          * 
112          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
113          * getAdminNotificationVOS()
114          */
115         @Override
116         public List<EpNotificationItemVO> getAdminNotificationVOS(Long userId) {
117                 Map<String, String> params = new HashMap<String, String>();
118                 params.put("user_id", userId.toString());
119                 @SuppressWarnings("unchecked")
120                 List<EpNotificationItemVO> notificationList = dataAccessService
121                                 .executeNamedQuery("getAdminNotificationHistoryVO", params, null);
122                 return notificationList;
123         }
124
125         /*
126          * (non-Javadoc)
127          * 
128          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
129          * getNotificationRoles(java.lang.Long)
130          */
131         @Override
132         public List<EpRoleNotificationItem> getNotificationRoles(Long notificationId) {
133                 Map<String, String> params = new HashMap<String, String>();
134                 params.put("notificationId", Long.toString(notificationId));
135                 @SuppressWarnings("unchecked")
136                 List<EpRoleNotificationItem> roleNotifList = dataAccessService.executeNamedQuery("getNotificationRoles", params,
137                                 null);
138                 return roleNotifList;
139         }
140
141         /*
142          * (non-Javadoc)
143          * 
144          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
145          * getAppRoleList()
146          */
147         @SuppressWarnings("unchecked")
148         @Override
149         public List<EcompAppRole> getAppRoleList() {
150                 List<EcompAppRole> appRoleList = (List<EcompAppRole>) dataAccessService
151                                 .executeNamedQuery("getEpNotificationAppRoles", null, null);
152                 return appRoleList;
153         }
154
155         /*
156          * (non-Javadoc)
157          * 
158          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
159          * setNotificationsRead(java.lang.Long, int)
160          */
161         @Override
162         public void setNotificationRead(Long notificationId, int userId) {
163                 EPUserNotification userNotification = new EPUserNotification();
164                 userNotification.setNotificationId(notificationId);
165                 userNotification.setUpdateTime(new Date());
166                 userNotification.setViewed("Y");
167                 userNotification.setUserId((long) userId);
168                 getDataAccessService().saveDomainObject(userNotification, null);
169         }
170
171         /*
172          * (non-Javadoc)
173          * 
174          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
175          * saveNotification(org.openecomp.portalapp.portal.transport.
176          * EpNotificationItem)
177          */
178         @Override
179         public String saveNotification(EpNotificationItem notificationItem) throws Exception {
180
181                 // gather the roles
182                 if (notificationItem.getRoleIds() != null && !notificationItem.getIsForAllRoles().equals("Y")) {
183                         if (notificationItem.getRoles() == null) {
184                                 Set<EpRoleNotificationItem> roleSet = new HashSet<EpRoleNotificationItem>();
185                                 notificationItem.setRoles(roleSet);
186                         }
187                         for (Long roleId : notificationItem.getRoleIds()) {
188                                 EpRoleNotificationItem roleItem = new EpRoleNotificationItem();
189                                 roleItem.setNotificationId(notificationItem.getNotificationId());
190                                 roleItem.setRoleId(roleId.intValue());
191                                 notificationItem.getRoles().add(roleItem);
192                         }
193                 }
194
195                 // for updates fetch roles and then save
196                 if (notificationItem.getNotificationId() != null) {
197                         EpNotificationItem updateNotificationItem = (EpNotificationItem) getDataAccessService()
198                                         .getDomainObject(EpNotificationItem.class, notificationItem.getNotificationId(), null);
199                         notificationItem.setRoles(updateNotificationItem.getRoles());
200                 }
201                 if (notificationItem.msgSource == null) {
202                         notificationItem.setMsgSource("EP");
203                 }
204                 getDataAccessService().saveDomainObject(notificationItem, null);
205                 return "";
206
207         }
208
209         @Override
210         public List<EPUser> getUsersByOrgIds(List<String> OrgIds) {
211                 Map<String, Object> params = new HashMap<String, Object>();
212                 params.put("OrgIds", OrgIds);
213                 @SuppressWarnings("unchecked")
214                 List<EPUser> userList = dataAccessService.executeNamedQuery("getUsersByOrgIdsNotifications", params, null);
215                 return userList;
216         }
217
218         @Override
219         public List<String> getMessageRecipients(Long notificationId) {
220                 Map<String, String> params = new HashMap<>();
221                 params.put("notificationId", Long.toString(notificationId));
222                 @SuppressWarnings("unchecked")
223                 List<String> activeUsers = dataAccessService.executeNamedQuery("messageRecipients", params, null);
224                 return activeUsers;
225         }
226
227         public DataAccessService getDataAccessService() {
228                 return dataAccessService;
229         }
230
231         public void setDataAccessService(DataAccessService dataAccessService) {
232                 this.dataAccessService = dataAccessService;
233         }
234
235         public SessionFactory getSessionFactory() {
236                 return sessionFactory;
237         }
238
239         public void setSessionFactory(SessionFactory sessionFactory) {
240                 this.sessionFactory = sessionFactory;
241         }
242
243         @Override
244         public void deleteNotificationsFromEpNotificationTable() {
245                 Map<String, String> params = new HashMap<String, String>();
246                 dataAccessService.executeNamedUpdateQuery("deleteNotificationsFromEpNotificationTable", params, null);
247         }
248
249         @Override
250         public void deleteNotificationsFromEpUserNotificationTable() {
251                 Map<String, String> params = new HashMap<String, String>();
252                 dataAccessService.executeNamedUpdateQuery("deleteNotificationsFromEpUserNotificationTable", params, null);
253
254         }
255
256         @Override
257         public void deleteNotificationsFromEpRoleNotificationTable() {
258                 Map<String, String> params = new HashMap<String, String>();
259                 dataAccessService.executeNamedUpdateQuery("deleteNotificationsFromEpRoleNotificationTable", params, null);
260         }
261
262 }