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@siemens.com>
  • Loading branch information
nikkuma7 committed Oct 22, 2024
1 parent cd97b61 commit 9549846
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2539,13 +2539,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 @@ -2560,17 +2563,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 @@ -41,6 +41,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 @@ -83,6 +84,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 @@ -1150,6 +1152,72 @@ public Integer loadPreferredClearingDateLimit() {
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;
}

public List<Map<String, String>> serveDependencyNetworkListView(String projectId, User sw360User) throws TException {
try {
ProjectService.Iface sw360ProjectClient = getThriftProjectClient();
Expand All @@ -1166,7 +1234,7 @@ public List<Map<String, String>> serveDependencyNetworkListView(String projectId
}
}

public ProjectLink serveLinkedResourcesOfProjectInDependencyNetwork(String projectId, boolean transitive, User sw360User) throws TException {
public ProjectLink serveLinkedResourcesOfProjectInDependencyNetwork(String projectId, boolean transitive, User sw360User) throws TException{
Project project = getProjectForUserById(projectId, sw360User);
final Collection<ProjectLink> linkedProjects = (!transitive)
? SW360Utils.getLinkedProjectsAsFlatList(project, false, new ThriftClients(), log, sw360User)
Expand Down

0 comments on commit 9549846

Please sign in to comment.