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
nikkuma7 authored and GMishx committed Jan 6, 2025
1 parent 2e4b511 commit b91d3ad
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2736,13 +2736,16 @@ public ResponseEntity<?> removeOrphanObligation(
tags = {"Projects"}
)
@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 @@ -2757,17 +2760,36 @@ 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);
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);
}
// Include obligation status in the response
Map<String, Object> responseBody = new HashMap<>();
responseBody.put("processedLicenses", processedLicenses);
responseBody.put("obligationStatusMap", obligationStatusMap);
return new ResponseEntity<>(responseBody, 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);
}

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);
}
}

@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.eclipse.sw360.datahandler.thrift.components.ReleaseNode;
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 @@ -93,6 +94,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 @@ -440,22 +442,26 @@ static AttachmentUsage mergeAttachmentUsages(AttachmentUsage u1, AttachmentUsage
}
AttachmentUsage mergedUsage = u1.deepCopy();
switch (u1.getUsageData().getSetField()) {
case LICENSE_INFO:
Set<String> mergedExcludedLicenseIds = new HashSet<>(
Optional.of(u1).map(AttachmentUsage::getUsageData).map(UsageData::getLicenseInfo)
.map(LicenseInfoUsage::getExcludedLicenseIds).orElse(Collections.emptySet()));
mergedExcludedLicenseIds
.addAll(Optional.of(u2).map(AttachmentUsage::getUsageData).map(UsageData::getLicenseInfo)
.map(LicenseInfoUsage::getExcludedLicenseIds).orElse(Collections.emptySet()));
mergedUsage.getUsageData().getLicenseInfo().setExcludedLicenseIds(mergedExcludedLicenseIds);
break;
case SOURCE_PACKAGE:
case MANUALLY_SET:
// do nothing
// source package and manual usages do not have any information to be merged
break;
default:
throw new IllegalArgumentException("Unexpected UsageData type: " + u1.getUsageData().getSetField());
case LICENSE_INFO:
mergedUsage.getUsageData().getLicenseInfo().setExcludedLicenseIds(
Sets.union(Optional.of(u1)
.map(AttachmentUsage::getUsageData)
.map(UsageData::getLicenseInfo)
.map(LicenseInfoUsage::getExcludedLicenseIds)
.orElse(Collections.emptySet()),
Optional.of(u2)
.map(AttachmentUsage::getUsageData)
.map(UsageData::getLicenseInfo)
.map(LicenseInfoUsage::getExcludedLicenseIds)
.orElse(Collections.emptySet())));
break;
case SOURCE_PACKAGE:
case MANUALLY_SET:
// do nothing
// source package and manual usages do not have any information to be mergedSw360ProjectService.java
break;
default:
throw new IllegalArgumentException("Unexpected UsageData type: " + u1.getUsageData().getSetField());
}

return mergedUsage;
Expand Down Expand Up @@ -699,7 +705,7 @@ public Map<String, ObligationStatusInfo> setLicenseInfoWithObligations(
.createLicenseToObligationMapping(licenseResults.get(0), obligationResults.get(0)));
}
} catch (TException exception) {
log.error(String.format("Error fetchinig license Information for attachment: %s in release: %s",
log.error(String.format("Error fetchinig Sw360ProjectService.javalicense Information for attachment: %s in release: %s",
filteredAttachment.getFilename(), releaseId), exception);
}
}
Expand Down Expand Up @@ -1434,8 +1440,73 @@ private Set<String> filteredProjectIds(List<ProjectLink> filteredProjectLinks) {
return filteredProjectLinks.stream().map(ProjectLink::getId).collect(Collectors.toSet());
}

public List<Map<String, String>> serveDependencyNetworkListView(String projectId, User sw360User)
throws TException {
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();
return sw360ProjectClient.getAccessibleDependencyNetworkForListView(projectId, sw360User);
Expand All @@ -1451,7 +1522,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 b91d3ad

Please sign in to comment.