Adds create DOI backend service for DMP, using Zenodo (Issue #111)

This commit is contained in:
gkolokythas 2019-07-04 17:30:15 +03:00
parent 8b1f1d834e
commit 41319ee320
3 changed files with 67 additions and 0 deletions

View File

@ -257,4 +257,14 @@ public class DMPs extends BaseController {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<DMP>().status(ApiMessageCode.ERROR_MESSAGE).message(e.getMessage()));
}
}
@RequestMapping(method = RequestMethod.POST, value = {"/createZenodoDoi/{id}"})
public ResponseEntity<ResponseItem<String>> createZenodoDoi(@PathVariable String id, Principal principal) {
try {
String zenodoDOI = this.dataManagementPlanManager.createZenodoDoi(UUID.fromString(id), principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<String>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Successfully created DOI for Data Datamanagement Plan in question.").payload(zenodoDOI));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<String>().status(ApiMessageCode.ERROR_MESSAGE).message(e.getMessage()));
}
}
}

View File

@ -49,8 +49,11 @@ import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
@ -967,4 +970,54 @@ public class DataManagementPlanManager {
private boolean isUserOwnerOfDmp(DMP dmp, Principal principal) {
return (dmp.getUsers().stream().filter(userDMP -> userDMP.getRole().equals(UserDMP.UserDMPRoles.OWNER.getValue())).findFirst().get().getUser().getId()).equals(principal.getId());
}
public String createZenodoDoi(UUID id, Principal principal) throws Exception {
DMP dmp = this.apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(id);
if (!isUserOwnerOfDmp(dmp, principal))
throw new Exception("User is not authorized to invoke this action");
if (!dmp.getStatus().equals(DMP.DMPStatus.FINALISED.getValue()))
throw new Exception("DMP is not finalized");
// First step, post call to Zenodo, to create the entry.
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.set("accept", "application/json");
headers.setContentType(MediaType.APPLICATION_JSON);
String createData = "{\n" +
" \"metadata\": {\n" +
" \"title\": \"" + dmp.getLabel() + "\",\n" +
" \"upload_type\": \"publication\",\n" +
" \"publication_type\": \"datamanagementplan\",\n" +
" \"description\": \"" + dmp.getDescription() + "\",\n" +
" \"creators\": [{\n" +
" \t\t\"name\": \"Kolokythas, Georgios\",\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);
// Second step, add the file to the entry.
HttpHeaders fileHeaders = new HttpHeaders();
fileHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
LinkedMultiValueMap<String, Object> addFileMap = new LinkedMultiValueMap<>();
File file = getWordDocument(id.toString());
addFileMap.add("filename", file.getName());
FileSystemResource fileSystemResource = new FileSystemResource(file);
addFileMap.add("file", fileSystemResource);
HttpEntity<MultiValueMap<String, Object>> addFileMapRequest = new HttpEntity<>(addFileMap, fileHeaders);
LinkedHashMap<String, String> links = (LinkedHashMap<String, String>) createResponse.get("links");
String addFileUrl = links.get("files") + "?access_token=" + this.environment.getProperty("zenodo.access_token");
ResponseEntity<String> addFileResponse = restTemplate.postForEntity(addFileUrl, addFileMapRequest, String.class);
Files.deleteIfExists(file.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");
Map<String, Object> publishResponce = restTemplate.postForObject(publishUrl, "", Map.class);
return (String) publishResponce.get("conceptdoi");
}
}

View File

@ -34,3 +34,7 @@ facebook.login.namespace=
b2access.externallogin.user_info_url=https://b2access-integration.fz-juelich.de:443/oauth2/userinfo
b2access.externallogin.access_token_url=https://b2access-integration.fz-juelich.de:443/oauth2/token
b2access.externallogin.redirect_uri=http://opendmp.eu/api/oauth/authorized/b2access
#############ZENODO CONFIGURATIONS#########
zenodo.url=https://sandbox.zenodo.org/api/
zenodo.access_token=