Skip to content

Commit

Permalink
feat(spdx): Add API for feature SPDX Document tab
Browse files Browse the repository at this point in the history
Signed-off-by: hoangnt2 <[email protected]>
  • Loading branch information
tuannn2 authored and hoangnt2 committed Oct 31, 2024
1 parent 1f31935 commit 59ff8aa
Show file tree
Hide file tree
Showing 11 changed files with 1,934 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,13 @@ public AddDocumentRequestSummary addRelease(Release release, User user) throws S
}
// Add release to database
releaseRepository.add(release);
if (SW360Constants.SPDX_DOCUMENT_ENABLED) {
try {
spdxDocumentDatabaseHandler.updateSPDX(user, release, true);
} catch (TException ex) {
log.error("Error updateSPDX "+ ex.getMessage());
}
}
final String id = release.getId();

// Update the underlying component
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@

import com.ibm.cloud.cloudant.v1.Cloudant;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TType;
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant;
import org.eclipse.sw360.datahandler.common.DatabaseSettings;
import org.eclipse.sw360.datahandler.thrift.*;
import org.eclipse.sw360.datahandler.thrift.spdx.annotations.Annotations;
import org.eclipse.sw360.datahandler.thrift.spdx.documentcreationinformation.DocumentCreationInformation;
import org.eclipse.sw360.datahandler.thrift.spdx.documentcreationinformation.DocumentCreationInformationService;
import org.eclipse.sw360.datahandler.thrift.spdx.otherlicensinginformationdetected.OtherLicensingInformationDetected;
import org.eclipse.sw360.datahandler.thrift.spdx.relationshipsbetweenspdxelements.RelationshipsBetweenSPDXElements;
import org.eclipse.sw360.datahandler.thrift.spdx.snippetinformation.SnippetInformation;
import org.eclipse.sw360.datahandler.thrift.spdx.spdxpackageinfo.PackageInformation;
import org.eclipse.sw360.datahandler.thrift.spdx.spdxpackageinfo.PackageInformationService;
import org.eclipse.sw360.datahandler.thrift.spdx.spdxpackageinfo.PackageVerificationCode;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.users.RequestedAction;
import org.eclipse.sw360.datahandler.thrift.spdx.spdxdocument.*;
Expand All @@ -33,8 +44,11 @@

import java.net.MalformedURLException;
import java.util.*;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.eclipse.sw360.datahandler.common.SW360Assert.assertNotNull;
import static org.eclipse.sw360.datahandler.common.CommonUtils.*;
import static org.eclipse.sw360.datahandler.permissions.PermissionUtils.makePermission;
Expand Down Expand Up @@ -143,6 +157,19 @@ public RequestStatus updateSPDXDocument(SPDXDocument spdx, User user) throws SW3
prepareSPDXDocument(spdx);
SPDXDocument actual = SPDXDocumentRepository.get(spdx.getId());
assertNotNull(actual, "Could not find SPDX Document to update!");
if(spdx.getSnippets().size() < actual.getSnippets().size()) {
updateSnippets(spdx);
}
if(spdx.getAnnotations().size() < actual.getAnnotations().size()) {
updateAnnotations(spdx);
}
if(spdx.getRelationships().size() < actual.getRelationships().size()) {
updateRelationships(spdx);
}
if(spdx.getOtherLicensingInformationDetecteds().size() < actual.getOtherLicensingInformationDetecteds().size()) {
updateOtherLicensingInformationDetecteds(spdx);
}

