2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ===================================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
28 * https://creativecommons.org/licenses/by/4.0/
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.
36 * ============LICENSE_END============================================
41 package org.onap.portal.service.epNotification;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Optional;
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;
61 public class EpNotificationService {
63 private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(EpNotificationService.class);
66 private final EpNotificationDao epNotificationDao;
67 private final FnRoleService fnRoleService;
68 private final EntityManager entityManager;
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";
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;
82 public Optional<EpNotification> getOne(final long notficationId){
83 return Optional.of(epNotificationDao.getOne(notficationId));
86 public List<EpNotification> getNotifications(final Long userId) {
87 List<EpNotification> notificationList = epNotificationDao.getNotifications(userId);
88 for (EpNotification item : notificationList) {
89 item.setEpRoleNotifications(null);
91 return notificationList;
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 ))
102 public EpNotification saveNotification(final EpNotification notificationItem) {
105 if (notificationItem.getRoleIds() != null && !notificationItem.getIsForAllRoles().equals("Y")) {
106 if (notificationItem.getEpRoleNotifications() == null) {
107 Set<EpRoleNotification> roleSet = new HashSet<>();
108 notificationItem.setEpRoleNotifications(roleSet);
110 for (Long roleId : notificationItem.getRoleIds()) {
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());
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()));
130 if (notificationItem.getMsgSource() == null) {
131 notificationItem.setMsgSource("EP");
133 return epNotificationDao.saveAndFlush(notificationItem);
136 public List<String> getMessageRecipients(final Long notificationId) {
137 return entityManager.createQuery(messageRecipients, String.class).setParameter("notificationId", notificationId).getResultList();