remove redundant parameter in deposit interface

This commit is contained in:
Bernaldo Mihasi 2022-11-22 09:58:47 +02:00
parent 5e2639848b
commit 573aab059b
4 changed files with 36 additions and 62 deletions

View File

@ -69,7 +69,7 @@ public class DataverseDeposit implements RepositoryDeposit {
}
@Override
public String deposit(DMPDepositModel dmpDepositModel, boolean update, String repositoryAccessToken) throws Exception {
public String deposit(DMPDepositModel dmpDepositModel, String repositoryAccessToken) throws Exception {
if(!this.isApiSet)
this.setDataverseApi();

View File

@ -4,7 +4,7 @@ import eu.eudat.depositinterface.models.DMPDepositModel;
public interface RepositoryDeposit {
String deposit(DMPDepositModel dmpDepositModel, boolean update, String repositoryAccessToken) throws Exception;
String deposit(DMPDepositModel dmpDepositModel, String repositoryAccessToken) throws Exception;
String authenticate(String code);

View File

@ -2100,7 +2100,7 @@ public class DataManagementPlanManager {
String finalDoi = null;
for(RepositoryDeposit repo: this.repositoriesDeposit) { //temp
if(repo.getConfiguration().getRepositoryId().equals("Zenodo")) {
finalDoi = repo.deposit(dmpDepositModel, update, zenodoToken);
finalDoi = repo.deposit(dmpDepositModel, zenodoToken);
if (finalDoi != null) {
EntityDoi doiEntity = new EntityDoi();
doiEntity.setId(UUID.randomUUID());
@ -2158,7 +2158,7 @@ public class DataManagementPlanManager {
Optional<RepositoryDeposit> repo = this.repositoriesDeposit.stream().filter(x -> x.getConfiguration().getRepositoryId().equals(depositRequest.getRepositoryId())).findFirst();
String finalDoi = repo.map(r -> {
try {
return r.deposit(dmpDepositModel, false, depositRequest.getAccessToken());
return r.deposit(dmpDepositModel, depositRequest.getAccessToken());
} catch (Exception e) {
logger.error(e.getMessage(), e);
return null;

View File

@ -41,7 +41,7 @@ public class ZenodoDeposit implements RepositoryDeposit {
}
@Override
public String deposit(DMPDepositModel dmpDepositModel, boolean update, String zenodoToken) throws Exception {
public String deposit(DMPDepositModel dmpDepositModel, String zenodoToken) throws Exception {
RepositoryDepositConfiguration conf = this.getConfiguration();
@ -71,7 +71,6 @@ public class ZenodoDeposit implements RepositoryDeposit {
String createUrl = zenodoUrl + "deposit/depositions" + "?access_token=" + zenodoToken;
createResponse = restTemplate.postForEntity(createUrl, request, Map.class).getBody();
links = (LinkedHashMap<String, String>) createResponse.get("links");
finalDoi = (String) createResponse.get("conceptdoi");
}
else {
unpublishedUrl = this.getUnpublishedDOI(zenodoUrl, previousDOI, zenodoToken, dmpDepositModel.getVersion());
@ -90,7 +89,6 @@ public class ZenodoDeposit implements RepositoryDeposit {
String latestDraftUrl = links.get("latest_draft") + "?access_token=" + zenodoToken;
createResponse = restTemplate.getForObject(latestDraftUrl, Map.class);
links = (LinkedHashMap<String, String>) createResponse.get("links");
finalDoi = (String) createResponse.get("conceptdoi");
//At this point it might fail to perform the next requests so enclose them with try catch
try {
//Forth, update the new deposit's metadata
@ -117,48 +115,42 @@ public class ZenodoDeposit implements RepositoryDeposit {
}
}
if (!update) {
if (unpublishedUrl == null) {
// Second step, add the file to the entry.
File pdfFile = dmpDepositModel.getPdfFile();
String fileName = dmpDepositModel.getPdfFileName();
FileSystemResource fileSystemResource = new FileSystemResource(pdfFile);
HttpEntity<FileSystemResource> addFileMapRequest = new HttpEntity<>(fileSystemResource, null);
if (unpublishedUrl == null) {
// Second step, add the file to the entry.
File pdfFile = dmpDepositModel.getPdfFile();
String fileName = dmpDepositModel.getPdfFileName();
FileSystemResource fileSystemResource = new FileSystemResource(pdfFile);
HttpEntity<FileSystemResource> addFileMapRequest = new HttpEntity<>(fileSystemResource, null);
String addFileUrl = links.get("bucket") + "/" + fileName + "?access_token=" + zenodoToken;
restTemplate.put(addFileUrl, addFileMapRequest);
String addFileUrl = links.get("bucket") + "/" + fileName + "?access_token=" + zenodoToken;
restTemplate.put(addFileUrl, addFileMapRequest);
ResponseEntity<byte[]> jsonFile = dmpDepositModel.getRdaJson();
UUID jsonFileUUID = UUID.randomUUID();
File tempJsonFile = new File(jsonFileUUID + ".json"); // temp storage??
try (FileOutputStream jsonFos = new FileOutputStream(tempJsonFile)) {
jsonFos.write(jsonFile.getBody());
jsonFos.flush();
}
fileSystemResource = new FileSystemResource(tempJsonFile);
HttpHeaders jsonHeaders = new HttpHeaders();
jsonHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
addFileMapRequest = new HttpEntity<>(fileSystemResource, jsonHeaders);
String jsonFileName = jsonFile.getHeaders().get("Content-Disposition").get(0).substring(jsonFile.getHeaders().get("Content-Disposition").get(0).lastIndexOf('=') + 1);
addFileUrl = links.get("bucket") + "/" + jsonFileName + "?access_token=" + zenodoToken;
restTemplate.put(addFileUrl, addFileMapRequest);
Files.deleteIfExists(tempJsonFile.toPath());
// Third post call to Zenodo to publish the entry and return the DOI.
publishUrl = links.get("publish") + "?access_token=" + zenodoToken;
ResponseEntity<byte[]> jsonFile = dmpDepositModel.getRdaJson();
UUID jsonFileUUID = UUID.randomUUID();
File tempJsonFile = new File(jsonFileUUID + ".json"); // temp storage??
try (FileOutputStream jsonFos = new FileOutputStream(tempJsonFile)) {
jsonFos.write(jsonFile.getBody());
jsonFos.flush();
}
else {
publishUrl = unpublishedUrl + "?access_token=" + zenodoToken;
}
finalDoi = this.publish(publishUrl);
} else {
Map<String, Object> editResponce = restTemplate.postForObject(links.get("edit") + "?access_token=" + zenodoToken, "", Map.class);
restTemplate.put(links.get("self") + "?access_token=" + zenodoToken, request);
finalDoi = this.publish(links.get("publish") + "?access_token=" + zenodoToken);
fileSystemResource = new FileSystemResource(tempJsonFile);
HttpHeaders jsonHeaders = new HttpHeaders();
jsonHeaders.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_VALUE);
addFileMapRequest = new HttpEntity<>(fileSystemResource, jsonHeaders);
String jsonFileName = jsonFile.getHeaders().get("Content-Disposition").get(0).substring(jsonFile.getHeaders().get("Content-Disposition").get(0).lastIndexOf('=') + 1);
addFileUrl = links.get("bucket") + "/" + jsonFileName + "?access_token=" + zenodoToken;
restTemplate.put(addFileUrl, addFileMapRequest);
Files.deleteIfExists(tempJsonFile.toPath());
// 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;
}
return finalDoi;
return this.publish(publishUrl);
} catch (HttpClientErrorException | HttpServerErrorException ex) {
Map<String, String> parsedException = objectMapper.readValue(ex.getResponseBodyAsString(), HashMap.class);
throw new IOException(parsedException.get("message"), ex);
@ -172,24 +164,6 @@ public class ZenodoDeposit implements RepositoryDeposit {
return (String) publishResponce.get("conceptdoi");
}
private void publishIfNot(String zenodoUrl, String doi, String zenodoToken, DMPDepositModel dmpDepositModel){
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
eu.eudat.depositinterface.zenodorepository.models.ZenodoDeposit deposit = DMPToZenodoMapper.fromDMP(dmpDepositModel, "argos", "ARGOS", "https://argos.openaire.eu/", this.configLoader.getDOIFunders());
HttpEntity<eu.eudat.depositinterface.zenodorepository.models.ZenodoDeposit> request = new HttpEntity<>(deposit, headers);
String listUrl = zenodoUrl + "deposit/depositions" + "?q=conceptdoi:\"" + doi + "\"&access_token=" + zenodoToken;
ResponseEntity<Map[]> listResponses = restTemplate.getForEntity(listUrl, Map[].class);
Map createResponse = listResponses.getBody()[0];
LinkedHashMap<String, String> links = (LinkedHashMap<String, String>) createResponse.get("links");
Map<String, Object> editResponce = restTemplate.postForObject(links.get("edit") + "?access_token=" + zenodoToken, "", Map.class);
restTemplate.put(links.get("self") + "?access_token=" + zenodoToken, request);
this.publish(links.get("publish") + "?access_token=" + zenodoToken);
}
@Override
public RepositoryDepositConfiguration getConfiguration() {