Skip to content

Commit

Permalink
Merge pull request #242 from TAMULib/tamu-dspace-7_x-proxy-license-up…
Browse files Browse the repository at this point in the history
…load-patch

Refactor proxy license bundle upload and license bundle management
  • Loading branch information
wwelling authored Jan 30, 2024
2 parents 13cda26 + 847bd0f commit 8afb60b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.BooleanUtils;
import org.dspace.app.rest.utils.ProxyLicenseUtils;
import org.dspace.content.InProgressSubmission;
import org.dspace.content.Item;
import org.dspace.content.ProxyLicenseUtils;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

import javax.servlet.http.HttpServletRequest;

import org.dspace.app.rest.utils.ProxyLicenseUtils;
import org.dspace.content.InProgressSubmission;
import org.dspace.content.Item;
import org.dspace.content.ProxyLicenseUtils;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@
package org.dspace.app.rest.submit.factory.impl;

import java.io.File;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.app.rest.utils.ProxyLicenseUtils;
import org.dspace.content.InProgressSubmission;
import org.dspace.content.Item;
import org.dspace.content.LicenseUtils;
import org.dspace.content.ProxyLicenseUtils;
import org.dspace.content.service.BundleService;
import org.dspace.content.service.ItemService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.core.service.LicenseService;
import org.dspace.eperson.EPerson;
Expand Down Expand Up @@ -109,21 +105,6 @@ void add(Context context, HttpServletRequest currentRequest, InProgressSubmissio
: licenseService.getLicenseText(licensePath);

if (StringUtils.isNotBlank(licenseText)) {
if (selected.equalsIgnoreCase("proxy")) {
List<Bundle> licenseBundles = itemService.getBundles(item, Constants.LICENSE_BUNDLE_NAME);
if (licenseBundles.size() > 0) {
Bundle licenseBundle = licenseBundles.get(0);
for (Bitstream bitstream: licenseBundle.getBitstreams()) {
if (Constants.LICENSE_BITSTREAM_NAME.equals(bitstream.getName())) {
bundleService.removeBitstream(context, licenseBundle, bitstream);
break;
}
}
}
} else {
itemService.removeDSpaceLicense(context, item);
}

ProxyLicenseUtils.addLicense(context, item, selected, licenseText);
} else {
throw new RuntimeException(String.format("Unable to find license file at %s", licenseFilename));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.dspace.app.rest.submit.step;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -19,10 +17,9 @@
import org.dspace.app.rest.submit.UploadableStep;
import org.dspace.app.rest.submit.factory.PatchOperationFactory;
import org.dspace.app.rest.submit.factory.impl.PatchOperation;
import org.dspace.app.rest.utils.Utils;
import org.dspace.app.rest.utils.ProxyLicenseUtils;
import org.dspace.app.util.SubmissionStepConfig;
import org.dspace.content.Bitstream;
import org.dspace.content.BitstreamFormat;
import org.dspace.content.Bundle;
import org.dspace.content.InProgressSubmission;
import org.dspace.content.Item;
Expand All @@ -36,8 +33,6 @@ public class ProxyLicenseStep extends LicenseStep implements UploadableStep {

private static final String LICENSE_STEP_SELECTED_OPERATION_ENTRY = "selected";

private static final String PROXY_LICENSE_NAME = "PERMISSION";

private static final String DCTERMS_RIGHTSDATE = "dcterms.accessRights";
private static final String DCTERMS_ALTERNATIVE = "dcterms.alternative";

Expand All @@ -53,16 +48,16 @@ public DataLicense getData(SubmissionService submissionService, InProgressSubmis

DataLicense result = new DataLicense();

List<Bundle> bundles = itemService.getBundles(obj.getItem(), Constants.LICENSE_BUNDLE_NAME);

Bitstream licenseBitstream = null;
Bitstream proxyBitstream = null;

for (Bundle bundle : bundles) {
for (Bitstream bitstream : bundle.getBitstreams()) {
List<Bundle> licenseBundles = itemService.getBundles(obj.getItem(), Constants.LICENSE_BUNDLE_NAME);

if (licenseBundles.size() > 0) {
for (Bitstream bitstream : licenseBundles.get(0).getBitstreams()) {
if (bitstream.getName().equals(Constants.LICENSE_BITSTREAM_NAME)) {
licenseBitstream = bitstream;
} else if (bitstream.getName().startsWith(PROXY_LICENSE_NAME)) {
} else if (bitstream.getName().startsWith(ProxyLicenseUtils.PROXY_LICENSE_NAME)) {
proxyBitstream = bitstream;
}
}
Expand Down Expand Up @@ -116,57 +111,14 @@ public ErrorRest upload(Context context, SubmissionService submissionService, Su
InProgressSubmission wsi, MultipartFile file) {
Item item = wsi.getItem();

List<Bundle> licenseBundles = null;
Bundle licenseBundle = null;
Bitstream proxyBitstream = null;

try (InputStream in = new BufferedInputStream(file.getInputStream())) {

licenseBundles = itemService.getBundles(item, Constants.LICENSE_BUNDLE_NAME);

if (licenseBundles.size() > 0) {
licenseBundle = licenseBundles.get(0);
for (Bitstream bitstream: licenseBundle.getBitstreams()) {
if (bitstream.getName().startsWith(PROXY_LICENSE_NAME)) {
bundleService.removeBitstream(context, licenseBundle, bitstream);
break;
}
}
} else {
licenseBundle = bundleService.create(context, item, Constants.LICENSE_BUNDLE_NAME);
}

proxyBitstream = bitstreamService.create(context, licenseBundle, in);

String filename = Utils.getFileName(file);
String[] parts = filename.split("\\.");
String permissionLicenseName = parts.length == 1
? String.join(".", PROXY_LICENSE_NAME, "license")
: String.join(".", PROXY_LICENSE_NAME, parts[parts.length - 1]);

proxyBitstream.setName(context, permissionLicenseName);

proxyBitstream.setSource(context, file.getOriginalFilename());
proxyBitstream.setDescription(context, "Proxy license");

BitstreamFormat bf = bitstreamFormatService.guessFormat(context, proxyBitstream);

proxyBitstream.setFormat(context, bf);

bitstreamService.update(context, proxyBitstream);

try {
ProxyLicenseUtils.addProxyLicense(context, item, file);
} catch (Exception e) {
log.error(e.getMessage(), e);
ErrorRest result = new ErrorRest();
result.setMessage(e.getMessage());
if (licenseBundles != null && licenseBundles.size() > 0) {
result.getPaths().add(
"/" + WorkspaceItemRestRepository.OPERATION_PATH_SECTIONS + "/" + stepConfig.getId() + "/files/" +
licenseBundles.get(0).getBitstreams().size());
} else {
result.getPaths()
.add("/" + WorkspaceItemRestRepository.OPERATION_PATH_SECTIONS + "/" + stepConfig.getId());
}
result.getPaths()
.add("/" + WorkspaceItemRestRepository.OPERATION_PATH_SECTIONS + "/" + stepConfig.getId());
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@
*
* http://www.dspace.org/license/
*/
package org.dspace.content;
package org.dspace.app.rest.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.List;

import org.dspace.authorize.AuthorizeException;
import org.dspace.content.Bitstream;
import org.dspace.content.BitstreamFormat;
import org.dspace.content.Bundle;
import org.dspace.content.DCDate;
import org.dspace.content.Item;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.BitstreamFormatService;
import org.dspace.content.service.BitstreamService;
import org.dspace.content.service.BundleService;
import org.dspace.content.service.ItemService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.springframework.web.multipart.MultipartFile;

/**
* TAMU Customization - Proxy license utilility for decoupling license selection from accepting licence
Expand All @@ -31,23 +39,72 @@ public class ProxyLicenseUtils {
private static final BundleService bundleService = ContentServiceFactory.getInstance().getBundleService();
private static final ItemService itemService = ContentServiceFactory.getInstance().getItemService();

public static final String PROXY_LICENSE_NAME = "PERMISSION";

/**
* Default constructor
*/
private ProxyLicenseUtils() { }

/**
* Store a copy of the proxy license as the user uploaded for the item.
*
* @param context the dspace context
* @param item the item object of the license
* @param file the multipart file upload
* @throws SQLException if database error
* @throws IOException if IO error
* @throws AuthorizeException if authorization error
*/
public static void addProxyLicense(Context context, Item item, MultipartFile file)
throws SQLException, IOException, AuthorizeException {

InputStream is = new BufferedInputStream(file.getInputStream());

List<Bundle> licenseBundles = itemService.getBundles(item, Constants.LICENSE_BUNDLE_NAME);
Bundle licenseBundle = licenseBundles.size() > 0
? licenseBundles.get(0)
: bundleService.create(context, item, Constants.LICENSE_BUNDLE_NAME);

// remove existing proxy license bitstream
for (Bitstream bitstream: licenseBundle.getBitstreams()) {
if (bitstream.getName().startsWith(PROXY_LICENSE_NAME)) {
bundleService.removeBitstream(context, licenseBundle, bitstream);
}
}

Bitstream bitstream = bitstreamService.create(context, licenseBundle, is);

String filename = Utils.getFileName(file);
String[] parts = filename.split("\\.");
String proxyLicenseName = parts.length == 1
? String.join(".", PROXY_LICENSE_NAME, "license")
: String.join(".", PROXY_LICENSE_NAME, parts[parts.length - 1]);

bitstream.setName(context, proxyLicenseName);

bitstream.setSource(context, file.getOriginalFilename());
bitstream.setDescription(context, "Proxy license");

BitstreamFormat bf = bitstreamFormatService.guessFormat(context, bitstream);

bitstream.setFormat(context, bf);

bitstreamService.update(context, bitstream);
}

/**
* Store a copy of the license as the user selected for the item.
*
* @param context the dspace context
* @param item the item object of the license
* @param selection the license the user selected
* @param selected the license the user selected
* @param licenseText the license text
* @throws SQLException if database error
* @throws IOException if IO error
* @throws AuthorizeException if authorization error
*/
public static void addLicense(Context context, Item item, String selection, String licenseText)
public static void addLicense(Context context, Item item, String selected, String licenseText)
throws SQLException, IOException, AuthorizeException {

// Store text as a bitstream
Expand All @@ -59,6 +116,22 @@ public static void addLicense(Context context, Item item, String selection, Stri
? licenseBundles.get(0)
: bundleService.create(context, item, Constants.LICENSE_BUNDLE_NAME);

// remove existing license bitstream
for (Bitstream bitstream: licenseBundle.getBitstreams()) {
if (Constants.LICENSE_BITSTREAM_NAME.equals(bitstream.getName())) {
bundleService.removeBitstream(context, licenseBundle, bitstream);
}
}

// if proxy license not selected, remove proxy license
if (!selected.equalsIgnoreCase("proxy")) {
for (Bitstream bitstream: licenseBundle.getBitstreams()) {
if (bitstream.getName().startsWith(PROXY_LICENSE_NAME)) {
bundleService.removeBitstream(context, licenseBundle, bitstream);
}
}
}

Bitstream bitstream = bitstreamService.create(context, licenseBundle, is);

// Now set the format and name of the bitstream
Expand All @@ -68,10 +141,10 @@ public static void addLicense(Context context, Item item, String selection, Stri
// Find the License format
BitstreamFormat bf = bitstreamFormatService.findByShortDescription(context,
"License");
bitstream.setFormat(bf);
bitstream.setFormat(context, bf);

bitstreamService
.setMetadataSingleValue(context, bitstream, "dcterms", "alternative", null, null, selection);
.setMetadataSingleValue(context, bitstream, "dcterms", "alternative", null, null, selected);

bitstreamService.update(context, bitstream);
}
Expand Down

0 comments on commit 8afb60b

Please sign in to comment.