aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/api/openecomp-sdc-rest-webapp/notifications-rest/notifications-rest-services/src/main/java/org/openecomp/sdcrests/notifications/rest/services/impl/NotificationsImpl.java
blob: 4463717910a2f3f230e12876c0760f9d33a3753a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.openecomp.sdcrests.notifications.rest.services.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import javax.inject.Named;
import javax.ws.rs.core.Response;
import org.openecomp.sdc.common.errors.Messages;
import org.openecomp.sdc.datatypes.error.ErrorLevel;
import org.openecomp.sdc.datatypes.error.ErrorMessage;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.notification.dao.types.NotificationEntity;
import org.openecomp.sdc.notification.dtos.NotificationsStatus;
import org.openecomp.sdc.notification.exceptons.NotificationNotExistException;
import org.openecomp.sdc.notification.factories.NotificationsServiceFactory;
import org.openecomp.sdc.notification.services.NotificationsService;
import org.openecomp.sdcrests.notifications.rest.mapping.MapNotificationsStatusToDto;
import org.openecomp.sdcrests.notifications.rest.mapping.MapNotificationsToDto;
import org.openecomp.sdcrests.notifications.rest.services.Notifications;
import org.openecomp.sdcrests.notifications.types.NotificationsStatusDto;
import org.openecomp.sdcrests.notifications.types.UpdateNotificationResponseStatus;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

/**
 * @author Avrahamg
 * @since June 22, 2017
 */
@Named
@Service("notifications")
@Scope(value = "prototype")
public class NotificationsImpl implements Notifications {

    private static final Logger LOGGER = LoggerFactory.getLogger(NotificationsImpl.class);
    private static int selectionLimit = 10;
    private NotificationsService notificationsService = NotificationsServiceFactory.getInstance().createInterface();

    @Override
    public Response getNotifications(String user, UUID lastDelivered, UUID endOfPage) {
        NotificationsStatus notificationsStatus = notificationsService.getNotificationsStatus(user, lastDelivered, selectionLimit, endOfPage);
        MapNotificationsStatusToDto converter = new MapNotificationsStatusToDto();
        NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
        converter.doMapping(notificationsStatus, notificationsStatusDto);
        return Response.ok(notificationsStatusDto).build();
    }

    @Override
    public Response updateLastSeenNotification(String notificationId, String user) throws InvocationTargetException, IllegalAccessException {
        UpdateNotificationResponseStatus updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
        try {
            notificationsService.updateLastSeenNotification(user, UUID.fromString(notificationId));
        } catch (Exception ex) {
            LOGGER.error(String.format(Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage(), user), ex);
            updateNotificationResponseStatus.addStructureError(notificationId,
                new ErrorMessage(ErrorLevel.ERROR, Messages.FAILED_TO_UPDATE_LAST_SEEN_NOTIFICATION.getErrorMessage()));
        }
        return Response.ok(updateNotificationResponseStatus).build();
    }

    @Override
    public Response markAsRead(String notificationId, String user) throws InvocationTargetException, IllegalAccessException {
        UpdateNotificationResponseStatus updateNotificationResponseStatus = new UpdateNotificationResponseStatus();
        try {
            notificationsService.markAsRead(user, notificationId);
        } catch (NotificationNotExistException ex) {
            LOGGER.error(Messages.FAILED_TO_MARK_NOTIFICATION_AS_READ.getErrorMessage(), ex);
            updateNotificationResponseStatus.addStructureError(notificationId,
                new ErrorMessage(ErrorLevel.ERROR, Messages.FAILED_TO_MARK_NOTIFICATION_AS_READ.getErrorMessage()));
        }
        return Response.ok(updateNotificationResponseStatus).build();
    }

    @Override
    public Response getNewNotificationsByOwnerId(String user, String eventId, String limitStr) {
        int limit = selectionLimit;
        if (Objects.nonNull(limitStr)) {
            try {
                limit = Integer.parseInt(limitStr);
            } catch (NumberFormatException f) {
                LOGGER.error("Non numeric selection list size value specified: " + limitStr);
            }
        }
        List<NotificationEntity> notifications = Objects.isNull(eventId) ? notificationsService.getNotificationsByOwnerId(user, limit)
            : notificationsService.getNewNotificationsByOwnerId(user, UUID.fromString(eventId), limit);
        MapNotificationsToDto converter = new MapNotificationsToDto();
        NotificationsStatusDto notificationsStatusDto = new NotificationsStatusDto();
        converter.doMapping(notifications, notificationsStatusDto);
        return Response.ok(notificationsStatusDto).build();
    }
}