From d0cb186ab2a3db34761e4b916b00251bc26dcb11 Mon Sep 17 00:00:00 2001 From: George Kalampokis Date: Tue, 21 Apr 2020 12:40:50 +0300 Subject: [PATCH] Fixed issue with Zenodo DOI generation with user token --- .../managers/DataManagementPlanManager.java | 75 ++++++++++++++----- 1 file changed, 56 insertions(+), 19 deletions(-) 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 ede74e290..5473eb0e8 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 @@ -1623,6 +1623,32 @@ public class DataManagementPlanManager { return doi; } + private String getUnpublishedDOI(String DOI, String token, Integer version) { + try { + RestTemplate restTemplate = new RestTemplate(); + HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); + headers.setContentType(MediaType.APPLICATION_JSON); + Map createResponse = null; + LinkedHashMap links = null; + LinkedHashMap metadata = null; + String listUrl = this.environment.getProperty("zenodo.url") + "deposit/depositions" + "?q=conceptdoi:\"" + DOI + "\"&access_token=" + token; + ResponseEntity listResponses = restTemplate.getForEntity(listUrl, Map[].class); + createResponse = listResponses.getBody()[0]; + metadata = (LinkedHashMap) createResponse.get("metadata"); + links = (LinkedHashMap) createResponse.get("links"); + + if (metadata.get("version").equals(version.toString())) { + return links.get("publish"); + } else { + return null; + } + }catch (Exception e) { + logger.warn(e.getMessage(), e); + return null; + } + } + public String createZenodoDoi(UUID id, Principal principal, ConfigLoader configLoader) throws Exception { DMP dmp = this.apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(id); if (!isUserOwnerOfDmp(dmp, principal)) @@ -1662,14 +1688,19 @@ public class DataManagementPlanManager { Map createResponse = null; LinkedHashMap links = null; String previousDOI = this.getPreviousDOI(dmp.getGroupId(), dmp.getId()); + String unpublishedUrl = null; + String publishUrl = null; try { + if (previousDOI == null) { String createUrl = this.environment.getProperty("zenodo.url") + "deposit/depositions" + "?access_token=" + zenodoToken; createResponse = restTemplate.postForEntity(createUrl, request, Map.class).getBody(); links = (LinkedHashMap) createResponse.get("links"); } else { - //It requires more than one step to create a new version - //First, get the deposit related to the concept DOI + unpublishedUrl = this.getUnpublishedDOI(previousDOI, zenodoToken, dmp.getVersion()); + if (unpublishedUrl == null) { + //It requires more than one step to create a new version + //First, get the deposit related to the concept DOI String listUrl = this.environment.getProperty("zenodo.url") + "deposit/depositions" + "?q=conceptdoi:\"" + previousDOI + "\"&access_token=" + zenodoToken; ResponseEntity listResponses = restTemplate.getForEntity(listUrl, Map[].class); createResponse = listResponses.getBody()[0]; @@ -1688,36 +1719,42 @@ public class DataManagementPlanManager { String updateUrl = links.get("self") + "?access_token=" + zenodoToken; restTemplate.put(updateUrl, request); //And finally remove pre-existing files from it - String fileListUrl = links.get("self") + "/files" + "?access_token=" + this.environment.getProperty("zenodo.access_token"); + String fileListUrl = links.get("self") + "/files" + "?access_token=" + zenodoToken; ResponseEntity fileListResponse = restTemplate.getForEntity(fileListUrl, Map[].class); for (Map file : fileListResponse.getBody()) { - String fileDeleteUrl = links.get("self") + "/files/" + file.get("id") + "?access_token=" + this.environment.getProperty("zenodo.access_token"); + String fileDeleteUrl = links.get("self") + "/files/" + file.get("id") + "?access_token=" + zenodoToken; restTemplate.delete(fileDeleteUrl); } - }catch (Exception e) { + } catch (Exception e) { //In case the last two steps fail delete the latest Deposit it in order to create a new one (only one at a time is allowed) restTemplate.delete(latestDraftUrl); throw e; } + } } - // Second step, add the file to the entry. - HttpHeaders fileHeaders = new HttpHeaders(); - fileHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); - LinkedMultiValueMap addFileMap = new LinkedMultiValueMap<>(); + if (unpublishedUrl == null) { + // Second step, add the file to the entry. + HttpHeaders fileHeaders = new HttpHeaders(); + fileHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); + LinkedMultiValueMap addFileMap = new LinkedMultiValueMap<>(); - FileEnvelope file = getWordDocument(id.toString(), principal, configLoader); - addFileMap.add("filename", file.getFilename()); - FileSystemResource fileSystemResource = new FileSystemResource(file.getFile()); - addFileMap.add("file", fileSystemResource); - HttpEntity> addFileMapRequest = new HttpEntity<>(addFileMap, fileHeaders); + FileEnvelope file = getWordDocument(id.toString(), principal, configLoader); + addFileMap.add("filename", file.getFilename()); + FileSystemResource fileSystemResource = new FileSystemResource(file.getFile()); + addFileMap.add("file", fileSystemResource); + HttpEntity> addFileMapRequest = new HttpEntity<>(addFileMap, fileHeaders); - String addFileUrl = links.get("files") + "?access_token=" + this.environment.getProperty("zenodo.access_token"); - ResponseEntity addFileResponse = restTemplate.postForEntity(addFileUrl, addFileMapRequest, String.class); - Files.deleteIfExists(file.getFile().toPath()); + String addFileUrl = links.get("files") + "?access_token=" + zenodoToken; + ResponseEntity addFileResponse = restTemplate.postForEntity(addFileUrl, addFileMapRequest, String.class); + Files.deleteIfExists(file.getFile().toPath()); - // Third post call to Zenodo to publish the entry and return the DOI. - String publishUrl = links.get("publish") + "?access_token=" + this.environment.getProperty("zenodo.access_token"); + + // Third post call to Zenodo to publish the entry and return the DOI. + publishUrl = links.get("publish") + "?access_token=" + zenodoToken; + } else { + publishUrl = unpublishedUrl + "?access_token=" + zenodoToken; + } Map publishResponce = restTemplate.postForObject(publishUrl, "", Map.class); dmp.setDoi((String) publishResponce.get("conceptdoi"));