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

TaxonomyService#find(genus, sp, ...) is cached

parent 48253342
......@@ -44,6 +44,7 @@ public interface Taxonomy2Repository extends JpaRepository<Taxonomy2, Long> {
@Query("select distinct t.taxonName from Taxonomy2 t where t.taxonName like ?1")
List<String> autocompleteTaxonomy(String term, Pageable page);
@Query("select t from Taxonomy2 t where lower(t.genus) = lower(?1) and lower(t.species) = lower(?2) and lower(t.spAuthor) = lower(?3) and lower(t.subtaxa) = lower(?4) and lower(t.subtAuthor) = lower(?5)")
Taxonomy2 findByGenusAndSpeciesAndSpAuthorAndSubtaxaAndSubtAuthor(String genus, String species, String spAuthor, String subtaxa, String subtAuthor);
List<Taxonomy2> findByGenus(String genus);
......
......@@ -43,4 +43,6 @@ public interface TaxonomyService {
Taxonomy2 get(String genus, String species);
Taxonomy2 find(String genus, String species, String spAuthor, String subtaxa, String subtAuthor);
}
......@@ -41,6 +41,7 @@ import org.genesys2.server.model.genesys.AccessionRemark;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.domain.AccessionCustomRepository;
import org.genesys2.server.service.BatchRESTService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
......@@ -80,6 +81,9 @@ public class BatchRESTServiceImpl implements BatchRESTService {
@Autowired
InstituteService instituteService;
@Autowired
AccessionCustomRepository accessionCustomRepository;
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE') or hasPermission(#institute, 'CREATE')")
......@@ -105,6 +109,22 @@ public class BatchRESTServiceImpl implements BatchRESTService {
final Map<Accession, ArrayNode> donorNumbs = new HashMap<Accession, ArrayNode>();
final Map<Accession, ArrayNode> collNumbs = new HashMap<Accession, ArrayNode>();
List<String> acceNumbs = new ArrayList<String>();
List<String> genera = new ArrayList<String>();
for (AccessionHeaderJson dataJson : batch.keySet()) {
acceNumbs.add(dataJson.acceNumb);
genera.add(dataJson.genus);
}
List<Accession> loaded = null;
try {
loaded = accessionCustomRepository.find(institute, acceNumbs, genera);
} catch (NonUniqueAccessionException e) {
LOG.warn(e.getMessage());
throw new RESTApiException(e.getMessage());
}
for (final AccessionHeaderJson dataJson : batch.keySet()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Loading accession " + dataJson);
......@@ -114,24 +134,24 @@ public class BatchRESTServiceImpl implements BatchRESTService {
throw new RuntimeException("Accession does not belong to instCode=" + institute.getCode() + " acn=" + dataJson);
}
Accession accession = null;
try {
if (useUniqueAcceNumbs) {
accession = genesysService.getAccession(dataJson.instCode, dataJson.acceNumb);
} else {
accession = genesysService.getAccession(dataJson.instCode, dataJson.acceNumb, dataJson.genus);
Accession accession = CollectionUtils.find(loaded, new Predicate<Accession>() {
@Override
public boolean evaluate(Accession a) {
if (useUniqueAcceNumbs) {
return a.getAccessionName().equalsIgnoreCase(dataJson.acceNumb);
} else {
return a.getAccessionName().equalsIgnoreCase(dataJson.acceNumb) && a.getTaxonomy().getGenus().equalsIgnoreCase(dataJson.genus);
}
}
} catch (NonUniqueAccessionException e) {
LOG.warn(e.getMessage());
throw new RESTApiException(e.getMessage());
}
});
boolean updated = false;
final ObjectNode accnJson = batch.get(dataJson);
if (accession == null) {
LOG.warn("New accession " + dataJson);
if (LOG.isDebugEnabled())
LOG.debug("New accession " + dataJson);
accession = new Accession();
accession.setInstitute(institute);
......@@ -142,6 +162,9 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
updated = true;
} else {
if (LOG.isTraceEnabled())
LOG.trace("*** Updating accession " + dataJson);
}
if (accession.getId() == null || useUniqueAcceNumbs && accnJson.get("genus") != null || accnJson.get("newGenus") != null
......@@ -646,18 +669,27 @@ public class BatchRESTServiceImpl implements BatchRESTService {
// Load JSON values into "current"
current.setGenus(stringIfProvided(accnJson.get("genus"), current.getGenus()));
current.setGenus(stringIfProvided(accnJson.get("newGenus"), current.getGenus()));
current.setSpecies(stringIfProvided(accnJson.get("species"), current.getSpecies()));
current.setSpAuthor(stringIfProvided(accnJson.get("spauthor"), current.getSpAuthor()));
current.setSubtaxa(stringIfProvided(accnJson.get("subtaxa"), current.getSubtaxa()));
current.setSubtAuthor(stringIfProvided(accnJson.get("subtauthor"), current.getSubtAuthor()));
current.setSpecies(StringUtils.defaultIfBlank(stringIfProvided(accnJson.get("species"), current.getSpecies()), "sp."));
current.setSpAuthor(StringUtils.defaultIfBlank(stringIfProvided(accnJson.get("spauthor"), current.getSpAuthor()), ""));
current.setSubtaxa(StringUtils.defaultIfBlank(stringIfProvided(accnJson.get("subtaxa"), current.getSubtaxa()), ""));
current.setSubtAuthor(StringUtils.defaultIfBlank(stringIfProvided(accnJson.get("subtauthor"), current.getSubtAuthor()), ""));
final Taxonomy2 ensuredTaxonomy = taxonomyService.ensureTaxonomy2(current.getGenus(), current.getSpecies(), current.getSpAuthor(),
current.getSubtaxa(), current.getSubtAuthor());
Taxonomy2 ensuredTaxonomy = null;
try {
ensuredTaxonomy = taxonomyService.find(current.getGenus(), current.getSpecies(), current.getSpAuthor(), current.getSubtaxa(),
current.getSubtAuthor());
} catch (Throwable e) {
LOG.warn("*** lower(t.genus)=lower(" + current.getGenus() + ") and lower(t.species)=lower(" + current.getSpecies()
+ ") and lower(t.spauthor)=lower(" + current.getSpAuthor() + ") and lower(t.subtaxa)=lower(" + current.getSubtaxa()
+ ") and lower(subtauthor)=lower(" + current.getSubtAuthor() + ")");
}
if (ensuredTaxonomy == null) {
ensuredTaxonomy = taxonomyService.ensureTaxonomy2(current.getGenus(), current.getSpecies(), current.getSpAuthor(), current.getSubtaxa(),
current.getSubtAuthor());
}
if (!ensuredTaxonomy.sameAs(taxonomy)) {
accession.setTaxonomy(ensuredTaxonomy);
updated = true;
}
......
......@@ -31,6 +31,7 @@ import org.genesys2.server.persistence.domain.Taxonomy2Repository;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.TaxonomyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
......@@ -45,7 +46,7 @@ public class TaxonomyServiceImpl implements TaxonomyService {
@Autowired
private Taxonomy2Repository taxonomy2Repository;
@Autowired
private CropService cropService;
......@@ -121,7 +122,7 @@ public class TaxonomyServiceImpl implements TaxonomyService {
species = "sp.";
}
final Taxonomy2 existing = taxonomy2Repository.findByGenusAndSpeciesAndSpAuthorAndSubtaxaAndSubtAuthor(genus, species, spAuthor, subtaxa, subtAuthor);
final Taxonomy2 existing = find(genus, species, spAuthor, subtaxa, subtAuthor);
if (existing == null) {
try {
Taxonomy2 taxonomy = internalEnsure(genus, species, spAuthor, subtaxa, subtAuthor);
......@@ -134,6 +135,12 @@ public class TaxonomyServiceImpl implements TaxonomyService {
return existing;
}
@Override
@Cacheable(value = "hibernate.org.genesys2.server.model.impl.Taxonomy2.fullname", key = "#genus + '-' + #species + '-' + #spAuthor + '-' + #subtaxa + '-' + #subtAuthor")
public Taxonomy2 find(String genus, String species, String spAuthor, String subtaxa, String subtAuthor) {
return taxonomy2Repository.findByGenusAndSpeciesAndSpAuthorAndSubtaxaAndSubtAuthor(genus, species, spAuthor, subtaxa, subtAuthor);
}
private synchronized Taxonomy2 internalEnsure(String genus, String species, String spAuthor, String subtaxa, String subtAuthor) throws InterruptedException {
Long taxSpeciesId = null, taxGenusId = null;
......@@ -185,7 +192,7 @@ public class TaxonomyServiceImpl implements TaxonomyService {
// Update crop taxonomy lists
cropService.updateCropTaxonomyLists(taxonomy);
return taxonomy;
} catch (final Throwable e) {
......@@ -204,18 +211,18 @@ public class TaxonomyServiceImpl implements TaxonomyService {
@Override
public long getTaxonomy2Id(String genus) {
return taxonomy2Repository.findByGenusAndSpeciesAndSpAuthorAndSubtaxaAndSubtAuthor(genus, "sp.", "", "", "").getId();
return find(genus, "sp.", "", "", "").getId();
}
@Override
public long getTaxonomy2Id(String genus, String species) {
final Taxonomy2 tax = taxonomy2Repository.findByGenusAndSpeciesAndSpAuthorAndSubtaxaAndSubtAuthor(genus, species, "", "", "");
final Taxonomy2 tax = find(genus, species, "", "", "");
return tax.getId();
}
@Override
public Taxonomy2 get(String genus, String species) {
return taxonomy2Repository.findByGenusAndSpeciesAndSpAuthorAndSubtaxaAndSubtAuthor(genus, species, "", "", "");
return find(genus, species, "", "", "");
}
@Override
......@@ -225,6 +232,6 @@ public class TaxonomyServiceImpl implements TaxonomyService {
@Override
public Taxonomy2 get(String genus) {
return taxonomy2Repository.findByGenusAndSpeciesAndSpAuthorAndSubtaxaAndSubtAuthor(genus, "sp.", "", "", "");
return find(genus, "sp.", "", "", "");
}
}
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