Skip to content

Commit

Permalink
Merge pull request DSpace#9639 from 4Science/task/main/DURACOM-265_CC…
Browse files Browse the repository at this point in the history
…-licence-step-does-not-work

[ DSpace#2161] Provide a way to require Creative Commons license in submission form
  • Loading branch information
tdonohue authored Sep 3, 2024
2 parents e5ddcdf + edfb892 commit 658b452
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* 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 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;
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;

/**
* This class validates that the Creative Commons License has been granted for the
* in-progress submission.
*
* @author Mattia Vianelli ([email protected])
*/
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;

/**
* 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<ErrorRest> validateLicense(Item item, SubmissionStepConfig config) {
List<ErrorRest> errors = new ArrayList<>(1);

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() {
return configurationService.getBooleanProperty("cc.license.required", false);
}

@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<? extends ErrorRest> validate(SubmissionService submissionService,
InProgressSubmission obj,
SubmissionStepConfig config)
throws DCInputsReaderException, SQLException {

if (this.isRequired() && obj != null && obj.getItem() != null) {
return validateLicense(obj.getItem(), config);
} else {
return List.of();
}
}

public void setName(String name) {
this.name = name;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
<property name="uploadConfigurationService" ref="uploadConfigurationService"/>
</bean>

<bean name="cclicenseValidation" class="org.dspace.app.rest.submit.step.validation.CclicenseValidator">
<property name="itemService" ref="org.dspace.content.ItemServiceImpl"/>
<property name="name" value="cclicense"/>
</bean>

<bean name="licenseValidation" class="org.dspace.app.rest.submit.step.validation.LicenseValidation">
<property name="name" value="license"/>
</bean>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> 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());
}
}
}
5 changes: 5 additions & 0 deletions dspace/config/dspace.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 658b452

Please sign in to comment.