InventoryViabilityRuleFilter.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.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.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.gringlobal.model.InventoryViabilityAction;
import org.gringlobal.model.InventoryViabilityRule;
import org.gringlobal.model.InventoryViabilityRuleMap;
import org.gringlobal.model.QInventoryViabilityRule;

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

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

	private static final long serialVersionUID = -5632211466851048351L;

	public StringFilter name;

	public Set<String> categoryCode;
	
	public NumberFilter<Integer> numberOfReplicates;
	
	public NumberFilter<Integer> seedsPerReplicate;

	public TaxonomySpeciesFilter taxonomySpecies;

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

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

		if (CollectionUtils.isNotEmpty(categoryCode)) {
			predicates.add(inventoryViabilityRule.categoryCode.in(categoryCode));
		}
		if (name != null) {
			predicates.add(name.buildQuery(inventoryViabilityRule.name));
		}
		if (numberOfReplicates != null) {
			predicates.add(numberOfReplicates.buildQuery(inventoryViabilityRule.numberOfReplicates));
		}
		if (seedsPerReplicate != null) {
			predicates.add(seedsPerReplicate.buildQuery(inventoryViabilityRule.seedsPerReplicate));
		}
		if (taxonomySpecies != null) {
			var ruleMapFilter = new InventoryViabilityRuleMapFilter();
			ruleMapFilter.taxonomySpecies = taxonomySpecies;
			var qRuleMap = new QInventoryViabilityRuleMap(inventoryViabilityRule.ruleMaps.getMetadata().getName()); // alias MUST match inventoryViabilityRule.ruleMaps path
			predicates.add(nestedFilter(inventoryViabilityRule.ruleMaps, qRuleMap, qRuleMap.inventoryViabilityRule().eq(inventoryViabilityRule), ruleMapFilter.collectPredicates(qRuleMap)));
		}
		return predicates;
	}

	private static class InventoryViabilityRuleMapFilter extends CooperatorOwnedModelFilter<InventoryViabilityRuleMapFilter, InventoryViabilityRuleMap> {
		private static final long serialVersionUID = 6491011291039476604L;
		private TaxonomySpeciesFilter taxonomySpecies;

		@Override
		public List<Predicate> collectPredicates() {
			return collectPredicates(QInventoryViabilityRuleMap.inventoryViabilityRuleMap);
		}

		public List<Predicate> collectPredicates(QInventoryViabilityRuleMap ruleMap) {
			List<Predicate> predicates = super.collectPredicates(ruleMap, ruleMap._super);
			if (taxonomySpecies != null) {
				predicates.addAll(taxonomySpecies.collectPredicates(ruleMap.taxonomySpecies()));
			}
			return predicates;
		}
	}
}