CropTraitFilter.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.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.StringFilter;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.CropTraitLang;
import org.gringlobal.model.QCropTrait;

import com.querydsl.core.types.Predicate;

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

	private static final long serialVersionUID = 8424281927306885550L;

	/** Any text. */
	public String _text;
	
	/** The crop */
	public Set<Long> crop;

	/** The codedName */
	public Set<String> codedName;

	/** The categoryCode */
	public Set<String> categoryCode;

	/** The description from {@link CropTraitLang} */
	public StringFilter description;

	/** The title from {@link CropTraitLang} */
	public StringFilter title;

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

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

		if (CollectionUtils.isNotEmpty(crop)) {
			predicates.add(cropTrait.crop().id.in(crop));
		}
		if (CollectionUtils.isNotEmpty(codedName)) {
			predicates.add(cropTrait.codedName.in(codedName));
		}
		if (CollectionUtils.isNotEmpty(categoryCode)) {
			predicates.add(cropTrait.categoryCode.in(categoryCode));
		}
		if (description != null) {
			predicates.add(description.buildQuery(cropTrait.langs.any().description));
		}
		if (title != null) {
			predicates.add(title.buildQuery(cropTrait.langs.any().title));
		}

		return predicates;
	}

	/**
	 * Gets the text.
	 *
	 * @return the text
	 */
	@Override
	public String get_text() {
		return _text;
	}
}