Commit 7c59eda1 authored by Matija Obreza's avatar Matija Obreza

Genesys API /{instCode}/names returns results per accession

parent fffb1b22
/** /**
* Copyright 2014 Global Crop Diversity Trust * Copyright 2015 Global Crop Diversity Trust
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -30,7 +30,7 @@ public interface BatchRESTService { ...@@ -30,7 +30,7 @@ public interface BatchRESTService {
List<UpsertResponse> upsertAccessionData(FaoInstitute institute, Map<AccessionHeaderJson, ObjectNode> batch) throws RESTApiException; List<UpsertResponse> upsertAccessionData(FaoInstitute institute, Map<AccessionHeaderJson, ObjectNode> batch) throws RESTApiException;
void upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch)throws RESTApiException; List<UpsertResponse> upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch) throws RESTApiException;
int deleteAccessions(FaoInstitute institute, List<AccessionHeaderJson> batch) throws RESTApiException; int deleteAccessions(FaoInstitute institute, List<AccessionHeaderJson> batch) throws RESTApiException;
......
/** /**
* Copyright 2014 Global Crop Diversity Trust * Copyright 2015 Global Crop Diversity Trust
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -109,7 +109,7 @@ public class BatchRESTServiceImpl implements BatchRESTService { ...@@ -109,7 +109,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
if (current.getGenus().contains(" ") || current.getGenus().contains("sp.")) { if (current.getGenus().contains(" ") || current.getGenus().contains("sp.")) {
throw new RESTApiException("GENUS cannot contain spaces or 'sp.'. Offending genus: " + current.getGenus()); throw new RESTApiException("GENUS cannot contain spaces or 'sp.'. Offending genus: " + current.getGenus());
} }
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("Ensuring " + current); LOG.debug("Ensuring " + current);
} }
...@@ -932,14 +932,24 @@ public class BatchRESTServiceImpl implements BatchRESTService { ...@@ -932,14 +932,24 @@ public class BatchRESTServiceImpl implements BatchRESTService {
@Override @Override
@Transactional @Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE') or hasPermission(#institute, 'CREATE')") @PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE') or hasPermission(#institute, 'CREATE')")
public void upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch) throws RESTApiException { public List<UpsertResponse> upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch) throws RESTApiException {
LOG.info("Batch processing " + batch.size() + " entries for " + institute); LOG.info("Batch processing " + batch.size() + " entries for " + institute);
final boolean useUniqueAcceNumbs = institute.hasUniqueAcceNumbs();
final List<UpsertResponse> upsertResponses = new ArrayList<UpsertResponse>();
final List<AccessionAlias> toSave = new ArrayList<AccessionAlias>(); final List<AccessionAlias> toSave = new ArrayList<AccessionAlias>();
final List<AccessionAlias> toRemove = new ArrayList<AccessionAlias>(); final List<AccessionAlias> toRemove = new ArrayList<AccessionAlias>();
final boolean useUniqueAcceNumbs = institute.hasUniqueAcceNumbs();
for (final AccessionNamesJson dataJson : batch) { for (final AccessionNamesJson dataJson : batch) {
UpsertResponse upsertResponse = new UpsertResponse(dataJson.instCode, dataJson.acceNumb, dataJson.genus);
upsertResponses.add(upsertResponse);
UpsertResult upsertResult = null;
if (!institute.getCode().equals(dataJson.instCode)) {
throw new RESTApiException("Accession does not belong to instCode=" + institute.getCode() + " acn=" + dataJson);
}
Accession accession = null; Accession accession = null;
try { try {
if (useUniqueAcceNumbs) { if (useUniqueAcceNumbs) {
...@@ -954,13 +964,20 @@ public class BatchRESTServiceImpl implements BatchRESTService { ...@@ -954,13 +964,20 @@ public class BatchRESTServiceImpl implements BatchRESTService {
if (accession == null) { if (accession == null) {
LOG.warn("No such accession " + dataJson); LOG.warn("No such accession " + dataJson);
upsertResponse.setError("No such asccession " + dataJson);
continue; continue;
} else {
upsertResult = new UpsertResult(UpsertResult.Type.UPDATE);
upsertResult.setUUID(accession.getUuid());
} }
// LOG.info("Updating " + dataJson + " with=" + dataJson.aliases); // LOG.info("Updating " + dataJson + " with=" + dataJson.aliases);
final List<AccessionAliasJson> aliases = dataJson.aliases; final List<AccessionAliasJson> aliases = dataJson.aliases;
final List<AccessionAlias> existingAliases = genesysService.listAccessionAliases(accession.getAccessionId()); final List<AccessionAlias> existingAliases = genesysService.listAccessionAliases(accession.getAccessionId());
upsertAccessionAliases(accession.getAccessionId(), aliases, null, toRemove, toSave, existingAliases); upsertAccessionAliases(accession.getAccessionId(), aliases, null, toRemove, toSave, existingAliases);
// Set upsert result
upsertResponse.setResult(upsertResult);
} }
if (toSave.size() > 0) { if (toSave.size() > 0) {
...@@ -972,6 +989,7 @@ public class BatchRESTServiceImpl implements BatchRESTService { ...@@ -972,6 +989,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
genesysService.removeAliases(toRemove); genesysService.removeAliases(toRemove);
} }
return upsertResponses;
} }
private void upsertAccessionAliases(AccessionId accession, List<AccessionAliasJson> aliases, final AliasType aliasType, List<AccessionAlias> toRemove, private void upsertAccessionAliases(AccessionId accession, List<AccessionAliasJson> aliases, final AliasType aliasType, List<AccessionAlias> toRemove,
......
...@@ -261,12 +261,12 @@ public class AccessionController extends RestController { ...@@ -261,12 +261,12 @@ public class AccessionController extends RestController {
for (AccessionHeaderJson acceJ : batch.keySet()) { for (AccessionHeaderJson acceJ : batch.keySet()) {
try { try {
batchOfOne.clear(); batchOfOne.clear();
batchOfOne.put(acceJ, batch.get(acceJ)); batchOfOne.put(acceJ, batch.get(acceJ));
UpsertResponse accessionResponse = batchRESTService.upsertAccessionData(institute, batchOfOne).get(0); UpsertResponse accessionResponse = batchRESTService.upsertAccessionData(institute, batchOfOne).get(0);
response.add(accessionResponse); response.add(accessionResponse);
} catch (RESTApiException e) { } catch (RESTApiException e) {
if (LOG.isInfoEnabled()) { if (LOG.isInfoEnabled()) {
LOG.info("Error upserting " + acceJ.instCode + ": " + e.getMessage()); LOG.info("Error upserting " + acceJ.instCode + ": " + e.getMessage());
...@@ -290,7 +290,7 @@ public class AccessionController extends RestController { ...@@ -290,7 +290,7 @@ public class AccessionController extends RestController {
*/ */
@RequestMapping(value = "/{instCode}/names", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = { @RequestMapping(value = "/{instCode}/names", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = {
MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE }) MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody String upsertAccessionNames(@PathVariable("instCode") String instCode, @RequestBody List<AccessionNamesJson> batch) public @ResponseBody List<UpsertResponse> upsertAccessionNames(@PathVariable("instCode") String instCode, @RequestBody List<AccessionNamesJson> batch)
throws RESTApiException { throws RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in // User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService. // BatchRESTService.
...@@ -299,14 +299,47 @@ public class AccessionController extends RestController { ...@@ -299,14 +299,47 @@ public class AccessionController extends RestController {
throw new ResourceNotFoundException(); throw new ResourceNotFoundException();
} }
for (final AccessionNamesJson aid3 : batch) { try {
if (!instCode.equals(aid3.instCode)) { List<UpsertResponse> response = null;
throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + aid3);
try {
response = batchRESTService.upsertAccessionNames(institute, batch);
} catch (RESTApiException e) {
LOG.info("Retrying upsert names one by one due to " + e.getMessage());
response = upsertAccessionNames1by1(institute, batch);
} }
return response;
} catch (Throwable e) {
throw new RESTApiException("Could not upsert accession names data. " + e.getMessage(), e);
} }
}
private List<UpsertResponse> upsertAccessionNames1by1(FaoInstitute institute, List<AccessionNamesJson> batch) {
LOG.info("Attempting upsert accession names 1 by 1");
final List<AccessionNamesJson> batchOfOne = new ArrayList<AccessionNamesJson>(1);
List<UpsertResponse> response = new ArrayList<UpsertResponse>();
for (AccessionNamesJson acceJ : batch) {
batchRESTService.upsertAccessionNames(institute, batch); try {
return JSON_OK;
batchOfOne.clear();
batchOfOne.add(acceJ);
UpsertResponse accessionResponse = batchRESTService.upsertAccessionNames(institute, batchOfOne).get(0);
response.add(accessionResponse);
} catch (RESTApiException e) {
if (LOG.isInfoEnabled()) {
LOG.info("Error upserting " + acceJ.instCode + ": " + e.getMessage());
}
UpsertResponse accessionResponse = new UpsertResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus);
accessionResponse.setError(e.getMessage());
response.add(accessionResponse);
}
}
return response;
} }
/** /**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment