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 <kumar.nikesh@simens.com>
  • Loading branch information
Nikesh kumar committed Jun 14, 2024
1 parent 20c818a commit a2e50ff
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2284,13 +2284,15 @@ 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,
@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 @@ -2305,17 +2307,24 @@ 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 @@ -43,6 +43,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 @@ -84,6 +85,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 @@ -905,5 +907,67 @@ 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()) {
approvedCliAttachments = SW360Utils.getClxAttachmentForRelease(release);
}
final String releaseId = release.getId();

if (approvedCliAttachments.size() == 1) {
final Attachment filteredAttachment = approvedCliAttachments.get(0);
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 a2e50ff

Please sign in to comment.