WebCooperatorFilter.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.apache.commons.lang3.StringUtils;
import org.genesys.blocks.model.filters.StringFilter;
import org.gringlobal.model.QWebCooperator;
import org.gringlobal.model.WebCooperator;

import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;

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

	private static final long serialVersionUID = -7106339534261340666L;

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

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

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

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

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

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

	/** The discipline. */
	public StringFilter discipline;

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

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

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

	/** The initials. */
	public StringFilter initials;

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

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

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

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

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

	/** The organizationRegion. */
	public StringFilter organizationRegion;

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

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

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

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

	/** The active. */
	public Boolean active;

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

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

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

		if (StringUtils.isNotBlank(_text)) {
			String likeString = _text + "%";
			BooleanExpression expression = webCooperator.title.like(likeString).or(webCooperator.firstName.like(likeString))
					.or(webCooperator.lastName.like(likeString)).or(webCooperator.email.like(likeString));
			predicates.add(expression);
		}
		if (addressLine1 != null) {
			predicates.add(addressLine1.buildQuery(webCooperator.addressLine1));
		}
		if (addressLine2 != null) {
			predicates.add(addressLine2.buildQuery(webCooperator.addressLine2));
		}
		if (addressLine3 != null) {
			predicates.add(addressLine3.buildQuery(webCooperator.addressLine3));
		}
		if (CollectionUtils.isNotEmpty(categoryCode)) {
			predicates.add(webCooperator.categoryCode.in(categoryCode));
		}
		if (city != null) {
			predicates.add(city.buildQuery(webCooperator.city));
		}
		if (discipline != null) {
			predicates.add(discipline.buildQuery(webCooperator.discipline));
		}
		if (email != null) {
			predicates.add(email.buildQuery(webCooperator.email));
		}
		if (firstName != null) {
			predicates.add(firstName.buildQuery(webCooperator.firstName));
		}
		if (lastName != null) {
			predicates.add(lastName.buildQuery(webCooperator.lastName));
		}
		if (initials != null) {
			predicates.add(initials.buildQuery(webCooperator.initials));
		}
		if (fax != null) {
			predicates.add(fax.buildQuery(webCooperator.fax));
		}
		if (job != null) {
			predicates.add(job.buildQuery(webCooperator.job));
		}
		if (note != null) {
			predicates.add(note.buildQuery(webCooperator.note));
		}
		if (organization != null) {
			predicates.add(organization.buildQuery(webCooperator.organization));
		}
		if (CollectionUtils.isNotEmpty(organizationCode)) {
			predicates.add(webCooperator.organizationCode.in(organizationCode));
		}
		if (organizationRegion != null) {
			predicates.add(organizationRegion.buildQuery(webCooperator.organizationRegion));
		}
		if (postalIndex != null) {
			predicates.add(postalIndex.buildQuery(webCooperator.postalIndex));
		}
		if (primaryPhone != null) {
			predicates.add(primaryPhone.buildQuery(webCooperator.primaryPhone));
		}
		if (secondaryPhone != null) {
			predicates.add(secondaryPhone.buildQuery(webCooperator.secondaryPhone));
		}
		if (title != null) {
			predicates.add(title.buildQuery(webCooperator.title));
		}
		if (geography != null) {
			predicates.addAll(geography.collectPredicates(webCooperator.geography()));
		}
		if (active != null) {
			predicates.add(webCooperator.isActive.eq(convertToString(active)));
		}

		return predicates;
	}
}