Skip to content

Commit

Permalink
feat(rest): saveUsages in project page
Browse files Browse the repository at this point in the history
Signed-off-by: Rudra Chopra <prabhuchopra@gmail.com>
  • Loading branch information
rudra-superrr committed Mar 20, 2024
1 parent f051d63 commit 01359d1
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,37 @@ private void saveAttachmentUsages(ResourceRequest request, ResourceResponse resp
User user = UserCacheHolder.getUserFromRequest(request);
if (PermissionUtils.makePermission(project, user).isActionAllowed(RequestedAction.WRITE)) {
List<AttachmentUsage> deselectedUsagesFromRequest = ProjectPortletUtils.deselectedAttachmentUsagesFromRequest(request);
System.out.println("list of deselectedUsagesFromRequest :" + deselectedUsagesFromRequest);
List<AttachmentUsage> selectedUsagesFromRequest = ProjectPortletUtils.selectedAttachmentUsagesFromRequest(request);
System.out.println("list of selectedUsagesFromRequest :" + selectedUsagesFromRequest);
List<AttachmentUsage> allUsagesByProject = attachmentClient.getUsedAttachments(Source.projectId(projectId), null);
System.out.println("list of allUsagesByProject :" + allUsagesByProject);
List<String> selectedData = new ArrayList<>();
for (AttachmentUsage usage : allUsagesByProject) {
if (usage.getUsageData().getSetField().equals(UsageData._Fields.LICENSE_INFO)) {
StringBuilder result = new StringBuilder();
result.append(usage.getUsageData().getLicenseInfo().getProjectPath())
.append("-")
.append(usage.getOwner().getReleaseId())
.append("_")
.append(usage.getUsageData().getSetField().getFieldName())
.append("_")
.append(usage.getAttachmentContentId());
String stringResult = result.toString();
selectedData.add(stringResult);
}
else {
StringBuilder result = new StringBuilder();
result.append(usage.getOwner().getReleaseId())
.append("_")
.append(usage.getUsageData().getSetField().getFieldName())
.append("_")
.append(usage.getAttachmentContentId());
String stringResult = result.toString();
selectedData.add(stringResult);
}
}
System.out.println("Resulting string: " + selectedData);
List<AttachmentUsage> usagesToDelete = allUsagesByProject.stream()
.filter(usage -> deselectedUsagesFromRequest.stream()
.anyMatch(isUsageEquivalent(usage)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,15 @@ private static List<AttachmentUsage> makeAttachmentUsagesFromRequestParameters(R
BiFunction<Set<String>, Set<String>, Set<String>> computeUsagesFromCheckboxes, boolean deselectUsage) {
final String projectId = request.getParameter(PROJECT_ID);
Set<String> selectedUsages = new HashSet<>(arrayToList(request.getParameterValues(PROJECT_SELECTED_ATTACHMENT_USAGES)));
System.out.println("Set of selectedUsages: " + selectedUsages);
Set<String> changedUsages = new HashSet<>(arrayToList(request.getParameterValues(PROJECT_SELECTED_ATTACHMENT_USAGES_SHADOWS)));
System.out.println("Set of changedUsages: " + changedUsages);
Set<String> changedIncludeConludedLicenses = new HashSet<>(
arrayToList(request.getParameterValues(INCLUDE_CONCLUDED_LICENSE_SHADOWS)));
System.out.println("changedIncludeConludedLicenses = " + changedIncludeConludedLicenses);
changedUsages = Sets.union(changedUsages, new HashSet(changedIncludeConludedLicenses));
List<String> includeConludedLicenses = arrayToList(request.getParameterValues(INCLUDE_CONCLUDED_LICENSE));
System.out.println("includeConludedLicenses = " + includeConludedLicenses);
Set<String> usagesSubset = computeUsagesFromCheckboxes.apply(changedUsages, selectedUsages);
if (deselectUsage) {
usagesSubset = Sets.union(usagesSubset, new HashSet(changedIncludeConludedLicenses));
Expand Down
24 changes: 24 additions & 0 deletions rest/resource-server/src/docs/asciidoc/projects.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,30 @@ include::{snippets}/should_document_get_attachment_usage_for_project/curl-reques
===== Example response
include::{snippets}/should_document_get_attachment_usage_for_project/http-response.adoc[]

[[resources-project-save-attachmentUsages]]
==== Save attachmentUsages of the project

A `POST` request is used to save attachmentUsages of the project. Please pass a Map<String, List<String>> having string as key and list<string> as value in request body.

===== Request structure 1
Pass a Map<String, List<String>> in request body.

Keys can be selected, deselected, selectedConcludedUsages or deselectedConcludedUsages.

Format of String inside list varies according to the usageFields (sourcePackage, licenseInfo, manuallySet) :

for `sourcePackage`, releaseId_sourcePakage_attachmentContentId

for `licenseInfo`, projectId-releaseId_licenseInfo_attachmentContentId (for directly linked project) OR projectPath-releaseId_licenseInfo_attachmentContentId (for nested projects)

for `manuallySet`, releaseId_manuallySet_attachmentContentId

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

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

[[resources-project-get-project-vulnerabilities]]
==== Listing project vulnerabilities

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.eclipse.sw360.datahandler.thrift.Source;
import org.eclipse.sw360.datahandler.thrift.attachments.Attachment;
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent;
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentService;
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentType;
import org.eclipse.sw360.datahandler.thrift.attachments.AttachmentUsage;
import org.eclipse.sw360.datahandler.thrift.attachments.UsageData;
Expand Down Expand Up @@ -1492,6 +1493,87 @@ public ResponseEntity<CollectionModel<EntityModel<Project>>> getUsedByProjectDet
return new ResponseEntity<>(resources, status);
}

@PreAuthorize("hasAuthority('WRITE')")
@Operation(
summary = "save attachment usages",
description = "Pass an array of string in request body.",
tags = {"Projects"}
)
@RequestMapping(value = PROJECTS_URL + "/{id}/saveAttachmentUsages", method = RequestMethod.POST)
public ResponseEntity<?> saveAttachmentUsages(
@Parameter(description = "Project ID.")
@PathVariable("id") String id,
@Parameter(description = "Map of key-value pairs where each key is associated with a list of strings.",
example = "{\"selected\": [\"4427a8e723ad405db63f75170ef240a2_sourcePackage_5c5d6f54ac6a4b33bcd3c5d3a8fefc43\", \"value2\"],"
+ " \"deselected\": [\"de213309ba0842ac8a7251bf27ea8f36_manuallySet_eec66c3465f64f0292dfc2564215c681\", \"value2\"]},"
+ " \"selectedConcludedUsages\": [\"de213309ba0842ac8a7251bf27ea8f36_licenseInfo_eec66c3465f64f0292dfc2564215c681\", \"value2\"]},"
+ " \"deselectedConcludedUsages\": [\"ade213309ba0842ac8a7251bf27ea8f36_licenseInfo_aeec66c3465f64f0292dfc2564215c681\", \"value2\"]}"
)
@RequestBody Map<String,List<String>> allUsages
) throws TException {
final User user = restControllerHelper.getSw360UserFromAuthentication();
final Project project = projectService.getProjectForUserById(id, user);
try {
if (PermissionUtils.makePermission(project, user).isActionAllowed(RequestedAction.WRITE)) {
Source usedBy = Source.projectId(id);
List<String> selectedUsages = new ArrayList<>();
List<String> deselectedUsages = new ArrayList<>();
List<String> selectedConcludedUsages = new ArrayList<>();
List<String> deselectedConcludedUsages = new ArrayList<>();
List<String> changedUsages = new ArrayList<>();
for (Map.Entry<String, List<String>> entry : allUsages.entrySet()) {
String key = entry.getKey();
List<String> list = entry.getValue();
if (key.equals("selected")) {
selectedUsages.addAll(list);
} else if (key.equals("deselected")) {
deselectedUsages.addAll(list);
} else if (key.equals("selectedConcludedUsages")) {
selectedConcludedUsages.addAll(list);
} else if (key.equals("deselectedConcludedUsages")) {
deselectedConcludedUsages.addAll(list);
}
}
Set<String> totalReleaseIds = projectService.getReleaseIds(id, user, true);
changedUsages.addAll(selectedUsages);
changedUsages.addAll(deselectedUsages);
boolean valid = projectService.validate(changedUsages, user, releaseService, totalReleaseIds);
if (!valid) {
return new ResponseEntity<>("Not a valid attachment type OR release does not belong to project", HttpStatus.CONFLICT);
}
List<AttachmentUsage> allUsagesByProject = projectService.getUsedAttachments(usedBy, null);
List<String> savedUsages = projectService.savedUsages(allUsagesByProject);
savedUsages.removeAll(deselectedUsages);
deselectedUsages.addAll(selectedUsages);
selectedUsages.addAll(savedUsages);
deselectedConcludedUsages.addAll(selectedConcludedUsages);
List<AttachmentUsage> deselectedUsagesFromRequest = projectService.deselectedAttachmentUsagesFromRequest(deselectedUsages, selectedUsages, deselectedConcludedUsages, selectedConcludedUsages, id);
List<AttachmentUsage> selectedUsagesFromRequest = projectService.selectedAttachmentUsagesFromRequest(deselectedUsages, selectedUsages, deselectedConcludedUsages, selectedConcludedUsages, id);
List<AttachmentUsage> usagesToDelete = allUsagesByProject.stream()
.filter(usage -> deselectedUsagesFromRequest.stream()
.anyMatch(projectService.isUsageEquivalent(usage)))
.collect(Collectors.toList());
if (!usagesToDelete.isEmpty()) {
projectService.deleteAttachmentUsages(usagesToDelete);
}
List<AttachmentUsage> allUsagesByProjectAfterCleanUp = projectService.getUsedAttachments(usedBy, null);
List<AttachmentUsage> usagesToCreate = selectedUsagesFromRequest.stream()
.filter(usage -> allUsagesByProjectAfterCleanUp.stream()
.noneMatch(projectService.isUsageEquivalent(usage)))
.collect(Collectors.toList());

if (!usagesToCreate.isEmpty()) {
projectService.makeAttachmentUsages(usagesToCreate);
}
return new ResponseEntity<>("AttachmentUsages Saved Successfully", HttpStatus.CREATED);
} else {
return new ResponseEntity<>("No write permission for project", HttpStatus.FORBIDDEN);
}
} catch (TException e) {
return new ResponseEntity<>("Saving attachment usages for project " + id + " failed", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@Operation(
description = "Get all attachmentUsages of the projects.",
tags = {"Projects"}
Expand Down
Loading

0 comments on commit 01359d1

Please sign in to comment.