Add Zenodo versioning support (ref #221)

This commit is contained in:
George Kalampokis 2020-02-21 14:04:14 +02:00
parent 8dba2eb543
commit d522bd7c2f
1 changed files with 53 additions and 2 deletions

View File

@ -1233,6 +1233,25 @@ public class DataManagementPlanManager {
return (dmp.getUsers().stream().filter(userDMP -> userDMP.getRole().equals(UserDMP.UserDMPRoles.OWNER.getValue())).findFirst().get().getUser().getId()).equals(principal.getId());
}
private String getPreviousDOI(UUID groupId, UUID selfId) {
DataManagementPlanCriteria criteria = new DataManagementPlanCriteria();
List<UUID> groupIds = new ArrayList<>();
groupIds.add(groupId);
criteria.setGroupIds(groupIds);
criteria.setAllVersions(true);
List<DMP> dmps = this.databaseRepository.getDmpDao().getWithCriteria(criteria).toList();
String doi = null;
for (DMP dmp: dmps) {
if (!dmp.getId().equals(selfId)) {
if (dmp.getDoi() != null && !dmp.getDoi().isEmpty()) {
doi = dmp.getDoi();
}
}
}
return doi;
}
public String createZenodoDoi(UUID id, Principal principal, ConfigLoader configLoader) throws Exception {
DMP dmp = this.apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(id);
if (!isUserOwnerOfDmp(dmp, principal))
@ -1253,14 +1272,46 @@ public class DataManagementPlanManager {
" \"upload_type\": \"publication\",\n" +
" \"publication_type\": \"datamanagementplan\",\n" +
" \"description\": \"" + dmp.getDescription() + "\",\n" +
" \"version\": \"" + dmp.getVersion() + "\",\n" +
" \"creators\": [{\n" +
" \t\t\"name\": \"" + dmp.getUsers().stream().filter(userDMP -> userDMP.getRole().equals(UserDMP.UserDMPRoles.OWNER.getValue())).findFirst().get().getUser().getName() + "\",\n" +
" \t\t\"affiliation\": \"OpenDMP\"}]\n" +
" }\n" +
"}";
HttpEntity<String> request = new HttpEntity<>(createData, headers);
String createUrl = this.environment.getProperty("zenodo.url") + "deposit/depositions" + "?access_token=" + this.environment.getProperty("zenodo.access_token");
Map createResponse = restTemplate.postForObject(createUrl, request, Map.class);
Map createResponse = null;
if (dmp.getVersion() == 0) {
String createUrl = this.environment.getProperty("zenodo.url") + "deposit/depositions" + "?access_token=" + this.environment.getProperty("zenodo.access_token");
createResponse = restTemplate.postForObject(createUrl, request, Map.class);
} else {
//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:\"" + this.getPreviousDOI(dmp.getGroupId(), dmp.getId()) + "\"&access_token=" + this.environment.getProperty("zenodo.access_token");
ResponseEntity<Map[]> listResponses = restTemplate.getForEntity(listUrl, Map[].class);
createResponse = listResponses.getBody()[0];
LinkedHashMap<String, String> links = (LinkedHashMap<String, String>) createResponse.get("links");
//Second, make the new version (not in the links?)
String newVersionUrl = links.get("self") + "/actions/newversion" + "?access_token=" + this.environment.getProperty("zenodo.access_token");
createResponse = restTemplate.postForObject(newVersionUrl, null, Map.class);
links = (LinkedHashMap<String, String>) createResponse.get("links");
//Third, get the new deposit
String latestDraftUrl = links.get("latest_draft") + "?access_token=" + this.environment.getProperty("zenodo.access_token");
createResponse = restTemplate.getForObject(latestDraftUrl, Map.class);
String fileListUrl = links.get("latest_draft") + "/files" + "?access_token=" + this.environment.getProperty("zenodo.access_token");
try {
//And finally remove pre-existing files from it
ResponseEntity<Map[]> fileListResponse = restTemplate.getForEntity(fileListUrl, Map[].class);
for (Map file : fileListResponse.getBody()) {
String fileDeleteUrl = links.get("latest_draft") + "/files/" + file.get("id") + "?access_token=" + this.environment.getProperty("zenodo.access_token");
restTemplate.delete(fileDeleteUrl);
}
}catch (Exception e) {
//In case the last step 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();