UserNotificationController up
[portal.git] / portal-BE / src / main / java / org / onap / portal / service / epNotification / EpNotificationService.java
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  * Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  *
39  */
40
41 package org.onap.portal.service.epNotification;
42
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Optional;
46 import java.util.Set;
47 import javax.persistence.EntityManager;
48 import javax.transaction.Transactional;
49 import org.hibernate.transform.Transformers;
50 import org.onap.portal.domain.db.ep.EpNotification;
51 import org.onap.portal.domain.db.ep.EpRoleNotification;
52 import org.onap.portal.domain.db.fn.FnRole;
53 import org.onap.portal.domain.dto.transport.EpNotificationItemVO;
54 import org.onap.portal.service.role.FnRoleService;
55 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.stereotype.Service;
58
59 @Service
60 @Transactional
61 public class EpNotificationService {
62
63     private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(EpNotificationService.class);
64
65
66     private final EpNotificationDao epNotificationDao;
67     private final FnRoleService fnRoleService;
68     private final EntityManager entityManager;
69
70     private final String messageRecipients = "select u.org_user_id from ep_notification n join ep_role_notification r on "
71         + " r.notification_ID=n.notification_ID join fn_user u on u.user_id=r.recv_user_id where n.notification_id=:notificationId\n";
72
73     @Autowired
74     public EpNotificationService(
75         final EpNotificationDao epNotificationDao,
76         FnRoleService fnRoleService, final EntityManager entityManager) {
77         this.epNotificationDao = epNotificationDao;
78         this.fnRoleService = fnRoleService;
79         this.entityManager = entityManager;
80     }
81
82     public Optional<EpNotification> getOne(final long notficationId){
83         return Optional.of(epNotificationDao.getOne(notficationId));
84     }
85
86     public List<EpNotification> getNotifications(final Long userId) {
87         List<EpNotification> notificationList = epNotificationDao.getNotifications(userId);
88         for (EpNotification item : notificationList) {
89             item.setEpRoleNotifications(null);
90         }
91         return notificationList;
92     }
93
94     public List<EpNotificationItemVO> getAdminNotificationVOS(final Long userId) {
95         return  entityManager.createNamedQuery("getAdminNotificationHistoryVO")
96             .setParameter("user_id", userId).unwrap(org.hibernate.query.NativeQuery.class)
97             .setResultTransformer(Transformers.aliasToBean( EpNotificationItemVO.class ))
98             .getResultList();
99     }
100
101
102     public EpNotification saveNotification(final EpNotification notificationItem) {
103
104         // gather the roles
105         if (notificationItem.getRoleIds() != null && !notificationItem.getIsForAllRoles().equals("Y")) {
106             if (notificationItem.getEpRoleNotifications() == null) {
107                 Set<EpRoleNotification> roleSet = new HashSet<>();
108                 notificationItem.setEpRoleNotifications(roleSet);
109             }
110             for (Long roleId : notificationItem.getRoleIds()) {
111                 FnRole role = null;
112                 try {
113                      role = fnRoleService.getById(roleId);
114                     EpRoleNotification roleItem = new EpRoleNotification();
115                     roleItem.setNotificationId(notificationItem);
116                     roleItem.setRoleId(role);
117                     notificationItem.getEpRoleNotifications().add(roleItem);
118                 }catch (Exception e){
119                     LOGGER.error(e.getMessage());
120                 }
121             }
122         }
123
124         // for updates fetch roles and then save
125         if (notificationItem.getNotificationId() != null) {
126             Optional<EpNotification> updateNotificationItem = Optional.of(epNotificationDao.getOne(notificationItem.getNotificationId()));
127             updateNotificationItem.ifPresent(
128                 epNotification -> notificationItem.setEpRoleNotifications(epNotification.getEpRoleNotifications()));
129         }
130         if (notificationItem.getMsgSource() == null) {
131             notificationItem.setMsgSource("EP");
132         }
133         return epNotificationDao.saveAndFlush(notificationItem);
134     }
135
136     public List<String> getMessageRecipients(final Long notificationId) {
137         return entityManager.createQuery(messageRecipients, String.class).setParameter("notificationId", notificationId).getResultList();
138     }
139 }