Commit 816d09f1 authored by Matija Obreza's avatar Matija Obreza
Browse files

/{instCode}/delete-named method modified to return accession-level results

parent 13578a7b
......@@ -22,7 +22,7 @@ import java.util.UUID;
/**
* Upsert response
*/
public class UpsertResponse implements Serializable {
public class AccessionOpResponse implements Serializable {
private static final long serialVersionUID = 3266359129838078166L;
private String instCode;
......@@ -64,7 +64,7 @@ public class UpsertResponse implements Serializable {
}
}
public UpsertResponse(String instCode, String acceNumb, String genus) {
public AccessionOpResponse(String instCode, String acceNumb, String genus) {
this.instCode = instCode;
this.acceNumb = acceNumb;
this.genus = genus;
......
......@@ -28,11 +28,11 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
public interface BatchRESTService {
List<UpsertResponse> upsertAccessionData(FaoInstitute institute, Map<AccessionHeaderJson, ObjectNode> batch) throws RESTApiException;
List<AccessionOpResponse> upsertAccessionData(FaoInstitute institute, Map<AccessionHeaderJson, ObjectNode> batch) throws RESTApiException;
List<UpsertResponse> upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch) throws RESTApiException;
List<AccessionOpResponse> upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch) throws RESTApiException;
int deleteAccessions(FaoInstitute institute, List<AccessionHeaderJson> batch) throws RESTApiException;
List<AccessionOpResponse> deleteAccessions(FaoInstitute institute, List<AccessionHeaderJson> batch) throws RESTApiException;
int deleteAccessionsById(FaoInstitute institute, List<Long> batch);
......
......@@ -50,8 +50,8 @@ import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.OrganizationService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.UpsertResponse;
import org.genesys2.server.service.UpsertResponse.UpsertResult;
import org.genesys2.server.service.AccessionOpResponse;
import org.genesys2.server.service.AccessionOpResponse.UpsertResult;
import org.genesys2.server.servlet.controller.rest.model.AccessionAliasJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionHeaderJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionNamesJson;
......@@ -142,12 +142,12 @@ public class BatchRESTServiceImpl implements BatchRESTService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE') or hasPermission(#institute, 'CREATE')")
public List<UpsertResponse> upsertAccessionData(FaoInstitute institute, Map<AccessionHeaderJson, ObjectNode> batch) throws RESTApiException {
public List<AccessionOpResponse> upsertAccessionData(FaoInstitute institute, Map<AccessionHeaderJson, ObjectNode> batch) throws RESTApiException {
LOG.info("Batch processing " + batch.size() + " entries for " + institute.getCode());
final boolean useUniqueAcceNumbs = institute.hasUniqueAcceNumbs();
final List<UpsertResponse> upsertResponses = new ArrayList<UpsertResponse>();
final List<AccessionOpResponse> upsertResponses = new ArrayList<AccessionOpResponse>();
final List<Accession> toSave = new ArrayList<Accession>();
final List<AccessionCollect> toSaveColl = new ArrayList<AccessionCollect>();
......@@ -187,7 +187,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
LOG.debug("Loading accession " + dataJson);
}
UpsertResponse upsertResponse = new UpsertResponse(dataJson.instCode, dataJson.acceNumb, dataJson.genus);
AccessionOpResponse upsertResponse = new AccessionOpResponse(dataJson.instCode, dataJson.acceNumb, dataJson.genus);
upsertResponses.add(upsertResponse);
UpsertResult upsertResult = null;
......@@ -932,17 +932,17 @@ public class BatchRESTServiceImpl implements BatchRESTService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE') or hasPermission(#institute, 'CREATE')")
public List<UpsertResponse> upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch) throws RESTApiException {
public List<AccessionOpResponse> upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch) throws RESTApiException {
LOG.info("Batch processing " + batch.size() + " entries for " + institute);
final boolean useUniqueAcceNumbs = institute.hasUniqueAcceNumbs();
final List<UpsertResponse> upsertResponses = new ArrayList<UpsertResponse>();
final List<AccessionOpResponse> upsertResponses = new ArrayList<AccessionOpResponse>();
final List<AccessionAlias> toSave = new ArrayList<AccessionAlias>();
final List<AccessionAlias> toRemove = new ArrayList<AccessionAlias>();
for (final AccessionNamesJson dataJson : batch) {
UpsertResponse upsertResponse = new UpsertResponse(dataJson.instCode, dataJson.acceNumb, dataJson.genus);
AccessionOpResponse upsertResponse = new AccessionOpResponse(dataJson.instCode, dataJson.acceNumb, dataJson.genus);
upsertResponses.add(upsertResponse);
UpsertResult upsertResult = null;
......@@ -1043,8 +1043,10 @@ public class BatchRESTServiceImpl implements BatchRESTService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'DELETE') or hasPermission(#institute, 'MANAGE')")
public int deleteAccessions(FaoInstitute institute, List<AccessionHeaderJson> batch) throws RESTApiException {
public List<AccessionOpResponse> deleteAccessions(FaoInstitute institute, List<AccessionHeaderJson> batch) throws RESTApiException {
LOG.info("Batch deleting " + batch.size() + " entries for " + institute);
final List<AccessionOpResponse> upsertResponses = new ArrayList<AccessionOpResponse>();
final List<Accession> toDelete = new ArrayList<Accession>(batch.size());
final boolean useUniqueAcceNumbs = institute.hasUniqueAcceNumbs();
......@@ -1053,6 +1055,10 @@ public class BatchRESTServiceImpl implements BatchRESTService {
LOG.debug("Loading accession " + dataJson);
}
AccessionOpResponse upsertResponse = new AccessionOpResponse(dataJson.instCode, dataJson.acceNumb, dataJson.genus);
upsertResponses.add(upsertResponse);
UpsertResult upsertResult;
if (!institute.getCode().equals(dataJson.instCode)) {
throw new RESTApiException("Accession does not belong to instCode=" + institute.getCode() + " acn=" + dataJson);
}
......@@ -1071,14 +1077,20 @@ public class BatchRESTServiceImpl implements BatchRESTService {
if (accession != null) {
toDelete.add(accession);
upsertResult=new UpsertResult(UpsertResult.Type.DELETE);
upsertResult.setUUID(accession.getUuid());
} else {
upsertResult=new UpsertResult(UpsertResult.Type.NOOP);
}
upsertResponse.setResult(upsertResult);
}
if (toDelete.size() > 0) {
genesysService.removeAccessions(institute, toDelete);
}
return toDelete.size();
return upsertResponses;
}
@Override
......
......@@ -38,7 +38,7 @@ import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.UpsertResponse;
import org.genesys2.server.service.AccessionOpResponse;
import org.genesys2.server.service.impl.FilterHandler;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilter;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
......@@ -232,7 +232,7 @@ public class AccessionController extends RestController {
// persisted
batchRESTService.ensureTaxonomies(institute, batch);
// Step 2: Upsert data
List<UpsertResponse> response = null;
List<AccessionOpResponse> response = null;
try {
response = batchRESTService.upsertAccessionData(institute, batch);
} catch (RESTApiException e) {
......@@ -253,18 +253,18 @@ public class AccessionController extends RestController {
throw new RESTApiException("Could not upsert accession data. " + cause.getMessage(), cause);
}
private List<UpsertResponse> upsertAccessionData1by1(FaoInstitute institute, Map<AccessionHeaderJson, ObjectNode> batch) {
private List<AccessionOpResponse> upsertAccessionData1by1(FaoInstitute institute, Map<AccessionHeaderJson, ObjectNode> batch) {
LOG.info("Attempting insert 1 by 1");
final Map<AccessionHeaderJson, ObjectNode> batchOfOne = new HashMap<AccessionHeaderJson, ObjectNode>();
List<UpsertResponse> response = new ArrayList<UpsertResponse>();
List<AccessionOpResponse> response = new ArrayList<AccessionOpResponse>();
for (AccessionHeaderJson acceJ : batch.keySet()) {
try {
batchOfOne.clear();
batchOfOne.put(acceJ, batch.get(acceJ));
UpsertResponse accessionResponse = batchRESTService.upsertAccessionData(institute, batchOfOne).get(0);
AccessionOpResponse accessionResponse = batchRESTService.upsertAccessionData(institute, batchOfOne).get(0);
response.add(accessionResponse);
} catch (RESTApiException e) {
......@@ -272,7 +272,7 @@ public class AccessionController extends RestController {
LOG.info("Error upserting " + acceJ.instCode + ": " + e.getMessage());
}
UpsertResponse accessionResponse = new UpsertResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus);
AccessionOpResponse accessionResponse = new AccessionOpResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus);
accessionResponse.setError(e.getMessage());
response.add(accessionResponse);
}
......@@ -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 List<UpsertResponse> upsertAccessionNames(@PathVariable("instCode") String instCode, @RequestBody List<AccessionNamesJson> batch)
public @ResponseBody List<AccessionOpResponse> upsertAccessionNames(@PathVariable("instCode") String instCode, @RequestBody List<AccessionNamesJson> batch)
throws RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
......@@ -300,7 +300,7 @@ public class AccessionController extends RestController {
}
try {
List<UpsertResponse> response = null;
List<AccessionOpResponse> response = null;
try {
response = batchRESTService.upsertAccessionNames(institute, batch);
......@@ -315,18 +315,18 @@ public class AccessionController extends RestController {
}
}
private List<UpsertResponse> upsertAccessionNames1by1(FaoInstitute institute, List<AccessionNamesJson> batch) {
private List<AccessionOpResponse> 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>();
List<AccessionOpResponse> response = new ArrayList<AccessionOpResponse>();
for (AccessionNamesJson acceJ : batch) {
try {
batchOfOne.clear();
batchOfOne.add(acceJ);
UpsertResponse accessionResponse = batchRESTService.upsertAccessionNames(institute, batchOfOne).get(0);
AccessionOpResponse accessionResponse = batchRESTService.upsertAccessionNames(institute, batchOfOne).get(0);
response.add(accessionResponse);
} catch (RESTApiException e) {
......@@ -334,7 +334,7 @@ public class AccessionController extends RestController {
LOG.info("Error upserting " + acceJ.instCode + ": " + e.getMessage());
}
UpsertResponse accessionResponse = new UpsertResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus);
AccessionOpResponse accessionResponse = new AccessionOpResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus);
accessionResponse.setError(e.getMessage());
response.add(accessionResponse);
}
......@@ -352,7 +352,7 @@ public class AccessionController extends RestController {
*/
@RequestMapping(value = "/{instCode}/delete-named", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = {
MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody JsonDeleteResult deleteAccessions(@PathVariable("instCode") String instCode, @RequestBody List<AccessionHeaderJson> batch)
public @ResponseBody List<AccessionOpResponse> deleteAccessions(@PathVariable("instCode") String instCode, @RequestBody List<AccessionHeaderJson> batch)
throws RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
......@@ -362,15 +362,51 @@ public class AccessionController extends RestController {
}
try {
final int deleted = batchRESTService.deleteAccessions(institute, batch);
LOG.info("Deleted " + deleted + " accessions from " + instCode);
List<AccessionOpResponse> response = null;
try {
response = batchRESTService.deleteAccessions(institute, batch);
LOG.info("Deleted " + response.size() + " accessions from " + instCode);
} catch (RESTApiException e) {
LOG.info("Retrying upsert one by one due to " + e.getMessage());
response = deleteAccessions1by1(institute, batch);
}
// Force update institute#accessionCount (outside previous
// transaction)
genesysService.updateAccessionCount(institute);
return new JsonDeleteResult(deleted);
return response;
} catch (CannotAcquireLockException e) {
throw new PleaseRetryException("Operation failed, please retry.", e);
}
}
private List<AccessionOpResponse> deleteAccessions1by1(FaoInstitute institute, List<AccessionHeaderJson> batch) {
LOG.info("Attempting delete 1 by 1");
final List<AccessionHeaderJson> batchOfOne = new ArrayList<AccessionHeaderJson>();
List<AccessionOpResponse> response = new ArrayList<AccessionOpResponse>();
for (AccessionHeaderJson acceJ : batch) {
try {
batchOfOne.clear();
batchOfOne.add(acceJ);
AccessionOpResponse accessionResponse = batchRESTService.deleteAccessions(institute, batchOfOne).get(0);
response.add(accessionResponse);
} catch (RESTApiException e) {
if (LOG.isInfoEnabled()) {
LOG.info("Error deleting " + acceJ.instCode + ": " + e.getMessage());
}
AccessionOpResponse accessionResponse = new AccessionOpResponse(acceJ.instCode, acceJ.acceNumb, acceJ.genus);
accessionResponse.setError(e.getMessage());
response.add(accessionResponse);
}
}
return response;
}
/**
* Delete accessions by acceNumb (and genus)?
*
......@@ -381,8 +417,7 @@ public class AccessionController extends RestController {
*/
@RequestMapping(value = "/{instCode}/delete", method = { RequestMethod.POST }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = {
MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody JsonDeleteResult deleteAccessionsById(@PathVariable("instCode") String instCode, @RequestBody List<Long> batch)
throws RESTApiException {
public @ResponseBody int deleteAccessionsById(@PathVariable("instCode") String instCode, @RequestBody List<Long> batch) throws RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
final FaoInstitute institute = instituteService.getInstitute(instCode);
......@@ -393,7 +428,7 @@ public class AccessionController extends RestController {
final int deleted = batchRESTService.deleteAccessionsById(institute, batch);
LOG.info("Deleted " + deleted + " accessions from " + instCode);
genesysService.updateAccessionCount(institute);
return new JsonDeleteResult(deleted);
return deleted;
}
@RequestMapping(value = "/search", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
......@@ -463,20 +498,4 @@ public class AccessionController extends RestController {
return dataJson;
}
public static final class JsonDeleteResult {
private final int deleted;
public JsonDeleteResult(int deleted) {
this.deleted = deleted;
}
public int getDeleted() {
return deleted;
}
public boolean getResult() {
return true;
}
}
}
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