OrderRequestFilter.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.HashSet;
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.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.TemporalFilter;
import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.gringlobal.model.OrderRequest;
import org.gringlobal.model.QOrderRequest;

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

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

	private static final long serialVersionUID = -6829266535027252579L;

	/** Any text. */
	public String _text;

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

	/** The intended use code. */
	public Set<String> intendedUseCode;

	/** The local number. */
	public StringFilter localNumber;

	/** The order obtained via. */
	public StringFilter orderObtainedVia;

	/** The order type code. */
	public Set<String> orderTypeCode;

	/** The ordered date. */
	public TemporalFilter<Date> orderedDate;

	/** The requestor cooperator. */
	public CooperatorFilter requestorCooperator;

	/** The ship to cooperator. */
	public CooperatorFilter shipToCooperator;

	/** The final recipient cooperator. */
	public CooperatorFilter finalRecipientCooperator;

	/** The authorized official. */
	public CooperatorFilter authorizedOfficialCooperator;

	/** The number of order request items */
	public NumberFilter<Integer> numberOfItems;

	/** The duration of request in days */
	public NumberFilter<Integer> durationInDays;

	/** The MTA Type. */
	public Set<String> mtaType;

	/** The order request actions. */
	public OrderRequestActionFilter actions;

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

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

		if (completedDate != null) {
			predicates.add(completedDate.buildQuery(orderRequest.completedDate));
		}
		if (finalRecipientCooperator != null) {
			predicates.addAll(finalRecipientCooperator.collectPredicates(orderRequest.finalRecipientCooperator()));
		}
		if (authorizedOfficialCooperator != null) {
			predicates.addAll(authorizedOfficialCooperator.collectPredicates(orderRequest.authorizedOfficialCooperator()));
		}
		if (CollectionUtils.isNotEmpty(intendedUseCode)) {
			predicates.add(orderRequest.intendedUseCode.in(intendedUseCode));
		}
		if (localNumber != null) {
			predicates.add(localNumber.buildQuery(orderRequest.localNumber));
		}
		if (orderObtainedVia != null) {
			predicates.add(orderObtainedVia.buildQuery(orderRequest.orderObtainedVia));
		}
		if (CollectionUtils.isNotEmpty(orderTypeCode)) {
			predicates.add(orderRequest.orderTypeCode.in(orderTypeCode));
		}
		if (orderedDate != null) {
			predicates.add(orderedDate.buildQuery(orderRequest.orderedDate));
		}
		if (requestorCooperator != null) {
			predicates.addAll(requestorCooperator.collectPredicates(orderRequest.requestorCooperator()));
		}
		if (shipToCooperator != null) {
			predicates.addAll(shipToCooperator.collectPredicates(orderRequest.shipToCooperator()));
		}
		if (numberOfItems != null) {
			predicates.add(numberOfItems.buildQuery(orderRequest.numberOfItems));
		}
		if (durationInDays != null) {
			predicates.add(durationInDays.buildQuery(orderRequest.durationInDays));
		}
		if (CollectionUtils.isNotEmpty(mtaType)) {
			predicates.add(orderRequest.mtaType.in(mtaType));
		}
		if (actions != null) {
			var qOrderRequestAction = new QOrderRequestAction(orderRequest.actions.getMetadata().getName());
			predicates.add(nestedFilter(orderRequest.actions, qOrderRequestAction, qOrderRequestAction.orderRequest().eq(orderRequest), actions.collectPredicates(qOrderRequestAction)));
		}

		return predicates;
	}

	public synchronized Set<String> intendedUseCode() {
		return this.intendedUseCode == null ? this.intendedUseCode = new HashSet<>() : this.intendedUseCode;
	}
	

	public String get_text() {
		return _text;
	}
}