AccessionSourceFilter.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.Date;
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.NumberFilter;
import org.gringlobal.model.AccessionInvGroup;
import org.gringlobal.model.AccessionSource;
import org.gringlobal.model.QAccessionSource;

import com.querydsl.core.types.Predicate;

/**
 * Filters for {@link AccessionInvGroup}
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class AccessionSourceFilter extends CooperatorOwnedModelFilter<AccessionSourceFilter, AccessionSource> {

	private static final long serialVersionUID = 8108778483347390574L;

	/** sourceTypeCodes */
	public Set<String> sourceTypeCode;

	/** acquisitionSource */
	public Set<String> acquisitionSource;

	/** Origin */
	public Boolean origin;

	/** Geography */
	public GeographyFilter geography;

	/** Accession */
	public AccessionFilter accession;
	
	/** latitude */
	public NumberFilter<Double> latitude;

	/** longitude */
	public NumberFilter<Double> longitude;

	/** elevationMeters */
	public NumberFilter<Integer> elevationMeters;
	
	/** collected form code */
	public Set<String> collectedFormCode;
	
	/** source date */
	public TemporalFilter<Date> sourceDate;

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

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

		if (CollectionUtils.isNotEmpty(sourceTypeCode)) {
			predicates.add(accessionSource.sourceTypeCode.in(sourceTypeCode));
		}
		if (CollectionUtils.isNotEmpty(acquisitionSource)) {
			predicates.add(accessionSource.acquisitionSource.in(acquisitionSource));
		}
		if (origin != null) {
			predicates.add(accessionSource.isOrigin.eq(origin ? "Y" : "N"));
		}
		if (geography != null) {
			predicates.addAll(geography.collectPredicates(accessionSource.geography()));
		}
		if (accession != null) {
			predicates.addAll(accession.collectPredicates(accessionSource.accession()));
		}
		if (latitude != null) {
			predicates.add(latitude.buildQuery(accessionSource.latitude));
		}
		if (longitude != null) {
			predicates.add(longitude.buildQuery(accessionSource.longitude));
		}
		if (elevationMeters != null) {
			predicates.add(elevationMeters.buildQuery(accessionSource.elevationMeters));
		}
		if (CollectionUtils.isNotEmpty(collectedFormCode)) {
			predicates.add(accessionSource.collectedFormCode.in(collectedFormCode));
		}
		if (sourceDate != null) {
			predicates.add(sourceDate.buildQuery(accessionSource.sourceDate));
		}

		return predicates;
	}
}