deposit datasets' attached files in zip format

This commit is contained in:
Bernaldo Mihasi 2022-11-30 09:43:03 +02:00
parent ed79d27cdc
commit 679354f699
7 changed files with 63 additions and 11 deletions

View File

@ -3,7 +3,9 @@ package eu.eudat.data.dao.entities;
import eu.eudat.data.dao.DatabaseAccessLayer;
import eu.eudat.data.entities.FileUpload;
import java.util.List;
import java.util.UUID;
public interface FileUploadDao extends DatabaseAccessLayer<FileUpload, UUID> {
List<FileUpload> getFileUploads(UUID entityId);
}

View File

@ -7,6 +7,7 @@ import eu.eudat.queryable.QueryableList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
@ -33,6 +34,11 @@ public class FileUploadDaoImpl extends DatabaseAccess<FileUpload> implements Fil
return getDatabaseService().getQueryable(FileUpload.class).where((builder, root) -> builder.equal(root.get("id"), id)).getSingle();
}
@Override
public List<FileUpload> getFileUploads(UUID entityId) {
return this.getDatabaseService().getQueryable(FileUpload.class).where((builder, root) -> builder.equal(root.get("entityId"), entityId)).toList();
}
@Override
public FileUpload find(UUID id, String hint) {
return null;

View File

@ -149,6 +149,8 @@ public class DataverseDeposit implements RepositoryDeposit {
}
this.uploadFile(jsonFileName, rdaJson, doi);
Files.deleteIfExists(rdaJson.toPath());
this.uploadFile(dmpDepositModel.getSupportingFilesZip().getName(), dmpDepositModel.getSupportingFilesZip(), doi);
}
private Map<String, Object> getDatasetIdentifier(String previousDOI) {

View File

@ -19,6 +19,7 @@ public class DMPDepositModel {
private File pdfFile;
private String pdfFileName;
private ResponseEntity<byte[]> rdaJson;
private File supportingFilesZip;
private String previousDOI;
private String extraProperties;
@ -106,6 +107,13 @@ public class DMPDepositModel {
this.rdaJson = rdaJson;
}
public File getSupportingFilesZip() {
return supportingFilesZip;
}
public void setSupportingFilesZip(File supportingFilesZip) {
this.supportingFilesZip = supportingFilesZip;
}
public String getPreviousDOI() {
return previousDOI;
}

View File

@ -95,6 +95,8 @@ import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Component
public class DataManagementPlanManager {
@ -2090,7 +2092,9 @@ public class DataManagementPlanManager {
}
String previousDOI = this.getPreviousDOI(dmp.getGroupId(), dmp.getId(), "Zenodo");
DMPDepositModel dmpDepositModel = DMPToDepositMapper.fromDMP(dmp, pdfFile, fileName, jsonFile, previousDOI);
File supportingFilesZip = this.createSupportingFilesZip(dmp);
DMPDepositModel dmpDepositModel = DMPToDepositMapper.fromDMP(dmp, pdfFile, fileName, jsonFile, supportingFilesZip, previousDOI);
String zenodoToken = "";
try {
@ -2122,6 +2126,7 @@ public class DataManagementPlanManager {
}
}
}
Files.deleteIfExists(supportingFilesZip.toPath());
Files.deleteIfExists(file.getFile().toPath());
return finalDoi;
@ -2148,16 +2153,9 @@ public class DataManagementPlanManager {
}
String previousDOI = this.getPreviousDOI(dmp.getGroupId(), dmp.getId(), depositRequest.getRepositoryId());
DMPDepositModel dmpDepositModel = DMPToDepositMapper.fromDMP(dmp, pdfFile, fileName, jsonFile, previousDOI);
File supportingFilesZip = this.createSupportingFilesZip(dmp);
// String zenodoToken = "";
// try {
// if (this.userManager.isDOITokenValid(principal)) {
// zenodoToken = principal.getZenodoToken();
// }
// } catch (NonValidTokenException e) {
// zenodoToken = this.environment.getProperty("zenodo.access_token");
// }
DMPDepositModel dmpDepositModel = DMPToDepositMapper.fromDMP(dmp, pdfFile, fileName, jsonFile, supportingFilesZip, previousDOI);
Optional<RepositoryDeposit> repo = this.repositoriesDeposit.stream().filter(x -> x.getConfiguration().getRepositoryId().equals(depositRequest.getRepositoryId())).findFirst();
String finalDoi = repo.map(r -> {
@ -2188,11 +2186,37 @@ public class DataManagementPlanManager {
doiModel = new Doi().fromDataModel(doiEntity);
}
Files.deleteIfExists(supportingFilesZip.toPath());
Files.deleteIfExists(file.getFile().toPath());
return doiModel;
}
private File createSupportingFilesZip(DMP dmp) throws IOException {
FileOutputStream fout = new FileOutputStream(this.environment.getProperty("temp.temp") + "supportingFiles.zip");
ZipOutputStream zout = new ZipOutputStream(fout);
//List<File> supportingFiles = new ArrayList<>();
Set<Dataset> datasets = dmp.getDataset();
for (Dataset dataset : datasets) {
List<FileUpload> files = this.apiContext.getOperationsContext().getDatabaseRepository().getFileUploadDao().getFileUploads(dataset.getId());
for (FileUpload f : files) {
if(!f.getIsDeleted()){
File exportFile = new File(this.environment.getProperty("file.storage") + f.getId());
String filename = f.getName().replace(" ", "_").replace(",", "_");
byte[] content = Files.readAllBytes(exportFile.toPath());
ZipEntry ze = new ZipEntry(filename);
zout.putNextEntry(ze);
zout.write(content, 0, content.length);
zout.closeEntry();
}
}
}
zout.close();
return new File(this.environment.getProperty("temp.temp") + "supportingFiles.zip");
}
/*
* Misc
* */

View File

@ -9,7 +9,7 @@ import java.util.stream.Collectors;
public class DMPToDepositMapper {
public static DMPDepositModel fromDMP(DMP entity, File pdfFile, String fileName, ResponseEntity<byte[]> jsonFile, String previousDOI) {
public static DMPDepositModel fromDMP(DMP entity, File pdfFile, String fileName, ResponseEntity<byte[]> jsonFile, File supportingFilesZip, String previousDOI) {
DMPDepositModel deposit = new DMPDepositModel();
deposit.setId(entity.getId());
deposit.setVersion(entity.getVersion());
@ -24,6 +24,7 @@ public class DMPToDepositMapper {
deposit.setPdfFile(pdfFile);
deposit.setPdfFileName(fileName);
deposit.setRdaJson(jsonFile);
deposit.setSupportingFilesZip(supportingFilesZip);
deposit.setPreviousDOI(previousDOI);
deposit.setExtraProperties(entity.getExtraProperties());

View File

@ -141,6 +141,15 @@ public class ZenodoDeposit implements RepositoryDeposit {
restTemplate.put(addFileUrl, addFileMapRequest);
Files.deleteIfExists(tempJsonFile.toPath());
if(dmpDepositModel.getSupportingFilesZip() != null) {
File supportinFilesZip = dmpDepositModel.getSupportingFilesZip();
String supportinFilesZipName = dmpDepositModel.getSupportingFilesZip().getName();
fileSystemResource = new FileSystemResource(supportinFilesZip);
addFileMapRequest = new HttpEntity<>(fileSystemResource, null);
addFileUrl = links.get("bucket") + "/" + supportinFilesZipName + "?access_token=" + zenodoToken;
restTemplate.put(addFileUrl, addFileMapRequest);
}
// Third post call to Zenodo to publish the entry and return the DOI.
publishUrl = links.get("publish") + "?access_token=" + zenodoToken;