AccessionGeoFilter.java

/*
 * Copyright 2021 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.genesys.server.service.filter;

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

import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.BasicModelFilter;
import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.server.model.genesys.AccessionId;
import org.genesys.server.model.genesys.QAccessionId;

import com.querydsl.core.types.Predicate;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * The Class CountryFilter.
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class AccessionGeoFilter extends BasicModelFilter<AccessionGeoFilter, AccessionId> {

	private static final long serialVersionUID = 1456709522018735306L;

	/** The longitude. */
	public NumberFilter<Double> longitude;
	
	/** The latitude. */
	public NumberFilter<Double> latitude;
	
	/** The elevation. */
	public NumberFilter<Double> elevation;
	
	public Boolean referenced;
	
	/** The climate. */
	public ClimateFilter climate;

	/** The tile index. */
	public Set<Long> tileIndex;

	/** The tile index 3min grid. */
	public Set<Integer> tileIndex3min;

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

	/**
	 * Builds the query.
	 *
	 * @param qAccessionId the country
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates(QAccessionId qAccessionId) {
		final List<Predicate> predicates = super.collectPredicates(qAccessionId);

		if (referenced != null) {
			if (referenced) {
				predicates.add(qAccessionId.longitude.isNotNull().and(qAccessionId.latitude.isNotNull()));
			} else {
				predicates.add(qAccessionId.longitude.isNull().or(qAccessionId.latitude.isNull()));
			}
		}
		if (longitude != null) {
			predicates.add(longitude.buildQuery(qAccessionId.longitude));
		}
		if (latitude != null) {
			predicates.add(latitude.buildQuery(qAccessionId.latitude));
		}
		if (elevation != null) {
			predicates.add(elevation.buildQuery(qAccessionId.elevation));
		}
		if (climate != null) {
			predicates.addAll(climate.collectPredicates(qAccessionId.climate()));
		}
		if (CollectionUtils.isNotEmpty(tileIndex)) {
			predicates.add(qAccessionId.tileIndex.in(tileIndex));
		}
		if (CollectionUtils.isNotEmpty(tileIndex3min)) {
			predicates.add(qAccessionId.tileIndex3min.in(tileIndex3min));
		}

		return predicates;
	}
}