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 / MapNotificationsToDto.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.apache.commons.collections4.CollectionUtils;
25 import org.openecomp.core.utilities.json.JsonUtil;
26 import org.openecomp.sdc.notification.dao.types.NotificationEntity;
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 MapNotificationsToDto extends MappingBase<List<NotificationEntity>, NotificationsStatusDto> {
37     private static final DateFormat formatter =
38             DateFormat.getDateTimeInstance(DateFormat.LONG,
39                     DateFormat.SHORT);
40     @Override
41     public void doMapping(List<NotificationEntity> source, NotificationsStatusDto target) {
42         List<NotificationEntityDto> entityDtos = new ArrayList<>();
43         if(CollectionUtils.isNotEmpty(source)) {
44             source.forEach(notification -> entityDtos.add(new NotificationEntityDto(notification.isRead(),
45                     notification.getEventId(), notification.getEventType(), JsonUtil.json2Object(notification.getEventAttributes(), Map.class),
46                     extractDate(notification))));
47             target.setNotifications(entityDtos);
48             target.setLastScanned(source.get(0).getEventId());
49         }
50     }
51
52     private String extractDate(NotificationEntity notification) {
53         return formatter.format(UUIDs.unixTimestamp
54                 (notification
55                         .getEventId()));
56     }
57 }