SiteFilter.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.QSite;
import org.gringlobal.model.Site;

import com.querydsl.core.types.Predicate;

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

	private static final long serialVersionUID = -5977903514548844153L;

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

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

	/** Is distribution site */
	public Boolean distributionSite;

	/** Is internal */
	public Boolean internal;

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

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

	/** The provider identifier. */
	public StringFilter providerIdentifier;

	/** The site long name. */
	public StringFilter siteLongName;

	/** The site short name. */
	public StringFilter siteShortName;

	/** The type code. */
	public Set<String> typeCode;

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

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

		if (CollectionUtils.isNotEmpty(id)) {
			predicates.add(site.id.in(id));
		}
		if (faoInstituteNumber != null) {
			predicates.add(faoInstituteNumber.buildQuery(site.faoInstituteNumber));
		}
		if (distributionSite != null) {
			predicates.add(site.isDistributionSite.eq(convertToString(distributionSite)));
		}
		if (internal != null) {
			predicates.add(site.isInternal.eq(convertToString(internal)));
		}
		if (note != null) {
			predicates.add(note.buildQuery(site.note));
		}
		if (organizationAbbrev != null) {
			predicates.add(organizationAbbrev.buildQuery(site.organizationAbbrev));
		}
		if (providerIdentifier != null) {
			predicates.add(providerIdentifier.buildQuery(site.providerIdentifier));
		}
		if (siteLongName != null) {
			predicates.add(siteLongName.buildQuery(site.siteLongName));
		}
		if (siteShortName != null) {
			predicates.add(siteShortName.buildQuery(site.siteShortName));
		}
		if (CollectionUtils.isNotEmpty(typeCode)) {
			predicates.add(site.typeCode.in(typeCode));
		}
		return predicates;
	}

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

	public synchronized StringFilter siteLongName() {
		if (this.siteLongName == null) {
			this.siteLongName = new StringFilter();
		}
		return this.siteLongName;
	}
}