Commit 294271f1 authored by Matija Obreza's avatar Matija Obreza

Merge branch '434-filter-by-genus-species'

* 434-filter-by-genus-species:
  TaxonomyFilter on `genusSpecies`
parents e4d7f563 fa0a30e7
......@@ -23,7 +23,6 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
......@@ -71,14 +70,10 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Sets;
import com.hazelcast.util.CollectionUtil;
import io.swagger.annotations.Api;
......@@ -128,10 +123,7 @@ public class AccessionController {
private final Set<String> terms = Sets.newHashSet("institute.code", "institute.country.code3", "cropName", "crop.shortName", "taxonomy.genus", "taxonomy.species",
"taxonomy.genusSpecies", "countryOfOrigin.code3", "sampStat", "available", "mlsStatus", "donorCode", "sgsv", "storage", "duplSite", "breederCode");
private ObjectMapper objectMapper = new ObjectMapper();
@GetMapping(value = "/id/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@GetMapping(value = "/id/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
public UUID uuidFromId(@PathVariable("id") final long id) {
return accessionService.uuidFromId(id);
}
......@@ -371,10 +363,6 @@ public class AccessionController {
return geoJson;
}
private AccessionFilter copyFilter(AccessionFilter filter) throws JsonParseException, JsonMappingException, JsonProcessingException, IOException {
return objectMapper.readValue(objectMapper.writeValueAsString(filter), AccessionFilter.class);
}
@GetMapping(value = "/autocomplete/{field:.+}", produces = MediaType.APPLICATION_JSON_VALUE)
public List<LabelValue<String>> autocomplete(@PathVariable("field") String field,
@RequestParam(value = "term", required = true) String term,
......
......@@ -86,4 +86,10 @@ public interface Taxonomy2Repository extends JpaRepository<Taxonomy2, Long> {
@Query("delete from Taxonomy2 t where t.id in (?1)")
@Modifying
void removeUnusedIds(Set<Long> ids);
@Query("select t.id from Taxonomy2 t where t.id = t.taxGenus and t.genus in (?1)")
List<Long> findTaxGenusId(List<String> genera);
@Query("select t.id from Taxonomy2 t where t.id = t.taxSpecies and t.taxonName in (?1)")
List<Long> findTaxSpeciesId(List<String> speciesNames);
}
......@@ -17,27 +17,40 @@ package org.genesys2.server.service.filter;
import static org.genesys2.server.model.genesys.QTaxonomy2.taxonomy2;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.AuditedVersionedModelFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.util.CurrentApplicationContext;
import org.genesys2.server.model.genesys.QTaxonomy2;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.persistence.Taxonomy2Repository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
// TODO: Auto-generated Javadoc
/**
* The Class TaxonomyFilter.
*/
public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter, Taxonomy2> {
private static Taxonomy2Repository taxonomyRepository;
static {
taxonomyRepository = CurrentApplicationContext.getContext().getBean(Taxonomy2Repository.class);
}
/** The genus. */
public Set<String> genus;
/** The species. */
/** The specific epithet. */
public Set<String> species;
/** The species. */
public Set<String> genusSpecies;
/** The subtaxa. */
public StringFilter subtaxa;
......@@ -64,19 +77,32 @@ public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter,
final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(taxonomy2, taxonomy2._super._super, and);
if (genus != null && !genus.isEmpty()) {
and.and(taxonomy.genus.in(genus));
}
if (species != null && !species.isEmpty()) {
and.and(taxonomy.species.in(species));
}
if (subtaxa!=null) {
and.and(subtaxa.buildQuery(taxonomy.subtaxa));
}
if (taxonName!=null) {
and.and(taxonName.buildQuery(taxonomy.taxonName));
}
if (CollectionUtils.isNotEmpty(genus) && taxonomyRepository != null) {
// We need to look up the taxonomy2.id for provided names so that ES queries also work out of the box
List<Long> taxGenus = taxonomyRepository.findTaxGenusId(new ArrayList<>(genus));
if (CollectionUtils.isNotEmpty(taxGenus)) {
and.and(taxonomy.taxGenus.in(taxGenus));
}
}
if (species != null && !species.isEmpty()) {
and.and(taxonomy.species.in(species));
}
if (subtaxa != null) {
and.and(subtaxa.buildQuery(taxonomy.subtaxa));
}
if (taxonName != null) {
and.and(taxonName.buildQuery(taxonomy.taxonName));
}
if (CollectionUtils.isNotEmpty(genusSpecies) && taxonomyRepository != null) {
// We need to look up the taxonomy2.id for provided names so that ES queries also work out of the box
List<Long> taxSpecies = taxonomyRepository.findTaxSpeciesId(new ArrayList<>(genusSpecies));
if (CollectionUtils.isNotEmpty(taxSpecies)) {
and.and(taxonomy.taxSpecies.in(taxSpecies));
}
}
return and;
}
}
......@@ -79,7 +79,7 @@ public class AccessionServiceImpl implements AccessionService {
public static final Logger LOG = LoggerFactory.getLogger(AccessionServiceImpl.class);
private final Set<String> suggestions = Sets.newHashSet("crop.shortName", "institute.code", "taxonomy.genus", "countryOfOrigin.code3", "historic", "sampStat", "available", "mlsStatus", "sgsv", "storage");
private final Set<String> suggestions = Sets.newHashSet("crop.shortName", "institute.code", "taxonomy.genus", "taxonomy.genusSpecies", "taxonomy.species", "countryOfOrigin.code3", "historic", "sampStat", "available", "mlsStatus", "sgsv", "storage");
/** The accession repository. */
@Autowired
......
......@@ -5808,3 +5808,12 @@ databaseChangeLog:
comment: dataset_version#currentVersion_id must be null if subset is not published
sql: update dataset_version dv inner join dataset d on dv.currentVersion_id = d.id
set dv.currentVersion_id = null where d.state != 1
- changeSet:
id: 1554822569-1
author: mobreza
comment: Create short index on taxonName
changes:
- sql:
sql: >-
CREATE INDEX IX_taxonName ON taxonomy2(taxonName(250))
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