From 3ee7a4a868e5a2407aadb1298b0cb617ecd4c359 Mon Sep 17 00:00:00 2001 From: Mykhaylo Boychuk Date: Mon, 30 Oct 2023 23:10:59 +0100 Subject: [PATCH] [CST-12042] improved code --- .../PrimaryBitstreamAddPatchOperation.java | 27 +++++++++---------- ...PrimaryBitstreamReplacePatchOperation.java | 16 +++++------ .../app/rest/submit/step/UploadStep.java | 2 +- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/factory/impl/PrimaryBitstreamAddPatchOperation.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/factory/impl/PrimaryBitstreamAddPatchOperation.java index 0e23349d76d0..5653678a502a 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/factory/impl/PrimaryBitstreamAddPatchOperation.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/factory/impl/PrimaryBitstreamAddPatchOperation.java @@ -11,6 +11,7 @@ import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.UUID; import javax.servlet.http.HttpServletRequest; @@ -39,27 +40,25 @@ void add(Context context, HttpServletRequest currentRequest, InProgressSubmissio Item item = source.getItem(); UUID primaryUUID = parseValue(value); List bundles = itemService.getBundles(item, CONTENT_BUNDLE_NAME); - Bundle currentPrimaryBundle = bundles.stream() - .filter(bundle -> Objects.nonNull(bundle.getPrimaryBitstream())) - .findFirst() - .orElse(null); + Optional currentPrimaryBundle = bundles.stream() + .filter(bundle -> Objects.nonNull(bundle.getPrimaryBitstream())) + .findFirst(); - Bitstream primaryBitstreamToAdd = null; + Optional primaryBitstreamToAdd = null; for (Bundle bundle : bundles) { - primaryBitstreamToAdd = bundle.getBitstreams().stream() - .filter(b -> b.getID().equals(primaryUUID)) - .findFirst() - .orElse(null); - if (Objects.nonNull(primaryBitstreamToAdd)) { - if (Objects.nonNull(currentPrimaryBundle)) { - currentPrimaryBundle.setPrimaryBitstreamID(null); + primaryBitstreamToAdd = bundle.getBitstreams().stream() + .filter(b -> b.getID().equals(primaryUUID)) + .findFirst(); + if (primaryBitstreamToAdd.isPresent()) { + if (currentPrimaryBundle.isPresent()) { + currentPrimaryBundle.get().setPrimaryBitstreamID(null); } - bundle.setPrimaryBitstreamID(primaryBitstreamToAdd); + bundle.setPrimaryBitstreamID(primaryBitstreamToAdd.get()); break; } } - if (Objects.isNull(primaryBitstreamToAdd)) { + if (primaryBitstreamToAdd.isEmpty()) { throw new UnprocessableEntityException("The provided uuid: " + primaryUUID + " of bitstream to set as primary doesn't match any bitstream!"); } diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/factory/impl/PrimaryBitstreamReplacePatchOperation.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/factory/impl/PrimaryBitstreamReplacePatchOperation.java index 6572801242d6..abd59101f448 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/factory/impl/PrimaryBitstreamReplacePatchOperation.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/factory/impl/PrimaryBitstreamReplacePatchOperation.java @@ -11,6 +11,7 @@ import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.UUID; import javax.servlet.http.HttpServletRequest; @@ -46,20 +47,19 @@ void replace(Context context, HttpServletRequest request, InProgressSubmission s .findFirst() .orElseThrow(() -> new UnprocessableEntityException(EX_MESSAGE)); - Bitstream primaryBitstream = null; + Optional primaryBitstream = null; for (Bundle bundle : bundles) { - primaryBitstream = bundle.getBitstreams().stream() - .filter(b -> b.getID().equals(primaryUUID)) - .findFirst() - .orElse(null); - if (Objects.nonNull(primaryBitstream)) { + primaryBitstream = bundle.getBitstreams().stream() + .filter(b -> b.getID().equals(primaryUUID)) + .findFirst(); + if (primaryBitstream.isPresent()) { currentPrimaryBundle.setPrimaryBitstreamID(null); - bundle.setPrimaryBitstreamID(primaryBitstream); + bundle.setPrimaryBitstreamID(primaryBitstream.get()); break; } } - if (Objects.isNull(primaryBitstream)) { + if (primaryBitstream.isEmpty()) { throw new UnprocessableEntityException("The provided uuid: " + primaryUUID + " of bitstream to set as primary doesn't match any bitstream!"); } diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/UploadStep.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/UploadStep.java index 57913160d31b..1dea6bbeeb7a 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/UploadStep.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/step/UploadStep.java @@ -73,7 +73,7 @@ public DataUpload getData(SubmissionService submissionService, InProgressSubmiss public void doPatchProcessing(Context context, HttpServletRequest currentRequest, InProgressSubmission source, Operation op, SubmissionStepConfig stepConf) throws Exception { - String instance = StringUtils.EMPTY; + String instance = null; if ("remove".equals(op.getOp())) { if (op.getPath().contains(UPLOAD_STEP_METADATA_PATH)) { instance = UPLOAD_STEP_METADATA_OPERATION_ENTRY;