Skip to content

Commit

Permalink
Merge pull request #2084 from KoukiHama/license-changelogs
Browse files Browse the repository at this point in the history
feat(ui) : Add changelogs for license pages

Reviewed by: [email protected]
Tested by: [email protected]
  • Loading branch information
ag4ums authored Aug 23, 2023
2 parents afe96c1 + ba6c743 commit 4db4c6e
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.eclipse.sw360.datahandler.couchdb.DatabaseMixInForChangeLog.RepositoryMixin;
import org.eclipse.sw360.datahandler.couchdb.DatabaseMixInForChangeLog.VendorMixin;
import org.eclipse.sw360.datahandler.couchdb.DatabaseMixInForChangeLog.ObligationMixin;
import org.eclipse.sw360.datahandler.couchdb.DatabaseMixInForChangeLog.LicenseTypeMixin;
import org.eclipse.sw360.datahandler.couchdb.DatabaseMixInForChangeLog.*;
import org.eclipse.sw360.datahandler.thrift.ProjectReleaseRelationship;
import org.eclipse.sw360.datahandler.thrift.RequestStatus;
Expand Down Expand Up @@ -118,6 +119,9 @@
import org.eclipse.sw360.datahandler.thrift.spdx.spdxpackageinfo.PackageInformation;
import org.eclipse.sw360.datahandler.thrift.spdx.spdxpackageinfo.PackageVerificationCode;
import org.eclipse.sw360.datahandler.thrift.licenses.Obligation;
import org.eclipse.sw360.datahandler.thrift.licenses.License;
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseType;
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseObligationList;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -459,6 +463,16 @@ public static <T, R> void trimStringFields(T obj, List<R> listOfStrFields) {
changeLog.setDocumentId(newPkgVer.getId());
changeLog.setDocumentType(newPkgVer.getType());
changeLog.setDbName(DatabaseSettings.COUCH_DB_DATABASE);
} else if (newDocVersion instanceof License) {
License newProjVer = (License) newDocVersion;
changeLog.setDocumentId(newProjVer.getId());
changeLog.setDocumentType(newProjVer.getType());
changeLog.setDbName(DatabaseSettings.COUCH_DB_DATABASE);
} else if (newDocVersion instanceof LicenseObligationList) {
LicenseObligationList newProjVer = (LicenseObligationList) newDocVersion;
changeLog.setDocumentId(newProjVer.getId());
changeLog.setDocumentType(newProjVer.getType());
changeLog.setDbName(DatabaseSettings.COUCH_DB_DATABASE);
}

log.info("Initialize ChangeLogs for Document Id : " + changeLog.getDocumentId());
Expand Down Expand Up @@ -649,6 +663,10 @@ private static <T extends TBase> void changeLogsForNewlyCreatedOrDeleted(T newor
fields = Obligation._Fields.values();
} else if (neworDeletedVersion instanceof Package) {
fields = Package._Fields.values();
} else if (neworDeletedVersion instanceof License) {
fields = License._Fields.values();
} else if (neworDeletedVersion instanceof LicenseObligationList) {
fields = LicenseObligationList._Fields.values();
} else {
return;
}
Expand Down Expand Up @@ -683,6 +701,10 @@ private static <T extends TBase> void changeLogsForNewlyCreatedOrDeleted(T newor
fields = Obligation._Fields.values();
} else if (newVersion instanceof Package) {
fields = Package._Fields.values();
} else if (newVersion instanceof License) {
fields = License._Fields.values();
} else if (newVersion instanceof LicenseObligationList) {
fields = LicenseObligationList._Fields.values();
} else {
return;
}
Expand Down Expand Up @@ -908,6 +930,7 @@ private static ObjectMapper initAndGetObjectMapper() {
mapper.addMixInAnnotations(SnippetInformation.class, SnippetInformationMixin.class);
mapper.addMixInAnnotations(SnippetRange.class, SnippetRangeMixin.class);
mapper.addMixInAnnotations(Obligation.class, ObligationMixin.class);
mapper.addMixInAnnotations(LicenseType.class, LicenseTypeMixin.class);
}
return mapper;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.eclipse.sw360.datahandler.thrift.users.RequestedAction;
import org.eclipse.sw360.datahandler.thrift.users.User;
import org.eclipse.sw360.datahandler.thrift.users.UserGroup;
import org.eclipse.sw360.datahandler.thrift.changelogs.ChangeLogs;
import org.eclipse.sw360.datahandler.thrift.changelogs.Operation;
import org.eclipse.sw360.licenses.tools.SpdxConnector;
import org.eclipse.sw360.licenses.tools.OSADLObligationConnector;
import org.apache.http.HttpStatus;
Expand Down Expand Up @@ -55,7 +57,6 @@
import static org.eclipse.sw360.datahandler.thrift.ThriftValidate.*;

