Skip to content

Commit

Permalink
original-metadata.zip
Browse files Browse the repository at this point in the history
  • Loading branch information
janvanmansum committed Dec 14, 2024
1 parent d33d775 commit e7055cc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public interface DansBagMappingService {
/**
* Maps file information in the DANS bag to edit actions for the files in the dataset. The edit actions are used to update the files in the dataset.
*
* @param dansDeposit the DANS deposit
* @param updatesDataset the DOI of the dataset that needs to be updated, or null if the deposit is to create a new dataset
* @param dansDeposit the DANS deposit
* @param updatesDataset the DOI of the dataset that needs to be updated, or null if the deposit is to create a new dataset
* @return the edit actions for the files in the dataset
*/
EditFiles getEditFilesFromDansDeposit(DansBagDeposit dansDeposit, String updatesDataset);
Expand All @@ -95,13 +95,4 @@ public interface DansBagMappingService {
* @return the update action for the dataset
*/
UpdateAction getUpdateActionFromDansDeposit(DansBagDeposit dansDeposit);

/**
* Packages the original metadata of the DANS bag into a ZIP file and returns the local path to the ZIP file.
*
* @param dansDeposit the DANS deposit
* @return the local path to the ZIP file
* @throws IOException if there was an error reading the deposit or writing the ZIP file
*/
String packageOriginalMetadata(DansBagDeposit dansDeposit) throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@
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.dataset.UpdateType;
import nl.knaw.dans.lib.dataverse.model.file.Checksum;
import nl.knaw.dans.lib.dataverse.model.file.DataFile;
import nl.knaw.dans.lib.dataverse.model.file.FileMeta;
import nl.knaw.dans.lib.dataverse.model.user.AuthenticatedUser;
import nl.knaw.dans.lib.util.ZipUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -68,6 +73,7 @@

@Slf4j
public class DansBagMappingServiceImpl implements DansBagMappingService {
private static final String ORIGINAL_METADATA_ZIP = "original-metadata.zip";
private static final DateTimeFormatter yyyymmddPattern = DateTimeFormat.forPattern("YYYY-MM-dd");

private final DepositToDvDatasetMetadataMapper depositToDvDatasetMetadataMapper;
Expand Down Expand Up @@ -186,6 +192,14 @@ public Dataset getDatasetMetadataFromDansDeposit(DansBagDeposit dansDeposit, Dat
@Override
public EditFiles getEditFilesFromDansDeposit(DansBagDeposit dansDeposit, String updatesDataset) {
var files = getFileInfo(dansDeposit);
if (!depositToDvDatasetMetadataMapper.isMigration()) {
try {
files.put(Path.of(ORIGINAL_METADATA_ZIP), createOriginalMetadataFileInfo(dansDeposit));
}
catch (IOException e) {
throw new RuntimeException("Error creating original metadata zip", e);
}
}
var dateAvailable = getDateAvailable(dansDeposit);
if (updatesDataset == null) {
return new EditFilesComposer(files, dateAvailable, fileExclusionPattern, embargoExclusions).composeEditFiles();
Expand All @@ -195,6 +209,24 @@ public EditFiles getEditFilesFromDansDeposit(DansBagDeposit dansDeposit, String
}
}

private FileInfo createOriginalMetadataFileInfo(DansBagDeposit dansDeposit) throws IOException {
var metadataDir = dansDeposit.getBagDir().resolve("metadata");
var zipFile = dansDeposit.getBagDir().resolve("data/" + ORIGINAL_METADATA_ZIP);
ZipUtil.zipDirectory(metadataDir, zipFile, false);
var checksum = DigestUtils.sha1Hex(new FileInputStream(zipFile.toFile()));
var fileMeta = new FileMeta();
fileMeta.setLabel(ORIGINAL_METADATA_ZIP);
var dataFile = new DataFile();
dataFile.setFilename(ORIGINAL_METADATA_ZIP);
var dfChecksum = new Checksum();
dfChecksum.setType("SHA-1");
dfChecksum.setValue(checksum);
dataFile.setChecksum(dfChecksum);
fileMeta.setDataFile(dataFile);
fileMeta.setRestrict(false);
return new FileInfo(zipFile, checksum, false, fileMeta);
}

@Override
public EditPermissions getEditPermissionsFromDansDeposit(DansBagDeposit dansDeposit, String updatesDataset) {
if (updatesDataset == null) {
Expand Down Expand Up @@ -233,18 +265,6 @@ public UpdateAction getUpdateActionFromDansDeposit(DansBagDeposit dansDeposit) {
}
}

@Override
public String packageOriginalMetadata(DansBagDeposit dansDeposit) throws IOException {
if (!depositToDvDatasetMetadataMapper.isMigration()) {
// Zip the contents of the metadata directory of the bag
var metadataDir = dansDeposit.getBagDir().resolve("metadata");
var zipFile = dansDeposit.getBagDir().resolve("data/original-metadata.zip");
ZipUtil.zipDirectory(metadataDir, zipFile, false);
return zipFile.toString();
}
return null;
}

// todo: move to mapping package
private Map<Path, FileInfo> getFileInfo(DansBagDeposit dansDeposit) {
var files = FileElement.pathToFileInfo(dansDeposit, false); // TODO: handle migration case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public class DansDepositConverter {

public void run() throws IOException {
deleteOldYamlFilesIfPresent();

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

var init = mappingService.getInitFromDansDeposit(dansDeposit, updatesDataset != null);
if (init != null) {
yamlService.writeYaml(new InitRoot(init), dansDeposit.getBagDir().resolve("init.yml"));
Expand Down

0 comments on commit e7055cc

Please sign in to comment.