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;
}
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 {
DMP dmp = this.apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(id);
if (!isUserOwnerOfDmp(dmp, principal))
@ -1662,12 +1688,17 @@ public class DataManagementPlanManager {
Map createResponse = null;
LinkedHashMap<String, String> 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<String, String>) createResponse.get("links");
} else {
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;
@ -1688,10 +1719,10 @@ 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<Map[]> 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) {
@ -1700,7 +1731,9 @@ public class DataManagementPlanManager {
throw e;
}
}
}
if (unpublishedUrl == null) {
// Second step, add the file to the entry.
HttpHeaders fileHeaders = new HttpHeaders();
fileHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
@ -1712,12 +1745,16 @@ public class DataManagementPlanManager {
addFileMap.add("file", fileSystemResource);
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);
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");
publishUrl = links.get("publish") + "?access_token=" + zenodoToken;
} else {
publishUrl = unpublishedUrl + "?access_token=" + zenodoToken;
}
Map<String, Object> publishResponce = restTemplate.postForObject(publishUrl, "", Map.class);
dmp.setDoi((String) publishResponce.get("conceptdoi"));