From 18ccb3ca0c5ecff8e16e739bf8de67dcda9abb1c Mon Sep 17 00:00:00 2001 From: Aldo Mihasi Date: Mon, 27 Mar 2023 15:24:50 +0300 Subject: [PATCH] changes in dmp deposit model --- .../main/java/eu/eudat/controllers/DMPs.java | 16 ++++++- .../managers/DataManagementPlanManager.java | 44 ++++++++----------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPs.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPs.java index 3e421e8fc..a2a1926f6 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPs.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPs.java @@ -182,7 +182,21 @@ public class DMPs extends BaseController { public @ResponseBody ResponseEntity getRDAJsonDocument(@PathVariable String id, @ClaimedAuthorities(claims = {Authorities.ADMIN, Authorities.MANAGER, Authorities.USER, Authorities.ANONYMOUS}) Principal principal) { 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) { return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseItem<>().message(e.getMessage()).status(ApiMessageCode.ERROR_MESSAGE)); } diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java index b54ad96d6..96e12f15d 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java @@ -1615,13 +1615,12 @@ public class DataManagementPlanManager { return fileEnvelope; } - public ResponseEntity 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)); if (!dmp.isPublic() && dmp.getUsers().stream().noneMatch(userInfo -> userInfo.getUser().getId() == principal.getId())) throw new UnauthorisedException(); -// RDAExportModel rdaExportModel = new RDAExportModel().fromDataModel(dmp, datasetManager, principal); - final Boolean isFinalized = dmp.getStatus() == DMP.DMPStatus.FINALISED.getValue(); - final Boolean isPublic = dmp.isPublic(); + final boolean isFinalized = dmp.getStatus() == DMP.DMPStatus.FINALISED.getValue(); + final boolean isPublic = dmp.isPublic(); dmp.setDataset(dmp.getDataset().stream() .filter(dataset -> dataset.getStatus() != Dataset.Status.DELETED.getValue() && dataset.getStatus() != Dataset.Status.CANCELED.getValue()) @@ -1629,15 +1628,12 @@ public class DataManagementPlanManager { .collect(Collectors.toSet())); String result = rdaManager.convertToRDA(dmp); - /*ObjectMapper mapper = new ObjectMapper(); - mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);*/ - String fileName = "DMP_" + dmp.getGrant().getLabel() + "_" + dmp.getVersion();//dmp.getLabel(); + String fileName = "DMP_" + dmp.getGrant().getLabel() + "_" + dmp.getVersion(); fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", "").replace(" ", "_").replace(",", "_"); String uuid = UUID.randomUUID().toString(); File file = new File(this.environment.getProperty("temp.temp") + uuid + ".json"); OutputStream output = new FileOutputStream(file); try { -// mapper.writeValue(file, rdaExportModel); output.write(result.getBytes()); output.flush(); output.close(); @@ -1645,18 +1641,10 @@ public class DataManagementPlanManager { logger.error(e.getMessage(), e); } - InputStream resource = new FileInputStream(file); - HttpHeaders responseHeaders = new HttpHeaders(); - responseHeaders.setContentLength(file.length()); - responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); - 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); + FileEnvelope rdaJsonDocument = new FileEnvelope(); + rdaJsonDocument.setFilename(fileName + ".json"); + rdaJsonDocument.setFile(file); + return rdaJsonDocument; } public ResponseEntity 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); String name = file.getFilename().substring(0, file.getFilename().length() - 5).replaceAll("[^a-zA-Z0-9_+ ]", "").replace(" ", "_").replace(",", "_"); File pdfFile = PDFUtils.convertToPDF(file, environment); - String fileName = name + ".pdf"; - ResponseEntity jsonFile; + eu.eudat.depositinterface.models.FileEnvelope pdfEnvelope = new eu.eudat.depositinterface.models.FileEnvelope(); + pdfEnvelope.setFile(pdfFile); + pdfEnvelope.setFilename(name + ".pdf"); + eu.eudat.depositinterface.models.FileEnvelope rdaJsonFile = new eu.eudat.depositinterface.models.FileEnvelope(); try { - jsonFile = getRDAJsonDocument(depositRequest.getDmpId(), principal); + FileEnvelope rdaJsonDocument = getRDAJsonDocument(depositRequest.getDmpId(), principal); + rdaJsonFile.setFile(rdaJsonDocument.getFile()); + rdaJsonFile.setFilename(rdaJsonDocument.getFilename()); } catch (Exception e) { - throw e; + logger.error(e.getMessage(), e); } String previousDOI = this.getPreviousDOI(dmp.getGroupId(), dmp.getId(), depositRequest.getRepositoryId()); 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 repo = this.repositoriesDeposit.stream().filter(x -> x.getConfiguration().getRepositoryId().equals(depositRequest.getRepositoryId())).findFirst(); String finalDoi = repo.map(r -> { @@ -2157,10 +2149,12 @@ public class DataManagementPlanManager { if(supportingFilesZip != null) { Files.deleteIfExists(supportingFilesZip.toPath()); } + Files.deleteIfExists(rdaJsonFile.getFile().toPath()); Files.deleteIfExists(pdfFile.toPath()); Files.deleteIfExists(file.getFile().toPath()); return doiModel; + } private File createSupportingFilesZip(DMP dmp) throws IOException {