Skip to content

Commit

Permalink
fix(rest): Added code to get obligation releaseView data in project.
Browse files Browse the repository at this point in the history
Signed-off-by: Nikesh kumar <[email protected]>
  • Loading branch information
Nikesh kumar committed Oct 14, 2024
1 parent eec9f15 commit 3c7a24f
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2428,13 +2428,16 @@ private Map<String, Object> createPaginationMetadata(Pageable pageable, Map<Stri
tags = {"Project"}
)
@RequestMapping(value = PROJECTS_URL + "/{id}/licenseObligations", method = RequestMethod.GET)
public ResponseEntity<HalResource> getLicenseObligations(Pageable pageable,
@Parameter(description = "Project ID.") @PathVariable("id") String id)
public ResponseEntity<Object> getLicenseObligations(Pageable pageable,
@Parameter(description = "Project ID.") @PathVariable("id") String id,
@Parameter(description = "If true, returns the license obligation data in release view. "
+ "Otherwise, returns it in project view.")
@RequestParam(value = "view", defaultValue = "false") boolean releaseView)
throws TException {
final User sw360User = restControllerHelper.getSw360UserFromAuthentication();
final Project sw360Project = projectService.getProjectForUserById(id, sw360User);
final Map<String, String> releaseIdToAcceptedCLI = Maps.newHashMap();
List<Release> releases = new ArrayList<>();;
List<Release> releases = new ArrayList<>();
ObligationList obligation = new ObligationList();
Map<String, ObligationStatusInfo> obligationStatusMap = Maps.newHashMap();
List<String> releaseIds = new ArrayList<>(sw360Project.getReleaseIdToUsage().keySet());
Expand All @@ -2449,17 +2452,25 @@ public ResponseEntity<HalResource> getLicenseObligations(Pageable pageable,
obligationStatusMap = CommonUtils.nullToEmptyMap(obligation.getLinkedObligationStatus());
releaseIdToAcceptedCLI.putAll(SW360Utils.getReleaseIdtoAcceptedCLIMappings(obligationStatusMap));
}
if (releaseView) {
final List<LicenseInfoParsingResult> licenseInfoWithObligations = new ArrayList<>();
List<LicenseInfoParsingResult> processedLicenses = projectService.processLicenseInfoWithObligations(
licenseInfoWithObligations, releaseIdToAcceptedCLI, releases, sw360User);
return new ResponseEntity<>(processedLicenses, HttpStatus.OK);
} else {
obligationStatusMap = projectService.setLicenseInfoWithObligations(obligationStatusMap,
releaseIdToAcceptedCLI, releases, sw360User);
for (Map.Entry<String, ObligationStatusInfo> entry : obligationStatusMap.entrySet()) {
ObligationStatusInfo statusInfo = entry.getValue();
Set<Release> limitedSet = releaseService
.getReleasesForUserByIds(statusInfo.getReleaseIdToAcceptedCLI().keySet());
statusInfo.setReleases(limitedSet);
}

obligationStatusMap = projectService.setLicenseInfoWithObligations(obligationStatusMap, releaseIdToAcceptedCLI, releases, sw360User);
for (Map.Entry<String, ObligationStatusInfo> entry : obligationStatusMap.entrySet()) {
ObligationStatusInfo statusInfo = entry.getValue();
Set<Release> limitedSet = releaseService.getReleasesForUserByIds(statusInfo.getReleaseIdToAcceptedCLI().keySet());
statusInfo.setReleases(limitedSet);
Map<String, Object> responseBody = createPaginationMetadata(pageable, obligationStatusMap);
HalResource<Map<String, Object>> halObligation = new HalResource<>(responseBody);
return new ResponseEntity<>(halObligation, HttpStatus.OK);
}

Map<String, Object> responseBody = createPaginationMetadata(pageable, obligationStatusMap);
HalResource<Map<String, Object>> halObligation = new HalResource<>(responseBody);
return new ResponseEntity<>(halObligation, HttpStatus.OK);
}

@PreAuthorize("hasAuthority('WRITE')")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.eclipse.sw360.datahandler.thrift.components.ReleaseLink;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoParsingResult;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseInfoService;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseNameWithText;
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseService;
import org.eclipse.sw360.datahandler.thrift.licenses.License;
import org.eclipse.sw360.datahandler.thrift.licenseinfo.LicenseObligationsStatusInfo;
Expand Down Expand Up @@ -85,6 +86,7 @@
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -953,5 +955,72 @@ public Integer loadPreferredClearingDateLimit() {
// returning default value 7 (days) if variable is not set
return limit < 1 ? 7 : limit;
}
}


