Commit c7f90072 authored by Matija Obreza's avatar Matija Obreza
Browse files

REST /api/v0/acn/{instCode}/update accepts array of accessions for

MLSSTAT update
parent 0b04bf65
...@@ -17,12 +17,15 @@ ...@@ -17,12 +17,15 @@
package org.genesys2.server.servlet.controller.rest; package org.genesys2.server.servlet.controller.rest;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator; import java.text.MessageFormat;
import java.util.Map.Entry; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.service.GenesysService; import org.genesys2.server.service.GenesysService;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
...@@ -37,6 +40,7 @@ import org.springframework.web.bind.annotation.ResponseBody; ...@@ -37,6 +40,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
@Controller("restAccessionController") @Controller("restAccessionController")
@PreAuthorize("isAuthenticated()") @PreAuthorize("isAuthenticated()")
...@@ -80,58 +84,99 @@ public class AccessionController extends RestController { ...@@ -80,58 +84,99 @@ public class AccessionController extends RestController {
boolean updateInstituteAccession(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException { boolean updateInstituteAccession(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException {
// TODO Check user's permissions to update this WIEWS institute. // TODO Check user's permissions to update this WIEWS institute.
JsonNode json = mapper.readTree(content); JsonNode json = mapper.readTree(content);
Map<DataJson, ObjectNode> batch = new HashMap<DataJson, ObjectNode>();
String genus = null; if (json.isArray()) {
String acceNumb = null; for (JsonNode j : json) {
DataJson dataJson = readAid3(j);
for (Iterator<Entry<String, JsonNode>> fields = json.fields(); fields.hasNext();) { if (!instCode.equals(dataJson.instCode)) {
Entry<String, JsonNode> field = fields.next(); throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + dataJson);
}
String key = field.getKey(); batch.put(dataJson, (ObjectNode) j);
JsonNode value = field.getValue();
if ("genus".equals(key)) {
genus = value.textValue();
} else if ("acceNumb".equals(key)) {
acceNumb = value.textValue();
} }
} else {
DataJson dataJson = readAid3(json);
if (!instCode.equals(dataJson.instCode)) {
throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + dataJson);
}
batch.put(dataJson, (ObjectNode) json);
} }
if (LOG.isDebugEnabled()) LOG.info("Batch processing " + batch.size() + " entries");
LOG.debug("Loading accession " + instCode + "." + acceNumb + " genus=" + genus); List<Accession> toSave = new ArrayList<Accession>();
Accession accession = genesysService.getAccession(instCode, genus, acceNumb);
if (accession == null) { for (DataJson dataJson : batch.keySet()) {
LOG.warn("No accession " + instCode + "." + acceNumb + " genus=" + genus); if (LOG.isDebugEnabled())
throw new ResourceNotFoundException(); LOG.debug("Loading accession " + dataJson);
}
boolean updated = false; Accession accession = genesysService.getAccession(dataJson.instCode, dataJson.genus, dataJson.acceNumb);
for (Iterator<Entry<String, JsonNode>> fields = json.fields(); fields.hasNext();) { if (accession == null) {
Entry<String, JsonNode> field = fields.next(); // LOG.warn("No accession " + dataJson);
continue;
}
String key = field.getKey(); ObjectNode accnJson = batch.get(dataJson);
JsonNode value = field.getValue(); boolean updated = false;
if ("mlsStat".equals(key)) { JsonNode value = accnJson.get("mlsStat");
if (value != null) {
Boolean inMls = value.isNull() ? null : value.asBoolean(); Boolean inMls = value.isNull() ? null : value.asBoolean();
if (inMls != accession.getMlsStatus()) { if (inMls != accession.getMlsStatus()) {
if (LOG.isInfoEnabled()) if (LOG.isDebugEnabled())
LOG.info("Setting MLSSTAT to " + inMls); LOG.debug("Setting MLSSTAT to " + inMls);
accession.setMlsStatus(inMls); accession.setMlsStatus(inMls);
updated = true; updated = true;
} }
} }
if (updated) {
toSave.add(accession);
}
} }
if (updated) { if (toSave.size() > 0) {
genesysService.saveAccession(accession); LOG.info("Storing " + toSave.size() + " accessions.");
genesysService.saveAccessions(toSave);
} }
return updated; return toSave.size() > 0;
}
private DataJson readAid3(JsonNode json) {
DataJson dataJson = new DataJson();
dataJson.instCode = json.has("instCode") ? json.get("instCode").textValue() : null;
dataJson.acceNumb = json.has("acceNumb") ? json.get("acceNumb").textValue() : null;
dataJson.genus = json.has("genus") ? json.get("genus").textValue() : null;
return dataJson;
}
public static class DataJson implements AccessionIdentifier3 {
public String instCode;
public String acceNumb;
public String genus;
@Override
public String getHoldingInstitute() {
return instCode;
}
@Override
public String getAccessionName() {
return acceNumb;
}
@Override
public String getGenus() {
return genus;
}
@Override
public String toString() {
return MessageFormat.format("AID3 instCode={0} acceNumb={1} genus={2}", instCode, acceNumb, genus);
}
} }
} }
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