changes in dmp deposit model

This commit is contained in:
Bernaldo Mihasi 2023-03-27 15:24:50 +03:00
parent a90f21b6aa
commit 18ccb3ca0c
2 changed files with 34 additions and 26 deletions

View File

@ -182,7 +182,21 @@ public class DMPs extends BaseController {
public @ResponseBody public @ResponseBody
ResponseEntity getRDAJsonDocument(@PathVariable String id, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) { ResponseEntity getRDAJsonDocument(@PathVariable String id, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) {
try { try {
return this.dataManagementPlanManager.getRDAJsonDocument(id, principal); FileEnvelope rdaJsonDocument = this.dataManagementPlanManager.getRDAJsonDocument(id, principal);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentLength(rdaJsonDocument.getFile().length());
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
responseHeaders.set("Content-Disposition", "attachment;filename=" + rdaJsonDocument.getFilename());
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
InputStream resource = new FileInputStream(rdaJsonDocument.getFile());
byte[] content = org.apache.poi.util.IOUtils.toByteArray(resource);
resource.close();
Files.deleteIfExists(rdaJsonDocument.getFile().toPath());
return new ResponseEntity<>(content, responseHeaders, HttpStatus.OK);
} catch (Exception e) { } catch (Exception e) {
return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseItem<>().message(e.getMessage()).status(ApiMessageCode.ERROR_MESSAGE)); return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseItem<>().message(e.getMessage()).status(ApiMessageCode.ERROR_MESSAGE));
} }

View File

@ -1615,13 +1615,12 @@ public class DataManagementPlanManager {
return fileEnvelope; return fileEnvelope;
} }
public ResponseEntity<byte[]> getRDAJsonDocument(String id, Principal principal) throws Exception { public FileEnvelope getRDAJsonDocument(String id, Principal principal) throws Exception {
eu.eudat.data.entities.DMP dmp = databaseRepository.getDmpDao().find(UUID.fromString(id)); eu.eudat.data.entities.DMP dmp = databaseRepository.getDmpDao().find(UUID.fromString(id));
if (!dmp.isPublic() && dmp.getUsers().stream().noneMatch(userInfo -> userInfo.getUser().getId() == principal.getId())) if (!dmp.isPublic() && dmp.getUsers().stream().noneMatch(userInfo -> userInfo.getUser().getId() == principal.getId()))
throw new UnauthorisedException(); throw new UnauthorisedException();
// RDAExportModel rdaExportModel = new RDAExportModel().fromDataModel(dmp, datasetManager, principal); final boolean isFinalized = dmp.getStatus() == DMP.DMPStatus.FINALISED.getValue();
final Boolean isFinalized = dmp.getStatus() == DMP.DMPStatus.FINALISED.getValue(); final boolean isPublic = dmp.isPublic();
final Boolean isPublic = dmp.isPublic();
dmp.setDataset(dmp.getDataset().stream() dmp.setDataset(dmp.getDataset().stream()
.filter(dataset -> dataset.getStatus() != Dataset.Status.DELETED.getValue() && .filter(dataset -> dataset.getStatus() != Dataset.Status.DELETED.getValue() &&
dataset.getStatus() != Dataset.Status.CANCELED.getValue()) dataset.getStatus() != Dataset.Status.CANCELED.getValue())
@ -1629,15 +1628,12 @@ public class DataManagementPlanManager {
.collect(Collectors.toSet())); .collect(Collectors.toSet()));
String result = rdaManager.convertToRDA(dmp); String result = rdaManager.convertToRDA(dmp);
/*ObjectMapper mapper = new ObjectMapper(); String fileName = "DMP_" + dmp.getGrant().getLabel() + "_" + dmp.getVersion();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);*/
String fileName = "DMP_" + dmp.getGrant().getLabel() + "_" + dmp.getVersion();//dmp.getLabel();
fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", "").replace(" ", "_").replace(",", "_"); fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", "").replace(" ", "_").replace(",", "_");
String uuid = UUID.randomUUID().toString(); String uuid = UUID.randomUUID().toString();
File file = new File(this.environment.getProperty("temp.temp") + uuid + ".json"); File file = new File(this.environment.getProperty("temp.temp") + uuid + ".json");
OutputStream output = new FileOutputStream(file); OutputStream output = new FileOutputStream(file);
try { try {
// mapper.writeValue(file, rdaExportModel);
output.write(result.getBytes()); output.write(result.getBytes());
output.flush(); output.flush();
output.close(); output.close();
@ -1645,18 +1641,10 @@ public class DataManagementPlanManager {
logger.error(e.getMessage(), e); logger.error(e.getMessage(), e);
} }
InputStream resource = new FileInputStream(file); FileEnvelope rdaJsonDocument = new FileEnvelope();
HttpHeaders responseHeaders = new HttpHeaders(); rdaJsonDocument.setFilename(fileName + ".json");
responseHeaders.setContentLength(file.length()); rdaJsonDocument.setFile(file);
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); return rdaJsonDocument;
responseHeaders.set("Content-Disposition", "attachment;filename=" + fileName + ".json");
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
byte[] content = org.apache.poi.util.IOUtils.toByteArray(resource);
resource.close();
Files.deleteIfExists(file.toPath());
return new ResponseEntity<>(content, responseHeaders, HttpStatus.OK);
} }
public ResponseEntity<byte[]> getDocument(String id, String contentType, Principal principal, ConfigLoader configLoader) throws InstantiationException, IllegalAccessException, IOException { public ResponseEntity<byte[]> getDocument(String id, String contentType, Principal principal, ConfigLoader configLoader) throws InstantiationException, IllegalAccessException, IOException {
@ -2112,18 +2100,22 @@ public class DataManagementPlanManager {
FileEnvelope file = getWordDocument(depositRequest.getDmpId(), principal, configLoader); FileEnvelope file = getWordDocument(depositRequest.getDmpId(), principal, configLoader);
String name = file.getFilename().substring(0, file.getFilename().length() - 5).replaceAll("[^a-zA-Z0-9_+ ]", "").replace(" ", "_").replace(",", "_"); String name = file.getFilename().substring(0, file.getFilename().length() - 5).replaceAll("[^a-zA-Z0-9_+ ]", "").replace(" ", "_").replace(",", "_");
File pdfFile = PDFUtils.convertToPDF(file, environment); File pdfFile = PDFUtils.convertToPDF(file, environment);
String fileName = name + ".pdf"; eu.eudat.depositinterface.models.FileEnvelope pdfEnvelope = new eu.eudat.depositinterface.models.FileEnvelope();
ResponseEntity<byte[]> jsonFile; pdfEnvelope.setFile(pdfFile);
pdfEnvelope.setFilename(name + ".pdf");
eu.eudat.depositinterface.models.FileEnvelope rdaJsonFile = new eu.eudat.depositinterface.models.FileEnvelope();
try { try {
jsonFile = getRDAJsonDocument(depositRequest.getDmpId(), principal); FileEnvelope rdaJsonDocument = getRDAJsonDocument(depositRequest.getDmpId(), principal);
rdaJsonFile.setFile(rdaJsonDocument.getFile());
rdaJsonFile.setFilename(rdaJsonDocument.getFilename());
} catch (Exception e) { } catch (Exception e) {
throw e; logger.error(e.getMessage(), e);
} }
String previousDOI = this.getPreviousDOI(dmp.getGroupId(), dmp.getId(), depositRequest.getRepositoryId()); String previousDOI = this.getPreviousDOI(dmp.getGroupId(), dmp.getId(), depositRequest.getRepositoryId());
File supportingFilesZip = this.createSupportingFilesZip(dmp); File supportingFilesZip = this.createSupportingFilesZip(dmp);
DMPDepositModel dmpDepositModel = DMPToDepositMapper.fromDMP(dmp, pdfFile, fileName, jsonFile, supportingFilesZip, previousDOI); DMPDepositModel dmpDepositModel = DMPToDepositMapper.fromDMP(dmp, pdfEnvelope, rdaJsonFile, supportingFilesZip, previousDOI);
Optional<RepositoryDeposit> repo = this.repositoriesDeposit.stream().filter(x -> x.getConfiguration().getRepositoryId().equals(depositRequest.getRepositoryId())).findFirst(); Optional<RepositoryDeposit> repo = this.repositoriesDeposit.stream().filter(x -> x.getConfiguration().getRepositoryId().equals(depositRequest.getRepositoryId())).findFirst();
String finalDoi = repo.map(r -> { String finalDoi = repo.map(r -> {
@ -2157,10 +2149,12 @@ public class DataManagementPlanManager {
if(supportingFilesZip != null) { if(supportingFilesZip != null) {
Files.deleteIfExists(supportingFilesZip.toPath()); Files.deleteIfExists(supportingFilesZip.toPath());
} }
Files.deleteIfExists(rdaJsonFile.getFile().toPath());
Files.deleteIfExists(pdfFile.toPath()); Files.deleteIfExists(pdfFile.toPath());
Files.deleteIfExists(file.getFile().toPath()); Files.deleteIfExists(file.getFile().toPath());
return doiModel; return doiModel;
} }
private File createSupportingFilesZip(DMP dmp) throws IOException { private File createSupportingFilesZip(DMP dmp) throws IOException {