Commit 2d343e50 authored by Matija Obreza's avatar Matija Obreza

REST /acn/{instcode}/check does not require genus or instCode

parent 5fd05216
......@@ -17,6 +17,7 @@
package org.genesys2.server.servlet.controller.rest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -29,6 +30,7 @@ import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.impl.RESTApiException;
import org.genesys2.server.service.impl.RESTApiValueException;
import org.genesys2.server.servlet.controller.rest.model.AccessionJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionNamesJson;
import org.genesys2.spring.ResourceNotFoundException;
......@@ -97,53 +99,55 @@ public class AccessionController extends RestController {
* @return
* @throws IOException
* @throws JsonProcessingException
* @throws RESTApiValueException
*/
@RequestMapping(value = "/{instCode}/check", method = { RequestMethod.POST, RequestMethod.PUT }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
JsonNode check(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException {
// TODO Check user's permissions to update this WIEWS institute.
JsonNode check(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException, RESTApiValueException {
FaoInstitute institute=instituteService.getInstitute(instCode);
if (institute==null) {
throw new ResourceNotFoundException("No institute " + instCode);
}
JsonNode json = mapper.readTree(content);
Map<AccessionJson, ObjectNode> batch = new HashMap<AccessionJson, ObjectNode>();
List<String> batch = new ArrayList<String>();
if (json.isArray()) {
for (JsonNode j : json) {
AccessionJson dataJson = readAid3(j);
if (!instCode.equals(dataJson.instCode)) {
throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + dataJson);
if (j.isNull() || !j.isTextual()) {
throw new RESTApiValueException("acceNumb must be a non-null String");
}
batch.put(dataJson, (ObjectNode) j);
batch.add(j.textValue());
}
} else {
AccessionJson dataJson = readAid3(json);
if (!instCode.equals(dataJson.instCode)) {
throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + dataJson);
if (json.isNull() || !json.isTextual()) {
throw new RESTApiValueException("acceNumb must be a non-null String");
}
batch.put(dataJson, (ObjectNode) json);
batch.add(json.textValue());
}
LOG.info("Batch processing " + batch.size() + " entries");
ArrayNode rets = mapper.createArrayNode();
for (AccessionJson dataJson : batch.keySet()) {
for (String acceNumb : batch) {
if (LOG.isDebugEnabled())
LOG.debug("Loading accession " + dataJson);
ObjectNode ret = mapper.createObjectNode();
ret.put("instCode", dataJson.instCode);
ret.put("genus", dataJson.genus);
ret.put("acceNumb", dataJson.acceNumb);
Accession accession = genesysService.getAccession(dataJson.instCode, dataJson.acceNumb, dataJson.genus);
if (accession != null) {
ret.put("id", accession.getId());
// Give them back the UUID
if (accession.getUuid() != null) {
ret.put("uuid", accession.getUuid());
LOG.debug("Loading accession " + acceNumb + " from " + instCode);
if (institute.hasUniqueAcceNumbs()) {
Accession accession = genesysService.getAccession(instCode, acceNumb);
rets.add(accession==null ? null : accession.getId());
} else {
List<Accession> accessions=genesysService.listAccessions(institute, acceNumb);
if (accessions.size()==1) {
rets.add(accessions.get(0).getId());
} else {
ArrayNode ret=rets.arrayNode();
for (Accession accession : accessions) {
ret.add(accession.getId());
}
rets.add(ret);
}
}
rets.add(ret);
}
return rets;
......
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