Add collaboration feature
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / notifications-rest / notifications-rest-services / src / main / java / org / openecomp / sdcrests / notifications / rest / mapping / MapNotificationsStatusToDto.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.mapping;
22
23 import com.datastax.driver.core.utils.UUIDs;
24 import org.openecomp.core.utilities.json.JsonUtil;
25 import org.openecomp.sdc.notification.dao.types.NotificationEntity;
26 import org.openecomp.sdc.notification.dtos.NotificationsStatus;
27 import org.openecomp.sdcrests.mapping.MappingBase;
28 import org.openecomp.sdcrests.notifications.types.NotificationEntityDto;
29 import org.openecomp.sdcrests.notifications.types.NotificationsStatusDto;
30
31 import java.text.DateFormat;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Map;
35
36 public class MapNotificationsStatusToDto
37     extends MappingBase<NotificationsStatus, NotificationsStatusDto> {
38
39     private static final DateFormat formatter =
40         DateFormat.getDateTimeInstance(DateFormat.LONG,
41             DateFormat.SHORT);
42
43     @Override
44     public void doMapping(NotificationsStatus source, NotificationsStatusDto target) {
45
46         target.setLastScanned(source.getLastScanned());
47         target.setNewEntries(source.getNewEntries());
48         target.setEndOfPage(source.getEndOfPage());
49         target.setNumOfNotSeenNotifications(source.getNumOfNotSeenNotifications());
50         List<NotificationEntityDto> entityDtos = new ArrayList<>();
51         source.getNotifications()
52             .forEach(notification -> entityDtos.add(new NotificationEntityDto(notification.isRead(),
53                 notification.getEventId(), notification.getEventType(),
54                 JsonUtil.json2Object(notification.getEventAttributes(), Map.class),
55                 extractDate(notification))));
56         target.setNotifications(entityDtos);
57     }
58
59     private String extractDate(NotificationEntity notification) {
60         return formatter.format(UUIDs.unixTimestamp
61             (notification
62                 .getEventId()));
63     }
64 }