Skip to content

Commit

Permalink
Update proxy license step upload exclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
wwelling committed Jan 19, 2024
1 parent 67be235 commit 5fe27c2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

Expand Down Expand Up @@ -87,7 +86,7 @@ public class SubmissionService {
private static final Logger log = org.apache.logging.log4j.LogManager.getLogger(SubmissionService.class);

// TAMU Customization - proxy license step
private static final String STEP_ID_PARAM = "stepId";
private static final String FORM_DATA_SECTION_ID = "sectionId";

@Autowired
protected ConfigurationService configurationService;
Expand Down Expand Up @@ -338,13 +337,12 @@ public DataCCLicense getDataCCLicense(InProgressSubmission obj)
public List<ErrorRest> uploadFileToInprogressSubmission(Context context, HttpServletRequest request,
AInprogressSubmissionRest wsi, InProgressSubmission source, MultipartFile file) {

List<ErrorRest> errors = new ArrayList<ErrorRest>();

// TAMU Customization - proxy license step
Optional<String> sectionId = getSectionId(request);
System.out.println("\n\nSubmissionService.uploadFileToInprogressSubmission");
System.out.println("\tsection id: " + sectionId);

List<ErrorRest> errors = new ArrayList<ErrorRest>();
SubmissionConfig submissionConfig =
submissionConfigService.getSubmissionConfigByName(wsi.getSubmissionDefinition().getName());
List<Object[]> stepInstancesAndConfigs = new ArrayList<Object[]>();
Expand All @@ -354,6 +352,7 @@ public List<ErrorRest> uploadFileToInprogressSubmission(Context context, HttpSer
// instance over all the phase and we will reduce initialization time as well
for (int i = 0; i < submissionConfig.getNumberOfSteps(); i++) {
SubmissionStepConfig stepConfig = submissionConfig.getStep(i);
System.out.println("\tstep config id: " + stepConfig.getId());
/*
* First, load the step processing class (using the current
* class loader)
Expand All @@ -362,23 +361,30 @@ public List<ErrorRest> uploadFileToInprogressSubmission(Context context, HttpSer
Class stepClass;
try {
stepClass = loader.loadClass(stepConfig.getProcessingClassName());
// TAMU Customization - proxy license step - match upload with step from request
boolean addStep = UploadableStep.class.isAssignableFrom(stepClass);
System.out.println("\tstep config id: " + stepConfig.getId());
// if section id is present skip sections not matching
if (sectionId.isPresent() && !stepConfig.getId().equals(sectionId.get())) {
addStep = false;
}
if (addStep) {
System.out.println("\tadding step: " + stepClass);
if (UploadableStep.class.isAssignableFrom(stepClass)) {
Object stepInstance = stepClass.newInstance();
stepInstancesAndConfigs.add(new Object[] {stepInstance, stepConfig});

// TAMU Customization - proxy license step - exclusive and only when matching step id
boolean isExclusiveMatchingStepId = ((UploadableStep) stepInstance).isExclusiveMatchingStepId();
System.out.println("\tstep class: " + stepClass);
System.out.println("\tstep is exclusive: " + isExclusiveMatchingStepId);
if (isExclusiveMatchingStepId) {
if (sectionId.isPresent() && stepConfig.getId().equals(sectionId.get())) {
System.out.println("\tclearing steps");
stepInstancesAndConfigs.clear();
System.out.println("\tadding exclusive step: " + stepClass);
stepInstancesAndConfigs.add(new Object[] {stepInstance, stepConfig});
break;
}
} else {
stepInstancesAndConfigs.add(new Object[] {stepInstance, stepConfig});
}
}
System.out.println("\n\n");
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
System.out.println("\n\n");
for (Object[] stepInstanceAndCfg : stepInstancesAndConfigs) {
UploadableStep uploadableStep = (UploadableStep) stepInstanceAndCfg[0];
if (uploadableStep instanceof ListenerProcessingStep) {
Expand Down Expand Up @@ -498,15 +504,18 @@ public void evaluatePatchToInprogressSubmission(Context context, HttpServletRequ
private Optional<String> getSectionId(HttpServletRequest request) {
String sectionId = null;
try {
Part part = request.getPart(STEP_ID_PARAM);
System.out.println("\t\t\trequest parts count: " + request.getParts().size());
Part part = request.getPart(FORM_DATA_SECTION_ID);
System.out.println("\t\t\trequest part " + FORM_DATA_SECTION_ID + ": " + part);
if (Objects.nonNull(part)) {
sectionId = IOUtils.toString(part.getInputStream(), StandardCharsets.UTF_8).trim();
System.out.println("\t\t\tsection id " + sectionId);
}
if (StringUtils.isBlank(sectionId)) {
sectionId = null;
}
} catch (Exception e) {

log.error(e.getMessage(), e);
}
return Optional.ofNullable(sectionId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@
import org.springframework.web.multipart.MultipartFile;

/**
* The interface for submission Steps that need to deal with file upload
* TAMU Customized interface for submission Steps that need to deal with file upload
*
* @author Luigi Andrea Pascarelli (luigiandrea.pascarelli at 4science.it)
* @author Andrea Bollini (andrea.bollini at 4science.it)
*/
public interface UploadableStep extends RestProcessingStep {

/**
* TAMU Customization - Method to specify step only invokes upload exclusively
* when matching step id from multipart form
*/
default public boolean isExclusiveMatchingStepId() {
return false;
}

/**
* The method to implement to support upload of a file in the submission section (aka panel / step)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -27,7 +26,6 @@
import org.dspace.content.Item;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

public class ProxyLicenseStep extends LicenseStep implements UploadableStep {
Expand All @@ -39,6 +37,11 @@ public class ProxyLicenseStep extends LicenseStep implements UploadableStep {
private static final String DCTERMS_RIGHTSDATE = "dcterms.accessRights";
private static final String DCTERMS_ALTERNATIVE = "dcterms.alternative";

@Override
public boolean isExclusiveMatchingStepId() {
return true;
}

@Override
public DataLicense getData(SubmissionService submissionService, InProgressSubmission obj,
SubmissionStepConfig config)
Expand Down Expand Up @@ -96,38 +99,14 @@ public ErrorRest upload(Context context, SubmissionService submissionService, Su

System.out.println("\nProxyLicenseStep.upload\n");

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

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

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

if (contentBundles.size() < 1) {
throw new RuntimeException("No content bundles with the proxy license upload!");
}

String filename = Utils.getFileName(file);
String checksum = DigestUtils.md5DigestAsHex(in);

for (Bundle bundle: contentBundles) {
for (Bitstream bitstream: bundle.getBitstreams()) {
if (filename.equals(bitstream.getName()) && checksum.equals(bitstream.getChecksum())) {
proxyBitstream = bitstream;
break;
}
}
}

if (Objects.isNull(proxyBitstream)) {
throw new RuntimeException("No proxy permission license bitstream found in content bundles!");
}

System.out.println("\tpermission bitstream added to content bundle: " + proxyBitstream.getName());

System.out.println("\t\tremove existing proxy license");
if (licenseBundles.size() > 0) {
licenseBundle = licenseBundles.get(0);
Expand All @@ -143,6 +122,9 @@ public ErrorRest upload(Context context, SubmissionService submissionService, Su
licenseBundle = bundleService.create(context, item, Constants.LICENSE_BUNDLE_NAME);
}

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

String filename = Utils.getFileName(file);
System.out.println("\t\tfilename: " + filename);
String[] parts = filename.split("\\.");
String permissionLicenseName = parts.length == 1
Expand All @@ -162,17 +144,19 @@ public ErrorRest upload(Context context, SubmissionService submissionService, Su
System.out.println("\t\tset proxy bitstream format:" + bf.getShortDescription());
proxyBitstream.setFormat(context, bf);

System.out.println("\t\tmove bitstream from content bundle to license bundle");
bundleService.moveBitstreamToBundle(context, licenseBundle, proxyBitstream);
System.out.println("\t\tupdate bitstream service");
bitstreamService.update(context, proxyBitstream);
System.out.println("\t\tupdate item service");
itemService.update(context, item);

} catch (Exception e) {
log.error(e.getMessage(), e);
ErrorRest result = new ErrorRest();
result.setMessage(e.getMessage());
if (contentBundles != null && contentBundles.size() > 0) {
if (licenseBundles != null && licenseBundles.size() > 0) {
result.getPaths().add(
"/" + WorkspaceItemRestRepository.OPERATION_PATH_SECTIONS + "/" + stepConfig.getId() + "/files/" +
contentBundles.get(0).getBitstreams().size());
licenseBundles.get(0).getBitstreams().size());
} else {
result.getPaths()
.add("/" + WorkspaceItemRestRepository.OPERATION_PATH_SECTIONS + "/" + stepConfig.getId());
Expand Down

0 comments on commit 5fe27c2

Please sign in to comment.