SourceDescObservationFilter.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 com.querydsl.core.types.Predicate;
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.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.gringlobal.model.QSourceDescObservation;
import org.gringlobal.model.SourceDescObservation;

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

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

	private static final long serialVersionUID = 3717855311144522539L;

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

	/** The acce source. */
	public AccessionSourceFilter accessionSource;

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

	/** The numeric value. */
	public NumberFilter<Double> numericValue;

	/** The original value. */
	public Set<String> originalValue;

	/** The source descriptor. */
	public SourceDescriptorFilter sourceDescriptor;

	/** The source descriptor code. */
	public SourceDescriptorCodeFilter sourceDescriptorCode;

	/** The string value. */
	public Set<String> stringValue;

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

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

		if (accessionSource != null) {
			predicates.addAll(accessionSource.collectPredicates(sourceDescObservation.accessionSource()));
		}
		if (note != null) {
			predicates.add(note.buildQuery(sourceDescObservation.note));
		}
		if (numericValue != null) {
			predicates.add(numericValue.buildQuery(sourceDescObservation.numericValue));
		}
		if (CollectionUtils.isNotEmpty(originalValue)) {
			predicates.add(sourceDescObservation.originalValue.in(originalValue));
		}
		if (sourceDescriptor != null) {
			predicates.addAll(sourceDescriptor.collectPredicates(sourceDescObservation.sourceDescriptor()));
		}
		if (sourceDescriptorCode != null) {
			predicates.addAll(sourceDescriptorCode.collectPredicates(sourceDescObservation.sourceDescriptorCode()));
		}
		if (CollectionUtils.isNotEmpty(stringValue)) {
			predicates.add(sourceDescObservation.stringValue.in(stringValue));
		}

		return predicates;
	}

	/**
	 * Gets the text.
	 *
	 * @return the text
	 */
	@Override
	public String get_text() {
		return _text;
	}
	
	public static class SourceDescValueFilter {
		public Long sourceDescriptorId;
		public NumberFilter<Double> numericValue;
		public StringFilter stringValue;
		public Set<String> codeValues;
		
		public Predicate buildPredicate(QSourceDescObservation qSourceDescObs) {
			var predicate = qSourceDescObs.sourceDescriptor().id.eq(sourceDescriptorId);
			if (numericValue != null) {
				predicate = predicate.and(numericValue.buildQuery(qSourceDescObs.numericValue));
			}
			if (stringValue != null) {
				predicate = predicate.and(stringValue.buildQuery(qSourceDescObs.stringValue));
			}
			if (CollectionUtils.isNotEmpty(codeValues)) {
				predicate = predicate.and(qSourceDescObs.sourceDescriptorCode().code.in(codeValues));
			}
			return predicate;
		}
	}
}