Added oparent to sdc main
[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 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.notification.services.impl;
22
23 import org.openecomp.sdc.notification.dao.LastNotificationDao;
24 import org.openecomp.sdc.notification.dao.NotificationsDao;
25 import org.openecomp.sdc.notification.dao.types.LastSeenNotificationEntity;
26 import org.openecomp.sdc.notification.dao.types.NotificationEntity;
27 import org.openecomp.sdc.notification.dtos.NotificationsStatus;
28 import org.openecomp.sdc.notification.exceptons.NotificationNotExistException;
29 import org.openecomp.sdc.notification.services.NotificationsService;
30
31 import java.util.List;
32 import java.util.Objects;
33 import java.util.UUID;
34
35 /**
36  * @author Avrahamg
37  * @since June 26, 2017
38  */
39 public class NotificationsServiceImpl implements NotificationsService {
40
41     private LastNotificationDao lastNotificationDao;
42     private NotificationsDao notificationsDao;
43
44     public NotificationsServiceImpl(LastNotificationDao lastNotificationDao,
45                                     NotificationsDao notificationsDao) {
46         this.lastNotificationDao = lastNotificationDao;
47         this.notificationsDao = notificationsDao;
48     }
49
50     @Override
51     public LastSeenNotificationEntity getLastNotification(String ownerId) {
52         return new LastSeenNotificationEntity(ownerId,
53             lastNotificationDao.getOwnerLastEventId(ownerId));
54     }
55
56     @Override
57     public void updateLastSeenNotification(String ownerId, UUID eventId)
58     {
59         lastNotificationDao.persistOwnerLastEventId(ownerId, eventId);
60     }
61
62     @Override
63     public NotificationsStatus getNotificationsStatus(String ownerId, UUID lastDelivered, int numOfRecordsToReturn, UUID endOfPage) {
64         if (Objects.isNull(lastDelivered)) {
65                 LastSeenNotificationEntity entity = getLastNotification(ownerId);
66                 if (Objects.nonNull(entity)) {
67                         lastDelivered = entity.getLastEventId();
68                 }
69                 if (Objects.isNull(lastDelivered)) {
70                         lastDelivered = UUID.fromString("00000000-0000-1000-8080-808080808080"); // Lowest time UUID value
71                 }
72         }
73         if (Objects.isNull(endOfPage)) {
74                 // First page
75                 return notificationsDao.getNotificationsStatus(ownerId, lastDelivered, numOfRecordsToReturn);
76         }
77         else {
78                 // Next page
79             return notificationsDao.getNotificationsStatus(ownerId, lastDelivered, numOfRecordsToReturn, endOfPage);
80         }
81     }
82
83     @Override
84     public void markAsRead(String ownerId, String notificationId) throws
85         NotificationNotExistException {
86         NotificationEntity notificationEntity =
87             notificationsDao.get(new NotificationEntity(ownerId, UUID.fromString(notificationId)));
88         if (Objects.isNull(notificationEntity)) {
89             throw new NotificationNotExistException(
90                 "Notification '" + notificationId + "' is not related to ownerId" +
91                     " '" + ownerId + "'");
92         }
93         notificationEntity.setRead(true);
94         notificationsDao.update(notificationEntity);
95     }
96
97     @Override
98     public List<NotificationEntity> getNotificationsByOwnerId(String ownerId, int limit) {
99         return notificationsDao.getNotificationsByOwnerId(ownerId, limit);
100     }
101
102     @Override
103     public List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId) {
104         return notificationsDao.getNewNotificationsByOwnerId(ownerId, eventId);
105     }
106
107     @Override
108     public List<NotificationEntity> getNewNotificationsByOwnerId(String ownerId, UUID eventId, int limit) {
109         return notificationsDao.getNewNotificationsByOwnerId(ownerId, eventId, limit);
110     }
111
112 }