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

Moved JSON classes to .....rest.model

parent 680fd109
......@@ -16,59 +16,18 @@
package org.genesys2.server.service;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.servlet.controller.rest.model.AccessionNamesJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionJson;
import com.fasterxml.jackson.databind.node.ObjectNode;
public interface BatchRESTService {
boolean upsertAccessionData(FaoInstitute institute, Map<DataJson, ObjectNode> batch);
boolean upsertAccessionData(FaoInstitute institute, Map<AccessionJson, ObjectNode> batch);
void upsertAccessionNames(FaoInstitute institute, List<AcceNames> batch);
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);
}
}
public static class AcceNames extends BatchRESTService.DataJson {
public List<AcceAlias> aliases;
}
public static class AcceAlias {
public String name;
public String instCode;
public int type = 0;
@Override
public String toString() {
return "" + name + "@" + instCode;
}
}
void upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch);
}
......@@ -40,6 +40,9 @@ 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.servlet.controller.rest.model.AccessionAliasJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionNamesJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
......@@ -72,13 +75,13 @@ public class BatchRESTServiceImpl implements BatchRESTService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE') or hasPermission(#institute, 'CREATE')")
public boolean upsertAccessionData(FaoInstitute institute, Map<BatchRESTService.DataJson, ObjectNode> batch) {
public boolean upsertAccessionData(FaoInstitute institute, Map<AccessionJson, ObjectNode> batch) {
LOG.info("Batch processing " + batch.size() + " entries for " + institute);
List<Accession> toSave = new ArrayList<Accession>();
List<AllAcqCollect> toSaveColl = new ArrayList<AllAcqCollect>();
List<AccessionGeo> toSaveGeo = new ArrayList<AccessionGeo>();
for (BatchRESTService.DataJson dataJson : batch.keySet()) {
for (AccessionJson dataJson : batch.keySet()) {
if (LOG.isDebugEnabled())
LOG.debug("Loading accession " + dataJson);
......@@ -255,13 +258,13 @@ public class BatchRESTServiceImpl implements BatchRESTService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE') or hasPermission(#institute, 'CREATE')")
public void upsertAccessionNames(FaoInstitute institute, List<AcceNames> batch) {
public void upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch) {
LOG.info("Batch processing " + batch.size() + " entries for " + institute);
List<AccessionAlias> toSave = new ArrayList<AccessionAlias>();
List<AccessionAlias> toRemove = new ArrayList<AccessionAlias>();
for (AcceNames dataJson : batch) {
for (AccessionNamesJson dataJson : batch) {
Accession accession = genesysService.getAccession(institute.getCode(), dataJson.genus, dataJson.acceNumb);
if (accession == null) {
LOG.warn("No such accession " + dataJson);
......@@ -272,9 +275,9 @@ public class BatchRESTServiceImpl implements BatchRESTService {
List<AccessionAlias> existingAliases = genesysService.listAccessionAliases(accession);
// Find aliases to remove
for (final AccessionAlias aa : existingAliases) {
if (null == CollectionUtils.find(dataJson.aliases, new Predicate<AcceAlias>() {
if (null == CollectionUtils.find(dataJson.aliases, new Predicate<AccessionAliasJson>() {
@Override
public boolean evaluate(AcceAlias alias) {
public boolean evaluate(AccessionAliasJson alias) {
return StringUtils.equals(alias.name, aa.getName());
}
})) {
......@@ -282,7 +285,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
}
// Add or update
for (final AcceAlias aa : dataJson.aliases) {
for (final AccessionAliasJson aa : dataJson.aliases) {
AccessionAlias accessionAlias = CollectionUtils.find(existingAliases, new Predicate<AccessionAlias>() {
@Override
public boolean evaluate(AccessionAlias alias) {
......
......@@ -28,6 +28,8 @@ 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.servlet.controller.rest.model.AccessionNamesJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionJson;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
......@@ -100,18 +102,18 @@ public class AccessionController extends RestController {
JsonNode check(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException {
// TODO Check user's permissions to update this WIEWS institute.
JsonNode json = mapper.readTree(content);
Map<BatchRESTService.DataJson, ObjectNode> batch = new HashMap<BatchRESTService.DataJson, ObjectNode>();
Map<AccessionJson, ObjectNode> batch = new HashMap<AccessionJson, ObjectNode>();
if (json.isArray()) {
for (JsonNode j : json) {
BatchRESTService.DataJson dataJson = readAid3(j);
AccessionJson dataJson = readAid3(j);
if (!instCode.equals(dataJson.instCode)) {
throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + dataJson);
}
batch.put(dataJson, (ObjectNode) j);
}
} else {
BatchRESTService.DataJson dataJson = readAid3(json);
AccessionJson dataJson = readAid3(json);
if (!instCode.equals(dataJson.instCode)) {
throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + dataJson);
}
......@@ -121,7 +123,7 @@ public class AccessionController extends RestController {
LOG.info("Batch processing " + batch.size() + " entries");
ArrayNode rets = mapper.createArrayNode();
for (BatchRESTService.DataJson dataJson : batch.keySet()) {
for (AccessionJson dataJson : batch.keySet()) {
if (LOG.isDebugEnabled())
LOG.debug("Loading accession " + dataJson);
......@@ -164,18 +166,18 @@ public class AccessionController extends RestController {
}
JsonNode json = mapper.readTree(content);
Map<BatchRESTService.DataJson, ObjectNode> batch = new HashMap<BatchRESTService.DataJson, ObjectNode>();
Map<AccessionJson, ObjectNode> batch = new HashMap<AccessionJson, ObjectNode>();
if (json.isArray()) {
for (JsonNode j : json) {
BatchRESTService.DataJson dataJson = readAid3(j);
AccessionJson dataJson = readAid3(j);
if (!instCode.equals(dataJson.instCode)) {
throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + dataJson);
}
batch.put(dataJson, (ObjectNode) j);
}
} else {
BatchRESTService.DataJson dataJson = readAid3(json);
AccessionJson dataJson = readAid3(json);
if (!instCode.equals(dataJson.instCode)) {
throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + dataJson);
}
......@@ -195,7 +197,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<BatchRESTService.AcceNames> batch) {
String upsertAccessionNames(@PathVariable("instCode") String instCode, @RequestBody List<AccessionNamesJson> batch) {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
FaoInstitute institute = instituteService.getInstitute(instCode);
......@@ -207,8 +209,8 @@ public class AccessionController extends RestController {
return JSON_OK;
}
private BatchRESTService.DataJson readAid3(JsonNode json) {
BatchRESTService.DataJson dataJson = new BatchRESTService.DataJson();
private AccessionJson readAid3(JsonNode json) {
AccessionJson dataJson = new AccessionJson();
dataJson.instCode = json.has("instCode") ? json.get("instCode").textValue() : null;
dataJson.acceNumb = json.has("acceNumb") ? json.get("acceNumb").textValue() : null;
......
package org.genesys2.server.servlet.controller.rest.model;
public class AccessionAliasJson {
public String name;
public String instCode;
public int type = 0;
@Override
public String toString() {
return "" + name + "@" + instCode;
}
}
\ No newline at end of file
package org.genesys2.server.servlet.controller.rest.model;
import java.text.MessageFormat;
import org.genesys2.server.model.impl.AccessionIdentifier3;
public class AccessionJson 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);
}
}
\ No newline at end of file
package org.genesys2.server.servlet.controller.rest.model;
import java.util.List;
public class AccessionNamesJson extends AccessionJson {
public List<AccessionAliasJson> aliases;
}
\ No newline at end of file
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