import org.eclipse.sw360.datahandler.db.DatabaseHandlerUtil;
import org.eclipse.sw360.datahandler.thrift.changelogs.Operation;
import com.google.common.collect.Lists;
import org.eclipse.sw360.datahandler.common.DatabaseSettings;
import org.spdx.library.InvalidSPDXAnalysisException;
Expand All @@ -80,6 +81,7 @@ public class LicenseDatabaseHandler {
private final ObligationElementRepository obligationElementRepository;
private final ObligationNodeRepository obligationNodeRepository;
private final LicenseTypeRepository licenseTypeRepository;
private final LicenseObligationListRepository obligationListRepository;
private final LicenseModerator moderator;
private final CustomPropertiesRepository customPropertiesRepository;
private final DatabaseRepositoryCloudantClient[] repositories;
Expand All @@ -104,14 +106,16 @@ public LicenseDatabaseHandler(Supplier<CloudantClient> httpClient, String dbName
obligationNodeRepository = new ObligationNodeRepository(db);
licenseTypeRepository = new LicenseTypeRepository(db);
customPropertiesRepository = new CustomPropertiesRepository(db);
obligationListRepository = new LicenseObligationListRepository(db);

repositories = new DatabaseRepositoryCloudantClient[]{
licenseRepository,
licenseTypeRepository,
obligRepository,
customPropertiesRepository,
obligationElementRepository,
obligationNodeRepository
obligationNodeRepository,
obligationListRepository
};

moderator = new LicenseModerator();
Expand Down Expand Up @@ -449,12 +453,17 @@ public RequestStatus updateLicense(License inputLicense, User user, User request

Optional<License> oldLicense = Optional.ofNullable(inputLicense.getId())
.map(id -> licenseRepository.get(inputLicense.getId()));
boolean isNewLicense = ! oldLicense.isPresent();
boolean isNewLicense = !oldLicense.isPresent();
License oldLicenseForChangelogs = new License();
Set<String> oldObligationDatabaseIds = new HashSet<>();

if(isNewLicense){
validateNewLicense(inputLicense);
} else {
validateExistingLicense(inputLicense);
oldObligationDatabaseIds = oldLicense.orElse(new License()).getObligationDatabaseIds();
oldLicenseForChangelogs = setLicenseForChangelogs(oldLicense.orElse(new License()));
oldLicenseForChangelogs.setShortname(inputLicense.getShortname());
}

boolean oldLicenseWasChecked = oldLicense.map(License::isChecked).orElse(false);
Expand All @@ -466,10 +475,56 @@ public RequestStatus updateLicense(License inputLicense, User user, User request
return RequestStatus.FAILURE;
}

License resultLicenseForChangelogs = setLicenseForChangelogs(resultLicense);
resultLicenseForChangelogs.setShortname(inputLicense.getShortname());

LicenseObligationList resultObligationList = new LicenseObligationList();
Map<String, Obligation> obligations = new HashMap<>();
getObligationsByIds(resultLicense.getObligationDatabaseIds()).forEach(oblig -> {
obligations.put(oblig.getTitle(), oblig);
});
resultObligationList.setLinkedObligations(obligations);
resultObligationList.setLicenseId(resultLicense.getId());

if(isNewLicense) {
if (!resultLicense.getObligationDatabaseIds().isEmpty()) {
obligationListRepository.add(resultObligationList);
resultLicense.setObligationListId(resultObligationList.getId());
}
licenseRepository.add(resultLicense);
dbHandlerUtil.addChangeLogs(resultLicenseForChangelogs, null, user.getEmail(), Operation.CREATE, null,
Lists.newArrayList(), null, null);
if(resultLicense.getObligationListId() != null){
dbHandlerUtil.addChangeLogs(resultObligationList, null, user.getEmail(), Operation.CREATE, null,
Lists.newArrayList(), resultLicense.getId(), Operation.LICENSE_CREATE);
}
} else {
licenseRepository.update(resultLicense);

dbHandlerUtil.addChangeLogs(resultLicenseForChangelogs, oldLicenseForChangelogs, user.getEmail(),
Operation.UPDATE, null,
Lists.newArrayList(), null, null);

LicenseObligationList oldObligationList = new LicenseObligationList();
if (!resultLicense.getObligationDatabaseIds().equals(oldObligationDatabaseIds)) {
resultObligationList.setId(resultLicense.getObligationListId());
LicenseObligationList baseObligationList = obligationListRepository
.get(resultLicense.getObligationListId());
resultObligationList.setId(baseObligationList.getId());
resultObligationList.setRevision(baseObligationList.getRevision());
obligationListRepository.update(resultObligationList);

Map<String, Obligation> oldObligations = new HashMap<>();
getObligationsByIds(oldObligationDatabaseIds).forEach(oblig -> {
oldObligations.put(oblig.getTitle(), oblig);
});
oldObligationList.setId(baseObligationList.getId());
oldObligationList.setLinkedObligations(oldObligations);
oldObligationList.setLicenseId(oldLicense.orElse(new License()).getId());
dbHandlerUtil.addChangeLogs(resultObligationList, oldObligationList, user.getEmail(),
Operation.UPDATE, null,
Lists.newArrayList(), resultLicense.getId(), Operation.LICENSE_UPDATE);
}
}
return RequestStatus.SUCCESS;
}
Expand Down Expand Up @@ -520,6 +575,19 @@ private License updateLicenseFromInputLicense(Optional<License> oldLicense, Lice
return license;
}

public License setLicenseForChangelogs(License license) {
License licenseForChangelogs = license.deepCopy();
if (licenseForChangelogs.isSetLicenseTypeDatabaseId()) {
LicenseType licenseTypeForChangelogs = getLicenseTypeById(licenseForChangelogs.getLicenseTypeDatabaseId());
licenseForChangelogs.setLicenseType(licenseTypeForChangelogs);
licenseForChangelogs.unsetLicenseTypeDatabaseId();
}
if (licenseForChangelogs.isSetObligationDatabaseIds()) {
licenseForChangelogs.unsetObligationDatabaseIds();
}
return licenseForChangelogs;
}

public RequestStatus updateLicenseFromAdditionsAndDeletions(License licenseAdditions,
License licenseDeletions,
User user,
Expand Down Expand Up @@ -699,6 +767,11 @@ public List<ObligationElement> getObligationElements() {
return obligationElements;
}

public List<LicenseObligationList> getLicenseObligationLists() {
final List<LicenseObligationList> licenseObligationLists = obligationListRepository.getAll();
return licenseObligationLists;
}

public List<LicenseType> getLicenseTypesByIds(Collection<String> ids) {
return licenseTypeRepository.get(ids);
}
Expand Down Expand Up @@ -815,6 +888,16 @@ public RequestStatus deleteLicense(String id, User user) throws SW360Exception {
if (makePermission(license, user).isActionAllowed(RequestedAction.DELETE)) {
licenseRepository.remove(license);
moderator.notifyModeratorOnDelete(license.getId());
dbHandlerUtil.addChangeLogs(null, license, user.getEmail(), Operation.DELETE, null,
Lists.newArrayList(), null, null);

if (license.getObligationListId() != null) {
LicenseObligationList obligationList = obligationListRepository.get(license.getObligationListId());
obligationListRepository.remove(obligationList);
dbHandlerUtil.addChangeLogs(null, obligationList, user.getEmail(), Operation.DELETE, null,
Lists.newArrayList(), license.getId(), Operation.LICENSE_DELETE);
}

return RequestStatus.SUCCESS;
} else {
log.error(user + " does not have the permission to delete the license.");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright TOSHIBA CORPORATION, 2023. Part of the SW360 Portal Project.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.sw360.licenses.db;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.eclipse.sw360.datahandler.cloudantclient.DatabaseConnectorCloudant;
import org.eclipse.sw360.datahandler.cloudantclient.DatabaseRepositoryCloudantClient;
import org.eclipse.sw360.datahandler.thrift.licenses.LicenseObligationList;

import org.ektorp.support.View;

import com.cloudant.client.api.model.DesignDocument.MapReduce;

/**
* CRUD access for the LicenseObligationList class
*
*
*/
public class LicenseObligationListRepository extends DatabaseRepositoryCloudantClient<LicenseObligationList> {

private static final String BY_LICENSE_ID =
"function(doc) {" +
" if (doc.type == 'licenseObligationList') {" +
" emit(doc.licenseId, null);" +
" }" +
"}";

private static final String ALL = "function(doc) { if (doc.type == 'licenseObligationList') emit(null, doc._id) }";

public LicenseObligationListRepository(DatabaseConnectorCloudant db) {
super(db, LicenseObligationList.class);
Map<String, MapReduce> views = new HashMap<String, MapReduce>();
views.put("byLicenseId", createMapReduce(BY_LICENSE_ID, null));
views.put("all", createMapReduce(ALL, null));
initStandardDesignDocument(views, db);
}

public Optional<LicenseObligationList> getObligationByLicenseid(String licenseId) {
return queryView("byLicenseId", licenseId).stream().findFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void testAddLicense() throws Exception {
License license = new License();
license.setShortname("GPL+3.0");
license.setFullname("The GPL Software License, Version 3.0");
license.addToObligationDatabaseIds("T1");

RequestStatus status = handler.updateLicense(license, user, user);
assertEquals(RequestStatus.SUCCESS, status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ResourceBundleUtil;
import com.liferay.portal.kernel.language.LanguageUtil;
import com.liferay.portal.kernel.json.JSONObject;

import org.eclipse.sw360.datahandler.common.CommonUtils;
import org.eclipse.sw360.datahandler.permissions.PermissionUtils;
Expand All @@ -31,6 +32,8 @@
import org.eclipse.sw360.portal.common.ErrorMessages;
import org.eclipse.sw360.portal.common.PortalConstants;
import org.eclipse.sw360.portal.common.UsedAsLiferayAction;
import org.eclipse.sw360.portal.common.ChangeLogsPortletUtils;
import org.eclipse.sw360.portal.common.PortletUtils;
import org.eclipse.sw360.portal.portlets.Sw360Portlet;
import org.eclipse.sw360.portal.users.UserCacheHolder;
import org.apache.commons.lang.StringUtils;
Expand Down Expand Up @@ -89,6 +92,12 @@ public void serveResource(ResourceRequest request, ResourceResponse response) th
} else if (PortalConstants.LOAD_LICENSE_OBLIGATIONS.equals(action)) {
request.setAttribute(LICENSE_OBLIGATION_DATA, loadLicenseObligation(request));
include("/html/licenses/includes/licObligations.jsp", request, response, PortletRequest.RESOURCE_PHASE);
} else if (PortalConstants.LOAD_CHANGE_LOGS.equals(action) || PortalConstants.VIEW_CHANGE_LOGS.equals(action)) {
ChangeLogsPortletUtils changeLogsPortletUtilsPortletUtils = PortletUtils
.getChangeLogsPortletUtils(thriftClients);
JSONObject dataForChangeLogs = changeLogsPortletUtilsPortletUtils.serveResourceForChangeLogs(request,
response, action);
writeJSON(request, response, dataForChangeLogs);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@
<portlet:param name="<%=PortalConstants.LICENSE_ID%>" value="${licenseDetail.id}" />
</portlet:renderURL>

<core_rt:set var = "docid" scope = "request" value = "${licenseDetail.id}"/>

<div class="container" style="display: none;">
<div class="row">
<div class="col-3 sidebar">
<div id="detailTab" class="list-group" data-initial-tab="${selectedTab}" role="tablist">
<a class="list-group-item list-group-item-action <core_rt:if test="${selectedTab == 'tab-Details'}">active</core_rt:if>" href="#tab-Details" data-toggle="list" role="tab"><liferay-ui:message key="details" /></a>
<a class="list-group-item list-group-item-action <core_rt:if test="${selectedTab == 'tab-LicenseText'}">active</core_rt:if>" href="#tab-LicenseText" data-toggle="list" role="tab"><liferay-ui:message key="text" /></a>
<a class="list-group-item list-group-item-action <core_rt:if test="${selectedTab == 'tab-TodosAndObligations'}">active</core_rt:if>" href="#tab-TodosAndObligations" data-toggle="list" role="tab"><liferay-ui:message key="obligations" /></a>
<a class="list-group-item list-group-item-action <core_rt:if test="${selectedTab == 'tab-ChangeLogs'}">active</core_rt:if>" href="#tab-ChangeLogs" data-toggle="list" role="tab"><liferay-ui:message key="change.log" /></a>
</div>
</div>
<div class="col">
Expand Down Expand Up @@ -59,6 +62,14 @@
<span class="badge badge-success"><liferay-ui:message key="checked" /></span>
</core_rt:if>
</div>
<div class="list-group-companion" data-belong-to="tab-ChangeLogs">
<div class="nav nav-pills justify-content-center bg-light font-weight-bold" id="pills-tab" role="tablist">
<a class="nav-item nav-link active" id="pills-changelogs-list-tab" data-toggle="pill" href="#pills-changelogslist" role="tab" aria-controls="pills-changeloglist" aria-selected="true">
<liferay-ui:message key="change.log" /></a>
<a class="nav-item nav-link" id="pills-changelogs-view-tab" href="#pills-changelogsView" role="tab" aria-controls="pills-changelogsView" aria-selected="false">
<liferay-ui:message key="changes" /></a>
</div>
</div>
</div>
<div class="row">
<div class="col">
Expand All @@ -77,6 +88,9 @@
<%@include file="/html/licenses/includes/detailAddTodo.jspf" %>
</div>
</core_rt:if>
<div id="tab-ChangeLogs" class="tab-pane <core_rt:if test="${selectedTab == 'tab-ChangeLogs'}">active show</core_rt:if>">
<jsp:include page="/html/changelogs/elementView.jsp" />
</div>
</div>
</div>
</div>
Expand Down
Loading

0 comments on commit 4db4c6e

Please sign in to comment.