Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remaining update logic #6

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<dependency>
<groupId>nl.knaw.dans</groupId>
<artifactId>dans-dataverse-client-lib</artifactId>
<version>1.0.2</version>
<version>1.0.3-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>nl.knaw.dans</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import nl.knaw.dans.dvingest.core.yaml.EditPermissions;
import nl.knaw.dans.lib.dataverse.DataverseException;
import nl.knaw.dans.lib.dataverse.model.dataset.Dataset;
import nl.knaw.dans.lib.dataverse.model.dataset.DatasetVersion;

import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -30,7 +31,7 @@ public interface DansBagMappingService {

DansBagDeposit readDansDeposit(Path depositDir) throws InvalidDepositException;

Dataset getDatasetMetadataFromDansDeposit(DansBagDeposit dansDeposit);
Dataset getDatasetMetadataFromDansDeposit(DansBagDeposit dansDeposit, DatasetVersion currentMetadata);

EditFiles getEditFilesFromDansDeposit(DansBagDeposit dansDeposit, String updatesDataset);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import nl.knaw.dans.lib.dataverse.DataverseException;
import nl.knaw.dans.lib.dataverse.model.RoleAssignment;
import nl.knaw.dans.lib.dataverse.model.dataset.Dataset;
import nl.knaw.dans.lib.dataverse.model.dataset.DatasetVersion;
import nl.knaw.dans.lib.dataverse.model.user.AuthenticatedUser;
import nl.knaw.dans.lib.util.ZipUtil;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -88,37 +89,39 @@ public String getUpdatesDataset(Path depositDir) throws IOException, DataverseEx
.findFirst().orElseThrow(() -> new IllegalArgumentException("No bag found in deposit"));
var bagInfo = new LightweightBagInfo(bag.resolve("bag-info.txt"));
var isVersionOf = bagInfo.get("Is-Version-Of");
if (isVersionOf != null) {
log.debug("Found Is-Version-Of in bag-info.txt, so this is an update-deposit: {}", isVersionOf);
List<String> results;
if (dansDepositProperties.getSwordToken() != null) {
log.debug("Found sword token in deposit.properties, looking for target dataset by sword token");
results = dataverseService.findDoiByMetadataField("dansSwordToken", dansDepositProperties.getSwordToken());
}
else if (depositToDvDatasetMetadataMapper.isMigration()) {
log.debug("This is a migration deposit, looking for target dataset by dansBagIt. Note that this will only work for two versions of the same dataset");
results = dataverseService.findDoiByMetadataField("dansBagId", isVersionOf);

}
else {
throw new IllegalArgumentException("Update deposit should have either a sword token or be a migration deposit");
}
if (results.size() == 1) {
return results.get(0);
}
else {
throw new IllegalArgumentException("Update deposit should update exactly one dataset, found " + results.size());
}
}
else {

if (isVersionOf == null) {
log.debug("No Is-Version-Of found in bag-info.txt, so this is a deposit of a new dataset");
return null;
}

log.debug("Found Is-Version-Of in bag-info.txt, so this is an update-deposit: {}", isVersionOf);
List<String> results;

if (dansDepositProperties.getSwordToken() != null) {
log.debug("Found sword token in deposit.properties, looking for target dataset by sword token");
results = dataverseService.findDoiByMetadataField("dansSwordToken", dansDepositProperties.getSwordToken());
}
else if (depositToDvDatasetMetadataMapper.isMigration()) {
log.debug("This is a migration deposit, looking for target dataset by dansBagId");
results = dataverseService.findDoiByMetadataField("dansBagId", isVersionOf);
}
else {
throw new IllegalArgumentException("Update deposit should have either a sword token or be a migration deposit");
}

if (results.size() == 1) {
return results.get(0);
}
else {
throw new IllegalArgumentException("Update deposit should update exactly one dataset, found " + results.size());
}
}
}

