TaxonomyExtraFilter.java
/*
* Copyright 2020 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.server.service.filter;
import java.util.ArrayList;
import java.util.HashSet;
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.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.util.CurrentApplicationContext;
import org.genesys.server.model.genesys.QTaxonomy2;
import org.genesys.server.model.genesys.Taxonomy2;
import org.genesys.server.service.TaxonomyService;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
/**
* Extra filters for {@link Taxonomy2}.
*/
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class TaxonomyExtraFilter extends AuditedVersionedModelFilter<TaxonomyExtraFilter, Taxonomy2> {
private static final long serialVersionUID = -3827138687385056989L;
/** The overrides. */
public Boolean overrides;
/** The grin. */
public Boolean grin;
public NumberFilter<Integer> accessions;
// Handle synonymous names, null is treated as true
public Boolean synonyms;
/** The genus. */
public Set<String> genus;
/** The specific epithet. */
public Set<String> species;
/** The species. */
public Set<String> genusSpecies;
/** The subtaxa. */
public StringFilter subtaxa;
/** Full taxonomic name */
public StringFilter taxonName;
/** The GRIN Taxon name. */
public Set<String> grinTaxonName;
/** The GRIN current Taxon name. */
public Set<String> grinCurrentName;
/** Current GRIN Taxon ID. */
public Set<Long> grinTaxonId;
/**
* Builds the query.
*
* @return the predicate
*/
public List<Predicate> collectPredicates() {
return collectPredicates(QTaxonomy2.taxonomy2);
}
/**
* Builds the query.
*
* @param taxonomy2 the taxonomy2
* @return the boolean builder
*/
public List<Predicate> collectPredicates(final QTaxonomy2 taxonomy2) {
final List<Predicate> predicates = super.collectPredicates(taxonomy2, taxonomy2._super._super);
final TaxonomyService taxonomyService = CurrentApplicationContext.getContext().getBean(TaxonomyService.class);
if (CollectionUtils.isNotEmpty(genus) && taxonomyService != 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 = taxonomyService.findTaxonomy2GenusId(new ArrayList<>(genus));
List<Long> taxByGrinGenus = taxonomyService.findTaxonomy2ByGrinGenus(new ArrayList<>(genus));
BooleanBuilder builder = new BooleanBuilder(taxonomy2.taxGenus.in(taxGenus));
if (!taxByGrinGenus.isEmpty()) {
builder.or(taxonomy2.id.in(taxByGrinGenus));
if (synonyms == null || synonyms) {
List<Long> taxByCurrentGrinGenus = taxonomyService.findTaxonomy2ByCurrentGrinGenus(new ArrayList<>(genus));
if (! taxByCurrentGrinGenus.isEmpty()) {
builder.or(taxonomy2.id.in(taxByCurrentGrinGenus));
}
}
}
predicates.add(builder);
}
if (species != null && !species.isEmpty()) {
BooleanBuilder builder = new BooleanBuilder(taxonomy2.species.in(species));
builder.or(taxonomy2.grinTaxonomySpecies().speciesName.in(species));
if (synonyms == null || synonyms) {
builder.or(taxonomy2.currentTaxonomySpecies().speciesName.in(species));
}
predicates.add(builder);
}
if (subtaxa != null) {
BooleanBuilder builder = new BooleanBuilder(subtaxa.buildQuery(taxonomy2.subtaxa));
builder.or(subtaxa.buildQuery(taxonomy2.grinTaxonomySpecies().subtaxa));
if (synonyms == null || synonyms) {
builder.or(subtaxa.buildQuery(taxonomy2.currentTaxonomySpecies().subtaxa));
}
predicates.add(builder);
}
if (taxonName != null) {
BooleanBuilder builder = new BooleanBuilder(taxonName.buildQuery(taxonomy2.taxonName));
builder.or(taxonName.buildQuery(taxonomy2.grinTaxonomySpecies().name));
if (synonyms == null || synonyms) {
builder.or(taxonName.buildQuery(taxonomy2.currentTaxonomySpecies().name));
}
predicates.add(builder);
}
if (CollectionUtils.isNotEmpty(genusSpecies) && taxonomyService != 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 = taxonomyService.findTaxonomy2SpeciesId(new ArrayList<>(genusSpecies));
List<Long> taxByGrinSpecies = taxonomyService.findTaxonomy2ByGrinSpecies(new ArrayList<>(genusSpecies));
BooleanBuilder builder = new BooleanBuilder(taxonomy2.taxSpecies.in(taxSpecies));
if (!taxByGrinSpecies.isEmpty()) {
builder.or(taxonomy2.id.in(taxByGrinSpecies));
if (synonyms == null || synonyms) {
List<Long> taxByCurrentGrinSpecies = taxonomyService.findTaxonomy2ByCurrentGrinSpecies(new ArrayList<>(genusSpecies));
if (! taxByCurrentGrinSpecies.isEmpty()) {
builder.or(taxonomy2.id.in(taxByCurrentGrinSpecies));
}
}
}
predicates.add(builder);
}
if (CollectionUtils.isNotEmpty(grinTaxonName) && taxonomyService != null) {
List<Long> taxByGrinNames = taxonomyService.findTaxonomy2ByGrinNames(new ArrayList<>(grinTaxonName));
predicates.add(taxonomy2.id.in(taxByGrinNames));
}
if (CollectionUtils.isNotEmpty(grinCurrentName) && taxonomyService != null) {
List<Long> taxByCurrentGrinNames = taxonomyService.findTaxonomy2ByCurrentGrinNames(new ArrayList<>(grinCurrentName));
predicates.add(taxonomy2.id.in(taxByCurrentGrinNames));
}
if (CollectionUtils.isNotEmpty(grinTaxonId) && taxonomyService != null) {
Set<Long> taxonomy2Id = new HashSet<>();
taxonomy2Id.addAll(taxonomyService.listTaxonomy2ByGrinId(grinTaxonId));
if (synonyms == null || synonyms) {
taxonomy2Id.addAll(taxonomyService.listTaxonomy2ByCurrentGrinId(grinTaxonId));
}
predicates.add(taxonomy2.id.in(taxonomy2Id));
}
if (overrides != null) {
if (overrides) {
predicates.add(taxonomy2.overrideTaxonomySpecies().isNotNull());
} else {
predicates.add(taxonomy2.overrideTaxonomySpecies().isNull());
}
}
if (grin != null) {
if (grin) {
predicates.add(taxonomy2.grinTaxonomySpecies().isNotNull());
} else {
predicates.add(taxonomy2.grinTaxonomySpecies().isNull());
}
}
if (accessions != null) {
predicates.add(accessions.buildQuery(taxonomy2.accessions.size()));
}
return predicates;
}
}