Commit 2c472049 authored by Matija Obreza's avatar Matija Obreza
Browse files

REST Upsert accessions for instCode

parent f9ec8d8d
......@@ -25,6 +25,8 @@ public interface GenesysLowlevelRepository {
void updateCountryRefs();
void updateFaoInstituteRefs();
void updateInstituteAccessionCount(long instituteId);
/**
* Relink {@link FaoInstitute} with {@link Country} based on institute code
......@@ -32,5 +34,4 @@ public interface GenesysLowlevelRepository {
void updateFaoInstituteCountries();
void ensureMethodTable(Method method);
}
......@@ -56,6 +56,15 @@ public class GenesysLowlevelRepositoryImpl implements GenesysLowlevelRepository
.update("UPDATE faoinstitute FI inner join (select instituteId, count(*) X from accession group by instituteId) A on A.instituteId=FI.id set FI.accessionCount=A.X;");
LOG.info("Institute accession counts updated: " + count);
}
@Override
@Transactional(readOnly = false)
public void updateInstituteAccessionCount(long instituteId) {
LOG.info("Updating institute accession count for instituteId=" + instituteId);
int count = jdbcTemplate
.update("UPDATE faoinstitute FI inner join (select instituteId, count(*) X from accession where instituteId= ? group by instituteId) A on A.instituteId=FI.id set FI.accessionCount=A.X where FI.id = ?;", instituteId, instituteId);
LOG.info("Institute accession counts updated: " + count);
}
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
......
......@@ -118,4 +118,6 @@ public interface GenesysService {
void saveAccession(Accession... accession);
void updateAccessionCount(FaoInstitute institute);
}
......@@ -382,6 +382,12 @@ public class GenesysServiceImpl implements GenesysService, TraitService, Dataset
genesysLowlevelRepository.updateFaoInstituteRefs();
}
@Override
@Transactional(readOnly = false)
public void updateAccessionCount(FaoInstitute institute) {
genesysLowlevelRepository.updateInstituteAccessionCount(institute.getId());
}
@Override
@Transactional(readOnly = false)
public void saveAccessions(List<Accession> accessions) {
......
......@@ -25,7 +25,12 @@ import java.util.Map;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
......@@ -53,6 +58,12 @@ public class AccessionController extends RestController {
@Autowired
GenesysService genesysService;
@Autowired
InstituteService instituteService;
@Autowired
GeoService geoService;
/**
* Check if accessions exists in the system
*
......@@ -136,6 +147,11 @@ public class AccessionController extends RestController {
public @ResponseBody
boolean updateInstituteAccession(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException {
// TODO Check user's permissions to update this WIEWS institute.
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
throw new ResourceNotFoundException();
}
JsonNode json = mapper.readTree(content);
Map<DataJson, ObjectNode> batch = new HashMap<DataJson, ObjectNode>();
......@@ -184,6 +200,110 @@ public class AccessionController extends RestController {
}
}
value = accnJson.get("availability");
if (value != null) {
Boolean availability = value.isNull() ? null : value.asBoolean();
if (availability != accession.getAvailability()) {
if (LOG.isDebugEnabled())
LOG.debug("Setting Availability to " + availability);
accession.setAvailability(availability);
updated = true;
}
}
if (updated) {
toSave.add(accession);
}
}
if (toSave.size() > 0) {
LOG.info("Storing " + toSave.size() + " accessions.");
genesysService.saveAccessions(toSave);
}
return toSave.size() > 0;
}
/**
* Update accessions in the system
*
* @return
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/upsert", method = { RequestMethod.POST, RequestMethod.PUT }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
boolean upsertInstituteAccession(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException {
// TODO Check user's permissions to update this WIEWS institute.
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
throw new ResourceNotFoundException();
}
JsonNode json = mapper.readTree(content);
Map<DataJson, ObjectNode> batch = new HashMap<DataJson, ObjectNode>();
if (json.isArray()) {
for (JsonNode j : json) {
DataJson 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 {
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);
}
LOG.info("Batch processing " + batch.size() + " entries");
List<Accession> toSave = new ArrayList<Accession>();
for (DataJson dataJson : batch.keySet()) {
if (LOG.isDebugEnabled())
LOG.debug("Loading accession " + dataJson);
Accession accession = genesysService.getAccession(dataJson.instCode, dataJson.genus, dataJson.acceNumb);
boolean updated = false;
if (accession == null) {
LOG.warn("New accession " + dataJson);
accession = new Accession();
accession.setInstituteCode(dataJson.instCode);
accession.setInstitute(institute);
accession.setGenus(dataJson.genus);
accession.setAccessionName(dataJson.acceNumb);
updated = true;
}
ObjectNode accnJson = batch.get(dataJson);
JsonNode value = accnJson.get("orgCty");
if (value != null) {
String orgCty = value.textValue();
// Validate!!!
Country country = geoService.getCountry(orgCty);
accession.setOrigin(orgCty);
accession.setCountryOfOrigin(country);
updated = true;
}
value = accnJson.get("mlsStat");
if (value != null) {
Boolean inMls = value.isNull() ? null : value.asBoolean();
if (inMls != accession.getMlsStatus()) {
accession.setMlsStatus(inMls);
updated = true;
}
}
if (updated) {
toSave.add(accession);
}
......@@ -192,6 +312,7 @@ public class AccessionController extends RestController {
if (toSave.size() > 0) {
LOG.info("Storing " + toSave.size() + " accessions.");
genesysService.saveAccessions(toSave);
genesysService.updateAccessionCount(institute);
}
return toSave.size() > 0;
......
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