diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseAddPatchOperation.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseAddPatchOperation.java new file mode 100644 index 0000000000..fc1d12be47 --- /dev/null +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseAddPatchOperation.java @@ -0,0 +1,70 @@ +/** + * 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.factory.impl; + +import org.dspace.content.Item; +import org.dspace.content.LicenseUtils; +import org.dspace.content.WorkspaceItem; +import org.dspace.content.service.ItemService; +import org.dspace.core.Context; +import org.dspace.eperson.EPerson; +import org.dspace.services.model.Request; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * Submission "add" PATCH operation + * + * To accept a license the timestamp of the acceptance the client must send a + * JSON Patch ADD operation as follow. + * + * Example: + * curl -X PATCH http://${dspace.url}/api/submission/workspaceitems/<:id-workspaceitem> -H " + * Content-Type: application/json" -d '[{ "op": "add", "path": " + * /sections/license/acceptanceDate", "value": "2017-11-20T10:32:42Z"}]' + * + * + * Please note that according to the JSON Patch specification RFC6902 a + * subsequent add operation on the acceptanceDate will have the effect to + * replace the previous granted license with a new one. + * + * + * @author Luigi Andrea Pascarelli (luigiandrea.pascarelli at 4science.it) + * + */ +public class LicenseAddPatchOperation extends AddPatchOperation { + + @Autowired + ItemService itemService; + + @Override + protected Class getArrayClassForEvaluation() { + return String[].class; + } + + @Override + protected Class getClassForEvaluation() { + return String.class; + } + + @Override + void add(Context context, Request currentRequest, WorkspaceItem source, String string, Object value) + throws Exception { + Item item = source.getItem(); + EPerson submitter = context.getCurrentUser(); + + // remove any existing DSpace license (just in case the user + // accepted it previously) + itemService.removeDSpaceLicense(context, item); + + String license = LicenseUtils.getLicenseText(context.getCurrentLocale(), source.getCollection(), item, + submitter); + + LicenseUtils.grantLicense(context, item, license, (String) value); + } + +} diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseRemovePatchOperation.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseRemovePatchOperation.java index 2d99309706..dda15f35e7 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseRemovePatchOperation.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseRemovePatchOperation.java @@ -15,7 +15,15 @@ import org.dspace.services.model.Request; import org.springframework.beans.factory.annotation.Autowired; /** - * Submission License "remove" patch operation + * Submission License "remove" patch operation. + * + * To remove a previous granted license: + * + * Example: + * curl -X PATCH http://${dspace.url}/api/submission/workspaceitems/<:id-workspaceitem> -H " + * Content-Type: application/json" -d '[{ "op": "remove", "path": " + * /sections/license/acceptanceDate"}]' + * * * @author Luigi Andrea Pascarelli (luigiandrea.pascarelli at 4science.it) * diff --git a/dspace-spring-rest/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseReplacePatchOperation.java b/dspace-spring-rest/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseReplacePatchOperation.java index fb056c12a7..78dc2747f2 100644 --- a/dspace-spring-rest/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseReplacePatchOperation.java +++ b/dspace-spring-rest/src/main/java/org/dspace/app/rest/submit/factory/impl/LicenseReplacePatchOperation.java @@ -7,6 +7,9 @@ */ package org.dspace.app.rest.submit.factory.impl; +import java.util.List; + +import org.dspace.content.Bundle; import org.dspace.content.Item; import org.dspace.content.LicenseUtils; import org.dspace.content.WorkspaceItem; @@ -15,10 +18,15 @@ import org.dspace.core.Context; import org.dspace.eperson.EPerson; import org.dspace.services.model.Request; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.Assert; /** * Submission "replace" patch operation * + * The replace operation allows to replace existent information with new one. + * Attempt to use the replace operation without a previous accepted license must + * return an error. + * * @author Luigi Andrea Pascarelli (luigiandrea.pascarelli at 4science.it) * */ @@ -26,10 +34,14 @@ public class LicenseReplacePatchOperation extends ReplacePatchOperation @Autowired ItemService itemService; - + @Override - void replace(Context context, Request currentRequest, WorkspaceItem source, String string, Object value) throws Exception { + void replace(Context context, Request currentRequest, WorkspaceItem source, String string, Object value) + throws Exception { Item item = source.getItem(); + List bunds = itemService.getBundles(item, "LICENSE"); + Assert.notEmpty(bunds); + EPerson submitter = context.getCurrentUser(); // remove any existing DSpace license (just in case the user @@ -38,10 +50,9 @@ public class LicenseReplacePatchOperation extends ReplacePatchOperation String license = LicenseUtils.getLicenseText(context.getCurrentLocale(), source.getCollection(), item, submitter); - - LicenseUtils.grantLicense(context, item, license, (String)value); - } + LicenseUtils.grantLicense(context, item, license, (String) value); + } @Override protected Class getArrayClassForEvaluation() { @@ -53,5 +64,4 @@ public class LicenseReplacePatchOperation extends ReplacePatchOperation return String.class; } - }