Removed reference to MDC for logging
[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.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;
20
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;
27
28 /**
29  * @author Avrahamg
30  * @since June 22, 2017
31  */
32 @Named
33 @Service("notifications")
34 @Scope(value = "prototype")
35 public class NotificationsImpl implements Notifications {
36
37         private static int selectionLimit = 10;
38
39         private static final Logger LOGGER = LoggerFactory.getLogger(NotificationsImpl.class);
40         private NotificationsService notificationsService = NotificationsServiceFactory.getInstance().createInterface();
41
42     @Override
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);
49
50         return Response.ok(notificationsStatusDto).build();
51     }
52
53     @Override
54     public Response updateLastSeenNotification(String notificationId, String user)
55         throws InvocationTargetException, IllegalAccessException {
56         UpdateNotificationResponseStatus
57             updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
58         try {
59             notificationsService.updateLastSeenNotification(user, UUID.fromString(notificationId));
60         } catch (Exception ex) {
61             LOGGER.error(
62                 String.format(Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage(),
63                     user), ex);
64             updateNotificationResponseStatus.addStructureError(notificationId,
65                 new ErrorMessage(ErrorLevel.ERROR,
66                     Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage()));
67         }
68         return Response.ok(updateNotificationResponseStatus).build();
69     }
70
71     @Override
72     public Response markAsRead(String notificationId, String user)
73         throws InvocationTargetException, IllegalAccessException {
74
75         UpdateNotificationResponseStatus
76             updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
77         try {
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
84                     .getErrorMessage()));
85         }
86         return Response.ok(updateNotificationResponseStatus).build();
87     }
88
89     @Override
90     public Response getNewNotificationsByOwnerId(String user, String eventId, String limitStr) {
91         int limit = selectionLimit;
92
93         if (Objects.nonNull(limitStr)) {
94             try {
95                 limit = Integer.parseInt(limitStr);
96             }
97             catch (NumberFormatException f) {
98                 LOGGER.error("Non numeric selection list size value specified: " + limitStr);
99             }
100         }
101
102         List<NotificationEntity> notifications = Objects.isNull(eventId)
103             ? notificationsService.getNotificationsByOwnerId(user, limit)
104             : notificationsService.getNewNotificationsByOwnerId(user, UUID.fromString(eventId), limit);
105
106         MapNotificationsToDto converter = new MapNotificationsToDto();
107         NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
108         converter.doMapping(notifications, notificationsStatusDto);
109
110         return Response.ok(notificationsStatusDto).build();
111     }
112 }