Added oparent to sdc main
[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 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdcrests.notifications.rest.services.impl;
22
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;
40
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;
47
48 /**
49  * @author Avrahamg
50  * @since June 22, 2017
51  */
52 @Named
53 @Service("notifications")
54 @Scope(value = "prototype")
55 public class NotificationsImpl implements Notifications {
56
57         private static int selectionLimit = 10;
58
59         private static final Logger LOGGER = LoggerFactory.getLogger(NotificationsImpl.class);
60         private NotificationsService notificationsService = NotificationsServiceFactory.getInstance().createInterface();
61
62     @Override
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);
69
70         return Response.ok(notificationsStatusDto).build();
71     }
72
73     @Override
74     public Response updateLastSeenNotification(String notificationId, String user)
75         throws InvocationTargetException, IllegalAccessException {
76         UpdateNotificationResponseStatus
77             updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
78         try {
79             notificationsService.updateLastSeenNotification(user, UUID.fromString(notificationId));
80         } catch (Exception ex) {
81             LOGGER.error(
82                 String.format(Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage(),
83                     user), ex);
84             updateNotificationResponseStatus.addStructureError(notificationId,
85                 new ErrorMessage(ErrorLevel.ERROR,
86                     Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage()));
87         }
88         return Response.ok(updateNotificationResponseStatus).build();
89     }
90
91     @Override
92     public Response markAsRead(String notificationId, String user)
93         throws InvocationTargetException, IllegalAccessException {
94
95         UpdateNotificationResponseStatus
96             updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
97         try {
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()));
105         }
106         return Response.ok(updateNotificationResponseStatus).build();
107     }
108
109     @Override
110     public Response getNewNotificationsByOwnerId(String user, String eventId, String limitStr) {
111         int limit = selectionLimit;
112
113         if (Objects.nonNull(limitStr)) {
114             try {
115                 limit = Integer.parseInt(limitStr);
116             }
117             catch (NumberFormatException f) {
118                 LOGGER.error("Non numeric selection list size value specified: " + limitStr);
119             }
120         }
121
122         List<NotificationEntity> notifications = Objects.isNull(eventId)
123             ? notificationsService.getNotificationsByOwnerId(user, limit)
124             : notificationsService.getNewNotificationsByOwnerId(user, UUID.fromString(eventId), limit);
125
126         MapNotificationsToDto converter = new MapNotificationsToDto();
127         NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
128         converter.doMapping(notifications, notificationsStatusDto);
129
130         return Response.ok(notificationsStatusDto).build();
131     }
132 }