Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-notification-lib / openecomp-sdc-notification-core / src / main / java / org / openecomp / sdc / notification / services / impl / NotificationsServiceImpl.java
1 package org.openecomp.sdc.notification.services.impl;
2
3 import org.openecomp.sdc.notification.dao.LastNotificationDao;
4 import org.openecomp.sdc.notification.dao.NotificationsDao;
5 import org.openecomp.sdc.notification.dao.types.LastSeenNotificationEntity;
6 import org.openecomp.sdc.notification.dao.types.NotificationEntity;
7 import org.openecomp.sdc.notification.dtos.NotificationsStatus;
8 import org.openecomp.sdc.notification.exceptons.NotificationNotExistException;
9 import org.openecomp.sdc.notification.services.NotificationsService;
10
11 import java.util.List;
12 import java.util.Objects;
13 import java.util.UUID;
14
15 /**
16  * @author Avrahamg
17  * @since June 26, 2017
18  */
19 public class NotificationsServiceImpl implements NotificationsService {
20
21     private LastNotificationDao lastNotificationDao;
22     private NotificationsDao notificationsDao;
23
24     public NotificationsServiceImpl(LastNotificationDao lastNotificationDao,
25                                     NotificationsDao notificationsDao) {
26         this.lastNotificationDao = lastNotificationDao;
27         this.notificationsDao = notificationsDao;
28     }
29
30     @Override
31     public LastSeenNotificationEntity getLastNotification(String ownerId) {
32         return new LastSeenNotificationEntity(ownerId,
33             lastNotificationDao.getOwnerLastEventId(ownerId));
34     }
35
36     @Override
37     public void updateLastSeenNotification(String ownerId, UUID eventId)
38     {
39         lastNotificationDao.persistOwnerLastEventId(ownerId, eventId);
40     }
41
42     @Override
43     public NotificationsStatus getNotificationsStatus(String ownerId, UUID lastDelivered, int numOfRecordsToReturn, UUID endOfPage) {
44         if (Objects.isNull(lastDelivered)) {
45                 LastSeenNotificationEntity entity = getLastNotification(ownerId);
46                 if (Objects.nonNull(entity)) {
47                         lastDelivered = entity.getLastEventId();
48                 }
49                 if (Objects.isNull(lastDelivered)) {
50                         lastDelivered = UUID.fromString("00000000-0000-1000-8080-808080808080"); // Lowest time UUID value
51                 }
52         }
53         if (Objects.isNull(endOfPage)) {
54                 // First page
55                 return notificationsDao.getNotificationsStatus(ownerId, lastDelivered, numOfRecordsToReturn);
56         }
57         else {
58                 // Next page
59             return notificationsDao.getNotificationsStatus(ownerId, lastDelivered, numOfRecordsToReturn, endOfPage);
60         }
61     }
62
63     @Override
64     public void markAsRead(String ownerId, String notificationId) throws
65         NotificationNotExistException {
66         NotificationEntity notificationEntity =
67             notificationsDao.get(new NotificationEntity(ownerId, UUID.fromString(notificationId)));
68         if (Objects.isNull(notificationEntity)) {
69             throw new NotificationNotExistException(
70                 "Notification '" + notificationId + "' is not related to ownerId" +
71                     " '" + ownerId + "'");
72         }
73         notificationEntity.setRead(true);
74         notificationsDao.update(notificationEntity);
75     }
76
77     @Override
78     public List<NotificationEntity> getNotificationsByOwnerId(String ownerId, int limit) {
79         return notificationsDao.getNotificationsByOwnerId(ownerId, limit);
80     }
81
82     @Override
83     public List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId) {
84         return notificationsDao.getNewNotificationsByOwnerId(ownerId, eventId);
85     }
86
87     @Override
88     public List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId, int limit) {
89         return notificationsDao.getNewNotificationsByOwnerId(ownerId, eventId, limit);
90     }
91
92 }