Fix and Improve Zenodo DOI creation

This commit is contained in:
George Kalampokis 2020-03-19 12:50:23 +02:00
parent 097d45dc0b
commit b5877f997c
1 changed files with 16 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package eu.eudat.logic.managers;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.configurations.dynamicgrant.DynamicGrantConfiguration;
import eu.eudat.configurations.dynamicgrant.entities.Property;
@ -68,6 +69,8 @@ import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
@ -1434,7 +1437,7 @@ public class DataManagementPlanManager {
// First step, post call to Zenodo, to create the entry.
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("accept", "application/json");
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
String createData = "{\n" +
" \"metadata\": {\n" +
@ -1448,17 +1451,20 @@ public class DataManagementPlanManager {
" \t\t\"affiliation\": \"OpenDMP\"}]\n" +
" }\n" +
"}";
HttpEntity<String> request = new HttpEntity<>(createData, headers);
JsonNode createDataJSON = new ObjectMapper().readTree(createData);
HttpEntity<JsonNode> request = new HttpEntity<>(createDataJSON, headers);
Map createResponse = null;
LinkedHashMap<String, String> links = null;
if (dmp.getVersion() == 0) {
String previousDOI = this.getPreviousDOI(dmp.getGroupId(), dmp.getId());
try {
if (previousDOI == null) {
String createUrl = this.environment.getProperty("zenodo.url") + "deposit/depositions" + "?access_token=" + this.environment.getProperty("zenodo.access_token");
createResponse = restTemplate.postForObject(createUrl, request, Map.class);
createResponse = restTemplate.postForEntity(createUrl, request, Map.class).getBody();
links = (LinkedHashMap<String, String>) createResponse.get("links");
} 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");
String listUrl = this.environment.getProperty("zenodo.url") + "deposit/depositions" + "?q=conceptdoi:\"" + previousDOI + "\"&access_token=" + this.environment.getProperty("zenodo.access_token");
ResponseEntity<Map[]> listResponses = restTemplate.getForEntity(listUrl, Map[].class);
createResponse = listResponses.getBody()[0];
links = (LinkedHashMap<String, String>) createResponse.get("links");
@ -1511,5 +1517,10 @@ public class DataManagementPlanManager {
dmp.setDoi((String) publishResponce.get("conceptdoi"));
apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(dmp);
return (String) publishResponce.get("conceptdoi");
} catch (HttpClientErrorException | HttpServerErrorException ex) {
ObjectMapper ob = new ObjectMapper();
Map<String, String> parsedException = ob.readValue(ex.getResponseBodyAsString(), HashMap.class);
throw new IOException(parsedException.get("message"), ex);
}
}
}