52bc4922ef17728e234709096e9a1da3f5e92f26
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / UserNotificationServiceImpl.java
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.service;
21
22 import java.util.Date;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.hibernate.SessionFactory;
30 import org.openecomp.portalapp.portal.domain.EPUser;
31 import org.openecomp.portalapp.portal.domain.EPUserNotification;
32 import org.openecomp.portalapp.portal.domain.EcompAppRole;
33 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
34 import org.openecomp.portalapp.portal.transport.EpNotificationItem;
35 import org.openecomp.portalapp.portal.transport.EpNotificationItemVO;
36 import org.openecomp.portalapp.portal.transport.EpRoleNotificationItem;
37 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
38 import org.openecomp.portalsdk.core.service.DataAccessService;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.context.annotation.EnableAspectJAutoProxy;
41 import org.springframework.stereotype.Service;
42
43 @Service("userNotificationService")
44 @org.springframework.context.annotation.Configuration
45 @EnableAspectJAutoProxy
46 @EPMetricsLog
47 public class UserNotificationServiceImpl implements UserNotificationService {
48         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FunctionalMenuServiceImpl.class);
49
50         @Autowired
51         private DataAccessService dataAccessService;
52         @Autowired
53         private SessionFactory sessionFactory;
54
55         /*
56          * (non-Javadoc)
57          * 
58          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
59          * getNotifications(java.lang.Long)
60          */
61         @Override
62         public List<EpNotificationItem> getNotifications(Long userId) {
63                 Map<String, String> params = new HashMap<String, String>();
64                 params.put("user_id", userId.toString());
65                 @SuppressWarnings("unchecked")
66                 List<EpNotificationItem> notificationList = dataAccessService.executeNamedQuery("getNotifications", params,
67                                 null);
68                 // set the roles to null for pure retrieval of all notifications
69                 for (EpNotificationItem item : notificationList) {
70                         item.setRoles(null);
71                 }
72                 return notificationList;
73         }
74
75         /*
76          * (non-Javadoc)
77          * 
78          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
79          * getNotificationHistoryVO(java.lang.Long)
80          */
81         @Override
82         public List<EpNotificationItemVO> getNotificationHistoryVO(Long userId) {
83                 Map<String, String> params = new HashMap<String, String>();
84                 params.put("user_id", userId.toString());
85                 @SuppressWarnings("unchecked")
86                 List<EpNotificationItemVO> notificationList = dataAccessService.executeNamedQuery("getNotificationHistoryVO",
87                                 params, null);
88                 return notificationList;
89         }
90
91         /*
92          * (non-Javadoc)
93          * 
94          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
95          * getAdminNotificationVOS()
96          */
97         @Override
98         public List<EpNotificationItemVO> getAdminNotificationVOS(Long userId) {
99                 Map<String, String> params = new HashMap<String, String>();
100                 params.put("user_id", userId.toString());
101                 @SuppressWarnings("unchecked")
102                 List<EpNotificationItemVO> notificationList = dataAccessService
103                                 .executeNamedQuery("getAdminNotificationHistoryVO", params, null);
104                 return notificationList;
105         }
106
107         /*
108          * (non-Javadoc)
109          * 
110          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
111          * getNotificationRoles(java.lang.Long)
112          */
113         @Override
114         public List<EpRoleNotificationItem> getNotificationRoles(Long notificationId) {
115                 Map<String, String> params = new HashMap<String, String>();
116                 params.put("notificationId", Long.toString(notificationId));
117                 @SuppressWarnings("unchecked")
118                 List<EpRoleNotificationItem> roleNotifList = dataAccessService.executeNamedQuery("getNotificationRoles", params,
119                                 null);
120                 return roleNotifList;
121         }
122
123         /*
124          * (non-Javadoc)
125          * 
126          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
127          * getAppRoleList()
128          */
129         @SuppressWarnings("unchecked")
130         @Override
131         public List<EcompAppRole> getAppRoleList() {
132                 List<EcompAppRole> appRoleList = (List<EcompAppRole>) dataAccessService
133                                 .executeNamedQuery("getEpNotificationAppRoles", null, null);
134                 return appRoleList;
135         }
136
137         /*
138          * (non-Javadoc)
139          * 
140          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
141          * setNotificationsRead(java.lang.Long, int)
142          */
143         @Override
144         public void setNotificationRead(Long notificationId, int userId) {
145                 EPUserNotification userNotification = new EPUserNotification();
146                 userNotification.setNotificationId(notificationId);
147                 userNotification.setUpdateTime(new Date());
148                 userNotification.setViewed("Y");
149                 userNotification.setUserId((long) userId);
150                 getDataAccessService().saveDomainObject(userNotification, null);
151         }
152
153         /*
154          * (non-Javadoc)
155          * 
156          * @see org.openecomp.portalapp.portal.service.UserNotificationService#
157          * saveNotification(org.openecomp.portalapp.portal.transport.
158          * EpNotificationItem)
159          */
160         @Override
161         public String saveNotification(EpNotificationItem notificationItem) throws Exception {
162
163                 // gather the roles
164                 if (notificationItem.getRoleIds() != null && !notificationItem.getIsForAllRoles().equals("Y")) {
165                         if (notificationItem.getRoles() == null) {
166                                 Set<EpRoleNotificationItem> roleSet = new HashSet<EpRoleNotificationItem>();
167                                 notificationItem.setRoles(roleSet);
168                         }
169                         for (Long roleId : notificationItem.getRoleIds()) {
170                                 EpRoleNotificationItem roleItem = new EpRoleNotificationItem();
171                                 roleItem.setNotificationId(notificationItem.getNotificationId());
172                                 roleItem.setRoleId(roleId.intValue());
173                                 notificationItem.getRoles().add(roleItem);
174                         }
175                 }
176
177                 // for updates fetch roles and then save
178                 if (notificationItem.getNotificationId() != null) {
179                         EpNotificationItem updateNotificationItem = (EpNotificationItem) getDataAccessService()
180                                         .getDomainObject(EpNotificationItem.class, notificationItem.getNotificationId(), null);
181                         notificationItem.setRoles(updateNotificationItem.getRoles());
182                 }
183                 if (notificationItem.msgSource == null) {
184                         notificationItem.setMsgSource("EP");
185                 }
186                 getDataAccessService().saveDomainObject(notificationItem, null);
187                 return "";
188
189         }
190
191         @Override
192         public List<EPUser> getUsersByOrgIds(List<String> OrgIds) {
193                 Map<String, Object> params = new HashMap<String, Object>();
194                 params.put("OrgIds", OrgIds);
195                 @SuppressWarnings("unchecked")
196                 List<EPUser> userList = dataAccessService.executeNamedQuery("getUsersByOrgIdsNotifications", params, null);
197                 return userList;
198         }
199
200         @Override
201         public List<String> getMessageRecipients(Long notificationId) {
202                 Map<String, String> params = new HashMap<>();
203                 params.put("notificationId", Long.toString(notificationId));
204                 @SuppressWarnings("unchecked")
205                 List<String> activeUsers = dataAccessService.executeNamedQuery("messageRecipients", params, null);
206                 return activeUsers;
207         }
208
209         public DataAccessService getDataAccessService() {
210                 return dataAccessService;
211         }
212
213         public void setDataAccessService(DataAccessService dataAccessService) {
214                 this.dataAccessService = dataAccessService;
215         }
216
217         public SessionFactory getSessionFactory() {
218                 return sessionFactory;
219         }
220
221         public void setSessionFactory(SessionFactory sessionFactory) {
222                 this.sessionFactory = sessionFactory;
223         }
224
225         @Override
226         public void deleteNotificationsFromEpNotificationTable() {
227                 Map<String, String> params = new HashMap<String, String>();
228                 dataAccessService.executeNamedUpdateQuery("deleteNotificationsFromEpNotificationTable", params, null);
229         }
230
231         @Override
232         public void deleteNotificationsFromEpUserNotificationTable() {
233                 Map<String, String> params = new HashMap<String, String>();
234                 dataAccessService.executeNamedUpdateQuery("deleteNotificationsFromEpUserNotificationTable", params, null);
235
236         }
237
238         @Override
239         public void deleteNotificationsFromEpRoleNotificationTable() {
240                 Map<String, String> params = new HashMap<String, String>();
241                 dataAccessService.executeNamedUpdateQuery("deleteNotificationsFromEpRoleNotificationTable", params, null);
242         }
243
244 }