InventoryViabilityDataFilter.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 com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.querydsl.core.types.Predicate;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.commons.collections.CollectionUtils;
import org.gringlobal.model.InventoryViabilityData;
import org.gringlobal.model.QInventoryViabilityData;

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

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

	private static final long serialVersionUID = -2982144698806187362L;

	@JsonUnwrapped
	@JsonIgnoreProperties({ "id", "ownedBy", "ownedDate", "createdBy", "createdDate", "modifiedBy", "modifiedDate" })
	public InventoryViabilityFilter inventoryViabilityFilter;
	
	public Set<Long> inventoryViabilityId;
	
	/**
	 * Builds the query.
	 *
	 * @return the predicate
	 */
	@Override
	public List<Predicate> collectPredicates() {
		return collectPredicates(QInventoryViabilityData.inventoryViabilityData);
	}

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

		// InventoryViabilityFilter
		if (inventoryViabilityFilter != null) {
			if (inventoryViabilityFilter.inventory != null) {
				predicates.addAll(inventoryViabilityFilter.inventory.collectPredicates(inventoryViabilityData.inventoryViability().inventory()));
			}
			if (inventoryViabilityFilter.inventoryViabilityRule != null) {
				predicates.addAll(inventoryViabilityFilter.inventoryViabilityRule.collectPredicates(inventoryViabilityData.inventoryViability().inventoryViabilityRule()));
			}
			if (inventoryViabilityFilter.testedDate != null) {
				predicates.add(inventoryViabilityFilter.testedDate.buildQuery(inventoryViabilityData.inventoryViability().testedDate));
			}
			if (CollectionUtils.isNotEmpty(inventoryViabilityFilter.vigorRatingCode)) {
				predicates.add(inventoryViabilityData.inventoryViability().vigorRatingCode.in(inventoryViabilityFilter.vigorRatingCode));
			}
			if (inventoryViabilityFilter.percentViable != null) {
				predicates.add(inventoryViabilityFilter.percentViable.buildQuery(inventoryViabilityData.inventoryViability().percentViable));
			}
			if (inventoryViabilityFilter.replicationCount != null) {
				predicates.add(inventoryViabilityFilter.replicationCount.buildQuery(inventoryViabilityData.inventoryViability().replicationCount));
			}
			if (inventoryViabilityFilter.totalTestedCount != null) {
				predicates.add(inventoryViabilityFilter.totalTestedCount.buildQuery(inventoryViabilityData.inventoryViability().totalTestedCount));
			}
		}
		
		if (CollectionUtils.isNotEmpty(inventoryViabilityId)) {
			predicates.add(inventoryViabilityData.inventoryViability().id.in(inventoryViabilityId));
		}
		return predicates;
	}
}