Skip to content

Commit

Permalink
Merge pull request DSpace#9195 from 4Science/CST-12042-addSupportForT…
Browse files Browse the repository at this point in the history
…hePrimaryBitstreamFlag

Add support for the primary bitstream flag
  • Loading branch information
tdonohue authored Feb 20, 2024
2 parents fc5ec8f + 34e027a commit 28ad35f
Show file tree
Hide file tree
Showing 9 changed files with 811 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonUnwrapped;

Expand All @@ -19,6 +20,11 @@
*/
public class DataUpload implements SectionData {

/*
* primary bitstream uuid
*/
private UUID primary;

@JsonUnwrapped
private List<UploadBitstreamRest> files;

Expand All @@ -32,4 +38,13 @@ public List<UploadBitstreamRest> getFiles() {
public void setFiles(List<UploadBitstreamRest> files) {
this.files = files;
}

public UUID getPrimary() {
return primary;
}

public void setPrimary(UUID primary) {
this.primary = primary;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public interface DataProcessingStep extends RestProcessingStep {
public static final String ACCESS_CONDITION_STEP_OPERATION_ENTRY = "discoverable";
public static final String ACCESS_CONDITION_POLICY_STEP_OPERATION_ENTRY = "accessConditions";
public static final String SHOW_IDENTIFIERS_ENTRY = "identifiers";
public static final String PRIMARY_FLAG_ENTRY = "primary";

public static final String UPLOAD_STEP_METADATA_PATH = "metadata";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void add(Context context, HttpServletRequest currentRequest, InProgressSubmissio
bitstreamMetadataValuePathUtils.validate(absolutePath);
Item item = source.getItem();
List<Bundle> bundle = itemService.getBundles(item, Constants.CONTENT_BUNDLE_NAME);
;

for (Bundle bb : bundle) {
int idx = 0;
for (Bitstream b : bb.getBitstreams()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* 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 static org.dspace.core.Constants.CONTENT_BUNDLE_NAME;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;

import org.dspace.app.rest.exception.UnprocessableEntityException;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.InProgressSubmission;
import org.dspace.content.Item;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Submission "add" operation to set primary bitstream.
*
* @author Mykhaylo Boychuk ([email protected])
*/
public class PrimaryBitstreamAddPatchOperation extends AddPatchOperation<String> {

@Autowired
private ItemService itemService;

@Override
void add(Context context, HttpServletRequest currentRequest, InProgressSubmission source, String path, Object value)
throws Exception {
Item item = source.getItem();
UUID primaryUUID = parseValue(value);
List<Bundle> bundles = itemService.getBundles(item, CONTENT_BUNDLE_NAME);
Optional<Bundle> currentPrimaryBundle = bundles.stream()
.filter(bundle -> Objects.nonNull(bundle.getPrimaryBitstream()))
.findFirst();

Optional<Bitstream> primaryBitstreamToAdd = null;
for (Bundle bundle : bundles) {
primaryBitstreamToAdd = bundle.getBitstreams().stream()
.filter(b -> b.getID().equals(primaryUUID))
.findFirst();
if (primaryBitstreamToAdd.isPresent()) {
if (currentPrimaryBundle.isPresent()) {
currentPrimaryBundle.get().setPrimaryBitstreamID(null);
}
bundle.setPrimaryBitstreamID(primaryBitstreamToAdd.get());
break;
}
}

if (primaryBitstreamToAdd.isEmpty()) {
throw new UnprocessableEntityException("The provided uuid: " + primaryUUID +
" of bitstream to set as primary doesn't match any bitstream!");
}
}

private UUID parseValue(Object value) {
UUID primaryBitstreamUUID;
try {
primaryBitstreamUUID = UUID.fromString((String) value);
} catch (Exception e) {
throw new UnprocessableEntityException("The provided value is invalid!", e);
}
return primaryBitstreamUUID;
}

@Override
protected Class<String[]> getArrayClassForEvaluation() {
return null;
}

@Override
protected Class<String> getClassForEvaluation() {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* 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 static org.dspace.core.Constants.CONTENT_BUNDLE_NAME;

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

import org.dspace.content.Bundle;
import org.dspace.content.InProgressSubmission;
import org.dspace.content.Item;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Submission "remove" operation to remove primary bitstream.
*
* @author Mykhaylo Boychuk ([email protected])
*/
public class PrimaryBitstreamRemovePatchOperation extends RemovePatchOperation<String> {

@Autowired
private ItemService itemService;

@Override
void remove(Context context, HttpServletRequest request, InProgressSubmission source, String path, Object value)
throws Exception {
Item item = source.getItem();
List<Bundle> bundles = itemService.getBundles(item, CONTENT_BUNDLE_NAME);
bundles.forEach(b -> b.setPrimaryBitstreamID(null));
}

@Override
protected Class<String[]> getArrayClassForEvaluation() {
return null;
}

@Override
protected Class<String> getClassForEvaluation() {
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* 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 static org.dspace.core.Constants.CONTENT_BUNDLE_NAME;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;

import org.dspace.app.rest.exception.UnprocessableEntityException;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.InProgressSubmission;
import org.dspace.content.Item;
import org.dspace.content.service.ItemService;
import org.dspace.core.Context;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Submission "replace" operation to replace primary bitstream.
*
* @author Mykhaylo Boychuk ([email protected])
*/
public class PrimaryBitstreamReplacePatchOperation extends ReplacePatchOperation<String> {

private final String EX_MESSAGE = "It is impossible to replace primary bitstrem if it wasn't set!";

@Autowired
private ItemService itemService;

@Override
void replace(Context context, HttpServletRequest request, InProgressSubmission source, String path, Object value)
throws Exception {
Item item = source.getItem();
UUID primaryUUID = parseValue(value);
List<Bundle> bundles = itemService.getBundles(item, CONTENT_BUNDLE_NAME);
Bundle currentPrimaryBundle = bundles.stream()
.filter(bundle -> Objects.nonNull(bundle.getPrimaryBitstream()))
.findFirst()
.orElseThrow(() -> new UnprocessableEntityException(EX_MESSAGE));

Optional<Bitstream> primaryBitstream = null;
for (Bundle bundle : bundles) {
primaryBitstream = bundle.getBitstreams().stream()
.filter(b -> b.getID().equals(primaryUUID))
.findFirst();
if (primaryBitstream.isPresent()) {
currentPrimaryBundle.setPrimaryBitstreamID(null);
bundle.setPrimaryBitstreamID(primaryBitstream.get());
break;
}
}

if (primaryBitstream.isEmpty()) {
throw new UnprocessableEntityException("The provided uuid: " + primaryUUID +
" of bitstream to set as primary doesn't match any bitstream!");
}
}

private UUID parseValue(Object value) {
UUID primaryBitstreamUUID;
try {
primaryBitstreamUUID = UUID.fromString((String) value);
} catch (Exception e) {
throw new UnprocessableEntityException("The provided value is invalid!", e);
}
return primaryBitstreamUUID;
}

@Override
protected Class<String[]> getArrayClassForEvaluation() {
return null;
}

@Override
protected Class<String> getClassForEvaluation() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
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;
import org.apache.logging.log4j.Logger;
import org.dspace.app.rest.exception.UnprocessableEntityException;
import org.dspace.app.rest.model.ErrorRest;
Expand Down Expand Up @@ -56,8 +58,12 @@ public DataUpload getData(SubmissionService submissionService, InProgressSubmiss
List<Bundle> bundles = itemService.getBundles(obj.getItem(), Constants.CONTENT_BUNDLE_NAME);
for (Bundle bundle : bundles) {
for (Bitstream source : bundle.getBitstreams()) {
Bitstream primaryBitstream = bundle.getPrimaryBitstream();
UploadBitstreamRest b = submissionService.buildUploadBitstream(configurationService, source);
result.getFiles().add(b);
if (Objects.nonNull(primaryBitstream)) {
result.setPrimary(primaryBitstream.getID());
}
}
}
return result;
Expand All @@ -73,6 +79,8 @@ public void doPatchProcessing(Context context, HttpServletRequest currentRequest
instance = UPLOAD_STEP_METADATA_OPERATION_ENTRY;
} else if (op.getPath().contains(UPLOAD_STEP_ACCESSCONDITIONS_OPERATION_ENTRY)) {
instance = stepConf.getType() + "." + UPLOAD_STEP_ACCESSCONDITIONS_OPERATION_ENTRY;
} else if (op.getPath().contains(PRIMARY_FLAG_ENTRY)) {
instance = PRIMARY_FLAG_ENTRY;
} else {
instance = UPLOAD_STEP_REMOVE_OPERATION_ENTRY;
}
Expand All @@ -87,9 +95,11 @@ public void doPatchProcessing(Context context, HttpServletRequest currentRequest
instance = stepConf.getType() + "." + UPLOAD_STEP_ACCESSCONDITIONS_OPERATION_ENTRY;
} else if (op.getPath().contains(UPLOAD_STEP_METADATA_PATH)) {
instance = UPLOAD_STEP_METADATA_OPERATION_ENTRY;
} else if (op.getPath().contains(PRIMARY_FLAG_ENTRY)) {
instance = PRIMARY_FLAG_ENTRY;
}
}
if (instance == null) {
if (StringUtils.isBlank(instance)) {
throw new UnprocessableEntityException("The path " + op.getPath() + " is not supported by the operation "
+ op.getOp());
}
Expand Down
Loading

0 comments on commit 28ad35f

Please sign in to comment.