Skip to content

Commit

Permalink
feat(rest): Add size parameter to clearing request.
Browse files Browse the repository at this point in the history
Signed-off-by: Akshit Joshi <[email protected]>
  • Loading branch information
akshitjoshii committed Nov 20, 2024
1 parent ffd83c6 commit c016baa
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClearingRequestSize, Integer> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,15 @@ private ThriftEnumUtils() {
ClearingRequestType.HIGH, "High ISR"
);

private static final ImmutableMap<ClearingRequestSize, String> 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<UserAccess, String> MAP_USER_ACCESS_STRING = ImmutableMap.<UserAccess, String>builder()
.put(UserAccess.READ, "Read")
.put(UserAccess.READ_WRITE, "Read and Write")
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions libraries/datahandler/src/main/thrift/moderation.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
**/
Expand Down
4 changes: 3 additions & 1 deletion libraries/datahandler/src/main/thrift/projects.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -247,7 +248,8 @@ struct ClearingRequest {
17: optional i64 modifiedOn,
18: optional list<i64> reOpenOn,
19: optional ClearingPriority priority,
20: optional ClearingType clearingType
20: optional ClearingType clearingType,
21: optional ClearingSize clearingSize
}

struct ProjectDTO{
Expand Down
8 changes: 8 additions & 0 deletions libraries/datahandler/src/main/thrift/sw360.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ private HalResource<ClearingRequest> 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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -131,6 +133,9 @@ public class RestControllerHelper<T> {
@NonNull
private final Sw360ObligationService obligationService;

@NonNull
private final Sw360ClearingRequestService clearingRequestService;

@NonNull
private final ResourceComparatorGenerator<T> resourceComparatorGenerator = new ResourceComparatorGenerator<>();

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -1636,4 +1642,17 @@ public HalResource<Release> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit c016baa

Please sign in to comment.