diff --git a/src/main/java/org/genesys2/server/service/BatchRESTService.java b/src/main/java/org/genesys2/server/service/BatchRESTService.java index d940579980f99b0e4b92e621fb5b1429a3a45891..d81b9bed9244285d9f4533ce9a04781bb5e1ca53 100644 --- a/src/main/java/org/genesys2/server/service/BatchRESTService.java +++ b/src/main/java/org/genesys2/server/service/BatchRESTService.java @@ -1,5 +1,5 @@ /** - * Copyright 2014 Global Crop Diversity Trust + * Copyright 2015 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,7 +30,7 @@ public interface BatchRESTService { List upsertAccessionData(FaoInstitute institute, Map batch) throws RESTApiException; - void upsertAccessionNames(FaoInstitute institute, List batch)throws RESTApiException; + List upsertAccessionNames(FaoInstitute institute, List batch) throws RESTApiException; int deleteAccessions(FaoInstitute institute, List batch) throws RESTApiException; diff --git a/src/main/java/org/genesys2/server/service/impl/BatchRESTServiceImpl.java b/src/main/java/org/genesys2/server/service/impl/BatchRESTServiceImpl.java index 2b0f1dc8e8413352b048dc7e97ba0cc90d9583ad..ad5413aa215ee829cf6226e6202fdb374cbc67f1 100644 --- a/src/main/java/org/genesys2/server/service/impl/BatchRESTServiceImpl.java +++ b/src/main/java/org/genesys2/server/service/impl/BatchRESTServiceImpl.java @@ -1,5 +1,5 @@ /** - * Copyright 2014 Global Crop Diversity Trust + * Copyright 2015 Global Crop Diversity Trust * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -109,7 +109,7 @@ public class BatchRESTServiceImpl implements BatchRESTService { if (current.getGenus().contains(" ") || current.getGenus().contains("sp.")) { throw new RESTApiException("GENUS cannot contain spaces or 'sp.'. Offending genus: " + current.getGenus()); } - + if (LOG.isDebugEnabled()) { LOG.debug("Ensuring " + current); } @@ -932,14 +932,24 @@ public class BatchRESTServiceImpl implements BatchRESTService { @Override @Transactional @PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE') or hasPermission(#institute, 'CREATE')") - public void upsertAccessionNames(FaoInstitute institute, List batch) throws RESTApiException { + public List upsertAccessionNames(FaoInstitute institute, List batch) throws RESTApiException { LOG.info("Batch processing " + batch.size() + " entries for " + institute); + final boolean useUniqueAcceNumbs = institute.hasUniqueAcceNumbs(); + final List upsertResponses = new ArrayList(); + final List toSave = new ArrayList(); final List toRemove = new ArrayList(); - final boolean useUniqueAcceNumbs = institute.hasUniqueAcceNumbs(); 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; try { if (useUniqueAcceNumbs) { @@ -954,13 +964,20 @@ public class BatchRESTServiceImpl implements BatchRESTService { if (accession == null) { LOG.warn("No such accession " + dataJson); + upsertResponse.setError("No such asccession " + dataJson); continue; + } else { + upsertResult = new UpsertResult(UpsertResult.Type.UPDATE); + upsertResult.setUUID(accession.getUuid()); } // LOG.info("Updating " + dataJson + " with=" + dataJson.aliases); final List aliases = dataJson.aliases; final List existingAliases = genesysService.listAccessionAliases(accession.getAccessionId()); upsertAccessionAliases(accession.getAccessionId(), aliases, null, toRemove, toSave, existingAliases); + + // Set upsert result + upsertResponse.setResult(upsertResult); } if (toSave.size() > 0) { @@ -972,6 +989,7 @@ public class BatchRESTServiceImpl implements BatchRESTService { genesysService.removeAliases(toRemove); } + return upsertResponses; } private void upsertAccessionAliases(AccessionId accession, List aliases, final AliasType aliasType, List toRemove, diff --git a/src/main/java/org/genesys2/server/servlet/controller/rest/AccessionController.java b/src/main/java/org/genesys2/server/servlet/controller/rest/AccessionController.java index 6c559453fcb101086f669a4649ac790c889d77f9..8b5fcf5f336e04b149ff28e30c3520e948725ebe 100644 --- a/src/main/java/org/genesys2/server/servlet/controller/rest/AccessionController.java +++ b/src/main/java/org/genesys2/server/servlet/controller/rest/AccessionController.java @@ -261,12 +261,12 @@ public class AccessionController extends RestController { for (AccessionHeaderJson acceJ : batch.keySet()) { try { - + batchOfOne.clear(); batchOfOne.put(acceJ, batch.get(acceJ)); UpsertResponse accessionResponse = batchRESTService.upsertAccessionData(institute, batchOfOne).get(0); response.add(accessionResponse); - + } catch (RESTApiException e) { if (LOG.isInfoEnabled()) { LOG.info("Error upserting " + acceJ.instCode + ": " + e.getMessage()); @@ -290,7 +290,7 @@ public class AccessionController extends RestController { */ @RequestMapping(value = "/{instCode}/names", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE }) - public @ResponseBody String upsertAccessionNames(@PathVariable("instCode") String instCode, @RequestBody List batch) + public @ResponseBody List upsertAccessionNames(@PathVariable("instCode") String instCode, @RequestBody List batch) throws RESTApiException { // User's permission to WRITE to this WIEWS institute are checked in // BatchRESTService. @@ -299,14 +299,47 @@ public class AccessionController extends RestController { throw new ResourceNotFoundException(); } - for (final AccessionNamesJson aid3 : batch) { - if (!instCode.equals(aid3.instCode)) { - throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + aid3); + try { + List response = null; + + 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 upsertAccessionNames1by1(FaoInstitute institute, List batch) { + LOG.info("Attempting upsert accession names 1 by 1"); + + final List batchOfOne = new ArrayList(1); + List response = new ArrayList(); + for (AccessionNamesJson acceJ : batch) { - batchRESTService.upsertAccessionNames(institute, batch); - return JSON_OK; + try { + + 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; } /**