LocationDataFilter.java

/*
 * Copyright 2026 Global Crop Diversity Trust
 * Licensed under the Apache License, Version 2.0
 * See LICENSE file in project root folder or http://www.apache.org/licenses/LICENSE-2.0
 */

package org.gringlobal.service.filter;

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

import org.genesys.blocks.model.filters.StringFilter;

import org.gringlobal.model.community.LocationData;
import org.gringlobal.model.community.QLocationData;

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

import com.querydsl.core.types.Predicate;
import org.apache.commons.collections4.CollectionUtils;

/**
 * Filters for {@link LocationData}
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class LocationDataFilter extends CooperatorOwnedModelFilter<LocationDataFilter, LocationData, QLocationData> {
	private static final long serialVersionUID = -3481862564793002267L;

	/** The location. */
	public LocationFilter location;

	/** The location property code. */
	public Set<String> locationPropertyCode;

	/** The value. */
	public StringFilter value;

	/** The unit code. */
	public Set<String> unitCode;

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

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

	/**
	 * Builds the query.
	 *
	 * @param locationData the location data
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates(QLocationData locationData) {
		final List<Predicate> predicates = super.collectSuperPredicates(locationData, locationData._super);

		if (location != null) {
			predicates.add(location.nestedPredicate(locationData.location()));
		}
		if (CollectionUtils.isNotEmpty(locationPropertyCode)) {
			predicates.add(locationData.locationPropertyCode.in(locationPropertyCode));
		}
		if (value != null) {
			predicates.add(value.buildQuery(locationData.value));
		}
		if (CollectionUtils.isNotEmpty(unitCode)) {
			predicates.add(locationData.unitCode.in(unitCode));
		}
		if (note != null) {
			predicates.add(note.buildQuery(locationData.note));
		}

		return predicates;
	}

	/**
	 * The value filter.
	 *
	 * @return the value filter
	 */
	public synchronized StringFilter value() {
		return value == null ? value = new StringFilter() : this.value;
	}

	/**
	 * The note filter.
	 *
	 * @return the note filter
	 */
	public synchronized StringFilter note() {
		return note == null ? note = new StringFilter() : this.note;
	}

	/**
	 * The location property code filter.
	 *
	 * @return the set filter
	 */
	public synchronized Set<String> locationPropertyCode() {
		return locationPropertyCode == null ? locationPropertyCode = new HashSet<>() : this.locationPropertyCode;
	}

	/**
	 * The unit code filter.
	 *
	 * @return the set filter
	 */
	public synchronized Set<String> unitCode() {
		return unitCode == null ? unitCode = new HashSet<>() : this.unitCode;
	}
}