OrderRequestItemFilter.java

/*
 * Copyright 2020 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.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.TemporalFilter;
import org.gringlobal.model.OrderRequestItem;
import org.gringlobal.model.QOrderRequestItem;

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

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

	private static final long serialVersionUID = -8209395394721260358L;

	/** The completed date. */
	public TemporalFilter<Date> statusDate;

	/** The completed date code. */
	public Set<String> statusCode;

	/** The order request id. */
	public Set<Long> orderRequest;

	/** The inventory. */
	public InventoryFilter inventory;

	/** The distribution form code. */
	public Set<String> distributionFormCode;

	/** The external taxonomy. */
	public StringFilter externalTaxonomy;

	/** The withdrawn inventory. */
	public InventoryFilter withdrawnInventory;

	/** The name. */
	public StringFilter name;

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

	/** The quantity shipped. */
	public NumberFilter<Double> quantityShipped;

	/** The quantity shipped unit code. */
	public Set<String> quantityShippedUnitCode;

	/** The sequence number. */
	public NumberFilter<Integer> sequenceNumber;

	/** The source cooperator. */
	public CooperatorFilter sourceCooperator;

	/** The web user note. */
	public StringFilter webUserNote;

	/** The container type code. */
	public Set<String> containerTypeCode;

	/** The order request item actions. */
	public OrderRequestItemActionFilter actions;
	
	/**
	 * Builds the query.
	 *
	 * @return the predicate
	 */
	@Override
	public List<Predicate> collectPredicates() {
		return collectPredicates(QOrderRequestItem.orderRequestItem);
	}

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

		if (statusDate != null) {
			predicates.add(statusDate.buildQuery(orderRequestItem.statusDate));
		}
		if (CollectionUtils.isNotEmpty(orderRequest)) {
			predicates.add(orderRequestItem.orderRequest().id.in(orderRequest));
		}
		if (CollectionUtils.isNotEmpty(statusCode)) {
			predicates.add(orderRequestItem.statusCode.in(statusCode));
		}
		if (inventory != null) {
			predicates.addAll(inventory.collectPredicates(orderRequestItem.inventory()));
		}
		if (CollectionUtils.isNotEmpty(distributionFormCode)) {
			predicates.add(orderRequestItem.distributionFormCode.in(distributionFormCode));
		}
		if (externalTaxonomy != null) {
			predicates.add(externalTaxonomy.buildQuery(orderRequestItem.externalTaxonomy));
		}
		if (withdrawnInventory != null) {
			predicates.addAll(withdrawnInventory.collectPredicates(orderRequestItem.withdrawnInventory()));
		}
		if (name != null) {
			predicates.add(name.buildQuery(orderRequestItem.name));
		}
		if (note != null) {
			predicates.add(note.buildQuery(orderRequestItem.note));
		}
		if (quantityShipped != null) {
			predicates.add(quantityShipped.buildQuery(orderRequestItem.quantityShipped));
		}
		if (CollectionUtils.isNotEmpty(quantityShippedUnitCode)) {
			predicates.add(orderRequestItem.quantityShippedUnitCode.in(quantityShippedUnitCode));
		}
		if (sequenceNumber != null) {
			predicates.add(sequenceNumber.buildQuery(orderRequestItem.sequenceNumber));
		}
		if (sourceCooperator != null) {
			predicates.addAll(sourceCooperator.collectPredicates(orderRequestItem.sourceCooperator()));
		}
		if (webUserNote != null) {
			predicates.add(webUserNote.buildQuery(orderRequestItem.webUserNote));
		}
		if (CollectionUtils.isNotEmpty(containerTypeCode)) {
			predicates.add(orderRequestItem.containerTypeCode.in(containerTypeCode));
		}
		if (actions != null) {
			var qOrderRequestItemAction = new QOrderRequestItemAction(orderRequestItem.actions.getMetadata().getName());
			predicates.add(nestedFilter(orderRequestItem.actions, qOrderRequestItemAction, qOrderRequestItemAction.orderRequestItem().eq(orderRequestItem), actions.collectPredicates(qOrderRequestItemAction)));
		}
		return predicates;
	}

}