21c1f5199db578c190f228118904662a29ed5db5
[sdc.git] /
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openecomp.sdcrests.item.rest.services;
17
18 import org.openecomp.sdc.activitylog.ActivityLogManager;
19 import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
20 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
21 import org.openecomp.sdc.activitylog.dao.type.ActivityType;
22 import org.openecomp.sdc.common.errors.Messages;
23 import org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import org.openecomp.sdc.notification.dtos.Event;
26 import org.openecomp.sdc.notification.factories.NotificationPropagationManagerFactory;
27 import org.openecomp.sdc.notification.services.NotificationPropagationManager;
28 import org.openecomp.sdc.versioning.ItemManager;
29 import org.openecomp.sdc.versioning.ItemManagerFactory;
30 import org.openecomp.sdc.versioning.VersioningManager;
31 import org.openecomp.sdc.versioning.VersioningManagerFactory;
32 import org.openecomp.sdc.versioning.dao.types.Version;
33 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
34 import org.openecomp.sdc.versioning.types.Item;
35 import org.openecomp.sdc.versioning.types.NotificationEventTypes;
36 import org.openecomp.sdcrests.item.rest.Items;
37 import org.openecomp.sdcrests.item.rest.mapping.MapItemToDto;
38 import org.openecomp.sdcrests.item.types.ItemAction;
39 import org.openecomp.sdcrests.item.types.ItemActionRequestDto;
40 import org.openecomp.sdcrests.item.types.ItemDto;
41 import org.springframework.context.annotation.Scope;
42 import org.springframework.stereotype.Service;
43
44 import javax.inject.Named;
45 import javax.ws.rs.core.Response;
46 import java.util.EnumMap;
47 import java.util.HashMap;
48 import java.util.List;
49 import java.util.Map;
50
51 import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_USER;
52 import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.*;
53 import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.SUBMIT_DESCRIPTION;
54
55 @Named
56 @Service("items")
57 @Scope(value = "prototype")
58 public class ItemsImpl implements Items {
59
60     private ItemManager itemManager =
61             ItemManagerFactory.getInstance().createInterface();
62
63     private static ActivityLogManager activityLogManager =
64             ActivityLogManagerFactory.getInstance().createInterface();
65
66     private VersioningManager versioningManager =
67             VersioningManagerFactory.getInstance().createInterface();
68
69     private static final Logger LOGGER = LoggerFactory.getLogger(ItemsImpl.class);
70
71     private NotificationPropagationManager notifier =
72             NotificationPropagationManagerFactory.getInstance().createInterface();
73
74     private Map<ItemAction, ActionSideAffects> actionSideAffectsMap = new EnumMap<>(ItemAction.class);
75
76     {
77     actionSideAffectsMap.put(ItemAction.ARCHIVE, new ActionSideAffects(ActivityType.Archive,
78                     NotificationEventTypes.ARCHIVE));
79     actionSideAffectsMap.put(ItemAction.RESTORE, new  ActionSideAffects(ActivityType.Restore,
80                     NotificationEventTypes.RESTORE));
81     }
82
83   @Override
84   public Response actOn(ItemActionRequestDto request, String itemId, String user) {
85
86       Item item  = itemManager.get(itemId);
87       if( item == null){
88           return Response.status(Response.Status.NOT_FOUND)
89                   .entity(new Exception("Item does not exist.")).build();
90       }
91
92       switch (request.getAction()) {
93           case ARCHIVE:
94               itemManager.archive(item);
95                 break;
96           case RESTORE:
97               itemManager.restore(item);
98               break;
99           default:
100         }
101
102       actionSideAffectsMap.get(request.getAction()).execute(item,user);
103
104       return Response.ok().build();
105     }
106
107     @Override
108     public Response getItem(String itemId, String user) {
109         Item item  = itemManager.get(itemId);
110         ItemDto itemDto = new MapItemToDto().applyMapping(item, ItemDto.class);
111
112         return Response.ok(itemDto).build();
113     }
114
115     private Version getLatestVersion(String itemId){
116         List<Version> list = versioningManager.list(itemId);
117        return list.stream().max(Version::compareTo).get();
118     }
119
120     private void notifyUsers(String itemId, String itemName, String message,
121                                     String userName, NotificationEventTypes eventType) {
122         Map<String, Object> eventProperties = new HashMap<>();
123         eventProperties.put(ITEM_NAME, itemName == null ? itemManager.get(itemId).getName() : itemName);
124         eventProperties.put(ITEM_ID, itemId);
125
126         eventProperties.put(SUBMIT_DESCRIPTION, message);
127         eventProperties.put(PERMISSION_USER, userName);
128
129         Event syncEvent = new SyncEvent(eventType.getEventName(), itemId, eventProperties, itemId);
130         try {
131             notifier.notifySubscribers(syncEvent, userName);
132         } catch (Exception e) {
133             LOGGER.error("Failed to send sync notification to users subscribed to item '" + itemId);
134         }
135     }
136
137     private class SyncEvent implements Event {
138
139         private String eventType;
140         private String originatorId;
141         private Map<String, Object> attributes;
142         private String entityId;
143
144         SyncEvent(String eventType, String originatorId,
145                   Map<String, Object> attributes, String entityId) {
146             this.eventType = eventType;
147             this.originatorId = originatorId;
148             this.attributes = attributes;
149             this.entityId = entityId;
150         }
151
152         @Override
153         public String getEventType() {
154             return eventType;
155         }
156
157         @Override
158         public String getOriginatorId() {
159             return originatorId;
160         }
161
162         @Override
163         public Map<String, Object> getAttributes() {
164             return attributes;
165         }
166
167         @Override
168         public String getEntityId() {
169             return entityId;
170         }
171
172     }
173
174     private class ActionSideAffects{
175       private ActivityType activityType;
176       private NotificationEventTypes notificationType;
177
178       public ActionSideAffects(ActivityType activityType, NotificationEventTypes notificationType){
179           this.activityType = activityType;
180           this.notificationType = notificationType;
181
182       }
183         public void execute(Item item, String user){
184             notifyUsers(item.getId(), item.getName(), null, user,
185                     this.notificationType);
186             activityLogManager.logActivity(new ActivityLogEntity(item.getId(), getLatestVersion(item.getId()),
187                    this.activityType, user, true, "", ""));
188         }
189     }
190
191 }