ExplorationFilter.java

/*
 * Copyright 2024 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 com.querydsl.core.types.Predicate;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.TemporalFilter;
import org.gringlobal.model.Exploration;
import org.gringlobal.model.QExploration;

import java.util.Date;
import java.util.List;
import java.util.Set;

public class ExplorationFilter extends CooperatorOwnedModelFilter<ExplorationFilter, Exploration> {
	
	private static final long serialVersionUID = 8588489474890118470L;

	/** The began date. */
	public TemporalFilter<Date> beganDate;

	/** The exploration number. */
	public Set<String> explorationNumber;

	/** The finished date. */
	public TemporalFilter<Date> finishedDate;

	/** The fiscal year. */
	public NumberFilter<Integer> fiscalYear;

	/** The funding amount. */
	public NumberFilter<Double> fundingAmount;

	/** The funding source. */
	public StringFilter fundingSource;

	/** The host cooperator. */
	public CooperatorFilter hostCooperator;

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

	/** The permits. */
	public StringFilter permits;

	/** The restrictions. */
	public StringFilter restrictions;

	/** The target species. */
	public Set<String> targetSpecies;

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

	@Override
	public List<Predicate> collectPredicates() {
		return collectPredicates(QExploration.exploration);
	}

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

		if (beganDate != null) {
			predicates.add(beganDate.buildQuery(exploration.beganDate));
		}
		if (CollectionUtils.isNotEmpty(explorationNumber)) {
			predicates.add(exploration.explorationNumber.in(explorationNumber));
		}
		if (finishedDate != null) {
			predicates.add(finishedDate.buildQuery(exploration.finishedDate));
		}
		if (fiscalYear != null) {
			predicates.add(fiscalYear.buildQuery(exploration.fiscalYear));
		}
		if (fundingAmount != null) {
			predicates.add(fundingAmount.buildQuery(exploration.fundingAmount));
		}
		if (fundingSource != null) {
			predicates.add(fundingSource.buildQuery(exploration.fundingSource));
		}
		if (hostCooperator != null) {
			predicates.addAll(hostCooperator.collectPredicates(exploration.hostCooperator()));
		}
		if (note != null) {
			predicates.add(note.buildQuery(exploration.note));
		}
		if (permits != null) {
			predicates.add(permits.buildQuery(exploration.permits));
		}
		if (restrictions != null) {
			predicates.add(restrictions.buildQuery(exploration.restrictions));
		}
		if (CollectionUtils.isNotEmpty(targetSpecies)) {
			predicates.add(exploration.targetSpecies.in(targetSpecies));
		}
		if (title != null) {
			predicates.add(title.buildQuery(exploration.title));
		}
		
		return predicates;
	}
}