From 705fc2b672d5802696074be94b446a89d228b94d Mon Sep 17 00:00:00 2001 From: ayalaben Date: Thu, 15 Mar 2018 15:59:25 +0200 Subject: Archive Item Change-Id: Idd5eedc3b0ca9e3cc72f7de9fd432cdbbf77631d Issue-ID: SDC-1086 Signed-off-by: ayalaben --- .../org/openecomp/sdcrests/item/rest/Items.java | 55 ++++++ .../sdcrests/item/rest/mapping/MapItemToDto.java | 2 + .../sdcrests/item/rest/services/ItemsImpl.java | 191 +++++++++++++++++++++ 3 files changed, 248 insertions(+) create mode 100644 openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/Items.java create mode 100644 openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java (limited to 'openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services') diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/Items.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/Items.java new file mode 100644 index 0000000000..de63c4efdc --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/Items.java @@ -0,0 +1,55 @@ +/* + * Copyright © 2018 European Support Limited + * + * 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. + */ +package org.openecomp.sdcrests.item.rest; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.openecomp.sdcrests.item.types.ItemActionRequestDto; +import org.springframework.validation.annotation.Validated; + +import javax.validation.constraints.NotNull; +import javax.ws.rs.*; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import static org.openecomp.sdcrests.common.RestConstants.USER_ID_HEADER_PARAM; +import static org.openecomp.sdcrests.common.RestConstants.USER_MISSING_ERROR_MSG; + +@Path("/v1.0/items") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +@Api(value = "Items") +@Validated +public interface Items { + + @GET + @Path("/{itemId}") + @ApiOperation(value = "Get details of a item") + Response getItem(@PathParam("itemId") String itemId, + @NotNull(message = USER_MISSING_ERROR_MSG) + @HeaderParam(USER_ID_HEADER_PARAM) String user); + + @PUT + @Path("/{itemId}/actions") + @ApiOperation(value = "Acts on item version") + Response actOn(ItemActionRequestDto request, + @PathParam("itemId") String itemId, + @NotNull(message = USER_MISSING_ERROR_MSG) + @HeaderParam(USER_ID_HEADER_PARAM) String user); + + + +} diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/mapping/MapItemToDto.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/mapping/MapItemToDto.java index 0448407b99..1886dc462c 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/mapping/MapItemToDto.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/mapping/MapItemToDto.java @@ -1,6 +1,7 @@ package org.openecomp.sdcrests.item.rest.mapping; import org.openecomp.sdc.versioning.types.Item; +import org.openecomp.sdc.versioning.types.ItemStatus; import org.openecomp.sdcrests.item.types.ItemDto; import org.openecomp.sdcrests.mapping.MappingBase; @@ -12,5 +13,6 @@ public class MapItemToDto extends MappingBase { target.setName(source.getName()); target.setDescription(source.getDescription()); target.setOwner(source.getOwner()); + target.setStatus(source.getStatus().name()); } } diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java new file mode 100644 index 0000000000..21c1f5199d --- /dev/null +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-services/src/main/java/org/openecomp/sdcrests/item/rest/services/ItemsImpl.java @@ -0,0 +1,191 @@ +/* + * Copyright © 2018 European Support Limited + * + * 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. + */ +package org.openecomp.sdcrests.item.rest.services; + +import org.openecomp.sdc.activitylog.ActivityLogManager; +import org.openecomp.sdc.activitylog.ActivityLogManagerFactory; +import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity; +import org.openecomp.sdc.activitylog.dao.type.ActivityType; +import org.openecomp.sdc.common.errors.Messages; +import org.openecomp.sdc.logging.api.Logger; +import org.openecomp.sdc.logging.api.LoggerFactory; +import org.openecomp.sdc.notification.dtos.Event; +import org.openecomp.sdc.notification.factories.NotificationPropagationManagerFactory; +import org.openecomp.sdc.notification.services.NotificationPropagationManager; +import org.openecomp.sdc.versioning.ItemManager; +import org.openecomp.sdc.versioning.ItemManagerFactory; +import org.openecomp.sdc.versioning.VersioningManager; +import org.openecomp.sdc.versioning.VersioningManagerFactory; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.openecomp.sdc.versioning.dao.types.VersionStatus; +import org.openecomp.sdc.versioning.types.Item; +import org.openecomp.sdc.versioning.types.NotificationEventTypes; +import org.openecomp.sdcrests.item.rest.Items; +import org.openecomp.sdcrests.item.rest.mapping.MapItemToDto; +import org.openecomp.sdcrests.item.types.ItemAction; +import org.openecomp.sdcrests.item.types.ItemActionRequestDto; +import org.openecomp.sdcrests.item.types.ItemDto; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Service; + +import javax.inject.Named; +import javax.ws.rs.core.Response; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_USER; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.*; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.SUBMIT_DESCRIPTION; + +@Named +@Service("items") +@Scope(value = "prototype") +public class ItemsImpl implements Items { + + private ItemManager itemManager = + ItemManagerFactory.getInstance().createInterface(); + + private static ActivityLogManager activityLogManager = + ActivityLogManagerFactory.getInstance().createInterface(); + + private VersioningManager versioningManager = + VersioningManagerFactory.getInstance().createInterface(); + + private static final Logger LOGGER = LoggerFactory.getLogger(ItemsImpl.class); + + private NotificationPropagationManager notifier = + NotificationPropagationManagerFactory.getInstance().createInterface(); + + private Map actionSideAffectsMap = new EnumMap<>(ItemAction.class); + + { + actionSideAffectsMap.put(ItemAction.ARCHIVE, new ActionSideAffects(ActivityType.Archive, + NotificationEventTypes.ARCHIVE)); + actionSideAffectsMap.put(ItemAction.RESTORE, new ActionSideAffects(ActivityType.Restore, + NotificationEventTypes.RESTORE)); + } + + @Override + public Response actOn(ItemActionRequestDto request, String itemId, String user) { + + Item item = itemManager.get(itemId); + if( item == null){ + return Response.status(Response.Status.NOT_FOUND) + .entity(new Exception("Item does not exist.")).build(); + } + + switch (request.getAction()) { + case ARCHIVE: + itemManager.archive(item); + break; + case RESTORE: + itemManager.restore(item); + break; + default: + } + + actionSideAffectsMap.get(request.getAction()).execute(item,user); + + return Response.ok().build(); + } + + @Override + public Response getItem(String itemId, String user) { + Item item = itemManager.get(itemId); + ItemDto itemDto = new MapItemToDto().applyMapping(item, ItemDto.class); + + return Response.ok(itemDto).build(); + } + + private Version getLatestVersion(String itemId){ + List list = versioningManager.list(itemId); + return list.stream().max(Version::compareTo).get(); + } + + private void notifyUsers(String itemId, String itemName, String message, + String userName, NotificationEventTypes eventType) { + Map eventProperties = new HashMap<>(); + eventProperties.put(ITEM_NAME, itemName == null ? itemManager.get(itemId).getName() : itemName); + eventProperties.put(ITEM_ID, itemId); + + eventProperties.put(SUBMIT_DESCRIPTION, message); + eventProperties.put(PERMISSION_USER, userName); + + Event syncEvent = new SyncEvent(eventType.getEventName(), itemId, eventProperties, itemId); + try { + notifier.notifySubscribers(syncEvent, userName); + } catch (Exception e) { + LOGGER.error("Failed to send sync notification to users subscribed to item '" + itemId); + } + } + + private class SyncEvent implements Event { + + private String eventType; + private String originatorId; + private Map attributes; + private String entityId; + + SyncEvent(String eventType, String originatorId, + Map attributes, String entityId) { + this.eventType = eventType; + this.originatorId = originatorId; + this.attributes = attributes; + this.entityId = entityId; + } + + @Override + public String getEventType() { + return eventType; + } + + @Override + public String getOriginatorId() { + return originatorId; + } + + @Override + public Map getAttributes() { + return attributes; + } + + @Override + public String getEntityId() { + return entityId; + } + + } + + private class ActionSideAffects{ + private ActivityType activityType; + private NotificationEventTypes notificationType; + + public ActionSideAffects(ActivityType activityType, NotificationEventTypes notificationType){ + this.activityType = activityType; + this.notificationType = notificationType; + + } + public void execute(Item item, String user){ + notifyUsers(item.getId(), item.getName(), null, user, + this.notificationType); + activityLogManager.logActivity(new ActivityLogEntity(item.getId(), getLatestVersion(item.getId()), + this.activityType, user, true, "", "")); + } + } + +} -- cgit 1.2.3-korg