LocationFilter.java

/*
 * Copyright 2022 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.fasterxml.jackson.annotation.JsonIgnoreProperties;
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.gringlobal.model.community.Location;
import org.gringlobal.model.community.QLocation;

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

public class LocationFilter extends CooperatorOwnedModelFilter<LocationFilter, Location, QLocation> {
	private static final long serialVersionUID = -8961435546453267947L;

	/** Site */
	public SiteFilter site;

	/** The location number part1. */
	public StringFilter locationNumberPart1;

	/** The location number part2. */
	public NumberFilter<Long> locationNumberPart2;

	/** The location number part3. */
	public StringFilter locationNumberPart3;

	/** The location type code. */
	public Set<String> locationTypeCode;

	/** The geographu. */
	public GeographyFilter geography;

	/** The barcode. */
	public Set<String> barcode;

	/** The capacity of this location. */
	public NumberFilter<Integer> capacity;

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

	/** The note. */
	public StringFilter note;
	
	/** The parent location. */
	@JsonIgnoreProperties("parentLocation")
	public LocationFilter parentLocation;

	/** Is enabled */
	public Boolean isEnabled;

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

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

		if (site != null) {
			predicates.add(site.nestedPredicate(location.site()));
		}
		if (locationNumberPart1 != null) {
			predicates.add(locationNumberPart1.buildQuery(location.locationNumberPart1));
		}
		if (locationNumberPart2 != null) {
			predicates.add(locationNumberPart2.buildQuery(location.locationNumberPart2));
		}
		if (locationNumberPart3 != null) {
			predicates.add(locationNumberPart3.buildQuery(location.locationNumberPart3));
		}
		if (CollectionUtils.isNotEmpty(locationTypeCode)) {
			predicates.add(location.locationTypeCode.in(locationTypeCode));
		}
		if (geography != null) {
			predicates.add(geography.nestedPredicate(location.geography()));
		}
		if (CollectionUtils.isNotEmpty(barcode)) {
			predicates.add(location.barcode.in(barcode));
		}
		if (capacity != null) {
			predicates.add(capacity.buildQuery(location.capacity));
		}
		if (CollectionUtils.isNotEmpty(capacityUnitCode)) {
			predicates.add(location.capacityUnitCode.in(capacityUnitCode));
		}
		if (note != null) {
			predicates.add(note.buildQuery(location.note));
		}
		if (parentLocation != null) {
			parentLocation.parentLocation = null;
			predicates.add(parentLocation.nestedPredicate(location.parentLocation()));
		}
		if (isEnabled != null) {
			predicates.add(location.isEnabled.eq(convertToString(isEnabled)));
		}

		return predicates;
	}
}