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.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;
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;
32 * @since June 22, 2017
35 @Service("notifications")
36 @Scope(value = "prototype")
37 public class NotificationsImpl implements Notifications {
39 private static int selectionLimit = 10;
41 private static final Logger LOGGER = LoggerFactory.getLogger(NotificationsImpl.class);
42 private NotificationsService notificationsService = NotificationsServiceFactory.getInstance().createInterface();
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);
53 return Response.ok(notificationsStatusDto).build();
57 public Response updateLastSeenNotification(String notificationId, String user)
58 throws InvocationTargetException, IllegalAccessException {
59 UpdateNotificationResponseStatus
60 updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
62 notificationsService.updateLastSeenNotification(user, UUID.fromString(notificationId));
63 } catch (Exception ex) {
65 String.format(Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage(),
67 updateNotificationResponseStatus.addStructureError(notificationId,
68 new ErrorMessage(ErrorLevel.ERROR,
69 Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage()));
71 return Response.ok(updateNotificationResponseStatus).build();
75 public Response markAsRead(String notificationId, String user)
76 throws InvocationTargetException, IllegalAccessException {
78 UpdateNotificationResponseStatus
79 updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
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
89 return Response.ok(updateNotificationResponseStatus).build();
93 public Response getNewNotificationsByOwnerId(String user, String eventId, String limitStr) {
94 MdcUtil.initMdc(LoggerServiceName.notifications.toString());
96 int limit = selectionLimit;
98 if (Objects.nonNull(limitStr)) {
100 limit = Integer.parseInt(limitStr);
102 catch (NumberFormatException f) {
103 LOGGER.error("Non numeric selection list size value specified: " + limitStr);
107 List<NotificationEntity> notifications = Objects.isNull(eventId)
108 ? notificationsService.getNotificationsByOwnerId(user, limit)
109 : notificationsService.getNewNotificationsByOwnerId(user, UUID.fromString(eventId), limit);
111 MapNotificationsToDto converter = new MapNotificationsToDto();
112 NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
113 converter.doMapping(notifications, notificationsStatusDto);
115 return Response.ok(notificationsStatusDto).build();