TaxonomySpeciesFilter.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.gringlobal.service.filter;

import java.time.Instant;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.TemporalFilter;
import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.util.CurrentApplicationContext;
import org.gringlobal.model.QTaxonomySpecies;
import org.gringlobal.model.TaxonomySpecies;

import com.querydsl.core.types.Predicate;

/**
 * Filters for {@link TaxonomySpecies}
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class TaxonomySpeciesFilter extends CooperatorOwnedModelFilter<TaxonomySpeciesFilter, TaxonomySpecies> implements IFullTextFilter {

	private static final long serialVersionUID = 5863625926115352972L;

	/** Any text. */
	public String _text;

	/** The alternate name. */
	public StringFilter alternateName;

	/** The common fertilization code. */
	public Set<String> commonFertilizationCode;

	/** The curator1 cooperator. */
	public CooperatorFilter curator1Cooperator;

	/** The curator2 cooperator. */
	public CooperatorFilter curator2Cooperator;

	/** Is current version */
	public Boolean current;

	/** The forma authority. */
	public StringFilter formaAuthority;

	/** The forma name. */
	public StringFilter formaName;

	/** The forma rank type. */
	public StringFilter formaRankType;

	/** Is forma hybrid */
	public Boolean formaHybrid;

	/** Is name pending */
	public Boolean namePending;

	/** Is specific hybrid */
	public Boolean specificHybrid;

	/** Is subspecific hybrid */
	public Boolean subspecificHybrid;

	/** Is subvarietal hybrid */
	public Boolean subvarietalHybrid;

	/** Is varietal hybrid */
	public Boolean varietalHybrid;

	/** The life form code. */
	public Set<String> lifeFormCode;

	/** The name. */
	public StringFilter name;

	/** The name authority. */
	public StringFilter nameAuthority;

	/** The name verified date. */
	public TemporalFilter<Instant> nameVerifiedDate;

	/** The nomen number. */
	public NumberFilter<Integer> nomenNumber;

	/** The note. */
	public StringFilter note;

	/** The priority1 site. */
	public SiteFilter priority1Site;

	/** The priority2 site. */
	public SiteFilter priority2Site;

	/** The protologue. */
	public StringFilter protologue;

	/** The protologue virtual path. */
	public StringFilter protologueVirtualPath;

	/** The restriction code. */
	public Set<String> restrictionCode;

	/** The site note. */
	public StringFilter siteNote;

	/** The species authority. */
	public StringFilter speciesAuthority;

	/** The species name. */
	public StringFilter speciesName;

	/** The subspecies authority. */
	public StringFilter subspeciesAuthority;

	/** The subspecies name. */
	public StringFilter subspeciesName;

	/** The subvariety authority. */
	public StringFilter subvarietyAuthority;

	/** The subvariety name. */
	public StringFilter subvarietyName;

	/** The synonym code. */
	public Set<String> synonymCode;

	/** The taxonomy genus. */
	public TaxonomyGenusFilter taxonomyGenus;

	/** The variety authority. */
	public StringFilter varietyAuthority;

	/** The variety name. */
	public StringFilter varietyName;

	/** The verifier cooperator. */
	public CooperatorFilter verifierCooperator;
	
	/** The taxonomy genus name */
	public StringFilter genusName;

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

	/**
	 * Builds the query.
	 *
	 * @param taxonomySpecies the taxonomySpecies
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates(QTaxonomySpecies taxonomySpecies) {
		final List<Predicate> predicates = super.collectPredicates(taxonomySpecies, taxonomySpecies._super);

		if (alternateName != null) {
			predicates.add(alternateName.buildQuery(taxonomySpecies.alternateName));
		}
		if (CollectionUtils.isNotEmpty(commonFertilizationCode)) {
			predicates.add(taxonomySpecies.commonFertilizationCode.in(commonFertilizationCode));
		}
		if (curator1Cooperator != null) {
			predicates.addAll(curator1Cooperator.collectPredicates(taxonomySpecies.curator1Cooperator()));
		}
		if (curator2Cooperator != null) {
			predicates.addAll(curator2Cooperator.collectPredicates(taxonomySpecies.curator2Cooperator()));
		}
		if (current != null) {
			if (current) {
				predicates.add(taxonomySpecies.id.eq(taxonomySpecies.currentTaxonomySpecies().id));
			} else {
				predicates.add(taxonomySpecies.id.ne(taxonomySpecies.currentTaxonomySpecies().id));
			}
		}
		if (formaAuthority != null) {
			predicates.add(formaAuthority.buildQuery(taxonomySpecies.formaAuthority));
		}
		if (formaName != null) {
			predicates.add(formaName.buildQuery(taxonomySpecies.formaName));
		}
		if (formaRankType != null) {
			predicates.add(formaRankType.buildQuery(taxonomySpecies.formaRankType));
		}
		if (formaHybrid != null) {
			predicates.add(taxonomySpecies.isFormaHybrid.eq(convertToString(formaHybrid)));
		}
		if (namePending != null) {
			predicates.add(taxonomySpecies.isNamePending.eq(convertToString(namePending)));
		}
		if (specificHybrid != null) {
			predicates.add(taxonomySpecies.isSpecificHybrid.eq(convertToString(specificHybrid)));
		}
		if (subspecificHybrid != null) {
			predicates.add(taxonomySpecies.isSubspecificHybrid.eq(convertToString(subspecificHybrid)));
		}
		if (subvarietalHybrid != null) {
			predicates.add(taxonomySpecies.isSubvarietalHybrid.eq(convertToString(subvarietalHybrid)));
		}
		if (varietalHybrid != null) {
			predicates.add(taxonomySpecies.isVarietalHybrid.eq(convertToString(varietalHybrid)));
		}
		if (CollectionUtils.isNotEmpty(lifeFormCode)) {
			predicates.add(taxonomySpecies.lifeFormCode.in(lifeFormCode));
		}
		if (name != null) {
			predicates.add(name.buildQuery(taxonomySpecies.name));
		}
		if (nameAuthority != null) {
			predicates.add(nameAuthority.buildQuery(taxonomySpecies.nameAuthority));
		}
		if (nameVerifiedDate != null) {
			predicates.add(nameVerifiedDate.buildQuery(taxonomySpecies.nameVerifiedDate));
		}
		if (nomenNumber != null) {
			predicates.add(nomenNumber.buildQuery(taxonomySpecies.nomenNumber));
		}
		if (note != null) {
			predicates.add(note.buildQuery(taxonomySpecies.note));
		}
		if (priority1Site != null) {
			predicates.addAll(priority1Site.collectPredicates(taxonomySpecies.priority1Site()));
		}
		if (priority2Site != null) {
			predicates.addAll(priority2Site.collectPredicates(taxonomySpecies.priority2Site()));
		}
		if (protologue != null) {
			predicates.add(protologue.buildQuery(taxonomySpecies.protologue));
		}
		if (protologueVirtualPath != null) {
			predicates.add(protologueVirtualPath.buildQuery(taxonomySpecies.protologueVirtualPath));
		}
		if (CollectionUtils.isNotEmpty(restrictionCode)) {
			predicates.add(taxonomySpecies.restrictionCode.in(restrictionCode));
		}
		if (siteNote != null) {
			predicates.add(siteNote.buildQuery(taxonomySpecies.siteNote));
		}
		if (speciesAuthority != null) {
			predicates.add(speciesAuthority.buildQuery(taxonomySpecies.speciesAuthority));
		}
		if (speciesName != null) {
			predicates.add(speciesName.buildQuery(taxonomySpecies.speciesName));
		}
		if (subspeciesAuthority != null) {
			predicates.add(subspeciesAuthority.buildQuery(taxonomySpecies.subspeciesAuthority));
		}
		if (subspeciesName != null) {
			predicates.add(subspeciesName.buildQuery(taxonomySpecies.subspeciesName));
		}
		if (subvarietyAuthority != null) {
			predicates.add(subvarietyAuthority.buildQuery(taxonomySpecies.subvarietyAuthority));
		}
		if (subvarietyName != null) {
			predicates.add(subvarietyName.buildQuery(taxonomySpecies.subvarietyName));
		}
		if (CollectionUtils.isNotEmpty(synonymCode)) {
			predicates.add(taxonomySpecies.synonymCode.in(synonymCode));
		}
		if (taxonomyGenus != null) {
			predicates.addAll(taxonomyGenus.collectPredicates(taxonomySpecies.taxonomyGenus()));
		}
		if (varietyAuthority != null) {
			predicates.add(varietyAuthority.buildQuery(taxonomySpecies.varietyAuthority));
		}
		if (varietyName != null) {
			predicates.add(varietyName.buildQuery(taxonomySpecies.varietyName));
		}
		if (verifierCooperator != null) {
			predicates.addAll(verifierCooperator.collectPredicates(taxonomySpecies.verifierCooperator()));
		}
		if (genusName != null) {
			predicates.add(taxonomySpecies.id.in(getSpeciesIdsByGenusName(genusName)));
		}

		return predicates;
	}

	private List<Long> getSpeciesIdsByGenusName(StringFilter genusName) {
		var jpaQueryFactory = CurrentApplicationContext.getContext().getBean(JPAQueryFactory.class);
		return jpaQueryFactory.select(QTaxonomySpecies.taxonomySpecies.id)
			.from(QTaxonomySpecies.taxonomySpecies)
			.where(genusName.buildQuery(QTaxonomySpecies.taxonomySpecies.taxonomyGenus().genusName))
			.fetch();
	}

	/**
	 * Gets the text.
	 *
	 * @return the text
	 */
	@Override
	public String get_text() {
		return _text;
	}

	/**
	 * Id.
	 *
	 * @return the sets the
	 */
	public synchronized Set<Long> id() {
		if (id == null) {
			id = new HashSet<>();
		}
		return id;
	}

	public TaxonomyGenusFilter taxonomyGenus() {
		return this.taxonomyGenus == null ? this.taxonomyGenus = new TaxonomyGenusFilter() : this.taxonomyGenus;
	}

	/**
	 * Name filter.
	 *
	 * @return the string filter
	 */
	public synchronized StringFilter name() {
		return this.name == null ? this.name = new StringFilter() : this.name;
	}

	/**
	 * Species name filter.
	 *
	 * @return the string filter
	 */
	public synchronized StringFilter speciesName() {
		return this.speciesName == null ? this.speciesName = new StringFilter() : this.speciesName;
	}

	/**
	 * Alternate name filter.
	 *
	 * @return the string filter
	 */
	public synchronized StringFilter alternateName() {
		return this.alternateName == null ? this.alternateName = new StringFilter() : this.alternateName;
	}
}