public List<LicenseInfoParsingResult> processLicenseInfoWithObligations(
List<LicenseInfoParsingResult> licenseInfoWithObligations, Map<String, String> releaseIdToAcceptedCLI,
List<Release> releases, User user) throws TException {
ThriftClients thriftClients = new ThriftClients();
LicenseInfoService.Iface licenseClient = thriftClients.makeLicenseInfoClient();

for (Release release : releases) {
List<Attachment> approvedCliAttachments = SW360Utils.getApprovedClxAttachmentForRelease(release);
if (approvedCliAttachments.isEmpty()) {
log.info("No approved CLX attachments found for release: {}. Proceeding with attached CLX.",
release.getId());
approvedCliAttachments = SW360Utils.getClxAttachmentForRelease(release);
}
final String releaseId = release.getId();

for (Attachment filteredAttachment : approvedCliAttachments) {
final String attachmentContentId = filteredAttachment.getAttachmentContentId();

if (releaseIdToAcceptedCLI.containsKey(releaseId)
&& releaseIdToAcceptedCLI.get(releaseId).equals(attachmentContentId)) {
releaseIdToAcceptedCLI.remove(releaseId);
}

try {
List<LicenseInfoParsingResult> licenseResults = licenseClient.getLicenseInfoForAttachment(release,
attachmentContentId, false, user);
List<ObligationParsingResult> obligationResults = licenseClient.getObligationsForAttachment(release,
attachmentContentId, user);

if (CommonUtils.allAreNotEmpty(licenseResults, obligationResults)
&& obligationResults.get(0).getObligationsAtProjectSize() > 0) {
licenseInfoWithObligations.add(licenseClient
.createLicenseToObligationMapping(licenseResults.get(0), obligationResults.get(0)));
}
} catch (TException exception) {
log.error(String.format("Error fetching license Information for attachment: %s in release: %s",
filteredAttachment.getFilename(), releaseId), exception);
}
}
}

Predicate<LicenseNameWithText> filterLicense = license -> (license.isSetObligationsAtProject()
&& !(SW360Constants.LICENSE_NAME_UNKNOWN.equals(license.getLicenseName())
|| SW360Constants.NA.equalsIgnoreCase(license.getLicenseName())));

licenseInfoWithObligations.stream()
.sorted(Comparator.comparing(LicenseInfoParsingResult::getName, String.CASE_INSENSITIVE_ORDER))
.forEach(e -> {
Set<LicenseNameWithText> updatedLicenses = e.getLicenseInfo().getLicenseNamesWithTexts().stream()
.filter(filterLicense).map(license -> {
if (SW360Constants.LICENSE_TYPE_GLOBAL.equalsIgnoreCase(license.getType())) {
license.setType(SW360Constants.LICENSE_TYPE_GLOBAL);
} else {
license.setType(SW360Constants.LICENSE_TYPE_OTHERS);
}
return license;
})
.sorted(Comparator.comparing(LicenseNameWithText::getType, String.CASE_INSENSITIVE_ORDER)
.thenComparing(LicenseNameWithText::getLicenseName, String.CASE_INSENSITIVE_ORDER))
.collect(Collectors.toCollection(LinkedHashSet::new));
e.getLicenseInfo().setLicenseNamesWithTexts(updatedLicenses);
});

return licenseInfoWithObligations;
}
}

0 comments on commit 3c7a24f

Please sign in to comment.