TaxonomyFilter.java

/*
 * Copyright 2018 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 java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.filters.AuditedVersionedModelFilter;
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;

/**
 * The Class TaxonomyFilter.
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter, Taxonomy2> {

	private static final long serialVersionUID = 2428438809723761271L;

	// 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;

	/** The family name. */
	public Set<String> family;

	/**
	 * Builds the query.
	 *
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates() {
		return collectPredicates(QTaxonomy2.taxonomy2);
	}

	/**
	 * Builds the query.
	 *
	 * @param taxonomy the taxonomy
	 * @return the boolean builder
	 */
	public List<Predicate> collectPredicates(final QTaxonomy2 taxonomy) {
		final List<Predicate> predicates = super.collectPredicates(taxonomy, taxonomy._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(taxonomy.taxGenus.in(taxGenus));
			if (!taxByGrinGenus.isEmpty()) {
				builder.or(taxonomy.id.in(taxByGrinGenus));
				if (synonyms == null || synonyms) {
					List<Long> taxByCurrentGrinGenus = taxonomyService.findTaxonomy2ByCurrentGrinGenus(new ArrayList<>(genus));
					if (! taxByCurrentGrinGenus.isEmpty()) {
						builder.or(taxonomy.id.in(taxByCurrentGrinGenus));
					}
				}
			}
			predicates.add(builder);
		}
		if (species != null && !species.isEmpty()) {
			BooleanBuilder builder = new BooleanBuilder(taxonomy.species.in(species));
			builder.or(taxonomy.grinTaxonomySpecies().speciesName.in(species));
			if (synonyms == null || synonyms) {
				builder.or(taxonomy.currentTaxonomySpecies().speciesName.in(species));
			}
			predicates.add(builder);
		}
		if (subtaxa != null) {
			BooleanBuilder builder = new BooleanBuilder(subtaxa.buildQuery(taxonomy.subtaxa));
			builder.or(subtaxa.buildQuery(taxonomy.grinTaxonomySpecies().subtaxa));
			if (synonyms == null || synonyms) {
				builder.or(subtaxa.buildQuery(taxonomy.currentTaxonomySpecies().subtaxa));
			}
			predicates.add(builder);
		}
		if (taxonName != null) {
			BooleanBuilder builder = new BooleanBuilder(taxonName.buildQuery(taxonomy.taxonName));
			builder.or(taxonName.buildQuery(taxonomy.grinTaxonomySpecies().name));
			if (synonyms == null || synonyms) {
				builder.or(taxonName.buildQuery(taxonomy.currentTaxonomySpecies().name));
			}
			predicates.add(builder);
		}
		if (CollectionUtils.isNotEmpty(genusSpecies) && taxonomyService != null) {

			var sets = genusSpecies.stream().collect(Collectors.partitioningBy(gn -> StringUtils.endsWith(gn, " spp."), Collectors.toSet()));

			var speciesNames = sets.get(false);
			var genusNames = sets.get(true).stream().map(gn -> gn.replace(" spp.", "")).collect(Collectors.toSet());

			// 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<>(speciesNames));
			List<Long> taxByGrinSpecies = taxonomyService.findTaxonomy2ByGrinSpecies(new ArrayList<>(speciesNames));

			BooleanBuilder builder = new BooleanBuilder(taxonomy.taxSpecies.in(taxSpecies));
			if (!taxByGrinSpecies.isEmpty()) {
				builder.or(taxonomy.id.in(taxByGrinSpecies));
				if (synonyms == null || synonyms) {
					List<Long> taxByCurrentGrinSpecies = taxonomyService.findTaxonomy2ByCurrentGrinSpecies(new ArrayList<>(speciesNames));
					if (! taxByCurrentGrinSpecies.isEmpty()) {
						builder.or(taxonomy.id.in(taxByCurrentGrinSpecies));
					}
				}
			}

			if (!genusNames.isEmpty()) {
				List<Long> taxByGenus = taxonomyService.findByGenus(new ArrayList<>(genusNames));
				builder.or(taxonomy.id.in(taxByGenus));
			}

			predicates.add(builder);
		}
		if (CollectionUtils.isNotEmpty(grinTaxonName) && taxonomyService != null) {
			List<Long> taxByGrinNames = taxonomyService.findTaxonomy2ByGrinNames(new ArrayList<>(grinTaxonName));
			predicates.add(taxonomy.id.in(taxByGrinNames));
		}
		if (CollectionUtils.isNotEmpty(grinCurrentName) && taxonomyService != null) {
			List<Long> taxByCurrentGrinNames = taxonomyService.findTaxonomy2ByCurrentGrinNames(new ArrayList<>(grinCurrentName));
			predicates.add(taxonomy.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(taxonomy.id.in(taxonomy2Id));
		}
		if (CollectionUtils.isNotEmpty(family)) {
			predicates.add(taxonomy.family.in(family));
		}

		return predicates;
	}

	public StringFilter subtaxa() {
		return this.subtaxa == null ? this.subtaxa = new StringFilter() : this.subtaxa;
	}
}