Fixed issue with Zenodo DOI generation with user token

This commit is contained in:
George Kalampokis 2020-04-21 12:40:50 +03:00
parent 96576b16a8
commit d0cb186ab2
1 changed files with 56 additions and 19 deletions

View File

@ -1623,6 +1623,32 @@ public class DataManagementPlanManager {
return doi; 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<String, String> links = null;
LinkedHashMap<String, String> metadata = null;
String listUrl = this.environment.getProperty("zenodo.url") + "deposit/depositions" + "?q=conceptdoi:\"" + DOI + "\"&access_token=" + token;
ResponseEntity<Map[]> listResponses = restTemplate.getForEntity(listUrl, Map[].class);
createResponse = listResponses.getBody()[0];
metadata = (LinkedHashMap<String, String>) createResponse.get("metadata");
links = (LinkedHashMap<String, String>) 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 { public String createZenodoDoi(UUID id, Principal principal, ConfigLoader configLoader) throws Exception {
DMP dmp = this.apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(id); DMP dmp = this.apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(id);
if (!isUserOwnerOfDmp(dmp, principal)) if (!isUserOwnerOfDmp(dmp, principal))
@ -1662,14 +1688,19 @@ public class DataManagementPlanManager {
Map createResponse = null; Map createResponse = null;
LinkedHashMap<String, String> links = null; LinkedHashMap<String, String> links = null;
String previousDOI = this.getPreviousDOI(dmp.getGroupId(), dmp.getId()); String previousDOI = this.getPreviousDOI(dmp.getGroupId(), dmp.getId());
String unpublishedUrl = null;
String publishUrl = null;
try { try {
if (previousDOI == null) { if (previousDOI == null) {
String createUrl = this.environment.getProperty("zenodo.url") + "deposit/depositions" + "?access_token=" + zenodoToken; String createUrl = this.environment.getProperty("zenodo.url") + "deposit/depositions" + "?access_token=" + zenodoToken;
createResponse = restTemplate.postForEntity(createUrl, request, Map.class).getBody(); createResponse = restTemplate.postForEntity(createUrl, request, Map.class).getBody();
links = (LinkedHashMap<String, String>) createResponse.get("links"); links = (LinkedHashMap<String, String>) createResponse.get("links");
} else { } else {
//It requires more than one step to create a new version unpublishedUrl = this.getUnpublishedDOI(previousDOI, zenodoToken, dmp.getVersion());
//First, get the deposit related to the concept DOI 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; String listUrl = this.environment.getProperty("zenodo.url") + "deposit/depositions" + "?q=conceptdoi:\"" + previousDOI + "\"&access_token=" + zenodoToken;
ResponseEntity<Map[]> listResponses = restTemplate.getForEntity(listUrl, Map[].class); ResponseEntity<Map[]> listResponses = restTemplate.getForEntity(listUrl, Map[].class);
createResponse = listResponses.getBody()[0]; createResponse = listResponses.getBody()[0];
@ -1688,36 +1719,42 @@ public class DataManagementPlanManager {
String updateUrl = links.get("self") + "?access_token=" + zenodoToken; String updateUrl = links.get("self") + "?access_token=" + zenodoToken;
restTemplate.put(updateUrl, request); restTemplate.put(updateUrl, request);
//And finally remove pre-existing files from it //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<Map[]> fileListResponse = restTemplate.getForEntity(fileListUrl, Map[].class); ResponseEntity<Map[]> fileListResponse = restTemplate.getForEntity(fileListUrl, Map[].class);
for (Map file : fileListResponse.getBody()) { 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); 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) //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); restTemplate.delete(latestDraftUrl);
throw e; throw e;
} }
}
} }
// Second step, add the file to the entry. if (unpublishedUrl == null) {
HttpHeaders fileHeaders = new HttpHeaders(); // Second step, add the file to the entry.
fileHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); HttpHeaders fileHeaders = new HttpHeaders();
LinkedMultiValueMap<String, Object> addFileMap = new LinkedMultiValueMap<>(); fileHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
LinkedMultiValueMap<String, Object> addFileMap = new LinkedMultiValueMap<>();
FileEnvelope file = getWordDocument(id.toString(), principal, configLoader); FileEnvelope file = getWordDocument(id.toString(), principal, configLoader);
addFileMap.add("filename", file.getFilename()); addFileMap.add("filename", file.getFilename());
FileSystemResource fileSystemResource = new FileSystemResource(file.getFile()); FileSystemResource fileSystemResource = new FileSystemResource(file.getFile());
addFileMap.add("file", fileSystemResource); addFileMap.add("file", fileSystemResource);
HttpEntity<MultiValueMap<String, Object>> addFileMapRequest = new HttpEntity<>(addFileMap, fileHeaders); HttpEntity<MultiValueMap<String, Object>> addFileMapRequest = new HttpEntity<>(addFileMap, fileHeaders);
String addFileUrl = links.get("files") + "?access_token=" + this.environment.getProperty("zenodo.access_token"); String addFileUrl = links.get("files") + "?access_token=" + zenodoToken;
ResponseEntity<String> addFileResponse = restTemplate.postForEntity(addFileUrl, addFileMapRequest, String.class); ResponseEntity<String> addFileResponse = restTemplate.postForEntity(addFileUrl, addFileMapRequest, String.class);
Files.deleteIfExists(file.getFile().toPath()); 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<String, Object> publishResponce = restTemplate.postForObject(publishUrl, "", Map.class); Map<String, Object> publishResponce = restTemplate.postForObject(publishUrl, "", Map.class);
dmp.setDoi((String) publishResponce.get("conceptdoi")); dmp.setDoi((String) publishResponce.get("conceptdoi"));