InventoryViabilityFilter.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.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.collections.CollectionUtils;
import org.genesys.blocks.model.filters.TemporalFilter;
import org.genesys.blocks.model.filters.NumberFilter;
import org.gringlobal.model.InventoryViability;
import org.gringlobal.model.QInventoryViability;

import com.querydsl.core.types.Predicate;
import org.gringlobal.model.QInventoryViabilityAction;

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

	private static final long serialVersionUID = -3596916671543298857L;
	public InventoryFilter inventory;
	public InventoryViabilityRuleFilter inventoryViabilityRule;
	public TemporalFilter<Date> testedDate;
	public Set<String> vigorRatingCode;
	public NumberFilter<Double> percentViable;
	public NumberFilter<Integer> replicationCount;
	public NumberFilter<Integer> totalTestedCount;
	public InventoryViabilityActionFilter actions;

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

	/**
	 * Builds the query.
	 *
	 * @param inventoryViability the inventoryViability
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates(QInventoryViability inventoryViability) {
		List<Predicate> predicates = super.collectPredicates(inventoryViability, inventoryViability._super);

		if (inventory != null) {
			predicates.addAll(inventory.collectPredicates(inventoryViability.inventory()));
		}
		if (inventoryViabilityRule != null) {
			predicates.addAll(inventoryViabilityRule.collectPredicates(inventoryViability.inventoryViabilityRule()));
		}
		if (testedDate != null) {
			predicates.add(testedDate.buildQuery(inventoryViability.testedDate));
		}
		if (CollectionUtils.isNotEmpty(vigorRatingCode)) {
			predicates.add(inventoryViability.vigorRatingCode.in(vigorRatingCode));
		}
		if (percentViable != null) {
			predicates.add(percentViable.buildQuery(inventoryViability.percentViable));
		}
		if (replicationCount != null) {
			predicates.add(replicationCount.buildQuery(inventoryViability.replicationCount));
		}
		if (totalTestedCount != null) {
			predicates.add(totalTestedCount.buildQuery(inventoryViability.totalTestedCount));
		}
		if (actions != null) {
			var qInventoryViabilityAction = new QInventoryViabilityAction(inventoryViability.actions.getMetadata().getName());
			predicates.add(nestedFilter(inventoryViability.actions, qInventoryViabilityAction, qInventoryViabilityAction.inventoryViability().eq(inventoryViability), actions.collectPredicates(qInventoryViabilityAction)));
		}
		return predicates;
	}
}