From d923432dd7d0f8a581ff04e6ecfb045f46cdfaf0 Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Wed, 5 Jun 2024 16:45:58 +0200 Subject: [PATCH 1/6] DURACOM-265 Property to enable or disable the cclicense required validation --- dspace/config/dspace.cfg | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dspace/config/dspace.cfg b/dspace/config/dspace.cfg index 43f12d557f6..37e10c09483 100644 --- a/dspace/config/dspace.cfg +++ b/dspace/config/dspace.cfg @@ -996,6 +996,11 @@ metadata.hide.person.email = true # dspace-angular environment configuration property submission.typeBind.field #submit.type-bind.field = dc.type +# Wheter or not we REQUIRE that the cclicense is provided +# during the cclicense step in the submission process +# Defaults to false; If you set to 'true', submitter needs to provide cclicense +#cc.license.required = true + #### Creative Commons settings ###### # The url to the web service API From 90f4cabdedc4ba35e385f11d95c860cf021c34c7 Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Wed, 5 Jun 2024 16:46:50 +0200 Subject: [PATCH 2/6] DURACOM-265 provided bean for cclicense validation in dspace-addon-validation --- .../spring/spring-dspace-addon-validation-services.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dspace-server-webapp/src/main/resources/spring/spring-dspace-addon-validation-services.xml b/dspace-server-webapp/src/main/resources/spring/spring-dspace-addon-validation-services.xml index 013b406c1af..50204bf5c4f 100644 --- a/dspace-server-webapp/src/main/resources/spring/spring-dspace-addon-validation-services.xml +++ b/dspace-server-webapp/src/main/resources/spring/spring-dspace-addon-validation-services.xml @@ -27,6 +27,11 @@ + + + + + From 355cd4c614776720ff6d54dc686db047b3cc33b6 Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Wed, 5 Jun 2024 16:47:58 +0200 Subject: [PATCH 3/6] DURACOM-265 provided CclicenseValidator class --- .../step/validation/CclicenseValidator.java | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/validation/CclicenseValidator.java diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/validation/CclicenseValidator.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/validation/CclicenseValidator.java new file mode 100644 index 00000000000..2fea5ed2d30 --- /dev/null +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/validation/CclicenseValidator.java @@ -0,0 +1,133 @@ +/** + * The contents of this file are subject to the license and copyright + * detailed in the LICENSE and NOTICE files at the root of the source + * tree and available online at + * + * http://www.dspace.org/license/ + */ +package org.dspace.app.rest.submit.step.validation; + +import org.dspace.app.rest.model.ErrorRest; +import org.dspace.app.rest.submit.SubmissionService; +import org.dspace.app.util.DCInputsReaderException; +import org.dspace.app.util.SubmissionStepConfig; +import org.dspace.content.InProgressSubmission; +import org.dspace.content.Item; +import org.dspace.content.service.ItemService; +import org.dspace.license.CreativeCommonsServiceImpl; +import org.dspace.services.ConfigurationService; +import org.springframework.beans.factory.annotation.Autowired; + +import javax.inject.Inject; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import static org.dspace.app.rest.repository.WorkspaceItemRestRepository.OPERATION_PATH_SECTIONS; + + +/** + * This class validates that the Creative Commons License has been granted for the + * in-progress submission. + * + * @author Mattia Vianelli (Mattia.Vianelli@4science.com) + */ +public class CclicenseValidator extends AbstractValidation { + + /** + * Construct a Creative Commons License configuration. + * @param configurationService DSpace configuration provided by the DI container. + */ + @Inject + public CclicenseValidator(ConfigurationService configurationService) { + this.configurationService = configurationService; + } + + private final ConfigurationService configurationService; + + @Autowired + private ItemService itemService; + + @Autowired + private CreativeCommonsServiceImpl creativeCommonsService; + + public static final String ERROR_VALIDATION_CCLICENSEREQUIRED = "error.validation.cclicense.required"; + + private String name; + + private Boolean required; + + /** + * Validate the license of the item. + * @param item The item whose cclicense is to be validated. + * @param config The configuration for the submission step for cclicense. + * @return A list of validation errors. + */ + private List validateLicense(Item item, SubmissionStepConfig config) { + List errors = new ArrayList<>(); + + String licenseURI = creativeCommonsService.getLicenseURI(item); + if (licenseURI == null || licenseURI.isBlank()) { + addError(errors, ERROR_VALIDATION_CCLICENSEREQUIRED, "/" + OPERATION_PATH_SECTIONS + "/" + config.getId()); + } + + return errors; + } + + public ItemService getItemService() { + return itemService; + } + + public void setItemService(ItemService itemService) { + this.itemService = itemService; + } + + /** + * Check if at least one Creative Commons License is required when submitting a new Item. + * @return true if a Creative Commons License is required setting true for the property cc.license.required. + */ + public Boolean isRequired() { + if (required == null) { + return configurationService.getBooleanProperty("cc.license.required", false); + } + return required; + } + + /** + * Set whether at least one Creative Commons License is required when submitting a new Item. + * @param required true if a Creative Commons License is required. + */ + public void setRequired(Boolean required) { + this.required = required; + } + + @Override + public String getName() { + return name; + } + + /** + * Perform validation on the item and config(ccLicense). + * @param obj The submission to be validated. + * @param config The configuration for the submission step for cclicense. + * @return A list of validation errors. + * @throws SQLException If there is a problem accessing the database. + */ + + @Override + public List validate(SubmissionService submissionService, InProgressSubmission obj, SubmissionStepConfig config) throws DCInputsReaderException, SQLException { + List errors = new ArrayList<>(); + + + if (this.isRequired() && obj != null && obj.getItem() != null) { + errors.addAll(validateLicense(obj.getItem(), config)); + } + + return errors; + } + + public void setName(String name) { + this.name = name; + } + +} From bffa457e1617443cd8f33ef46a0bea777c764edf Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Wed, 5 Jun 2024 16:48:31 +0200 Subject: [PATCH 4/6] DURACOM-265 provided test case for cclicense validation --- .../rest/WorkspaceItemRestRepositoryIT.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/dspace-server-webapp/src/test/java/org/dspace/app/rest/WorkspaceItemRestRepositoryIT.java b/dspace-server-webapp/src/test/java/org/dspace/app/rest/WorkspaceItemRestRepositoryIT.java index 542688ea239..ecc2311eb38 100644 --- a/dspace-server-webapp/src/test/java/org/dspace/app/rest/WorkspaceItemRestRepositoryIT.java +++ b/dspace-server-webapp/src/test/java/org/dspace/app/rest/WorkspaceItemRestRepositoryIT.java @@ -9785,4 +9785,47 @@ public void patchReplaceProvidingWrongPrimaryTest() throws Exception { .andExpect(status().isOk()) .andExpect(jsonPath("$.sections.upload.primary", is(idFirstPdf.get()))); } + + @Test + public void createWorkspaceWithoutCclicense_CclicenseRequired() throws Exception { + context.turnOffAuthorisationSystem(); + + //1. A community-collection structure with one parent community with sub-community and one collection. + parentCommunity = CommunityBuilder.createCommunity(context) + .withName("Parent Community") + .build(); + Community child1 = CommunityBuilder.createSubCommunity(context, parentCommunity) + .withName("Sub Community") + .build(); + Collection col1 = CollectionBuilder.createCollection(context, child1) + .withName("Collection 1") + .withSubmitterGroup(eperson) + .build(); + + String authToken = getAuthToken(eperson.getEmail(), password); + + //disable file upload mandatory + configurationService.setProperty("webui.submit.upload.required", false); + configurationService.setProperty("cc.license.required", true); + + context.restoreAuthSystemState(); + + AtomicReference idRef = new AtomicReference<>(); + try { + // create an empty workspaceitem explicitly in the col1, check validation on creation + getClient(authToken).perform(post("/api/submission/workspaceitems") + .param("owningCollection", col1.getID().toString()) + .contentType(org.springframework.http.MediaType.APPLICATION_JSON)) + .andExpect(status().isCreated()) + // cclicense is required + .andExpect(jsonPath("$.errors[?(@.message=='error.validation.cclicense.required')]", + contains( + hasJsonPath("$.paths", contains( + hasJsonPath("$", Matchers.is("/sections/cclicense")) + ))))) + .andDo(result -> idRef.set(read(result.getResponse().getContentAsString(), "$.id"))); + } finally { + WorkspaceItemBuilder.deleteWorkspaceItem(idRef.get()); + } + } } From b0c0bf8b419b39127fe6a8f4db5bfa39a4418a81 Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Wed, 5 Jun 2024 17:13:04 +0200 Subject: [PATCH 5/6] DURACOM-265 checkstyle fix --- .../step/validation/CclicenseValidator.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/validation/CclicenseValidator.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/validation/CclicenseValidator.java index 2fea5ed2d30..def59ea105c 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/validation/CclicenseValidator.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/validation/CclicenseValidator.java @@ -7,6 +7,13 @@ */ package org.dspace.app.rest.submit.step.validation; +import static org.dspace.app.rest.repository.WorkspaceItemRestRepository.OPERATION_PATH_SECTIONS; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; +import javax.inject.Inject; + import org.dspace.app.rest.model.ErrorRest; import org.dspace.app.rest.submit.SubmissionService; import org.dspace.app.util.DCInputsReaderException; @@ -18,14 +25,6 @@ import org.dspace.services.ConfigurationService; import org.springframework.beans.factory.annotation.Autowired; -import javax.inject.Inject; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.List; - -import static org.dspace.app.rest.repository.WorkspaceItemRestRepository.OPERATION_PATH_SECTIONS; - - /** * This class validates that the Creative Commons License has been granted for the * in-progress submission. @@ -115,7 +114,10 @@ public String getName() { */ @Override - public List validate(SubmissionService submissionService, InProgressSubmission obj, SubmissionStepConfig config) throws DCInputsReaderException, SQLException { + public List validate(SubmissionService submissionService, + InProgressSubmission obj, + SubmissionStepConfig config) + throws DCInputsReaderException, SQLException { List errors = new ArrayList<>(); From edfb8920ccd315f8166193cdfb42bb34fd71cae8 Mon Sep 17 00:00:00 2001 From: Mattia Vianelli Date: Mon, 24 Jun 2024 18:40:18 +0200 Subject: [PATCH 6/6] DURACOM-265 Code improvement after review --- .../step/validation/CclicenseValidator.java | 25 ++++--------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/validation/CclicenseValidator.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/validation/CclicenseValidator.java index def59ea105c..d0cfd14ec8c 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/validation/CclicenseValidator.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/validation/CclicenseValidator.java @@ -54,8 +54,6 @@ public CclicenseValidator(ConfigurationService configurationService) { private String name; - private Boolean required; - /** * Validate the license of the item. * @param item The item whose cclicense is to be validated. @@ -63,7 +61,7 @@ public CclicenseValidator(ConfigurationService configurationService) { * @return A list of validation errors. */ private List validateLicense(Item item, SubmissionStepConfig config) { - List errors = new ArrayList<>(); + List errors = new ArrayList<>(1); String licenseURI = creativeCommonsService.getLicenseURI(item); if (licenseURI == null || licenseURI.isBlank()) { @@ -86,18 +84,7 @@ public void setItemService(ItemService itemService) { * @return true if a Creative Commons License is required setting true for the property cc.license.required. */ public Boolean isRequired() { - if (required == null) { - return configurationService.getBooleanProperty("cc.license.required", false); - } - return required; - } - - /** - * Set whether at least one Creative Commons License is required when submitting a new Item. - * @param required true if a Creative Commons License is required. - */ - public void setRequired(Boolean required) { - this.required = required; + return configurationService.getBooleanProperty("cc.license.required", false); } @Override @@ -118,14 +105,12 @@ public List validate(SubmissionService submissionService, InProgressSubmission obj, SubmissionStepConfig config) throws DCInputsReaderException, SQLException { - List errors = new ArrayList<>(); - if (this.isRequired() && obj != null && obj.getItem() != null) { - errors.addAll(validateLicense(obj.getItem(), config)); + return validateLicense(obj.getItem(), config); + } else { + return List.of(); } - - return errors; } public void setName(String name) {