diff options
author | ayalaben <ayala.benzvi@amdocs.com> | 2018-04-12 13:20:18 +0300 |
---|---|---|
committer | ayalaben <ayala.benzvi@amdocs.com> | 2018-04-12 16:37:18 +0300 |
commit | 8da1beb7fb630954a0c7adfc7ae6fbb175877b49 (patch) | |
tree | b6353cb051d382e3774e9a319a88160b9d028521 | |
parent | c7941c2287c3858e4006f3a15ffd6e5a2f33af2e (diff) |
Filter in onboarding - BE
Change-Id: I519ef44889de5314cf6675055cd15beef84dfb6f
Issue-ID: SDC-1213
Signed-off-by: ayalaben <ayala.benzvi@amdocs.com>
5 files changed, 474 insertions, 323 deletions
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 index de63c4efdc..942bc59495 100644 --- 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 @@ -17,6 +17,7 @@ package org.openecomp.sdcrests.item.rest; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; import org.openecomp.sdcrests.item.types.ItemActionRequestDto; import org.springframework.validation.annotation.Validated; @@ -35,6 +36,23 @@ import static org.openecomp.sdcrests.common.RestConstants.USER_MISSING_ERROR_MSG @Validated public interface Items { + @GET + @Path("/") + @ApiOperation(value = "Get list of items according to desired filters", + responseContainer = "List") + Response list(@ApiParam(value = "Filter by item status", allowableValues = "ACTIVE,ARCHIVED") + @QueryParam("itemStatus") String itemStatusFilter, + @ApiParam(value = "Filter by version status" , allowableValues = "Certified,Draft") + @QueryParam("versionStatus") String versionStatusFilter, + @ApiParam(value = "Filter by item type" , allowableValues = "vsp,vlm") + @QueryParam("itemType") String itemTypeFilter, + @ApiParam(value = "Filter by user permission" , allowableValues = "Owner,Contributor") + @QueryParam("permission") String permissionFilter, + @ApiParam(value = "Filter by onboarding method" , allowableValues = "NetworkPackage,Manual") + @QueryParam("onboardingMethod") String onboardingMethodFilter, + @NotNull(message = USER_MISSING_ERROR_MSG) + @HeaderParam(USER_ID_HEADER_PARAM) String user); + @GET @Path("/{itemId}") @ApiOperation(value = "Get details of a item") @@ -52,4 +70,6 @@ public interface Items { + + } 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 1886dc462c..55fa1679d7 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,7 +1,6 @@ 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; @@ -14,5 +13,6 @@ public class MapItemToDto extends MappingBase<Item, ItemDto> { target.setDescription(source.getDescription()); target.setOwner(source.getOwner()); target.setStatus(source.getStatus().name()); + target.setProperties(source.getProperties()); } } 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 index 21c1f5199d..ae6e24bb65 100644 --- 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 @@ -13,13 +13,35 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.openecomp.sdcrests.item.rest.services; +import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_USER; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_ID; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_NAME; + +import java.util.Arrays; +import java.util.Collection; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import javax.annotation.PostConstruct; +import javax.inject.Named; +import javax.ws.rs.core.Response; 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.datatypes.model.ItemType; +import org.openecomp.sdc.itempermissions.PermissionsManager; +import org.openecomp.sdc.itempermissions.PermissionsManagerFactory; +import org.openecomp.sdc.itempermissions.impl.types.PermissionTypes; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; import org.openecomp.sdc.notification.dtos.Event; @@ -32,39 +54,31 @@ 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.ItemStatus; 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.openecomp.sdcrests.wrappers.GenericCollectionWrapper; 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; +import org.springframework.validation.annotation.Validated; @Named @Service("items") @Scope(value = "prototype") +@Validated public class ItemsImpl implements Items { - private ItemManager itemManager = - ItemManagerFactory.getInstance().createInterface(); + private ItemManager itemManager = ItemManagerFactory.getInstance().createInterface(); + + private static ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface(); - private static ActivityLogManager activityLogManager = - ActivityLogManagerFactory.getInstance().createInterface(); + private VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface(); - private VersioningManager versioningManager = - VersioningManagerFactory.getInstance().createInterface(); + private final PermissionsManager permissionsManager = PermissionsManagerFactory.getInstance().createInterface(); private static final Logger LOGGER = LoggerFactory.getLogger(ItemsImpl.class); @@ -73,65 +87,63 @@ public class ItemsImpl implements Items { private Map<ItemAction, ActionSideAffects> 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)); + @PostConstruct + public void initActionSideAffectsMap() { + 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) { + private static final String ONBOARDING_METHOD = "onboardingMethod"; + + @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(); - } + 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); + switch (request.getAction()) { + case ARCHIVE: + itemManager.archive(item); + break; + case RESTORE: + itemManager.restore(item); break; - case RESTORE: - itemManager.restore(item); - break; - default: + default: } - actionSideAffectsMap.get(request.getAction()).execute(item,user); + actionSideAffectsMap.get(request.getAction()).execute(item, user); - return Response.ok().build(); + 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); + public Response list(String itemStatusFilter, String versionStatusFilter, String itemTypeFilter, + String permissionFilter, String onboardingMethodFilter, String user) { - return Response.ok(itemDto).build(); - } + Predicate<Item> itemPredicate = + createItemPredicate(itemStatusFilter, versionStatusFilter, itemTypeFilter, onboardingMethodFilter, + permissionFilter, user); - private Version getLatestVersion(String itemId){ - List<Version> list = versioningManager.list(itemId); - return list.stream().max(Version::compareTo).get(); - } + GenericCollectionWrapper<ItemDto> results = new GenericCollectionWrapper<>(); + MapItemToDto mapper = new MapItemToDto(); + itemManager.list(itemPredicate).stream() + .sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime())) + .forEach(item -> results.add(mapper.applyMapping(item, ItemDto.class))); - private void notifyUsers(String itemId, String itemName, String message, - String userName, NotificationEventTypes eventType) { - Map<String, Object> eventProperties = new HashMap<>(); - eventProperties.put(ITEM_NAME, itemName == null ? itemManager.get(itemId).getName() : itemName); - eventProperties.put(ITEM_ID, itemId); + return Response.ok(results).build(); - 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); - } + @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 class SyncEvent implements Event { @@ -141,8 +153,7 @@ public class ItemsImpl implements Items { private Map<String, Object> attributes; private String entityId; - SyncEvent(String eventType, String originatorId, - Map<String, Object> attributes, String entityId) { + SyncEvent(String eventType, String originatorId, Map<String, Object> attributes, String entityId) { this.eventType = eventType; this.originatorId = originatorId; this.attributes = attributes; @@ -171,21 +182,145 @@ public class ItemsImpl implements Items { } - private class ActionSideAffects{ - private ActivityType activityType; - private NotificationEventTypes notificationType; + private class ActionSideAffects { + + private ActivityType activityType; + private NotificationEventTypes notificationType; + + private ActionSideAffects(ActivityType activityType, NotificationEventTypes notificationType) { + this.activityType = activityType; + this.notificationType = notificationType; - public ActionSideAffects(ActivityType activityType, NotificationEventTypes notificationType){ - this.activityType = activityType; - this.notificationType = notificationType; + } + + private Version getLatestVersion(String itemId) { + List<Version> list = versioningManager.list(itemId); + Optional<Version> max = list.stream().max(Version::compareTo); - } - 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, "", "")); + return max.orElse(null); } + + private void execute(Item item, String user) { + notifyUsers(item.getId(), item.getName(), user, this.notificationType); + activityLogManager.logActivity( + new ActivityLogEntity(item.getId(), getLatestVersion(item.getId()), this.activityType, user, true, + "", "")); + } + + private void notifyUsers(String itemId, String itemName, String userName, NotificationEventTypes eventType) { + Map<String, Object> eventProperties = new HashMap<>(); + eventProperties.put(ITEM_NAME, itemName == null ? itemManager.get(itemId).getName() : itemName); + eventProperties.put(ITEM_ID, itemId); + + 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 Predicate<Item> createItemPredicate(String itemStatusFilter, String versionStatusFilter, + String itemTypeFilter, String onboardingMethodFilter, String permissionsFilter, String user) { + Predicate<Item> itemPredicate = item -> true; + + if (itemStatusFilter != null) { + validateItemStatusValue(itemStatusFilter); + itemPredicate = itemPredicate.and(createItemStatusPredicate(itemStatusFilter)); + } + if (versionStatusFilter != null) { + validateVersionStatusValue(versionStatusFilter); + itemPredicate = itemPredicate.and(createVersionStatusPredicate(versionStatusFilter)); + } + if (itemTypeFilter != null) { + validateItemTypeValue(itemTypeFilter); + itemPredicate = itemPredicate.and(createItemTypePredicate(itemTypeFilter)); + } + if (onboardingMethodFilter != null) { + validateOnboardingMethodValue(onboardingMethodFilter); + itemPredicate = itemPredicate.and(createOnboardingMethodPredicate(onboardingMethodFilter)); + } + if (permissionsFilter != null) { + validatePermissionValue(permissionsFilter); + itemPredicate = itemPredicate.and(createPermissionsPredicate(user, permissionsFilter)); + } + return itemPredicate; + } + + private String formatFilter(String filterValue) { + return filterValue.replace(",", "|"); + } + + private Predicate<Item> createItemStatusPredicate(String filterValue) { + return item -> item.getStatus().name().matches(formatFilter(filterValue)); + } + + private Predicate<Item> createVersionStatusPredicate(String filterValue) { + Set<VersionStatus> versionStatuses = + Arrays.stream(filterValue.split(",")).map(VersionStatus::valueOf).collect(Collectors.toSet()); + return item -> item.getVersionStatusCounters().keySet().stream().anyMatch(versionStatuses::contains); + } + + private Predicate<Item> createItemTypePredicate(String filterValue) { + return item -> item.getType().matches(formatFilter(filterValue)); + } + + private Predicate<Item> createOnboardingMethodPredicate(String filterValue) { + return item -> !ItemType.vsp.name().equals(item.getType()) || ((String) item.getProperties() + .get(ONBOARDING_METHOD)) + .matches(formatFilter(filterValue)); } + private Predicate<Item> createPermissionsPredicate(String user, String filterValue) { + String[] permissions = filterValue.split(","); + Set<String> itemIds = new HashSet<>(); + for (String permission : permissions) { + itemIds.addAll(permissionsManager.listUserPermittedItems(user, permission)); + } + return item -> itemIds.contains(item.getId()); + } + + private void validateItemStatusValue(String itemStatusFilter) { + String[] values = itemStatusFilter.split(","); + for (String value : values) { + ItemStatus.valueOf(value); + } + } + + private void validateVersionStatusValue(String versionStatusFilter) { + String[] values = versionStatusFilter.split(","); + for (String value : values) { + VersionStatus.valueOf(value); + } + + } + + private void validateItemTypeValue(String itemTypeFilter) { + String[] values = itemTypeFilter.split(","); + for (String value : values) { + ItemType.valueOf(value); + } + } + + private void validateOnboardingMethodValue(String onboardingMethodFilter) { + String[] values = onboardingMethodFilter.split(","); + for (String value : values) { + OnboardingMethod.valueOf(value); + } + } + + private void validatePermissionValue(String permissionsFilter) { + String[] values = permissionsFilter.split(","); + for (String value : values) { + PermissionTypes.valueOf(value); + } + } + + //Do not delete - is in use, duplicates code to prevent dependency on openecomp-sdc-vendor-software-product-api + private enum OnboardingMethod { + NetworkPackage, Manual; + } } diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-types/src/main/java/org/openecomp/sdcrests/item/types/ItemDto.java b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-types/src/main/java/org/openecomp/sdcrests/item/types/ItemDto.java index 22bea01c35..1b7c6a6bfb 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-types/src/main/java/org/openecomp/sdcrests/item/types/ItemDto.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/item-rest/item-rest-types/src/main/java/org/openecomp/sdcrests/item/types/ItemDto.java @@ -1,5 +1,7 @@ package org.openecomp.sdcrests.item.types; +import java.util.Map; + public class ItemDto { private String id; private String type; @@ -7,6 +9,8 @@ public class ItemDto { private String description; private String owner; private String status; + private Map<String, Object> properties; + public String getId() { return id; @@ -55,4 +59,12 @@ public class ItemDto { public void setStatus(String status) { this.status = status; } + + public Map<String, Object> getProperties() { + return properties; + } + + public void setProperties(Map<String, Object> properties) { + this.properties = properties; + } } diff --git a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImpl.java b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImpl.java index 2761c85502..0e645dd4e9 100644 --- a/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImpl.java +++ b/openecomp-be/api/openecomp-sdc-rest-webapp/vendor-license-rest/vendor-license-rest-services/src/main/java/org/openecomp/sdcrests/vendorlicense/rest/services/VendorLicenseModelsImpl.java @@ -17,6 +17,21 @@ package org.openecomp.sdcrests.vendorlicense.rest.services; +import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_USER; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_ID; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_NAME; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.SUBMIT_DESCRIPTION; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERSION_ID; +import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERSION_NAME; +import static org.openecomp.sdcrests.vendorlicense.types.VendorLicenseModelActionRequestDto.VendorLicenseModelAction.Submit; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Predicate; +import javax.inject.Named; +import javax.ws.rs.core.Response; import org.openecomp.core.dao.UniqueValueDaoFactory; import org.openecomp.core.util.UniqueValueUtil; import org.openecomp.sdc.activitylog.ActivityLogManager; @@ -47,12 +62,12 @@ 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.ItemStatus; import org.openecomp.sdc.versioning.types.NotificationEventTypes; import org.openecomp.sdcrests.item.rest.mapping.MapItemToDto; import org.openecomp.sdcrests.item.rest.mapping.MapVersionToDto; import org.openecomp.sdcrests.item.types.ItemCreationDto; import org.openecomp.sdcrests.item.types.ItemDto; -import org.openecomp.sdc.versioning.types.ItemStatus; import org.openecomp.sdcrests.item.types.VersionDto; import org.openecomp.sdcrests.vendorlicense.rest.VendorLicenseModels; import org.openecomp.sdcrests.vendorlicense.rest.mapping.MapVendorLicenseModelEntityToDto; @@ -65,285 +80,254 @@ import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; -import javax.inject.Named; -import javax.ws.rs.core.Response; -import java.util.*; -import java.util.function.Predicate; -import java.util.stream.Collectors; - -import static org.openecomp.sdc.itempermissions.notifications.NotificationConstants.PERMISSION_USER; -import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_ID; -import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.ITEM_NAME; -import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.SUBMIT_DESCRIPTION; -import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERSION_ID; -import static org.openecomp.sdc.versioning.VersioningNotificationConstansts.VERSION_NAME; -import static org.openecomp.sdcrests.vendorlicense.types.VendorLicenseModelActionRequestDto.VendorLicenseModelAction.Submit; - @Named @Service("vendorLicenseModels") @Scope(value = "prototype") @Validated public class VendorLicenseModelsImpl implements VendorLicenseModels { - private static final String SUBMIT_ITEM_ACTION = "Submit_Item"; - private static final String SUBMIT_HEALED_VERSION_ERROR = - "VLM Id %s: Error while submitting version %s created based on Certified version %s for healing purpose."; - private static final Logger LOGGER = LoggerFactory.getLogger(VendorLicenseModelsImpl.class); - private PermissionsManager permissionsManager = PermissionsManagerFactory.getInstance() - .createInterface(); - private NotificationPropagationManager notifier = - NotificationPropagationManagerFactory.getInstance().createInterface(); - - private AsdcItemManager asdcItemManager = AsdcItemManagerFactory.getInstance().createInterface(); - private VersioningManager versioningManager = - VersioningManagerFactory.getInstance().createInterface(); - private VendorLicenseManager vendorLicenseManager = - VendorLicenseManagerFactory.getInstance().createInterface(); - private ActivityLogManager activityLogManager = - ActivityLogManagerFactory.getInstance().createInterface(); - private UniqueValueUtil uniqueValueUtil = new UniqueValueUtil(UniqueValueDaoFactory.getInstance() - .createInterface()); - - @Override - public Response listLicenseModels(String versionStatus,String itemStatus, String user) { - Predicate<Item> itemPredicate = createItemPredicate(versionStatus, itemStatus, user); - - MapItemToDto mapper = new MapItemToDto(); - GenericCollectionWrapper<ItemDto> results = new GenericCollectionWrapper<>(asdcItemManager.list(itemPredicate) - .stream().sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime())) - .map(item ->mapper.applyMapping(item, ItemDto.class)).collect(Collectors.toList())); - - return Response.ok(results).build(); - } - - @Override - public Response createLicenseModel(VendorLicenseModelRequestDto request, String user) { - - Item item = new Item(); - item.setType(ItemType.vlm.name()); - item.setOwner(user); - item.setStatus(ItemStatus.ACTIVE); - item.setName(request.getVendorName()); - item.setDescription(request.getDescription()); - - uniqueValueUtil - .validateUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, item.getName()); - item = asdcItemManager.create(item); - uniqueValueUtil - .createUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, item.getName()); - - Version version = versioningManager.create(item.getId(), new Version(), null); - - VendorLicenseModelEntity vlm = new MapVendorLicenseModelRequestDtoToVendorLicenseModelEntity() - .applyMapping(request, VendorLicenseModelEntity.class); - vlm.setId(item.getId()); - vlm.setVersion(version); - - vendorLicenseManager.createVendorLicenseModel(vlm); - versioningManager.publish(item.getId(), version, "Initial vlm:" + vlm.getVendorName()); - - ItemCreationDto itemCreationDto = new ItemCreationDto(); - itemCreationDto.setItemId(item.getId()); - itemCreationDto.setVersion(new MapVersionToDto().applyMapping(version, VersionDto.class)); - - activityLogManager.logActivity(new ActivityLogEntity(vlm.getId(), version, - ActivityType.Create, user, true, "", "")); - return Response.ok(itemCreationDto).build(); - } - - @Override - public Response updateLicenseModel(VendorLicenseModelRequestDto request, String vlmId, - String versionId, String user) { - VendorLicenseModelEntity vlm = - new MapVendorLicenseModelRequestDtoToVendorLicenseModelEntity() - .applyMapping(request, VendorLicenseModelEntity.class); - vlm.setId(vlmId); - vlm.setVersion(new Version(versionId)); - - vendorLicenseManager.updateVendorLicenseModel(vlm); - return Response.ok().build(); - } - - @Override - public Response getLicenseModel(String vlmId, String versionId, String user) { - Version version = versioningManager.get(vlmId, new Version(versionId)); - VendorLicenseModelEntity vlm = vendorLicenseManager.getVendorLicenseModel(vlmId, version); - vlm.setWritetimeMicroSeconds(version.getModificationTime().getTime()); - - try { - Optional<Version> healedVersion = HealingManagerFactory.getInstance().createInterface() - .healItemVersion(vlmId, version, ItemType.vlm, false); - - if (healedVersion.isPresent()) { - vlm.setVersion(healedVersion.get()); - if (version.getStatus() == VersionStatus.Certified) { - submitHealedVersion(vlmId, healedVersion.get(), versionId, user); - } - } - } catch (Exception e) { - LOGGER.error( - String.format("Error while auto healing VLM with Id %s and version %s", vlmId, versionId), - e); - } + private static final String SUBMIT_ITEM_ACTION = "Submit_Item"; + private static final String SUBMIT_HEALED_VERSION_ERROR = + "VLM Id %s: Error while submitting version %s created based on Certified version %s for healing purpose."; + private static final Logger LOGGER = LoggerFactory.getLogger(VendorLicenseModelsImpl.class); + private PermissionsManager permissionsManager = PermissionsManagerFactory.getInstance().createInterface(); + private NotificationPropagationManager notifier = + NotificationPropagationManagerFactory.getInstance().createInterface(); + + private AsdcItemManager asdcItemManager = AsdcItemManagerFactory.getInstance().createInterface(); + private VersioningManager versioningManager = VersioningManagerFactory.getInstance().createInterface(); + private VendorLicenseManager vendorLicenseManager = VendorLicenseManagerFactory.getInstance().createInterface(); + private ActivityLogManager activityLogManager = ActivityLogManagerFactory.getInstance().createInterface(); + private UniqueValueUtil uniqueValueUtil = + new UniqueValueUtil(UniqueValueDaoFactory.getInstance().createInterface()); - VendorLicenseModelEntityDto vlmDto = - new MapVendorLicenseModelEntityToDto().applyMapping(vlm, VendorLicenseModelEntityDto.class); - return Response.ok(vlmDto).build(); - } + @Override + public Response listLicenseModels(String versionStatus, String itemStatus, String user) { + Predicate<Item> itemPredicate = createItemPredicate(versionStatus, itemStatus, user); - @Override - public Response deleteLicenseModel(String vlmId, String user) { - Item vlm = asdcItemManager.get(vlmId); + GenericCollectionWrapper<ItemDto> results = new GenericCollectionWrapper<>(); + MapItemToDto mapper = new MapItemToDto(); + asdcItemManager.list(itemPredicate).stream() + .sorted((o1, o2) -> o2.getModificationTime().compareTo(o1.getModificationTime())) + .forEach(item -> results.add(mapper.applyMapping(item, ItemDto.class))); - if (!vlm.getType().equals(ItemType.vlm.name())){ - throw new CoreException((new ErrorCode.ErrorCodeBuilder() - .withMessage(String.format("Vlm with id %s does not exist.", - vlmId)).build())); + return Response.ok(results).build(); } - Integer certifiedVersionsCounter = vlm.getVersionStatusCounters().get(VersionStatus.Certified); - if (Objects.isNull(certifiedVersionsCounter) || certifiedVersionsCounter == 0) { - asdcItemManager.delete(vlm); - permissionsManager.deleteItemPermissions(vlmId); - uniqueValueUtil - .deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlm.getName()); - notifyUsers(vlmId, vlm.getName(), null, null, user, - NotificationEventTypes.DELETE); - - return Response.ok().build(); - } else { - return Response.status(Response.Status.FORBIDDEN) - .entity(new Exception(Messages.DELETE_VLM_ERROR.getErrorMessage())).build(); - } - } + @Override + public Response createLicenseModel(VendorLicenseModelRequestDto request, String user) { - @Override - public Response actOnLicenseModel(VendorLicenseModelActionRequestDto request, String vlmId, - String versionId, String user) { - Version version = new Version(versionId); + Item item = new Item(); + item.setType(ItemType.vlm.name()); + item.setOwner(user); + item.setStatus(ItemStatus.ACTIVE); + item.setName(request.getVendorName()); + item.setDescription(request.getDescription()); - if (request.getAction() == Submit) { - if (!permissionsManager.isAllowed(vlmId, user, SUBMIT_ITEM_ACTION)) { - return Response.status(Response.Status.FORBIDDEN) - .entity(new Exception(Messages.PERMISSIONS_ERROR.getErrorMessage())).build(); - } - String message = - request.getSubmitRequest() == null ? "Submit" : request.getSubmitRequest().getMessage(); - submit(vlmId, version, message, user); + uniqueValueUtil.validateUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, item.getName()); + item = asdcItemManager.create(item); + uniqueValueUtil.createUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, item.getName()); - notifyUsers(vlmId, null, version, message, user, NotificationEventTypes.SUBMIT); + Version version = versioningManager.create(item.getId(), new Version(), null); + VendorLicenseModelEntity vlm = new MapVendorLicenseModelRequestDtoToVendorLicenseModelEntity() + .applyMapping(request, VendorLicenseModelEntity.class); + vlm.setId(item.getId()); + vlm.setVersion(version); + + vendorLicenseManager.createVendorLicenseModel(vlm); + versioningManager.publish(item.getId(), version, "Initial vlm:" + vlm.getVendorName()); + + ItemCreationDto itemCreationDto = new ItemCreationDto(); + itemCreationDto.setItemId(item.getId()); + itemCreationDto.setVersion(new MapVersionToDto().applyMapping(version, VersionDto.class)); + + activityLogManager + .logActivity(new ActivityLogEntity(vlm.getId(), version, ActivityType.Create, user, true, "", "")); + return Response.ok(itemCreationDto).build(); } - return Response.ok().build(); - } - - private void submit(String vlmId, Version version, String message, String user) { - - vendorLicenseManager.validate(vlmId, version); - versioningManager.submit(vlmId, version, message); - - activityLogManager.logActivity( - new ActivityLogEntity(vlmId, version, ActivityType.Submit, user, true, "", message)); - } - - private void submitHealedVersion(String vlmId, Version healedVersion, String baseVersionId, - String user) { - try { - submit(vlmId, healedVersion, "Submit after heal", user); - } catch (Exception ex) { - LOGGER.error( - String.format(SUBMIT_HEALED_VERSION_ERROR, vlmId, healedVersion.getId(), baseVersionId), - ex); - } - } - - private void notifyUsers(String itemId, String itemName, Version version, String message, - String userName, NotificationEventTypes eventType) { - Map<String, Object> eventProperties = new HashMap<>(); - eventProperties.put(ITEM_NAME, itemName == null ? asdcItemManager.get(itemId).getName() : itemName); - eventProperties.put(ITEM_ID, itemId); - - if (version != null) { - eventProperties.put(VERSION_NAME, version.getName() == null - ? versioningManager.get(itemId, version).getName() - : version.getName()); - eventProperties.put(VERSION_ID, version.getId()); + + @Override + public Response updateLicenseModel(VendorLicenseModelRequestDto request, String vlmId, String versionId, + String user) { + VendorLicenseModelEntity vlm = new MapVendorLicenseModelRequestDtoToVendorLicenseModelEntity() + .applyMapping(request, VendorLicenseModelEntity.class); + vlm.setId(vlmId); + vlm.setVersion(new Version(versionId)); + + vendorLicenseManager.updateVendorLicenseModel(vlm); + return Response.ok().build(); } - eventProperties.put(SUBMIT_DESCRIPTION, message); - eventProperties.put(PERMISSION_USER, userName); + @Override + public Response getLicenseModel(String vlmId, String versionId, String user) { + Version version = versioningManager.get(vlmId, new Version(versionId)); + VendorLicenseModelEntity vlm = vendorLicenseManager.getVendorLicenseModel(vlmId, version); + vlm.setWritetimeMicroSeconds(version.getModificationTime().getTime()); + + try { + Optional<Version> healedVersion = HealingManagerFactory.getInstance().createInterface() + .healItemVersion(vlmId, version, ItemType.vlm, + false); + + if (healedVersion.isPresent()) { + vlm.setVersion(healedVersion.get()); + if (version.getStatus() == VersionStatus.Certified) { + submitHealedVersion(vlmId, healedVersion.get(), versionId, user); + } + } + } catch (Exception e) { + LOGGER.error(String.format("Error while auto healing VLM with Id %s and version %s", vlmId, versionId), e); + } - 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 o item '" + itemId); + VendorLicenseModelEntityDto vlmDto = + new MapVendorLicenseModelEntityToDto().applyMapping(vlm, VendorLicenseModelEntityDto.class); + return Response.ok(vlmDto).build(); } - } - private class SyncEvent implements Event { + @Override + public Response deleteLicenseModel(String vlmId, String user) { + Item vlm = asdcItemManager.get(vlmId); - private String eventType; - private String originatorId; - private Map<String, Object> attributes; - private String entityId; + if (!vlm.getType().equals(ItemType.vlm.name())) { + throw new CoreException((new ErrorCode.ErrorCodeBuilder() + .withMessage(String.format("Vlm with id %s does not exist.", vlmId)) + .build())); + } - SyncEvent(String eventType, String originatorId, - Map<String, Object> attributes, String entityId) { - this.eventType = eventType; - this.originatorId = originatorId; - this.attributes = attributes; - this.entityId = entityId; + Integer certifiedVersionsCounter = vlm.getVersionStatusCounters().get(VersionStatus.Certified); + if (Objects.isNull(certifiedVersionsCounter) || certifiedVersionsCounter == 0) { + asdcItemManager.delete(vlm); + permissionsManager.deleteItemPermissions(vlmId); + uniqueValueUtil.deleteUniqueValue(VendorLicenseConstants.UniqueValues.VENDOR_NAME, vlm.getName()); + notifyUsers(vlmId, vlm.getName(), null, null, user, NotificationEventTypes.DELETE); + + return Response.ok().build(); + } else { + return Response.status(Response.Status.FORBIDDEN) + .entity(new Exception(Messages.DELETE_VLM_ERROR.getErrorMessage())).build(); + } } @Override - public String getEventType() { - return eventType; + public Response actOnLicenseModel(VendorLicenseModelActionRequestDto request, String vlmId, String versionId, + String user) { + Version version = new Version(versionId); + + if (request.getAction() == Submit) { + if (!permissionsManager.isAllowed(vlmId, user, SUBMIT_ITEM_ACTION)) { + return Response.status(Response.Status.FORBIDDEN) + .entity(new Exception(Messages.PERMISSIONS_ERROR.getErrorMessage())).build(); + } + String message = request.getSubmitRequest() == null ? "Submit" : request.getSubmitRequest().getMessage(); + submit(vlmId, version, message, user); + + notifyUsers(vlmId, null, version, message, user, NotificationEventTypes.SUBMIT); + + } + return Response.ok().build(); } - @Override - public String getOriginatorId() { - return originatorId; + private void submit(String vlmId, Version version, String message, String user) { + + vendorLicenseManager.validate(vlmId, version); + versioningManager.submit(vlmId, version, message); + + activityLogManager + .logActivity(new ActivityLogEntity(vlmId, version, ActivityType.Submit, user, true, "", message)); } - @Override - public Map<String, Object> getAttributes() { - return attributes; + private void submitHealedVersion(String vlmId, Version healedVersion, String baseVersionId, String user) { + try { + submit(vlmId, healedVersion, "Submit after heal", user); + } catch (Exception ex) { + LOGGER.error(String.format(SUBMIT_HEALED_VERSION_ERROR, vlmId, healedVersion.getId(), baseVersionId), ex); + } } - @Override - public String getEntityId() { - return entityId; + private void notifyUsers(String itemId, String itemName, Version version, String message, String userName, + NotificationEventTypes eventType) { + Map<String, Object> eventProperties = new HashMap<>(); + eventProperties.put(ITEM_NAME, itemName == null ? asdcItemManager.get(itemId).getName() : itemName); + eventProperties.put(ITEM_ID, itemId); + + if (version != null) { + eventProperties.put(VERSION_NAME, + version.getName() == null ? versioningManager.get(itemId, version).getName() : version.getName()); + eventProperties.put(VERSION_ID, version.getId()); + } + + 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 o item '" + itemId); + } + } + + private class SyncEvent implements Event { + + private String eventType; + private String originatorId; + private Map<String, Object> attributes; + private String entityId; + + SyncEvent(String eventType, String originatorId, Map<String, Object> 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<String, Object> getAttributes() { + return attributes; + } + + @Override + public String getEntityId() { + return entityId; + } } - } - - private boolean userHasPermission(String itemId, String userId) { - String permission = permissionsManager.getUserItemPermission(itemId, userId); - return (permission != null && permission - .matches(PermissionTypes.Contributor.name() + "|" + PermissionTypes.Owner.name())); - } - - private Predicate<Item> createItemPredicate(String versionStatus, - String itemStatus, - String user) { - Predicate<Item> itemPredicate = item -> ItemType.vlm.name().equals(item.getType()); - - if (ItemStatus.ARCHIVED.name().equals(itemStatus)) { - itemPredicate = itemPredicate.and(item -> ItemStatus.ARCHIVED.equals(item.getStatus())); - } else { - itemPredicate = itemPredicate.and(item -> ItemStatus.ACTIVE.equals(item.getStatus())); - - if (VersionStatus.Certified.name().equals(versionStatus)) { - itemPredicate = itemPredicate - .and(item -> item.getVersionStatusCounters().containsKey(VersionStatus.Certified)); - - } else if (VersionStatus.Draft.name().equals(versionStatus)) { - itemPredicate = itemPredicate.and( - item -> item.getVersionStatusCounters().containsKey(VersionStatus.Draft) - && userHasPermission(item.getId(), user)); - } + + private boolean userHasPermission(String itemId, String userId) { + String permission = permissionsManager.getUserItemPermission(itemId, userId); + return (permission != null && permission.matches( + PermissionTypes.Contributor.name() + "|" + PermissionTypes.Owner.name())); + } + + private Predicate<Item> createItemPredicate(String versionStatus, String itemStatus, String user) { + Predicate<Item> itemPredicate = item -> ItemType.vlm.name().equals(item.getType()); + + if (ItemStatus.ARCHIVED.name().equals(itemStatus)) { + itemPredicate = itemPredicate.and(item -> ItemStatus.ARCHIVED.equals(item.getStatus())); + } else { + itemPredicate = itemPredicate.and(item -> ItemStatus.ACTIVE.equals(item.getStatus())); + + if (VersionStatus.Certified.name().equals(versionStatus)) { + itemPredicate = + itemPredicate.and(item -> item.getVersionStatusCounters().containsKey(VersionStatus.Certified)); + + } else if (VersionStatus.Draft.name().equals(versionStatus)) { + itemPredicate = itemPredicate + .and(item -> item.getVersionStatusCounters().containsKey(VersionStatus.Draft) + && userHasPermission(item.getId(), user)); + } + } + return itemPredicate; } - return itemPredicate; - } } |