From dbb35a643486b3f0f85d2914b5da6cc561ce780a Mon Sep 17 00:00:00 2001 From: Jan van Mansum Date: Tue, 10 Dec 2024 15:18:39 +0100 Subject: [PATCH] Added embargo for subsequent versions. --- .../dansbag/DansBagMappingServiceImpl.java | 1 + .../core/dansbag/EditFilesComposer.java | 31 ++++++++++++------- .../dansbag/EditFilesComposerForUpdate.java | 6 ++-- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/DansBagMappingServiceImpl.java b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/DansBagMappingServiceImpl.java index 6642c86..5d9cb17 100644 --- a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/DansBagMappingServiceImpl.java +++ b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/DansBagMappingServiceImpl.java @@ -121,6 +121,7 @@ else if (depositToDvDatasetMetadataMapper.isMigration()) { @Override public Dataset getDatasetMetadataFromDansDeposit(DansBagDeposit dansDeposit, DatasetVersion currentMetadata) { + // TODO: rename to DatasetComposer en push the terms stuff into it as well. var dataset = depositToDvDatasetMetadataMapper.toDataverseDataset( dansDeposit.getDdm(), dansDeposit.getOtherDoiId(), diff --git a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/EditFilesComposer.java b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/EditFilesComposer.java index 8421ecb..b692be8 100644 --- a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/EditFilesComposer.java +++ b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/EditFilesComposer.java @@ -31,6 +31,7 @@ import java.util.Date; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -65,13 +66,8 @@ public EditFiles composeEditFiles() { editFiles.setAddUnrestrictedFiles(getUnrestrictedFilesToAdd(pathFileInfoMap)); editFiles.setUpdateFileMetas(getUpdatedFileMetas(pathFileInfoMap)); - var filePathsToEmbargo = getEmbargoedFiles(pathFileInfoMap, dateAvailable); - if (!filePathsToEmbargo.isEmpty()) { - var addEmbargo = new AddEmbargo(); - addEmbargo.setDateAvailable(yyyymmddFormat.format(Date.from(dateAvailable))); - addEmbargo.setFilePaths(filePathsToEmbargo.stream().map(Path::toString).toList()); - editFiles.setAddEmbargoes(List.of(addEmbargo)); - } + addEmbargo(editFiles, pathFileInfoMap.keySet()); + return editFiles; } @@ -123,10 +119,23 @@ private List getUpdatedFileMetas(Map files) { .toList(); } - private List getEmbargoedFiles(Map files, Instant dateAvailable) { - var now = Instant.now(); - if (dateAvailable.isAfter(now)) { - return files.keySet().stream() + protected void addEmbargo(EditFiles editFiles, Set candidates) { + if (dateAvailable.isAfter(Instant.now())) { + var filesToEmbargo = candidates.stream() + .filter(f -> !embargoExclusions.contains(f.toString())).toList(); + + if (!filesToEmbargo.isEmpty()) { + var addEmbargo = new AddEmbargo(); + addEmbargo.setDateAvailable(yyyymmddFormat.format(Date.from(dateAvailable))); + addEmbargo.setFilePaths(filesToEmbargo.stream().map(Path::toString).toList()); + editFiles.setAddEmbargoes(List.of(addEmbargo)); + } + } + } + + private List getEmbargoedFiles(Set candidates, Instant dateAvailable) { + if (dateAvailable.isAfter(Instant.now())) { + return candidates.stream() .filter(f -> !embargoExclusions.contains(f.toString())).toList(); } else { diff --git a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/EditFilesComposerForUpdate.java b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/EditFilesComposerForUpdate.java index c5ce787..34d5e3b 100644 --- a/src/main/java/nl/knaw/dans/dvingest/core/dansbag/EditFilesComposerForUpdate.java +++ b/src/main/java/nl/knaw/dans/dvingest/core/dansbag/EditFilesComposerForUpdate.java @@ -19,6 +19,7 @@ import nl.knaw.dans.dvingest.core.bagprocessor.FilesInDatasetCache; import nl.knaw.dans.dvingest.core.dansbag.deposit.FileInfo; import nl.knaw.dans.dvingest.core.service.DataverseService; +import nl.knaw.dans.dvingest.core.yaml.AddEmbargo; import nl.knaw.dans.dvingest.core.yaml.EditFiles; import nl.knaw.dans.dvingest.core.yaml.FromTo; import nl.knaw.dans.lib.dataverse.DataverseException; @@ -27,6 +28,7 @@ import java.io.IOException; import java.nio.file.Path; import java.time.Instant; +import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -144,11 +146,11 @@ public EditFiles composeEditFiles() { editFiles.setAddUnrestrictedFiles(pathsToAdd.stream() .filter(p -> !pathFileInfoMap.get(p).getMetadata().getRestricted()).toList().stream().map(Path::toString).toList()); - // todo: embargoes - + addEmbargo(editFiles, SetUtils.union(pathsToAdd, filesToReplace)); return editFiles; } + private Set getFilesToReplace(Map pathToFileInfo, Map fileReplacementCandidates) { var intersection = SetUtils.intersection(pathToFileInfo.keySet(), fileReplacementCandidates.keySet());