2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdcrests.notifications.rest.services.impl;
23 import org.openecomp.sdc.common.errors.Messages;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.datatypes.error.ErrorMessage;
26 import org.openecomp.sdc.logging.api.Logger;
27 import org.openecomp.sdc.logging.api.LoggerFactory;
28 import org.openecomp.sdc.notification.dao.types.NotificationEntity;
29 import org.openecomp.sdc.notification.dtos.NotificationsStatus;
30 import org.openecomp.sdc.notification.exceptons.NotificationNotExistException;
31 import org.openecomp.sdc.notification.factories.NotificationsServiceFactory;
32 import org.openecomp.sdc.notification.services.NotificationsService;
33 import org.openecomp.sdcrests.notifications.rest.mapping.MapNotificationsStatusToDto;
34 import org.openecomp.sdcrests.notifications.rest.mapping.MapNotificationsToDto;
35 import org.openecomp.sdcrests.notifications.rest.services.Notifications;
36 import org.openecomp.sdcrests.notifications.types.NotificationsStatusDto;
37 import org.openecomp.sdcrests.notifications.types.UpdateNotificationResponseStatus;
38 import org.springframework.context.annotation.Scope;
39 import org.springframework.stereotype.Service;
41 import javax.inject.Named;
42 import javax.ws.rs.core.Response;
43 import java.lang.reflect.InvocationTargetException;
44 import java.util.List;
45 import java.util.Objects;
46 import java.util.UUID;
50 * @since June 22, 2017
53 @Service("notifications")
54 @Scope(value = "prototype")
55 public class NotificationsImpl implements Notifications {
57 private static int selectionLimit = 10;
59 private static final Logger LOGGER = LoggerFactory.getLogger(NotificationsImpl.class);
60 private NotificationsService notificationsService = NotificationsServiceFactory.getInstance().createInterface();
63 public Response getNotifications(String user, UUID lastDelivered, UUID endOfPage) {
64 NotificationsStatus notificationsStatus = notificationsService
65 .getNotificationsStatus(user, lastDelivered, selectionLimit, endOfPage);
66 MapNotificationsStatusToDto converter = new MapNotificationsStatusToDto();
67 NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
68 converter.doMapping(notificationsStatus, notificationsStatusDto);
70 return Response.ok(notificationsStatusDto).build();
74 public Response updateLastSeenNotification(String notificationId, String user)
75 throws InvocationTargetException, IllegalAccessException {
76 UpdateNotificationResponseStatus
77 updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
79 notificationsService.updateLastSeenNotification(user, UUID.fromString(notificationId));
80 } catch (Exception ex) {
82 String.format(Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage(),
84 updateNotificationResponseStatus.addStructureError(notificationId,
85 new ErrorMessage(ErrorLevel.ERROR,
86 Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage()));
88 return Response.ok(updateNotificationResponseStatus).build();
92 public Response markAsRead(String notificationId, String user)
93 throws InvocationTargetException, IllegalAccessException {
95 UpdateNotificationResponseStatus
96 updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
98 notificationsService.markAsRead(user, notificationId);
99 } catch (NotificationNotExistException ex) {
100 LOGGER.error(Messages.FAILED_TO_MARK_NOTIFICATION_AS_READ.getErrorMessage(), ex);
101 updateNotificationResponseStatus.addStructureError(
102 notificationId, new ErrorMessage(ErrorLevel.ERROR, Messages
103 .FAILED_TO_MARK_NOTIFICATION_AS_READ
104 .getErrorMessage()));
106 return Response.ok(updateNotificationResponseStatus).build();
110 public Response getNewNotificationsByOwnerId(String user, String eventId, String limitStr) {
111 int limit = selectionLimit;
113 if (Objects.nonNull(limitStr)) {
115 limit = Integer.parseInt(limitStr);
117 catch (NumberFormatException f) {
118 LOGGER.error("Non numeric selection list size value specified: " + limitStr);
122 List<NotificationEntity> notifications = Objects.isNull(eventId)
123 ? notificationsService.getNotificationsByOwnerId(user, limit)
124 : notificationsService.getNewNotificationsByOwnerId(user, UUID.fromString(eventId), limit);
126 MapNotificationsToDto converter = new MapNotificationsToDto();
127 NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
128 converter.doMapping(notifications, notificationsStatusDto);
130 return Response.ok(notificationsStatusDto).build();