Skip to content

Commit

Permalink
feat(REST): New endpoint split components
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Ali <alimuhammad@siemens.com>
  • Loading branch information
rudra-superrr authored and muhammadali9699 committed Jul 7, 2023
1 parent af7f8ca commit a4e7f68
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -944,29 +944,30 @@ private void mergeAttachments(Component mergeSelection, Component mergeTarget, C
if (mergeTarget.getAttachments() == null) {
mergeTarget.setAttachments(new HashSet<>());
}

Set<String> attachmentIdsSelected = mergeSelection.getAttachments().stream()
.map(Attachment::getAttachmentContentId).collect(Collectors.toSet());
// add new attachments from source
Set<Attachment> attachmentsToAdd = new HashSet<>();
mergeSource.getAttachments().forEach(a -> {
if (attachmentIdsSelected.contains(a.getAttachmentContentId())) {
attachmentsToAdd.add(a);
}
});
// remove moved attachments in source
attachmentsToAdd.forEach(a -> {
mergeTarget.addToAttachments(a);
mergeSource.getAttachments().remove(a);
});
// delete unchosen attachments from target
Set<Attachment> attachmentsToDelete = new HashSet<>();
mergeTarget.getAttachments().forEach(a -> {
if (!attachmentIdsSelected.contains(a.getAttachmentContentId())) {
attachmentsToDelete.add(a);
}
});
mergeTarget.getAttachments().removeAll(attachmentsToDelete);
if (mergeSelection.getAttachments() != null) {
Set<String> attachmentIdsSelected = mergeSelection.getAttachments().stream()
.map(Attachment::getAttachmentContentId).collect(Collectors.toSet());
// add new attachments from source
Set<Attachment> attachmentsToAdd = new HashSet<>();
mergeSource.getAttachments().forEach(a -> {
if (attachmentIdsSelected.contains(a.getAttachmentContentId())) {
attachmentsToAdd.add(a);
}
});
// remove moved attachments in source
attachmentsToAdd.forEach(a -> {
mergeTarget.addToAttachments(a);
mergeSource.getAttachments().remove(a);
});
// delete unchosen attachments from target
Set<Attachment> attachmentsToDelete = new HashSet<>();
mergeTarget.getAttachments().forEach(a -> {
if (!attachmentIdsSelected.contains(a.getAttachmentContentId())) {
attachmentsToDelete.add(a);
}
});
mergeTarget.getAttachments().removeAll(attachmentsToDelete);
}
}

private void transferReleases(Set<String> releaseIds, Component mergeTarget, Component mergeSource) throws SW360Exception {
Expand Down
34 changes: 34 additions & 0 deletions rest/resource-server/src/docs/asciidoc/components.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,40 @@ include::{snippets}/should_document_update_component/http-response.adoc[]
include::{snippets}/should_document_update_component/links.adoc[]


[[resources-components-update]]
==== Merge a component

A `PATCH` request is used to merge two components

===== Response structure
include::{snippets}/should_document_merge_components/response-fields.adoc[]

===== Example request
include::{snippets}/should_document_merge_components/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_merge_components/http-response.adoc[]

===== Links
include::{snippets}/should_document_merge_components/links.adoc[]

[[resources-components-update]]
==== Split a component

A `PATCH` request is used to split two components

===== Response structure
include::{snippets}/should_document_split_components/response-fields.adoc[]

===== Example request
include::{snippets}/should_document_split_components/curl-request.adoc[]

===== Example response
include::{snippets}/should_document_split_components/http-response.adoc[]

===== Links
include::{snippets}/should_document_split_components/links.adoc[]

[[resources-components-delete]]
==== Delete a component

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -696,4 +696,36 @@ private ImportBomRequestPreparation handleImportBomRequestPreparation(ImportBomR
}
return importBomRequestPreparationResponse;
}

@PreAuthorize("hasAuthority('WRITE')")
@RequestMapping(value = COMPONENTS_URL + "/mergecomponents", method = RequestMethod.PATCH)
public ResponseEntity<RequestStatus> mergeComponents(
@RequestParam(value = "mergeTargetId", required = true) String mergeTargetId,
@RequestParam(value = "mergeSourceId", required = true) String mergeSourceId,
@RequestBody Component mergeSelection ) throws TException {


User sw360User = restControllerHelper.getSw360UserFromAuthentication();

// perform the real merge, update merge target and delete merge sources
RequestStatus requestStatus = componentService.mergeComponents(mergeTargetId, mergeSourceId, mergeSelection, sw360User);

return new ResponseEntity<>(requestStatus, HttpStatus.OK);
}

@PreAuthorize("hasAuthority('WRITE')")
@RequestMapping(value = COMPONENTS_URL + "/splitComponents", method = RequestMethod.PATCH)
public ResponseEntity<RequestStatus> splitComponents(
@RequestBody Map<String, Component> componentMap) throws TException {

User sw360User = restControllerHelper.getSw360UserFromAuthentication();

Component srcComponent = componentMap.get("srcComponent");
Component targetComponent = componentMap.get("targetComponent");

// perform the real merge, update merge target and delete merge source
RequestStatus requestStatus = componentService.splitComponents(srcComponent, targetComponent, sw360User);

return new ResponseEntity<>(requestStatus, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,37 @@ public ImportBomRequestPreparation prepareImportSBOM(User user, String attachmen
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
return sw360ComponentClient.prepareImportBom(user, attachmentContentId);
}

public RequestStatus mergeComponents(String componentTargetId, String componentSourceId, Component componentSelection, User user) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
RequestStatus requestStatus;
requestStatus = sw360ComponentClient.mergeComponents(componentTargetId, componentSourceId, componentSelection, user);

if (requestStatus == RequestStatus.IN_USE) {
throw new HttpMessageNotReadableException("Component already in use.");
} else if (requestStatus == RequestStatus.FAILURE) {
throw new HttpMessageNotReadableException("Cannot merge these components");
} else if (requestStatus == RequestStatus.ACCESS_DENIED) {
throw new RuntimeException("Access denied");
}

return requestStatus;
}


public RequestStatus splitComponents(Component srcComponent, Component targetComponent, User sw360User) throws TException {
ComponentService.Iface sw360ComponentClient = getThriftComponentClient();
RequestStatus requestStatus;
requestStatus = sw360ComponentClient.splitComponent(srcComponent, targetComponent, sw360User);

if (requestStatus == RequestStatus.IN_USE) {
throw new HttpMessageNotReadableException("Component already in use.");
} else if (requestStatus == RequestStatus.FAILURE) {
throw new HttpMessageNotReadableException("Cannot split these components");
} else if (requestStatus == RequestStatus.ACCESS_DENIED) {
throw new RuntimeException("Access denied...!");
}

return requestStatus;
}
}
Loading

0 comments on commit a4e7f68

Please sign in to comment.