Replace ecomp references
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / UserNotificationServiceImpl.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.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.onap.portalapp.portal.domain.EPUser;
49 import org.onap.portalapp.portal.domain.EPUserNotification;
50 import org.onap.portalapp.portal.domain.EcompAppRole;
51 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
52 import org.onap.portalapp.portal.transport.EpNotificationItem;
53 import org.onap.portalapp.portal.transport.EpNotificationItemVO;
54 import org.onap.portalapp.portal.transport.EpRoleNotificationItem;
55 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.onap.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.onap.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.onap.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.onap.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.onap.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.onap.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.onap.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.onap.portalapp.portal.service.UserNotificationService#
175          * saveNotification(org.onap.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                         if(updateNotificationItem != null) {
200                                 notificationItem.setRoles(updateNotificationItem.getRoles());
201                         }
202                 }
203                 if (notificationItem.msgSource == null) {
204                         notificationItem.setMsgSource("EP");
205                 }
206                 getDataAccessService().saveDomainObject(notificationItem, null);
207                 return "";
208
209         }
210
211         @Override
212         public List<EPUser> getUsersByOrgIds(List<String> OrgIds) {
213                 Map<String, Object> params = new HashMap<String, Object>();
214                 params.put("OrgIds", OrgIds);
215                 @SuppressWarnings("unchecked")
216                 List<EPUser> userList = dataAccessService.executeNamedQuery("getUsersByOrgIdsNotifications", params, null);
217                 return userList;
218         }
219
220         @Override
221         public List<String> getMessageRecipients(Long notificationId) {
222                 Map<String, String> params = new HashMap<>();
223                 params.put("notificationId", Long.toString(notificationId));
224                 @SuppressWarnings("unchecked")
225                 List<String> activeUsers = dataAccessService.executeNamedQuery("messageRecipients", params, null);
226                 return activeUsers;
227         }
228
229         public DataAccessService getDataAccessService() {
230                 return dataAccessService;
231         }
232
233         public void setDataAccessService(DataAccessService dataAccessService) {
234                 this.dataAccessService = dataAccessService;
235         }
236
237         public SessionFactory getSessionFactory() {
238                 return sessionFactory;
239         }
240
241         public void setSessionFactory(SessionFactory sessionFactory) {
242                 this.sessionFactory = sessionFactory;
243         }
244
245         @Override
246         public void deleteNotificationsFromEpNotificationTable() {
247                 Map<String, String> params = new HashMap<String, String>();
248                 dataAccessService.executeNamedQuery("deleteNotificationsFromEpNotificationTable", params, null);
249         }
250
251         @Override
252         public void deleteNotificationsFromEpUserNotificationTable() {
253                 Map<String, String> params = new HashMap<String, String>();
254                 dataAccessService.executeNamedQuery("deleteNotificationsFromEpUserNotificationTable", params, null);
255
256         }
257
258         @Override
259         public void deleteNotificationsFromEpRoleNotificationTable() {
260                 Map<String, String> params = new HashMap<String, String>();
261                 dataAccessService.executeNamedQuery("deleteNotificationsFromEpRoleNotificationTable", params, null);
262         }
263
264 }