Add collaboration feature
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / notifications-rest / notifications-rest-services / src / main / java / org / openecomp / sdcrests / notifications / rest / services / impl / NotificationsImpl.java
1 package org.openecomp.sdcrests.notifications.rest.services.impl;
2
3 import org.openecomp.sdc.common.errors.Messages;
4 import org.openecomp.sdc.datatypes.error.ErrorLevel;
5 import org.openecomp.sdc.datatypes.error.ErrorMessage;
6 import org.openecomp.sdc.logging.api.Logger;
7 import org.openecomp.sdc.logging.api.LoggerFactory;
8 import org.openecomp.sdc.logging.context.MdcUtil;
9 import org.openecomp.sdc.logging.types.LoggerServiceName;
10 import org.openecomp.sdc.notification.dao.types.NotificationEntity;
11 import org.openecomp.sdc.notification.dtos.NotificationsStatus;
12 import org.openecomp.sdc.notification.exceptons.NotificationNotExistException;
13 import org.openecomp.sdc.notification.factories.NotificationsServiceFactory;
14 import org.openecomp.sdc.notification.services.NotificationsService;
15 import org.openecomp.sdcrests.notifications.rest.mapping.MapNotificationsStatusToDto;
16 import org.openecomp.sdcrests.notifications.rest.mapping.MapNotificationsToDto;
17 import org.openecomp.sdcrests.notifications.rest.services.Notifications;
18 import org.openecomp.sdcrests.notifications.types.NotificationsStatusDto;
19 import org.openecomp.sdcrests.notifications.types.UpdateNotificationResponseStatus;
20 import org.springframework.context.annotation.Scope;
21 import org.springframework.stereotype.Service;
22
23 import javax.inject.Named;
24 import javax.ws.rs.core.Response;
25 import java.lang.reflect.InvocationTargetException;
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.UUID;
29
30 /**
31  * @author Avrahamg
32  * @since June 22, 2017
33  */
34 @Named
35 @Service("notifications")
36 @Scope(value = "prototype")
37 public class NotificationsImpl implements Notifications {
38
39         private static int selectionLimit = 10;
40
41         private static final Logger LOGGER = LoggerFactory.getLogger(NotificationsImpl.class);
42         private NotificationsService notificationsService = NotificationsServiceFactory.getInstance().createInterface();
43
44     @Override
45     public Response getNotifications(String user, UUID lastDelivered, UUID endOfPage) {
46         MdcUtil.initMdc(LoggerServiceName.notifications.toString());
47         NotificationsStatus notificationsStatus = notificationsService
48             .getNotificationsStatus(user, lastDelivered, selectionLimit, endOfPage);
49         MapNotificationsStatusToDto converter = new MapNotificationsStatusToDto();
50         NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
51         converter.doMapping(notificationsStatus, notificationsStatusDto);
52
53         return Response.ok(notificationsStatusDto).build();
54     }
55
56     @Override
57     public Response updateLastSeenNotification(String notificationId, String user)
58         throws InvocationTargetException, IllegalAccessException {
59         UpdateNotificationResponseStatus
60             updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
61         try {
62             notificationsService.updateLastSeenNotification(user, UUID.fromString(notificationId));
63         } catch (Exception ex) {
64             LOGGER.error(
65                 String.format(Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage(),
66                     user), ex);
67             updateNotificationResponseStatus.addStructureError(notificationId,
68                 new ErrorMessage(ErrorLevel.ERROR,
69                     Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage()));
70         }
71         return Response.ok(updateNotificationResponseStatus).build();
72     }
73
74     @Override
75     public Response markAsRead(String notificationId, String user)
76         throws InvocationTargetException, IllegalAccessException {
77
78         UpdateNotificationResponseStatus
79             updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
80         try {
81             notificationsService.markAsRead(user, notificationId);
82         } catch (NotificationNotExistException ex) {
83             LOGGER.error(Messages.FAILED_TO_MARK_NOTIFICATION_AS_READ.getErrorMessage(), ex);
84             updateNotificationResponseStatus.addStructureError(
85                 notificationId, new ErrorMessage(ErrorLevel.ERROR, Messages
86                     .FAILED_TO_MARK_NOTIFICATION_AS_READ
87                     .getErrorMessage()));
88         }
89         return Response.ok(updateNotificationResponseStatus).build();
90     }
91
92     @Override
93     public Response getNewNotificationsByOwnerId(String user, String eventId, String limitStr) {
94         MdcUtil.initMdc(LoggerServiceName.notifications.toString());
95
96         int limit = selectionLimit;
97
98         if (Objects.nonNull(limitStr)) {
99             try {
100                 limit = Integer.parseInt(limitStr);
101             }
102             catch (NumberFormatException f) {
103                 LOGGER.error("Non numeric selection list size value specified: " + limitStr);
104             }
105         }
106
107         List<NotificationEntity> notifications = Objects.isNull(eventId)
108             ? notificationsService.getNotificationsByOwnerId(user, limit)
109             : notificationsService.getNewNotificationsByOwnerId(user, UUID.fromString(eventId), limit);
110
111         MapNotificationsToDto converter = new MapNotificationsToDto();
112         NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
113         converter.doMapping(notifications, notificationsStatusDto);
114
115         return Response.ok(notificationsStatusDto).build();
116     }
117 }