LocationReservationFilter.java

/*
 * Copyright 2022 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.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
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.api.exception.InvalidApiUsageException;
import org.gringlobal.model.community.LocationReservation;
import org.gringlobal.model.community.QLocationReservation;

import java.time.LocalDate;
import java.util.List;
import java.util.Set;

public class LocationReservationFilter extends CooperatorOwnedModelFilter<LocationReservationFilter, LocationReservation, QLocationReservation> {
	private static final long serialVersionUID = 4689641557606380614L;

	/** Location */
	public LocationFilter location;

	/** Inventory */
	public InventoryFilter inventory;

	/** The size. */
	public NumberFilter<Integer> size;

	/** The offset. */
	public NumberFilter<Integer> offset;

	/** The startDate. */
	public TemporalFilter<LocalDate> startDate;

	/** The endDate. */
	public TemporalFilter<LocalDate> endDate;

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

	/** To use state filters, then the client must provide "today's date" */
	public LocalDate today;
	public Set<LocationReservationState> states;


	public enum LocationReservationState {
		PENDING,
		ACTIVE,
		COMPLETED
	}

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

	/**
	 * Builds the query.
	 *
	 * @param locationReservation the crop
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates(QLocationReservation locationReservation) {
		final List<Predicate> predicates = super.collectSuperPredicates(locationReservation, locationReservation._super);
		if (location != null) {
			predicates.add(location.nestedPredicate(locationReservation.location()));
		}
		if (inventory != null) {
			predicates.add(inventory.nestedPredicate(locationReservation.inventory()));
		}
		if (size != null) {
			predicates.add(size.buildQuery(locationReservation.size));
		}
		if (offset != null) {
			predicates.add(offset.buildQuery(locationReservation.offset));
		}
		if (startDate != null) {
			predicates.add(startDate.buildQuery(locationReservation.startDate));
		}
		if (endDate != null) {
			predicates.add(endDate.buildQuery(locationReservation.endDate));
		}
		if (note != null) {
			predicates.add(note.buildQuery(locationReservation.note));
		}
		if (CollectionUtils.isNotEmpty(states)) {
			if (states.size() < LocationReservationState.values().length) {
				if (today == null) {
					throw new InvalidApiUsageException("Value for today must be provided");
				}
				BooleanBuilder statePredicates = new BooleanBuilder();
				states.forEach((state) -> {
					switch (state) {
						case ACTIVE:
							statePredicates.or(locationReservation.startDate.isNotNull().and(locationReservation.endDate.isNotNull())
								.and(locationReservation.startDate.loe(today)).and(locationReservation.endDate.goe(today)));
							break;
						case PENDING:
							statePredicates.or(locationReservation.startDate.isNotNull().and(locationReservation.startDate.gt(today)));
							break;
						case COMPLETED:
							statePredicates.or(locationReservation.endDate.isNotNull().and(locationReservation.endDate.lt(today)));
							break;
						default:
							throw new InvalidApiUsageException("Unsupported action state " + state);
					}
				});
				predicates.add(statePredicates);
			}
		}

		return predicates;
	}
}