GeographyFilter.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 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.StringFilter;
import org.gringlobal.model.Geography;
import org.gringlobal.model.QGeography;

import com.querydsl.core.types.Predicate;

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

	private static final long serialVersionUID = -3980546591624378700L;

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

	/** The adm1. */
	public StringFilter adm1;

	/** The adm2. */
	public StringFilter adm2;

	/** The adm3. */
	public StringFilter adm3;

	/** The adm4. */
	public StringFilter adm4;

	/** The adm1Abbrev. */
	public StringFilter adm1Abbrev;

	/** The adm1TypeCode. */
	public Set<String> adm1TypeCode;

	/** The adm2Abbrev. */
	public StringFilter adm2Abbrev;

	/** The adm2Abbrev. */
	public Set<String> adm2TypeCode;

	/** The adm3Abbrev. */
	public StringFilter adm3Abbrev;

	/** The adm3TypeCode. */
	public Set<String> adm3TypeCode;

	/** The adm4Abbrev. */
	public StringFilter adm4Abbrev;

	/** The adm4TypeCode. */
	public Set<String> adm4TypeCode;

	/** The countryCode. */
	public Set<String> countryCode;

	/** The changed date. */
	public TemporalFilter<Instant> changedDate;

	/** Is valid */
	public Boolean valid;

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

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

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

		if (adm1 != null) {
			predicates.add(adm1.buildQuery(geography.adm1));
		}
		if (adm2 != null) {
			predicates.add(adm2.buildQuery(geography.adm2));
		}
		if (adm3 != null) {
			predicates.add(adm3.buildQuery(geography.adm3));
		}
		if (adm4 != null) {
			predicates.add(adm4.buildQuery(geography.adm4));
		}
		if (adm1Abbrev != null) {
			predicates.add(adm1Abbrev.buildQuery(geography.adm1Abbrev));
		}
		if (CollectionUtils.isNotEmpty(adm1TypeCode)) {
			predicates.add(geography.adm1TypeCode.in(adm1TypeCode));
		}
		if (adm2Abbrev != null) {
			predicates.add(adm2Abbrev.buildQuery(geography.adm2Abbrev));
		}
		if (CollectionUtils.isNotEmpty(adm2TypeCode)) {
			predicates.add(geography.adm2TypeCode.in(adm2TypeCode));
		}
		if (adm3Abbrev != null) {
			predicates.add(adm3Abbrev.buildQuery(geography.adm3Abbrev));
		}
		if (CollectionUtils.isNotEmpty(adm3TypeCode)) {
			predicates.add(geography.adm3TypeCode.in(adm3TypeCode));
		}
		if (adm4Abbrev != null) {
			predicates.add(adm4Abbrev.buildQuery(geography.adm4Abbrev));
		}
		if (CollectionUtils.isNotEmpty(adm4TypeCode)) {
			predicates.add(geography.adm4TypeCode.in(adm4TypeCode));
		}
		if (CollectionUtils.isNotEmpty(countryCode)) {
			predicates.add(geography.countryCode.in(countryCode));
		}
		if (note != null) {
			predicates.add(note.buildQuery(geography.note));
		}
		if (valid != null) {
			predicates.add(geography.isValid.eq(convertToString(valid)));
		}
		if (changedDate != null) {
			predicates.add(changedDate.buildQuery(geography.changedDate));
		}

		return predicates;
	}

	public synchronized Set<String> countryCode() {
		return this.countryCode == null ? this.countryCode = new HashSet<>() : this.countryCode;
	}

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