1 package org.openecomp.sdcrests.notifications.rest.services.impl;
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.notification.dao.types.NotificationEntity;
9 import org.openecomp.sdc.notification.dtos.NotificationsStatus;
10 import org.openecomp.sdc.notification.exceptons.NotificationNotExistException;
11 import org.openecomp.sdc.notification.factories.NotificationsServiceFactory;
12 import org.openecomp.sdc.notification.services.NotificationsService;
13 import org.openecomp.sdcrests.notifications.rest.mapping.MapNotificationsStatusToDto;
14 import org.openecomp.sdcrests.notifications.rest.mapping.MapNotificationsToDto;
15 import org.openecomp.sdcrests.notifications.rest.services.Notifications;
16 import org.openecomp.sdcrests.notifications.types.NotificationsStatusDto;
17 import org.openecomp.sdcrests.notifications.types.UpdateNotificationResponseStatus;
18 import org.springframework.context.annotation.Scope;
19 import org.springframework.stereotype.Service;
21 import javax.inject.Named;
22 import javax.ws.rs.core.Response;
23 import java.lang.reflect.InvocationTargetException;
24 import java.util.List;
25 import java.util.Objects;
26 import java.util.UUID;
30 * @since June 22, 2017
33 @Service("notifications")
34 @Scope(value = "prototype")
35 public class NotificationsImpl implements Notifications {
37 private static int selectionLimit = 10;
39 private static final Logger LOGGER = LoggerFactory.getLogger(NotificationsImpl.class);
40 private NotificationsService notificationsService = NotificationsServiceFactory.getInstance().createInterface();
43 public Response getNotifications(String user, UUID lastDelivered, UUID endOfPage) {
44 NotificationsStatus notificationsStatus = notificationsService
45 .getNotificationsStatus(user, lastDelivered, selectionLimit, endOfPage);
46 MapNotificationsStatusToDto converter = new MapNotificationsStatusToDto();
47 NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
48 converter.doMapping(notificationsStatus, notificationsStatusDto);
50 return Response.ok(notificationsStatusDto).build();
54 public Response updateLastSeenNotification(String notificationId, String user)
55 throws InvocationTargetException, IllegalAccessException {
56 UpdateNotificationResponseStatus
57 updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
59 notificationsService.updateLastSeenNotification(user, UUID.fromString(notificationId));
60 } catch (Exception ex) {
62 String.format(Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage(),
64 updateNotificationResponseStatus.addStructureError(notificationId,
65 new ErrorMessage(ErrorLevel.ERROR,
66 Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage()));
68 return Response.ok(updateNotificationResponseStatus).build();
72 public Response markAsRead(String notificationId, String user)
73 throws InvocationTargetException, IllegalAccessException {
75 UpdateNotificationResponseStatus
76 updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
78 notificationsService.markAsRead(user, notificationId);
79 } catch (NotificationNotExistException ex) {
80 LOGGER.error(Messages.FAILED_TO_MARK_NOTIFICATION_AS_READ.getErrorMessage(), ex);
81 updateNotificationResponseStatus.addStructureError(
82 notificationId, new ErrorMessage(ErrorLevel.ERROR, Messages
83 .FAILED_TO_MARK_NOTIFICATION_AS_READ
86 return Response.ok(updateNotificationResponseStatus).build();
90 public Response getNewNotificationsByOwnerId(String user, String eventId, String limitStr) {
91 int limit = selectionLimit;
93 if (Objects.nonNull(limitStr)) {
95 limit = Integer.parseInt(limitStr);
97 catch (NumberFormatException f) {
98 LOGGER.error("Non numeric selection list size value specified: " + limitStr);
102 List<NotificationEntity> notifications = Objects.isNull(eventId)
103 ? notificationsService.getNotificationsByOwnerId(user, limit)
104 : notificationsService.getNewNotificationsByOwnerId(user, UUID.fromString(eventId), limit);
106 MapNotificationsToDto converter = new MapNotificationsToDto();
107 NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
108 converter.doMapping(notifications, notificationsStatusDto);
110 return Response.ok(notificationsStatusDto).build();