D4CRIS-338 add implementation to grant license

This commit is contained in:
Luigi Andrea Pascarelli
2017-11-15 00:31:30 +01:00
parent 1e1416143d
commit fc13ad5725
10 changed files with 70 additions and 23 deletions

View File

@@ -10,6 +10,7 @@ package org.dspace.content;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.dspace.content.factory.ContentServiceFactory;
@@ -437,4 +438,16 @@ public class Bitstream extends DSpaceObject implements DSpaceObjectLegacySupport
return hash;
}
/**
* Add date for bitstream granted (used into the use case for license grant the {@link LicenseUtils#grantLicense(Context, Item, String, String)}
*
* @param context
* the dspace context
* @param acceptanceDate the granted date
* @throws SQLException
*/
public void setAcceptanceDate(Context context, DCDate acceptanceDate) throws SQLException {
getBitstreamService().setMetadataSingleValue(context, this, MetadataSchema.DC_SCHEMA, "rights", "date", null, acceptanceDate.toString());
}
}

View File

@@ -10,6 +10,7 @@ package org.dspace.content;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;
import java.util.Map;
@@ -133,12 +134,13 @@ public class LicenseUtils
* the item object of the license
* @param licenseText
* the license the user granted
* @param acceptanceDate TODO
* @throws SQLException if database error
* @throws IOException if IO error
* @throws AuthorizeException if authorization error
*/
public static void grantLicense(Context context, Item item,
String licenseText) throws SQLException, IOException,
String licenseText, String acceptanceDate) throws SQLException, IOException,
AuthorizeException
{
// Put together text to store
@@ -155,6 +157,11 @@ public class LicenseUtils
b.setName(context, "license.txt");
b.setSource(context, "Written by org.dspace.content.LicenseUtils");
DCDate acceptanceDCDate = DCDate.getCurrent();
if(acceptanceDate!=null) {
acceptanceDCDate = new DCDate(acceptanceDate);
}
b.setAcceptanceDate(context, acceptanceDCDate);
// Find the License format
BitstreamFormat bf = bitstreamFormat.findByShortDescription(context,
"License");

View File

@@ -233,7 +233,7 @@ public class LicenseUtilsTest extends AbstractUnitTest
Item item = installItemService.installItem(context, workspaceItemService.create(context, collection, false));
String defaultLicense = licenseService.getDefaultSubmissionLicense();
LicenseUtils.grantLicense(context, item, defaultLicense);
LicenseUtils.grantLicense(context, item, defaultLicense, null);
StringWriter writer = new StringWriter();
IOUtils.copy(bitstreamService.retrieve(context, itemService.getBundles(item, "LICENSE").get(0).getBitstreams().get(0)), writer);

View File

@@ -252,15 +252,15 @@ public class WorkspaceItemRestRepository extends DSpaceRestRepository<WorkspaceI
index = path[4];
}
String operation = op.getOp();
LateObjectEvaluator value = (LateObjectEvaluator)op.getValue();
evaluatePatch(context, request, source, wsi, operation, section, target, index, value);
evaluatePatch(context, request, source, wsi, operation, section, target, index, op.getValue());
}
}
wis.update(context, source);
}
private void evaluatePatch(Context context, HttpServletRequest request, WorkspaceItem source, WorkspaceItemRest wsi, String operation, String section, String target, String index,
LateObjectEvaluator value) throws Exception {
Object value) throws Exception {
SubmissionConfig submissionConfig = submissionConfigReader.getSubmissionConfigByName(wsi.getSubmissionDefinition().getName());
for(int stepNum = 0; stepNum<submissionConfig.getNumberOfSteps(); stepNum++) {

View File

@@ -26,7 +26,7 @@ public interface AbstractRestProcessingStep {
public <T extends Serializable> T getData(WorkspaceItem obj, SubmissionStepConfig config) throws Exception;
public void doPatchProcessing(Context context, Request currentRequest, WorkspaceItem source, String operation,
String target, String index, LateObjectEvaluator value) throws Exception;
String target, String index, Object value) throws Exception;
}

View File

@@ -34,7 +34,7 @@ public class CollectionStep extends org.dspace.submit.step.SelectCollectionStep
@Override
public void doPatchProcessing(Context context, Request currentRequest, WorkspaceItem source, String operation,
String target, String index, LateObjectEvaluator value) {
String target, String index, Object value) {
switch (operation) {
case "move":

View File

@@ -79,10 +79,11 @@ public class DescribeStep extends org.dspace.submit.step.DescribeStep implements
@Override
public void doPatchProcessing(Context context, Request currentRequest, WorkspaceItem source, String operation,
String target, String index, LateObjectEvaluator value) throws Exception {
String target, String index, Object value) throws Exception {
MetadataValueRest[] list = null;
if(value!=null) {
list = (MetadataValueRest[])value.evaluate(MetadataValueRest[].class);
LateObjectEvaluator object = (LateObjectEvaluator)value;
list = (MetadataValueRest[])object.evaluate(MetadataValueRest[].class);
}
switch (operation) {
case "add":

View File

@@ -7,15 +7,23 @@
*/
package org.dspace.app.rest.submit.step;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import org.atteo.evo.inflector.English;
import org.dspace.app.rest.model.BitstreamRest;
import org.dspace.app.rest.model.step.DataLicense;
import org.dspace.app.rest.submit.AbstractRestProcessingStep;
import org.dspace.app.util.SubmissionStepConfig;
import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream;
import org.dspace.content.Item;
import org.dspace.content.LicenseUtils;
import org.dspace.content.WorkspaceItem;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;
import org.dspace.services.model.Request;
import org.springframework.data.rest.webmvc.json.patch.LateObjectEvaluator;
@@ -43,16 +51,29 @@ public class LicenseStep extends org.dspace.submit.step.LicenseStep implements A
@Override
public void doPatchProcessing(Context context, Request currentRequest, WorkspaceItem source, String operation,
String target, String index, LateObjectEvaluator value) {
String target, String index, Object value) throws Exception {
if("acceptanceDate".equals(target)) {
Item item = source.getItem();
EPerson submitter = context.getCurrentUser();
switch (operation) {
case "replace":
// 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);
break;
case "remove":
itemService.removeDSpaceLicense(context, item);
break;
default:
throw new RuntimeException("Operation " + operation + " not yet implemented!");
}
}
}
}

View File

@@ -46,7 +46,7 @@ public class UploadStep extends org.dspace.submit.step.UploadStep implements Abs
@Override
public void doPatchProcessing(Context context, Request currentRequest, WorkspaceItem source, String operation,
String target, String index, LateObjectEvaluator value) {
String target, String index, Object value) {
switch (operation) {
case "add":

View File

@@ -590,5 +590,10 @@
<qualifier>license</qualifier>
<scope_note></scope_note>
</dc-type>
<dc-type>
<schema>dc</schema>
<element>rights</element>
<qualifier>date</qualifier>
<scope_note></scope_note>
</dc-type>
</dspace-dc-types>