2 * Copyright © 2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openecomp.sdcrests.item.rest.services;
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;
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;
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;
57 @Scope(value = "prototype")
58 public class ItemsImpl implements Items {
60 private ItemManager itemManager =
61 ItemManagerFactory.getInstance().createInterface();
63 private static ActivityLogManager activityLogManager =
64 ActivityLogManagerFactory.getInstance().createInterface();
66 private VersioningManager versioningManager =
67 VersioningManagerFactory.getInstance().createInterface();
69 private static final Logger LOGGER = LoggerFactory.getLogger(ItemsImpl.class);
71 private NotificationPropagationManager notifier =
72 NotificationPropagationManagerFactory.getInstance().createInterface();
74 private Map<ItemAction, ActionSideAffects> actionSideAffectsMap = new EnumMap<>(ItemAction.class);
77 actionSideAffectsMap.put(ItemAction.ARCHIVE, new ActionSideAffects(ActivityType.Archive,
78 NotificationEventTypes.ARCHIVE));
79 actionSideAffectsMap.put(ItemAction.RESTORE, new ActionSideAffects(ActivityType.Restore,
80 NotificationEventTypes.RESTORE));
84 public Response actOn(ItemActionRequestDto request, String itemId, String user) {
86 Item item = itemManager.get(itemId);
88 return Response.status(Response.Status.NOT_FOUND)
89 .entity(new Exception("Item does not exist.")).build();
92 switch (request.getAction()) {
94 itemManager.archive(item);
97 itemManager.restore(item);
102 actionSideAffectsMap.get(request.getAction()).execute(item,user);
104 return Response.ok().build();
108 public Response getItem(String itemId, String user) {
109 Item item = itemManager.get(itemId);
110 ItemDto itemDto = new MapItemToDto().applyMapping(item, ItemDto.class);
112 return Response.ok(itemDto).build();
115 private Version getLatestVersion(String itemId){
116 List<Version> list = versioningManager.list(itemId);
117 return list.stream().max(Version::compareTo).get();
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);
126 eventProperties.put(SUBMIT_DESCRIPTION, message);
127 eventProperties.put(PERMISSION_USER, userName);
129 Event syncEvent = new SyncEvent(eventType.getEventName(), itemId, eventProperties, itemId);
131 notifier.notifySubscribers(syncEvent, userName);
132 } catch (Exception e) {
133 LOGGER.error("Failed to send sync notification to users subscribed to item '" + itemId);
137 private class SyncEvent implements Event {
139 private String eventType;
140 private String originatorId;
141 private Map<String, Object> attributes;
142 private String entityId;
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;
153 public String getEventType() {
158 public String getOriginatorId() {
163 public Map<String, Object> getAttributes() {
168 public String getEntityId() {
174 private class ActionSideAffects{
175 private ActivityType activityType;
176 private NotificationEventTypes notificationType;
178 public ActionSideAffects(ActivityType activityType, NotificationEventTypes notificationType){
179 this.activityType = activityType;
180 this.notificationType = notificationType;
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, "", ""));