CooperatorFilter.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.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.Cooperator;
import org.gringlobal.model.QCooperator;

import com.querydsl.core.types.Predicate;

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

	private static final long serialVersionUID = -6902623022933217392L;

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

	/** The address line1. */
	public StringFilter addressLine1;

	/** The address line2. */
	public StringFilter addressLine2;

	/** The address line3. */
	public StringFilter addressLine3;

	/** The category code. */
	public Set<String> categoryCode;

	/** The city. */
	public StringFilter city;

	/** The current. */
	public Boolean current;

	/** The discipline code. */
	public Set<String> disciplineCode;

	/** The email. */
	public StringFilter email;

	/** The fax. */
	public StringFilter fax;

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

	/** The geography. */
	public GeographyFilter geography;

	/** The institute number. */
	public StringFilter faoInstituteNumber;

	/** The job. */
	public StringFilter job;

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

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

	/** The organization. */
	public StringFilter organization;

	/** The organization abbrev. */
	public StringFilter organizationAbbrev;

	/** The organization region code. */
	public Set<String> organizationRegionCode;

	/** The postal index. */
	public StringFilter postalIndex;

	/** The primary phone. */
	public StringFilter primaryPhone;

	/** The secondary address line1. */
	public StringFilter secondaryAddressLine1;

	/** The secondary address line2. */
	public StringFilter secondaryAddressLine2;

	/** The secondary address line3. */
	public StringFilter secondaryAddressLine3;

	/** The secondary city. */
	public StringFilter secondaryCity;

	/** The secondary email. */
	public StringFilter secondaryEmail;

	/** The secondary geography. */
	public GeographyFilter secondaryGeography;

	/** The secondary organization. */
	public StringFilter secondaryOrganization;

	/** The secondary organization abbrev. */
	public StringFilter secondaryOrganizationAbbrev;

	/** The secondary phone. */
	public StringFilter secondaryPhone;

	/** The secondary postal index. */
	public StringFilter secondaryPostalIndex;

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

	/** The status code. */
	public Set<String> statusCode;

	// FIXME implement me
	// public SysLangFilter sysLang;

	/** The title. */
	public StringFilter title;

	public Set<String> itpgrfaPid;

	// FIXME implement me
	// public WebCooperatorFilter webCooperator;

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

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

		if (addressLine1 != null) {
			predicates.add(addressLine1.buildQuery(cooperator.addressLine1));
		}
		if (addressLine2 != null) {
			predicates.add(addressLine2.buildQuery(cooperator.addressLine2));
		}
		if (addressLine3 != null) {
			predicates.add(addressLine3.buildQuery(cooperator.addressLine3));
		}
		if (CollectionUtils.isNotEmpty(categoryCode)) {
			predicates.add(cooperator.categoryCode.in(categoryCode));
		}
		if (city != null) {
			predicates.add(city.buildQuery(cooperator.city));
		}
		if (current != null) {
			if (current) {
				predicates.add(cooperator.id.eq(cooperator.currentCooperator().id));
			} else {
				predicates.add(cooperator.id.ne(cooperator.currentCooperator().id));
			}
		}
		if (CollectionUtils.isNotEmpty(disciplineCode)) {
			predicates.add(cooperator.disciplineCode.in(disciplineCode));
		}
		if (faoInstituteNumber != null) {
			predicates.add(faoInstituteNumber.buildQuery(cooperator.faoInstituteNumber));
		}
		if (email != null) {
			predicates.add(email.buildQuery(cooperator.email));
		}
		if (fax != null) {
			predicates.add(fax.buildQuery(cooperator.fax));
		}
		if (firstName != null) {
			predicates.add(firstName.buildQuery(cooperator.firstName));
		}
		if (job != null) {
			predicates.add(job.buildQuery(cooperator.job));
		}
		if (lastName != null) {
			predicates.add(lastName.buildQuery(cooperator.lastName));
		}
		if (note != null) {
			predicates.add(note.buildQuery(cooperator.note));
		}
		if (organization != null) {
			predicates.add(organization.buildQuery(cooperator.organization));
		}
		if (organizationAbbrev != null) {
			predicates.add(organizationAbbrev.buildQuery(cooperator.organizationAbbrev));
		}
		if (CollectionUtils.isNotEmpty(organizationRegionCode)) {
			predicates.add(cooperator.organizationRegionCode.in(organizationRegionCode));
		}
		if (postalIndex != null) {
			predicates.add(postalIndex.buildQuery(cooperator.postalIndex));
		}
		if (primaryPhone != null) {
			predicates.add(primaryPhone.buildQuery(cooperator.primaryPhone));
		}
		if (secondaryAddressLine1 != null) {
			predicates.add(secondaryAddressLine1.buildQuery(cooperator.secondaryAddressLine1));
		}
		if (secondaryAddressLine2 != null) {
			predicates.add(secondaryAddressLine2.buildQuery(cooperator.secondaryAddressLine2));
		}
		if (secondaryAddressLine3 != null) {
			predicates.add(secondaryAddressLine3.buildQuery(cooperator.secondaryAddressLine3));
		}
		if (secondaryCity != null) {
			predicates.add(secondaryCity.buildQuery(cooperator.secondaryCity));
		}
		if (secondaryEmail != null) {
			predicates.add(secondaryEmail.buildQuery(cooperator.secondaryEmail));
		}
		if (secondaryOrganization != null) {
			predicates.add(secondaryOrganization.buildQuery(cooperator.secondaryOrganization));
		}
		if (secondaryOrganizationAbbrev != null) {
			predicates.add(secondaryOrganizationAbbrev.buildQuery(cooperator.secondaryOrganizationAbbrev));
		}
		if (secondaryPhone != null) {
			predicates.add(secondaryPhone.buildQuery(cooperator.secondaryPhone));
		}
		if (secondaryPostalIndex != null) {
			predicates.add(secondaryPostalIndex.buildQuery(cooperator.secondaryPostalIndex));
		}
		if (site != null) {
			predicates.addAll(site.collectPredicates(cooperator.site()));
		}
		if (statusCode != null && !statusCode.isEmpty()) {
			predicates.add(cooperator.statusCode.in(statusCode));
		}
		if (title != null) {
			predicates.add(title.buildQuery(cooperator.title));
		}
		if (geography != null) {
			predicates.addAll(geography.collectPredicates(cooperator.geography()));
		}
		if (secondaryGeography != null) {
			predicates.addAll(secondaryGeography.collectPredicates(cooperator.secondaryGeography()));
		}
		if (itpgrfaPid != null && !itpgrfaPid.isEmpty()) {
			predicates.add(cooperator.itpgrfaPid.in(itpgrfaPid));
		}

		return predicates;
	}

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

}