From c016baa92f3be2d38bfb6c1054eeebdf9266a6a9 Mon Sep 17 00:00:00 2001 From: Akshit Joshi Date: Wed, 16 Oct 2024 14:36:40 +0530 Subject: [PATCH] feat(rest): Add size parameter to clearing request. Signed-off-by: Akshit Joshi --- .../db/ProjectDatabaseHandler.java | 5 +++++ .../sw360/moderation/ModerationHandler.java | 9 ++++++++ .../db/ModerationDatabaseHandler.java | 10 +++++++++ .../sw360/datahandler/common/SW360Utils.java | 21 +++++++++++++++++++ .../datahandler/common/ThriftEnumUtils.java | 10 +++++++++ .../src/main/thrift/moderation.thrift | 6 ++++++ .../src/main/thrift/projects.thrift | 4 +++- .../datahandler/src/main/thrift/sw360.thrift | 8 +++++++ .../ClearingRequestController.java | 2 ++ .../Sw360ClearingRequestService.java | 8 +++++++ .../core/JacksonCustomizations.java | 3 ++- .../core/RestControllerHelper.java | 19 +++++++++++++++++ .../project/ProjectController.java | 2 +- 13 files changed, 104 insertions(+), 3 deletions(-) diff --git a/backend/common/src/main/java/org/eclipse/sw360/datahandler/db/ProjectDatabaseHandler.java b/backend/common/src/main/java/org/eclipse/sw360/datahandler/db/ProjectDatabaseHandler.java index 8c4e3d2f95..46c196fd56 100644 --- a/backend/common/src/main/java/org/eclipse/sw360/datahandler/db/ProjectDatabaseHandler.java +++ b/backend/common/src/main/java/org/eclipse/sw360/datahandler/db/ProjectDatabaseHandler.java @@ -250,6 +250,11 @@ public AddDocumentRequestSummary createClearingRequest(ClearingRequest clearingR if (!(ProjectClearingState.CLOSED.equals(project.getClearingState()) || Visibility.PRIVATE.equals(project.getVisbility()))) { clearingRequest.setProjectBU(project.getBusinessUnit()); + Project projectWithInfo = fillClearingStateSummaryIncludingSubprojectsForSingleProject(project, user); + int initialOpenReleases = SW360Utils.getOpenReleaseCount(projectWithInfo.releaseClearingStateSummary); + ClearingRequestSize initialSize = SW360Utils.determineCRSize(initialOpenReleases); + clearingRequest.setClearingSize(initialSize); + log.info(initialOpenReleases + " open releases found for project: " + project.getId() + "and initial size is: " + initialSize); String crId = moderator.createClearingRequest(clearingRequest, user); if (CommonUtils.isNotNullEmptyOrWhitespace(crId)) { project.setClearingRequestId(crId); diff --git a/backend/moderation/src/main/java/org/eclipse/sw360/moderation/ModerationHandler.java b/backend/moderation/src/main/java/org/eclipse/sw360/moderation/ModerationHandler.java index 07cf1bcdf1..1c8923ad9a 100644 --- a/backend/moderation/src/main/java/org/eclipse/sw360/moderation/ModerationHandler.java +++ b/backend/moderation/src/main/java/org/eclipse/sw360/moderation/ModerationHandler.java @@ -19,6 +19,7 @@ import org.eclipse.sw360.datahandler.thrift.RemoveModeratorRequestStatus; import org.eclipse.sw360.datahandler.thrift.RequestStatus; import org.eclipse.sw360.datahandler.thrift.SW360Exception; +import org.eclipse.sw360.datahandler.thrift.ClearingRequestSize; import org.eclipse.sw360.datahandler.thrift.components.Component; import org.eclipse.sw360.datahandler.thrift.components.Release; import org.eclipse.sw360.datahandler.thrift.licenses.License; @@ -358,6 +359,14 @@ public void updateClearingRequestForChangeInProjectBU(String crId, String busine handler.updateClearingRequestForChangeInProjectBU(crId, businessUnit, user); } + @Override + public void updateClearingRequestForChangeInClearingSize(String crId, ClearingRequestSize size) throws TException { + assertId(crId); + assertNotNull(size); + + handler.updateClearingRequestForChangeInClearingSize(crId, size); + } + @Override public RequestStatus addCommentToClearingRequest(String id, Comment comment, User user) throws TException { assertId(id); diff --git a/backend/moderation/src/main/java/org/eclipse/sw360/moderation/db/ModerationDatabaseHandler.java b/backend/moderation/src/main/java/org/eclipse/sw360/moderation/db/ModerationDatabaseHandler.java index d56318caa8..463d33d458 100644 --- a/backend/moderation/src/main/java/org/eclipse/sw360/moderation/db/ModerationDatabaseHandler.java +++ b/backend/moderation/src/main/java/org/eclipse/sw360/moderation/db/ModerationDatabaseHandler.java @@ -30,6 +30,7 @@ import org.eclipse.sw360.datahandler.permissions.PermissionUtils; import org.eclipse.sw360.datahandler.thrift.ClearingRequestEmailTemplate; import org.eclipse.sw360.datahandler.thrift.ClearingRequestState; +import org.eclipse.sw360.datahandler.thrift.ClearingRequestSize; import org.eclipse.sw360.datahandler.thrift.Comment; import org.eclipse.sw360.datahandler.thrift.ModerationState; import org.eclipse.sw360.datahandler.thrift.PaginationData; @@ -320,6 +321,15 @@ public void updateClearingRequestForChangeInProjectBU(String crId, String busine log.error("Failed to update CR-ID: %s with error: %s", crId, e.getMessage()); } } + public void updateClearingRequestForChangeInClearingSize(String crId, ClearingRequestSize size) { + try{ + ClearingRequest clearingRequest = clearingRequestRepository.get(crId); + clearingRequest.setClearingSize(size); + clearingRequestRepository.update(clearingRequest); + } catch (Exception e) { + log.error("Failed to update CR-ID: %s with error: %s", crId, e.getMessage()); + } + } public RequestStatus addCommentToClearingRequest(String id, Comment comment, User user) { try { diff --git a/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/SW360Utils.java b/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/SW360Utils.java index 29c8c44abe..35b7826394 100644 --- a/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/SW360Utils.java +++ b/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/SW360Utils.java @@ -755,6 +755,27 @@ public static String convertEpochTimeToDate(long timestamp) { return date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); } + public static ClearingRequestSize determineCRSize(int totalReleaseCount) { + if (totalReleaseCount <= CLEARING_REQUEST_SIZE_MAP.get(ClearingRequestSize.VERY_SMALL)) { + return ClearingRequestSize.VERY_SMALL; + } else if (totalReleaseCount <= CLEARING_REQUEST_SIZE_MAP.get(ClearingRequestSize.SMALL)) { + return ClearingRequestSize.SMALL; + } else if (totalReleaseCount <= CLEARING_REQUEST_SIZE_MAP.get(ClearingRequestSize.MEDIUM)) { + return ClearingRequestSize.MEDIUM; + } else if (totalReleaseCount <= CLEARING_REQUEST_SIZE_MAP.get(ClearingRequestSize.LARGE)) { + return ClearingRequestSize.LARGE; + } else { + return ClearingRequestSize.VERY_LARGE; + } + } + + public static final HashMap CLEARING_REQUEST_SIZE_MAP = new HashMap<>() {{ + put(ClearingRequestSize.VERY_SMALL, 20); + put(ClearingRequestSize.SMALL, 50); + put(ClearingRequestSize.MEDIUM, 75); + put(ClearingRequestSize.LARGE, 150); + }}; + /** * Assumes that the process exists. */ diff --git a/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/ThriftEnumUtils.java b/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/ThriftEnumUtils.java index f18a6b948a..08e995e19d 100644 --- a/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/ThriftEnumUtils.java +++ b/libraries/datahandler/src/main/java/org/eclipse/sw360/datahandler/common/ThriftEnumUtils.java @@ -363,6 +363,15 @@ private ThriftEnumUtils() { ClearingRequestType.HIGH, "High ISR" ); + private static final ImmutableMap MAP_CLEARING_REQUEST_SIZE_STRING = ImmutableMap.of( + ClearingRequestSize.VERY_SMALL, "Very Small", + ClearingRequestSize.SMALL, "Small", + ClearingRequestSize.MEDIUM, "Medium", + ClearingRequestSize.LARGE, "Large", + ClearingRequestSize.VERY_LARGE, "Very Large" + ); + + private static final ImmutableMap MAP_USER_ACCESS_STRING = ImmutableMap.builder() .put(UserAccess.READ, "Read") .put(UserAccess.READ_WRITE, "Read and Write") @@ -456,6 +465,7 @@ private ThriftEnumUtils() { .put(PackageManager.class, MAP_PACKAGE_MANAGER_STRING) .put(CycloneDxComponentType.class, MAP_CYCLONE_DX_COMPONENT_TYPE_STRING) .put(ClearingRequestType.class, MAP_CLEARING_REQUEST_TYPE_STRING) + .put(ClearingRequestSize.class, MAP_CLEARING_REQUEST_SIZE_STRING) .build(); public static String enumToString(TEnum value) { diff --git a/libraries/datahandler/src/main/thrift/moderation.thrift b/libraries/datahandler/src/main/thrift/moderation.thrift index e6c6c02094..3e1b1d87bb 100644 --- a/libraries/datahandler/src/main/thrift/moderation.thrift +++ b/libraries/datahandler/src/main/thrift/moderation.thrift @@ -34,6 +34,7 @@ typedef licenses.License License typedef licenses.Obligation Obligation typedef components.ComponentType ComponentType typedef projects.ClearingRequest ClearingRequest +typedef sw360.ClearingRequestSize ClearingSize typedef spdxdocument.SPDXDocument SPDXDocument typedef documentcreationinformation.DocumentCreationInformation DocumentCreationInformation typedef packageinformation.PackageInformation PackageInformation @@ -314,6 +315,11 @@ service ModerationService { **/ oneway void updateClearingRequestForChangeInProjectBU(1: string crId, 2: string businessUnit, 3: User user); + /** + * update clearing request if project's BU is changed + **/ + oneway void updateClearingRequestForChangeInClearingSize(1: string crId, 2: ClearingSize size); + /** * get clearing request by Id for view/read **/ diff --git a/libraries/datahandler/src/main/thrift/projects.thrift b/libraries/datahandler/src/main/thrift/projects.thrift index 6238dbe945..d6db26574d 100644 --- a/libraries/datahandler/src/main/thrift/projects.thrift +++ b/libraries/datahandler/src/main/thrift/projects.thrift @@ -30,6 +30,7 @@ typedef sw360.SW360Exception SW360Exception typedef sw360.ClearingRequestState ClearingState typedef sw360.ClearingRequestPriority ClearingPriority typedef sw360.ClearingRequestType ClearingType +typedef sw360.ClearingRequestSize ClearingSize typedef sw360.Comment Comment typedef sw360.PaginationData PaginationData typedef components.Release Release @@ -247,7 +248,8 @@ struct ClearingRequest { 17: optional i64 modifiedOn, 18: optional list reOpenOn, 19: optional ClearingPriority priority, - 20: optional ClearingType clearingType + 20: optional ClearingType clearingType, + 21: optional ClearingSize clearingSize } struct ProjectDTO{ diff --git a/libraries/datahandler/src/main/thrift/sw360.thrift b/libraries/datahandler/src/main/thrift/sw360.thrift index af673f577d..752184ad92 100644 --- a/libraries/datahandler/src/main/thrift/sw360.thrift +++ b/libraries/datahandler/src/main/thrift/sw360.thrift @@ -89,6 +89,14 @@ enum ClearingRequestType { HIGH = 1 } +enum ClearingRequestSize{ + VERY_SMALL = 0, + SMALL = 1, + MEDIUM = 2, + LARGE = 3, + VERY_LARGE = 4 +} + enum Visibility { PRIVATE = 0, ME_AND_MODERATORS = 1, diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/clearingrequest/ClearingRequestController.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/clearingrequest/ClearingRequestController.java index 99c5ca3862..14241c9580 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/clearingrequest/ClearingRequestController.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/clearingrequest/ClearingRequestController.java @@ -140,6 +140,8 @@ private HalResource createHalClearingRequestWithAllDetails(Clea try{ Project project = projectService.getProjectForUserById(clearingRequest.getProjectId(), sw360User); Project projectWithClearingInfo = projectService.getClearingInfo(project, sw360User); + ClearingRequest updatedCR = restControllerHelper.updateCRSize(clearingRequest, projectWithClearingInfo, sw360User); + halClearingRequest = new HalResource<>(updatedCR); restControllerHelper.addEmbeddedReleaseDetails(halClearingRequest, projectWithClearingInfo); restControllerHelper.addEmbeddedProject(halClearingRequest, project, true); }catch (Exception e){ diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/clearingrequest/Sw360ClearingRequestService.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/clearingrequest/Sw360ClearingRequestService.java index 7dceef0549..81bec46ef6 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/clearingrequest/Sw360ClearingRequestService.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/clearingrequest/Sw360ClearingRequestService.java @@ -16,6 +16,7 @@ import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.THttpClient; import org.apache.thrift.transport.TTransportException; +import org.eclipse.sw360.datahandler.thrift.ClearingRequestSize; import org.eclipse.sw360.datahandler.thrift.ClearingRequestState; import org.eclipse.sw360.datahandler.thrift.Comment; import org.eclipse.sw360.datahandler.thrift.RequestStatus; @@ -122,4 +123,11 @@ public RequestStatus updateClearingRequest(ClearingRequest clearingRequest, User return requestStatus; } + public void updateClearingRequestForChangeInClearingSize(String crId, ClearingRequestSize size) throws TException{ + try { + getThriftModerationClient().updateClearingRequestForChangeInClearingSize(crId, size); + } catch (SW360Exception e) { + log.error("Error updating clearing request for change in clearing size: " + e.getMessage()); + } + } } diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/JacksonCustomizations.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/JacksonCustomizations.java index e36b38a648..20e5c5dbdc 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/JacksonCustomizations.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/JacksonCustomizations.java @@ -1890,7 +1890,8 @@ public static abstract class ReferenceDocDataMixin extends ReferenceDocData { "modifiedOn", "commentsSize", "setPriority", - "setClearingType" + "setClearingType", + "setClearingSize" }) @JsonRootName(value = "clearingRequest") public static abstract class ClearingRequestMixin extends ClearingRequest { diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/RestControllerHelper.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/RestControllerHelper.java index e246a9062d..14c51ca90c 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/RestControllerHelper.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/core/RestControllerHelper.java @@ -29,6 +29,7 @@ import org.eclipse.sw360.datahandler.resourcelists.ResourceComparatorGenerator; import org.eclipse.sw360.datahandler.resourcelists.ResourceListController; import org.eclipse.sw360.datahandler.thrift.Comment; +import org.eclipse.sw360.datahandler.thrift.ClearingRequestSize; import org.eclipse.sw360.datahandler.thrift.ProjectReleaseRelationship; import org.eclipse.sw360.datahandler.thrift.Quadratic; import org.eclipse.sw360.datahandler.thrift.SW360Exception; @@ -54,6 +55,7 @@ import org.eclipse.sw360.datahandler.thrift.vendors.Vendor; import org.eclipse.sw360.datahandler.thrift.vulnerabilities.*; import org.eclipse.sw360.rest.resourceserver.attachment.AttachmentController; +import org.eclipse.sw360.rest.resourceserver.clearingrequest.Sw360ClearingRequestService; import org.eclipse.sw360.rest.resourceserver.component.ComponentController; import org.eclipse.sw360.rest.resourceserver.license.LicenseController; import org.eclipse.sw360.rest.resourceserver.license.Sw360LicenseService; @@ -131,6 +133,9 @@ public class RestControllerHelper { @NonNull private final Sw360ObligationService obligationService; + @NonNull + private final Sw360ClearingRequestService clearingRequestService; + @NonNull private final ResourceComparatorGenerator resourceComparatorGenerator = new ResourceComparatorGenerator<>(); @@ -1394,6 +1399,7 @@ public ClearingRequest convertToEmbeddedClearingRequest(ClearingRequest clearing embeddedClearingRequest.setType(null); embeddedClearingRequest.setClearingType(clearingRequest.getClearingType()); embeddedClearingRequest.setTimestamp(clearingRequest.getTimestamp()); + embeddedClearingRequest.setClearingSize(clearingRequest.getClearingSize()); return embeddedClearingRequest; } @@ -1636,4 +1642,17 @@ public HalResource createHalReleaseResourceWithAllDetails(Release relea release.unsetAttachments(); return halRelease; } + public ClearingRequest updateCRSize(ClearingRequest clearingRequest, Project project, User sw360User) throws TException { + int openReleaseCount = SW360Utils.getOpenReleaseCount(project.getReleaseClearingStateSummary()); + ClearingRequestSize currentSize = SW360Utils.determineCRSize(openReleaseCount); + ClearingRequestSize initialSize = clearingRequest.getClearingSize(); + if(initialSize == null) return clearingRequest; + if(!initialSize.equals(ClearingRequestSize.VERY_LARGE)) { + int limit = SW360Utils.CLEARING_REQUEST_SIZE_MAP.get(initialSize); + if(openReleaseCount > limit){ + clearingRequestService.updateClearingRequestForChangeInClearingSize(clearingRequest.getId(), currentSize); + } + } + return clearingRequestService.getClearingRequestById(clearingRequest.getId(), sw360User); + } } diff --git a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java index 98083a1423..58a96a36d2 100644 --- a/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java +++ b/rest/resource-server/src/main/java/org/eclipse/sw360/rest/resourceserver/project/ProjectController.java @@ -3264,7 +3264,7 @@ public ResponseEntity createDuplicateProjectWithDependencyNetwork( if (reqBodyMap.get("dependencyNetwork") != null) { try { addOrPatchDependencyNetworkToProject(duplicatedProject, reqBodyMap, ProjectOperation.CREATE); - } catch (JsonProcessingException | NoSuchElementException | InvalidPropertiesFormatException e) { + } catch (JsonProcessingException | NoSuchElementException e) { log.error(e.getMessage()); return ResponseEntity.badRequest().body(e.getMessage()); }