Skip to content

Commit

Permalink
feat(rest) : endpoint to list license obligations from license database.
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 Feb 14, 2024
1 parent 325ac54 commit 1b49027
Show file tree
Hide file tree
Showing 5 changed files with 464 additions and 2 deletions.
28 changes: 28 additions & 0 deletions rest/resource-server/src/docs/asciidoc/projects.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,34 @@ include::{snippets}/should_document_get_project_releases_ecc_information/http-re
===== Links
include::{snippets}/should_document_get_project_releases_ecc_information/links.adoc[]

[[resources-project-get-license-obligation-information]]
==== Listing obligations from licenseDB

A `GET` request will get all license obligations from licenseDB.

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

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

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

[[resources-project-add-license-obligation-information]]
==== Adding obligations from licenseDB to project

A `POST` request will add the license obligations from licenseDB to project.

===== Request structure
Pass an array of license obligation ids to be added as request body.

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

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

[[resources-project-get-license-clearing-information-information]]
==== Project's license clearing count

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest;
import org.eclipse.sw360.datahandler.thrift.packages.Package;
import org.eclipse.sw360.datahandler.thrift.projects.ClearingRequest;
import org.eclipse.sw360.datahandler.thrift.projects.ObligationStatusInfo;
import org.eclipse.sw360.datahandler.thrift.projects.Project;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectProjectRelationship;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectState;
Expand Down Expand Up @@ -106,6 +107,7 @@ public Sw360Module() {
setMixInAnnotation(ClearingRequest.class, Sw360Module.ClearingRequestMixin.class);
setMixInAnnotation(Comment.class, Sw360Module.CommentMixin.class);
setMixInAnnotation(ProjectReleaseRelationship.class, Sw360Module.ProjectReleaseRelationshipMixin.class);
setMixInAnnotation(ObligationStatusInfo.class, Sw360Module.ObligationStatusInfoMixin.class);
setMixInAnnotation(ReleaseVulnerabilityRelation.class, Sw360Module.ReleaseVulnerabilityRelationMixin.class);
setMixInAnnotation(VerificationStateInfo.class, Sw360Module.VerificationStateInfoMixin.class);
setMixInAnnotation(ProjectProjectRelationship.class, Sw360Module.ProjectProjectRelationshipMixin.class);
Expand Down Expand Up @@ -156,6 +158,7 @@ public Sw360Module() {
.replaceWithClass(ClearingRequest.class, ClearingRequestMixin.class)
.replaceWithClass(Comment.class, CommentMixin.class)
.replaceWithClass(ProjectReleaseRelationship.class, ProjectReleaseRelationshipMixin.class)
.replaceWithClass(ObligationStatusInfo.class, ObligationStatusInfoMixin.class)
.replaceWithClass(ReleaseVulnerabilityRelation.class, ReleaseVulnerabilityRelationMixin.class)
.replaceWithClass(VerificationStateInfo.class, VerificationStateInfoMixin.class)
.replaceWithClass(ProjectProjectRelationship.class, ProjectProjectRelationshipMixin.class)
Expand Down Expand Up @@ -1810,6 +1813,29 @@ public static abstract class CommentMixin extends Comment {
public static abstract class ProjectReleaseRelationshipMixin extends ProjectReleaseRelationship {
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties({
"setComment",
"setText",
"setObligationType",
"setObligationLevel",
"setModifiedBy",
"setModifiedOn",
"setId",
"setStatus",
"setAction",
"setLicenseIds",
"setReleaseIdToAcceptedCLI",
"releaseIdToAcceptedCLISize",
"releasesSize",
"releasesIterator",
"setReleases",
"licenseIdsSize",
"licenseIdsIterator"
})
public static abstract class ObligationStatusInfoMixin extends ObligationStatusInfo {
}

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties({
"setId",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
Expand Down Expand Up @@ -49,6 +50,7 @@
import org.eclipse.sw360.datahandler.resourcelists.PaginationResult;
import org.eclipse.sw360.datahandler.resourcelists.ResourceClassNotFoundException;
import org.eclipse.sw360.datahandler.thrift.MainlineState;
import org.eclipse.sw360.datahandler.thrift.ObligationStatus;
import org.eclipse.sw360.datahandler.thrift.ProjectReleaseRelationship;
import org.eclipse.sw360.datahandler.thrift.ReleaseRelationship;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
Expand All @@ -72,6 +74,8 @@
import org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatInfo;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.OutputFormatVariant;
import org.eclipse.sw360.datahandler.thrift.licenses.License;
import org.eclipse.sw360.datahandler.thrift.projects.ObligationList;
import org.eclipse.sw360.datahandler.thrift.projects.ObligationStatusInfo;
import org.eclipse.sw360.datahandler.thrift.projects.Project;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectClearingState;
import org.eclipse.sw360.datahandler.thrift.projects.ProjectLink;
Expand Down Expand Up @@ -2061,6 +2065,71 @@ public void getlicenseClearingCount(HttpServletResponse response ,
}
}

@Operation(
description = "Get license obligations data from license database.",
tags = {"Project"}
)
@RequestMapping(value = PROJECTS_URL + "/{id}/licenseDbObligations", method = RequestMethod.GET)
public ResponseEntity<?> getLicObligations(Pageable pageable,
@Parameter(description = "Project ID.") @PathVariable("id") String id, HttpServletRequest request)
throws TException, URISyntaxException, PaginationParameterException, ResourceClassNotFoundException {

final User sw360User = restControllerHelper.getSw360UserFromAuthentication();
final Project sw360Project = projectService.getProjectForUserById(id, sw360User);
if (CommonUtils.isNullOrEmptyMap(sw360Project.getReleaseIdToUsage())) {
return new ResponseEntity<String>("No release linked to the project", HttpStatus.CONFLICT);
}
Map<String, AttachmentUsage> licenseInfoAttachmentUsage = projectService.getLicenseInfoAttachmentUsage(id);
if(licenseInfoAttachmentUsage.size() == 0) {
return new ResponseEntity<String>("No approved CLI or licenseInfo attachment usage present for the project", HttpStatus.CONFLICT);
}
Map<String, Set<Release>> licensesFromAttachmentUsage = projectService.getLicensesFromAttachmentUsage(
licenseInfoAttachmentUsage, sw360Project.getReleaseIdToUsage(), sw360User);
Map<String, ObligationStatusInfo> licenseObligation = projectService.getLicenseObligationData(licensesFromAttachmentUsage, sw360User);

HalResource<Map<String, ObligationStatusInfo>> halObligation = new HalResource<>(licenseObligation);
return new ResponseEntity<>(halObligation, HttpStatus.OK);
}

@PreAuthorize("hasAuthority('WRITE')")
@Operation(
summary = "Add licenseObligations from license DB",
description = "Pass an array of obligation ids in request body.",
tags = {"Projects"}
)
@RequestMapping(value = PROJECTS_URL + "/{id}/licenseObligation", method = RequestMethod.POST)
public ResponseEntity<?> addLicenseObligations(
@Parameter(description = "License Obligation ID.")
@PathVariable("id") String id,
@Parameter(description = "Set of license obligation IDs to be added.",
example = "[\"3765276512\",\"5578999\",\"3765276513\"]"
)
@RequestBody List<String> obligationIds
) throws URISyntaxException, TException {
final User sw360User = restControllerHelper.getSw360UserFromAuthentication();
final Project sw360Project = projectService.getProjectForUserById(id, sw360User);
Map<String, AttachmentUsage> licenseInfoAttachmentUsage = projectService.getLicenseInfoAttachmentUsage(id);
Map<String, Set<Release>> licensesFromAttachmentUsage = projectService.getLicensesFromAttachmentUsage(
licenseInfoAttachmentUsage, sw360Project.getReleaseIdToUsage(), sw360User);
Map<String, ObligationStatusInfo> licenseObligation = projectService.getLicenseObligationData(licensesFromAttachmentUsage, sw360User);
Map<String, ObligationStatusInfo> selectedLicenseObligation = new HashMap<String, ObligationStatusInfo>();

if(licenseObligation.size() == 0) {
return new ResponseEntity<>("No License Obligations Present", HttpStatus.NO_CONTENT);
}
for (Map.Entry<String, ObligationStatusInfo> entry : licenseObligation.entrySet()) {
String oblId = entry.getValue().getId();
if (obligationIds.contains(oblId)) {
selectedLicenseObligation.put(entry.getKey(), entry.getValue());
}
}
RequestStatus requestStatus= projectService.updateLinkedObligations(sw360Project, sw360User, selectedLicenseObligation);
if (requestStatus == RequestStatus.SUCCESS) {
return new ResponseEntity<>("License Obligation Added Successfully", HttpStatus.CREATED);
}
return new ResponseEntity<>("Failed to add/update obligation for project", HttpStatus.NOT_FOUND);
}

@Operation(
description = "Get summary and administration page of project tab.",
tags = {"Projects"}
Expand Down
Loading

0 comments on commit 1b49027

Please sign in to comment.