@Override
public Dataset getDatasetMetadataFromDansDeposit(DansBagDeposit dansDeposit) {
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(),
Expand All @@ -130,10 +133,15 @@ public Dataset getDatasetMetadataFromDansDeposit(DansBagDeposit dansDeposit) {
dansDeposit.getHasOrganizationalIdentifier(),
dansDeposit.getHasOrganizationalIdentifierVersion());
var version = dataset.getDatasetVersion();
version.setFileAccessRequest(dansDeposit.allowAccessRequests());

// TODO: when processing an update-deposit, retrieve terms of access from the previous version
if (!dansDeposit.allowAccessRequests() && StringUtils.isBlank(version.getTermsOfAccess())) {
// Allow access requests only if it was not forbidden in previous versions
var fileAccessRequest = dansDeposit.allowAccessRequests() && (currentMetadata == null || currentMetadata.getFileAccessRequest());
version.setFileAccessRequest(fileAccessRequest);
if (StringUtils.isBlank(version.getTermsOfAccess()) && currentMetadata != null) {
version.setTermsOfAccess(currentMetadata.getTermsOfAccess());
}
if (!fileAccessRequest && StringUtils.isBlank(version.getTermsOfAccess())) {
// Dataverse requires terms of access to be set if file access requests are disabled, so set it to "N/a" if not explicitly provided
version.setTermsOfAccess("N/a");
}
version.setLicense(supportedLicenses.getLicenseFromDansDeposit(dansDeposit));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,23 @@
import nl.knaw.dans.dvingest.core.yaml.EditFilesRoot;
import nl.knaw.dans.dvingest.core.yaml.EditPermissionsRoot;
import nl.knaw.dans.dvingest.core.yaml.UpdateState;
import nl.knaw.dans.lib.dataverse.model.dataset.DatasetVersion;

import java.io.IOException;

@AllArgsConstructor
public class DansDepositConverter {
private final DansBagDeposit dansDeposit;
private final String updatesDataset;
private final DatasetVersion currentMetadata;
private final DansBagMappingService mappingService;
private final YamlService yamlService;

public void run() throws IOException {
// TODO: pass to getEditFilesFromDansDeposit so that update-deposit can register it as a replaced file
var originalMetadataPath = mappingService.packageOriginalMetadata(dansDeposit);

var dataset = mappingService.getDatasetMetadataFromDansDeposit(dansDeposit);
var dataset = mappingService.getDatasetMetadataFromDansDeposit(dansDeposit, currentMetadata);
yamlService.writeYaml(dataset, dansDeposit.getBagDir().resolve("dataset.yml"));

var editFiles = mappingService.getEditFilesFromDansDeposit(dansDeposit, updatesDataset);
Expand All @@ -48,5 +50,4 @@ public void run() throws IOException {
updateState.setAction("publish-major");
yamlService.writeYaml(updateState, dansDeposit.getBagDir().resolve("update-state.yml"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import nl.knaw.dans.dvingest.core.service.DataverseService;
import nl.knaw.dans.dvingest.core.service.YamlService;
import nl.knaw.dans.lib.dataverse.DataverseException;
import nl.knaw.dans.lib.dataverse.model.dataset.DatasetVersion;

import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -49,7 +50,8 @@ public class DansDepositSupport implements Deposit {

private DansBagDeposit dansDeposit;

public DansDepositSupport(DataverseIngestDeposit dataverseIngestDeposit, ValidateDansBagService validateDansBagService, DansBagMappingService dansBagMappingService, DataverseService dataverseService, YamlService yamlService) {
public DansDepositSupport(DataverseIngestDeposit dataverseIngestDeposit, ValidateDansBagService validateDansBagService, DansBagMappingService dansBagMappingService,
DataverseService dataverseService, YamlService yamlService) {
this.validateDansBagService = validateDansBagService;
this.ingestDataverseIngestDeposit = dataverseIngestDeposit;
this.dansBagMappingService = dansBagMappingService;
Expand All @@ -69,11 +71,13 @@ public boolean convertDansDepositIfNeeded() {
log.info("Converting deposit to Dataverse ingest metadata");
try {
var updatesDataset = dansBagMappingService.getUpdatesDataset(ingestDataverseIngestDeposit.getLocation());
DatasetVersion currentMetadata = null;
if (updatesDataset != null) {
ingestDataverseIngestDeposit.updateProperties(Map.of(UPDATES_DATASET_KEY, updatesDataset));
currentMetadata = dataverseService.getDatasetMetadata(updatesDataset);
}
dansDeposit = dansBagMappingService.readDansDeposit(ingestDataverseIngestDeposit.getLocation());
new DansDepositConverter(dansDeposit, updatesDataset, dansBagMappingService, yamlService).run();
new DansDepositConverter(dansDeposit, updatesDataset, currentMetadata, dansBagMappingService, yamlService).run();
log.info("Conversion successful");
return true;
}
Expand Down Expand Up @@ -119,7 +123,8 @@ public void onSuccess(@NonNull String pid, String message) {
"identifier.urn", nbn
)
);
} catch (IOException | DataverseException e) {
}
catch (IOException | DataverseException e) {
throw new RuntimeException("Error getting URN:NBN", e); // Cancelling the "success"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -123,10 +119,23 @@ private List<FileMeta> getUpdatedFileMetas(Map<Path, FileInfo> files) {
.toList();
}

private List<Path> getEmbargoedFiles(Map<Path, FileInfo> files, Instant dateAvailable) {
var now = Instant.now();
if (dateAvailable.isAfter(now)) {
return files.keySet().stream()
protected void addEmbargo(EditFiles editFiles, Set<Path> 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<Path> getEmbargoedFiles(Set<Path> candidates, Instant dateAvailable) {
if (dateAvailable.isAfter(Instant.now())) {
return candidates.stream()
.filter(f -> !embargoExclusions.contains(f.toString())).toList();
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Path> getFilesToReplace(Map<Path, FileInfo> pathToFileInfo, Map<Path, FileMeta> fileReplacementCandidates) {

var intersection = SetUtils.intersection(pathToFileInfo.keySet(), fileReplacementCandidates.keySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import static nl.knaw.dans.dvingest.core.dansbag.xml.XPathConstants.DDM_DCMI_METADATA;

// TODO: move to mapping package
public class SupportedLicenses {
private final Map<URI, License> supportedLicenses;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ public interface DataverseService {
void addEmbargo(String pid, Embargo embargo) throws IOException, DataverseException;

List<String> findDoiByMetadataField(String fieldName, String value) throws IOException, DataverseException;

DatasetVersion getDatasetMetadata(String pid) throws IOException, DataverseException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ public String getDatasetUrnNbn(String pid) throws IOException, DataverseExceptio
.findFirst().orElseThrow(() -> new IllegalStateException("No URN:NBN found in dataset"));
}

@Override
public DatasetVersion getDatasetMetadata(String pid) throws IOException, DataverseException {
return dataverseClient.dataset(pid).getVersion().getData();
}

// TODO: move this to dans-dataverse-client-lib; it is similar to awaitLockState.
public void waitForState(String datasetId, String expectedState) {
var numberOfTimesTried = 0;
Expand Down Expand Up @@ -239,8 +244,8 @@ public void waitForState(String datasetId, String expectedState) {
}

private String getDatasetState(String datasetId) throws IOException, DataverseException {
var version = dataverseClient.dataset(datasetId).getLatestVersion();
return version.getData().getLatestVersion().getVersionState();
var version = dataverseClient.dataset(datasetId).getVersion(Version.LATEST.toString(), true);
return version.getData().getVersionState();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,4 @@ public void getUpdatesDataset_finds_dataset_for_update_deposit() throws Exceptio
// When / Then
assertThat(mappingService.getUpdatesDataset(testDepositDir)).isEqualTo(doi);
}





}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

import static org.assertj.core.api.Assertions.assertThat;

/**
* Test class for {@link DansDepositConverter}. It uses the valid examples from the dd-dans-sword2-examples project.
*/
public class DansDepositConverterTest extends DansConversionFixture {

private final YamlService yamlService = new YamlServiceImpl();
Expand All @@ -44,7 +47,7 @@ public void run_converts_dans_sword_all_mappings_example_to_dataverse_ingest_dep
var deposit = dansBagDepositReader.readDeposit(depositDir);

// When
new DansDepositConverter(deposit, null, mappingService, yamlService).run();
new DansDepositConverter(deposit, null, null, mappingService, yamlService).run();

// Then
assertThat(deposit.getBagDir().resolve("dataset.yml")).exists();
Expand Down
Loading