if (!makePermission(spdx, user).isActionAllowed(RequestedAction.WRITE)) {
if (isChanged(actual, spdx)) {
return moderator.updateSPDXDocument(spdx, user);
Expand All @@ -155,6 +182,62 @@ public RequestStatus updateSPDXDocument(SPDXDocument spdx, User user) throws SW3
return RequestStatus.SUCCESS;
}

public void updateOtherLicensingInformationDetecteds(SPDXDocument request) {
List<OtherLicensingInformationDetected> otherLicensingInformationDetecteds = request.getOtherLicensingInformationDetecteds().stream().collect(Collectors.toList());
Collections.sort(otherLicensingInformationDetecteds, new Comparator<OtherLicensingInformationDetected>() {
@Override
public int compare(OtherLicensingInformationDetected o1, OtherLicensingInformationDetected o2) {
return o1.getIndex() - o2.getIndex();
}
});
for (int i = 0; i < otherLicensingInformationDetecteds.size() ; i++) {
otherLicensingInformationDetecteds.get(i).setIndex(i);
}
request.setOtherLicensingInformationDetecteds(otherLicensingInformationDetecteds.stream().collect(Collectors.toSet()));
}

public void updateRelationships(SPDXDocument request) {
List<RelationshipsBetweenSPDXElements> relationshipsBetweenSPDXElements = request.getRelationships().stream().collect(Collectors.toList());
Collections.sort(relationshipsBetweenSPDXElements, new Comparator<RelationshipsBetweenSPDXElements>() {
@Override
public int compare(RelationshipsBetweenSPDXElements o1, RelationshipsBetweenSPDXElements o2) {
return o1.getIndex() - o2.getIndex();
}
});
for (int i = 0; i<relationshipsBetweenSPDXElements.size() ; i++) {
relationshipsBetweenSPDXElements.get(i).setIndex(i);
}
request.setRelationships(relationshipsBetweenSPDXElements.stream().collect(Collectors.toSet()));
}

public void updateAnnotations(SPDXDocument request) {
List<Annotations> annotations = request.getAnnotations().stream().collect(Collectors.toList());
Collections.sort(annotations, new Comparator<Annotations>() {
@Override
public int compare(Annotations o1, Annotations o2) {
return o1.getIndex() - o2.getIndex();
}
});
for (int i = 0; i<annotations.size() ; i++) {
annotations.get(i).setIndex(i);
}
request.setAnnotations(annotations.stream().collect(Collectors.toSet()));
}

public void updateSnippets(SPDXDocument request) {
List<SnippetInformation> snippetInformations = request.getSnippets().stream().collect(Collectors.toList());
Collections.sort(snippetInformations, new Comparator<SnippetInformation>() {
@Override
public int compare(SnippetInformation o1, SnippetInformation o2) {
return o1.getIndex() - o2.getIndex();
}
});
for (int i = 0; i<snippetInformations.size() ; i++) {
snippetInformations.get(i).setIndex(i);
}
request.setSnippets(snippetInformations.stream().collect(Collectors.toSet()));
}

public RequestStatus updateSPDXDocumentFromModerationRequest(SPDXDocument spdxAdditions, SPDXDocument spdxDeletions, User user) throws SW360Exception {
try {
SPDXDocument spdx = getSPDXDocumentById(spdxAdditions.getId(), user);
Expand Down Expand Up @@ -207,8 +290,8 @@ public RequestStatus deleteSPDXDocument(String id, User user) throws SW360Except

private boolean isChanged(SPDXDocument actual, SPDXDocument update) {
for (SPDXDocument._Fields field : SPDXDocument._Fields.values()) {
if (update.getFieldValue(field) == null && actual.getFieldValue(field) == null) {
continue;
if (null == actual.getFieldValue(field) && null == update.getFieldValue(field)) {
return false;
} else if (update.getFieldValue(field) != null && actual.getFieldValue(field) == null){
return true;
} else if (update.getFieldValue(field) == null && actual.getFieldValue(field) != null){
Expand All @@ -220,4 +303,132 @@ private boolean isChanged(SPDXDocument actual, SPDXDocument update) {
return false;
}

public static void updateSPDX(User user, Release release, boolean addNew) throws TException {
String spdxDocumentId = "";
String releaseId = release.getId();
Set<String> moderators;
if (CommonUtils.isNullOrEmptyCollection(release.getModerators())) {
moderators = new HashSet<>();
} else {
moderators = release.getModerators();
}

// Add SPDXDocument
SPDXDocument spdx = generateSpdxDocument();
spdx.setModerators(moderators);
SPDXDocumentService.Iface spdxClient = new ThriftClients().makeSPDXClient();
if (spdx != null) {
if (isNullOrEmpty(spdx.getReleaseId()) && !isNullOrEmpty(releaseId)) {
spdx.setReleaseId(releaseId);
}
if (isNullOrEmpty(spdx.getId())) {
spdx.unsetId();
spdx.unsetRevision();
spdxDocumentId = spdxClient.addSPDXDocument(spdx, user).getId();
} else {
spdxClient.updateSPDXDocument(spdx, user);
spdxDocumentId = spdx.getId();
}
}

// Add DocumentCreationInformation
DocumentCreationInformation document = generateDocumentCreationInformation();
document.setModerators(moderators);
if (document != null) {
DocumentCreationInformationService.Iface documentClient = new ThriftClients().makeSPDXDocumentInfoClient();
if (isNullOrEmpty(document.getSpdxDocumentId())) {
document.setSpdxDocumentId(spdxDocumentId);
}
if (isNullOrEmpty(document.getId())) {
document.unsetId();
document.unsetRevision();
documentClient.addDocumentCreationInformation(document, user);
} else {
documentClient.updateDocumentCreationInformation(document, user);
}
}

// Add PackageInformation
PackageInformation packageInfo = generatePackageInformation();
packageInfo.setModerators(moderators);
if (packageInfo != null) {
PackageInformationService.Iface packageClient = new ThriftClients().makeSPDXPackageInfoClient();
if (isNullOrEmpty(packageInfo.getSpdxDocumentId())) {
packageInfo.setSpdxDocumentId(spdxDocumentId);
}
if (isNullOrEmpty(packageInfo.getId())) {
packageInfo.unsetId();
packageInfo.unsetRevision();
packageClient.addPackageInformation(packageInfo, user);
} else {
packageClient.updatePackageInformation(packageInfo, user);
}
}
}

public static SPDXDocument generateSpdxDocument() {
SPDXDocument spdxDocument = new SPDXDocument();
for (SPDXDocument._Fields field : SPDXDocument._Fields.values()) {
switch (SPDXDocument.metaDataMap.get(field).valueMetaData.type) {
case TType.SET:
spdxDocument.setFieldValue(field, new HashSet<>());
break;
case TType.STRING:
spdxDocument.setFieldValue(field, "");
break;
default:
break;
}
}
return spdxDocument;
}

public static DocumentCreationInformation generateDocumentCreationInformation() {
DocumentCreationInformation documentCreationInfo = new DocumentCreationInformation();
for (DocumentCreationInformation._Fields field : DocumentCreationInformation._Fields.values()) {
switch (DocumentCreationInformation.metaDataMap.get(field).valueMetaData.type) {
case TType.SET:
documentCreationInfo.setFieldValue(field, new HashSet<>());
break;
case TType.STRING:
documentCreationInfo.setFieldValue(field, "");
break;
default:
break;
}
}
return documentCreationInfo;
}

public static PackageInformation generatePackageInformation() {
PackageInformation packageInfo = new PackageInformation();

for (PackageInformation._Fields field : PackageInformation._Fields.values()) {

switch (field) {
case PACKAGE_VERIFICATION_CODE: {
PackageVerificationCode packageVerificationCode = new PackageVerificationCode();
packageInfo.setPackageVerificationCode(packageVerificationCode);
break;
}
default: {
switch (PackageInformation.metaDataMap.get(field).valueMetaData.type) {
case TType.SET:
packageInfo.setFieldValue(field, new HashSet<>());
break;
case TType.STRING:
packageInfo.setFieldValue(field, "");
break;
case TType.BOOL:
packageInfo.setFieldValue(field, true);
default:
break;
}
break;
}
}
}
return packageInfo;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.net.MalformedURLException;
import java.util.*;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;

import org.eclipse.sw360.datahandler.common.DatabaseSettings;
Expand Down Expand Up @@ -128,8 +129,15 @@ public AddDocumentRequestSummary addDocumentCreationInformation(DocumentCreation

public RequestStatus updateDocumentCreationInformation(DocumentCreationInformation documentCreationInfo, User user) throws SW360Exception {
DocumentCreationInformation actual = SPDXDocumentCreationInfoRepository.get(documentCreationInfo.getId());
documentCreationInfo.setRevision(actual.getRevision());
if(documentCreationInfo.getExternalDocumentRefs().size() < actual.getExternalDocumentRefs().size()){
updateIndex(documentCreationInfo);
}
assertNotNull(actual, "Could not find SPDX Document Creation Information to update!");
prepareSpdxDocumentCreationInfo(documentCreationInfo);
if(documentCreationInfo.getExternalDocumentRefs().size() < actual.getExternalDocumentRefs().size()) {
updateExternalDocumentReferences(documentCreationInfo);
}
if (!makePermission(documentCreationInfo, user).isActionAllowed(RequestedAction.WRITE)) {
if (isChanged(actual, documentCreationInfo)) {
return moderator.updateSpdxDocumentCreationInfo(documentCreationInfo, user);
Expand All @@ -142,6 +150,35 @@ public RequestStatus updateDocumentCreationInformation(DocumentCreationInformati
return RequestStatus.SUCCESS;
}

public void updateIndex(DocumentCreationInformation documentCreationInformation) {
List<ExternalDocumentReferences> externalDocumentReferences = documentCreationInformation.getExternalDocumentRefs().stream().collect(Collectors.toList());
Collections.sort(externalDocumentReferences, new Comparator<ExternalDocumentReferences>() {
@Override
public int compare(ExternalDocumentReferences o1, ExternalDocumentReferences o2) {
return o1.getIndex() - o2.getIndex();
}
});
for (int i = 0; i < externalDocumentReferences.size(); i++) {
externalDocumentReferences.get(i).setIndex(i);
}
documentCreationInformation.setExternalDocumentRefs(externalDocumentReferences.stream().collect(Collectors.toSet()));
}

// Handle index of ExternalReferences
public void updateExternalDocumentReferences(DocumentCreationInformation request) {
List<ExternalDocumentReferences> externalDocumentReferences = request.getExternalDocumentRefs().stream().collect(Collectors.toList());
Collections.sort(externalDocumentReferences, new Comparator<ExternalDocumentReferences>() {
@Override
public int compare(ExternalDocumentReferences o1, ExternalDocumentReferences o2) {
return o1.getIndex() > o2.getIndex() ? 1 : (o1.getIndex() == o2.getIndex() ? 0 : -1);
}
});
for (int i = 0; i<externalDocumentReferences.size() ; i++) {
externalDocumentReferences.get(i).setIndex(i);
}
request.setExternalDocumentRefs(externalDocumentReferences.stream().collect(Collectors.toSet()));
}

public RequestStatus updateDocumentCreationInfomationFromModerationRequest(DocumentCreationInformation documentCreationInfoAdditions, DocumentCreationInformation documentCreationInfoDeletions, User user) throws SW360Exception {
try {
DocumentCreationInformation documentCreationInfo = getDocumentCreationInformationById(documentCreationInfoAdditions.getId(), user);
Expand Down Expand Up @@ -173,17 +210,17 @@ public RequestStatus deleteDocumentCreationInformation(String id, User user) thr
}

private boolean isChanged(DocumentCreationInformation actual, DocumentCreationInformation update) {

for (DocumentCreationInformation._Fields field : DocumentCreationInformation._Fields.values()) {
if(update.getFieldValue(field) == null) {
continue;
} else if (actual.getFieldValue(field) == null) {
if (null == actual.getFieldValue(field) && null == update.getFieldValue(field)) {
return false;
} else if (update.getFieldValue(field) != null && actual.getFieldValue(field) == null){
return true;
} else if (update.getFieldValue(field) == null && actual.getFieldValue(field) != null){
return true;
} else if (!actual.getFieldValue(field).equals(update.getFieldValue(field))) {
return true;
}
}

return false;
}

Expand Down
Loading

0 comments on commit 59ff8aa

Please sign in to comment.