RegionFilter.java

/*
 * Copyright 2026 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 lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.gringlobal.model.QRegion;
import org.gringlobal.model.Region;

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

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

	private static final long serialVersionUID = 8670328168999669487L;

	/** The continent. */
	public StringFilter continent;

	/** The continent abbreviation. */
	public Set<String> continentAbbreviation;

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

	/** The sequence number. */
	public NumberFilter<Integer> sequenceNumber;

	/** The subcontinent. */
	public StringFilter subcontinent;

	/** The subcontinent abbreviation. */
	public Set<String> subcontinentAbbreviation;

	@Override
	public List<Predicate> collectPredicates() {
		return collectPredicates(QRegion.region);
	}

	@Override
	protected List<Predicate> collectPredicates(QRegion region) {
		final List<Predicate> predicates = super.collectSuperPredicates(region, region._super);

		if (continent != null) {
			predicates.add(continent.buildQuery(region.continent));
		}
		if (CollectionUtils.isNotEmpty(continentAbbreviation)) {
			predicates.add(region.continentAbbreviation.in(continentAbbreviation));
		}
		if (note != null) {
			predicates.add(note.buildQuery(region.note));
		}
		if (sequenceNumber != null) {
			predicates.add(sequenceNumber.buildQuery(region.sequenceNumber));
		}
		if (subcontinent != null) {
			predicates.add(subcontinent.buildQuery(region.subcontinent));
		}
		if (CollectionUtils.isNotEmpty(subcontinentAbbreviation)) {
			predicates.add(region.subcontinentAbbreviation.in(subcontinentAbbreviation));
		}

		return predicates;
	}

}