TaxonomyFamilyFilter.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.util.HashSet;
import java.util.List;
import java.util.Set;

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.StringFilter;
import org.gringlobal.model.QTaxonomyFamily;
import org.gringlobal.model.TaxonomyFamily;

import com.querydsl.core.types.Predicate;

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

	private static final long serialVersionUID = -3214688900786590522L;

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

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

	/** The family authority. */
	public StringFilter familyAuthority;

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

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

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

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

	/** The suprafamily rank code. */
	public Set<String> suprafamilyRankCode;

	/** The suprafamily rank name. */
	public StringFilter suprafamilyRankName;

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

	/** The type taxonomy genus. */
	public TaxonomyGenusFilter typeTaxonomyGenus;

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

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

		if (current != null) {
			if (current) {
				predicates.add(taxonomyFamily.id.eq(taxonomyFamily.currentTaxonomyFamily().id));
			} else {
				predicates.add(taxonomyFamily.id.ne(taxonomyFamily.currentTaxonomyFamily().id));
			}
		}
		if (alternateName != null) {
			predicates.add(alternateName.buildQuery(taxonomyFamily.alternateName));
		}
		if (familyAuthority != null) {
			predicates.add(familyAuthority.buildQuery(taxonomyFamily.familyAuthority));
		}
		if (familyName != null) {
			predicates.add(familyName.buildQuery(taxonomyFamily.familyName));
		}
		if (note != null) {
			predicates.add(note.buildQuery(taxonomyFamily.note));
		}
		if (subfamilyName != null) {
			predicates.add(subfamilyName.buildQuery(taxonomyFamily.subfamilyName));
		}
		if (subtribeName != null) {
			predicates.add(subtribeName.buildQuery(taxonomyFamily.subtribeName));
		}
		if (CollectionUtils.isNotEmpty(suprafamilyRankCode)) {
			predicates.add(taxonomyFamily.suprafamilyRankCode.in(suprafamilyRankCode));
		}
		if (suprafamilyRankName != null) {
			predicates.add(suprafamilyRankName.buildQuery(taxonomyFamily.suprafamilyRankName));
		}
		if (tribeName != null) {
			predicates.add(tribeName.buildQuery(taxonomyFamily.tribeName));
		}
		if (typeTaxonomyGenus != null) {
			predicates.addAll(typeTaxonomyGenus.collectPredicates(taxonomyFamily.typeTaxonomyGenus()));
		}

		return predicates;
	}

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

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