fix zenodo new version
This commit is contained in:
parent
f716b0b0b1
commit
c8b3824073
|
@ -170,13 +170,13 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static LinkedHashMap<String, String> depositNewVersion(String zenodoToken, String zenodoUrl, String previousDOI, WebClient zenodoClient, ZenodoDeposit deposit) throws Exception {
|
private static LinkedHashMap<String, String> depositNewVersion(String zenodoToken, String zenodoUrl, String previousDOI, WebClient zenodoClient, ZenodoDeposit deposit) throws Exception {
|
||||||
Map<String, LinkedHashMap<String, String>> createResponse;
|
Map<String, Object> createResponse;
|
||||||
LinkedHashMap<String, String> links;
|
LinkedHashMap<String, String> links;
|
||||||
String listUrl = zenodoUrl + "deposit/depositions" + "?q=conceptdoi:\"" + previousDOI + "\"&access_token=" + zenodoToken;
|
String listUrl = zenodoUrl + "deposit/depositions" + "?q=conceptdoi:\"" + previousDOI + "\"&access_token=" + zenodoToken;
|
||||||
logger.debug("listUrl = " + listUrl);
|
logger.debug("listUrl = " + listUrl);
|
||||||
ResponseEntity<List<Map>> listResponses = zenodoClient.get().uri(listUrl).retrieve().toEntityList(Map.class).block();
|
ResponseEntity<List<Map>> listResponses = zenodoClient.get().uri(listUrl).retrieve().toEntityList(Map.class).block();
|
||||||
if (listResponses == null || listResponses.getBody() == null || listResponses.getBody().isEmpty()) return null;
|
if (listResponses == null || listResponses.getBody() == null || listResponses.getBody().isEmpty()) return null;
|
||||||
createResponse = (Map<String, LinkedHashMap<String, String>>) listResponses.getBody().get(0);
|
createResponse = (Map<String, Object>) listResponses.getBody().get(0);
|
||||||
logger.debug("createResponse-previousDoi:");
|
logger.debug("createResponse-previousDoi:");
|
||||||
logger.debug(objectMapper.writeValueAsString(createResponse));
|
logger.debug(objectMapper.writeValueAsString(createResponse));
|
||||||
links = (LinkedHashMap<String, String>) createResponse.getOrDefault(ZENODO_LINKS, new LinkedHashMap<>());
|
links = (LinkedHashMap<String, String>) createResponse.getOrDefault(ZENODO_LINKS, new LinkedHashMap<>());
|
||||||
|
@ -189,20 +189,20 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
||||||
.exchangeToMono(mono ->
|
.exchangeToMono(mono ->
|
||||||
mono.statusCode().isError() ?
|
mono.statusCode().isError() ?
|
||||||
mono.createException().flatMap(Mono::error) :
|
mono.createException().flatMap(Mono::error) :
|
||||||
mono.bodyToMono(new ParameterizedTypeReference<Map<String, LinkedHashMap<String, String>>>() {})
|
mono.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {})
|
||||||
).block();
|
).block();
|
||||||
logger.debug("createResponse-newVersion:");
|
logger.debug("createResponse-newVersion:");
|
||||||
logger.debug(objectMapper.writeValueAsString(createResponse));
|
logger.debug(objectMapper.writeValueAsString(createResponse));
|
||||||
links = createResponse == null ? new LinkedHashMap<>() : createResponse.getOrDefault(ZENODO_LINKS, new LinkedHashMap<>());
|
links = createResponse == null ? new LinkedHashMap<>() : (LinkedHashMap<String, String>) createResponse.getOrDefault(ZENODO_LINKS, new LinkedHashMap<>());
|
||||||
|
|
||||||
//Third, get the new deposit
|
//Third, get the new deposit
|
||||||
if (!links.containsKey(ZENODO_LINKS_LATEST_DRAFT)) throw new MyApplicationException("can not create latest draft");
|
if (!links.containsKey(ZENODO_LINKS_LATEST_DRAFT)) throw new MyApplicationException("can not create latest draft");
|
||||||
String latestDraftUrl = links.get(ZENODO_LINKS_LATEST_DRAFT) + "?access_token=" + zenodoToken;
|
String latestDraftUrl = links.get(ZENODO_LINKS_LATEST_DRAFT) + "?access_token=" + zenodoToken;
|
||||||
createResponse = zenodoClient.get().uri(latestDraftUrl)
|
createResponse = zenodoClient.get().uri(latestDraftUrl)
|
||||||
.exchangeToMono(mono -> mono.bodyToMono(new ParameterizedTypeReference<Map<String, LinkedHashMap<String, String>>>() {})).block();
|
.exchangeToMono(mono -> mono.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {})).block();
|
||||||
logger.debug("createResponse-latestDraft:");
|
logger.debug("createResponse-latestDraft:");
|
||||||
logger.debug(objectMapper.writeValueAsString(createResponse));
|
logger.debug(objectMapper.writeValueAsString(createResponse));
|
||||||
links = createResponse == null ? new LinkedHashMap<>() : createResponse.getOrDefault(ZENODO_LINKS, new LinkedHashMap<>());
|
links = createResponse == null ? new LinkedHashMap<>() : (LinkedHashMap<String, String>) createResponse.getOrDefault(ZENODO_LINKS, new LinkedHashMap<>());
|
||||||
|
|
||||||
//At this point it might fail to perform the next requests so enclose them with try catch
|
//At this point it might fail to perform the next requests so enclose them with try catch
|
||||||
try {
|
try {
|
||||||
|
@ -222,13 +222,13 @@ public class ZenodoDepositServiceImpl implements ZenodoDepositService {
|
||||||
ResponseEntity<List<Map>> fileListResponse = zenodoClient.get().uri(fileListUrl).retrieve().toEntityList(Map.class).block();
|
ResponseEntity<List<Map>> fileListResponse = zenodoClient.get().uri(fileListUrl).retrieve().toEntityList(Map.class).block();
|
||||||
for (Map file : fileListResponse.getBody()) {
|
for (Map file : fileListResponse.getBody()) {
|
||||||
String fileDeleteUrl = links.get(ZENODO_LINKS_SELF) + "/files/" + file.get("id") + "?access_token=" + zenodoToken;
|
String fileDeleteUrl = links.get(ZENODO_LINKS_SELF) + "/files/" + file.get("id") + "?access_token=" + zenodoToken;
|
||||||
zenodoClient.delete().uri(fileDeleteUrl).retrieve().toEntity(Map.class).block();
|
zenodoClient.delete().uri(fileDeleteUrl).retrieve().toEntity(Void.class).block();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
//In case the last two steps fail delete the latest Deposit it in order to create a new one (only one at a time is allowed)
|
//In case the last two steps fail delete the latest Deposit it in order to create a new one (only one at a time is allowed)
|
||||||
//restTemplate.delete(latestDraftUrl);
|
//restTemplate.delete(latestDraftUrl);
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
zenodoClient.delete().uri(latestDraftUrl).retrieve().toEntity(Map.class).block();
|
zenodoClient.delete().uri(latestDraftUrl).retrieve().toEntity(Void.class).block();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
return links;
|
return links;
|
||||||
|
|
Loading…
Reference in New Issue