DiversityTreeFilter.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.genesys.server.service.filter;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.UuidModelFilter;
import org.genesys.server.model.PublishState;
import org.genesys.server.model.impl.DiversityTree;
import org.genesys.server.model.impl.QDiversityTree;

import com.querydsl.core.types.Predicate;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * Filters for {@link DiversityTree}.
 *
 * @author Maxym Borodenko
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class DiversityTreeFilter extends UuidModelFilter<DiversityTreeFilter, DiversityTree> implements IFullTextFilter {

	private static final long serialVersionUID = 3948516024666269840L;

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

	/** The title. */
	public StringFilter title;

	/** The description. */
	public StringFilter description;

	/** The date created. */
	public StringFilter dateCreated;

	/** The publish state. */
	public Set<PublishState> state;

	public Set<String> crop;

	/** Is current version */
	public Boolean current;

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

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

		if (title != null) {
			predicates.add(title.buildQuery(diversityTree.title));
		}
		if (description != null) {
			predicates.add(description.buildQuery(diversityTree.description));
		}
		if (dateCreated != null) {
			predicates.add(dateCreated.buildQuery(diversityTree.dateCreated));
		}
		if (state != null && !state.isEmpty()) {
			predicates.add(diversityTree.state.in(state));
		}
		if (crop != null && !crop.isEmpty()) {
			predicates.add(diversityTree.crop.in(crop));
		}
		if (current != null) {
			predicates.add(diversityTree.current.eq(current));
		}

		return predicates;
	}

	public synchronized DiversityTreeFilter state(PublishState... state) {
		if (this.state == null) {
			this.state = new HashSet<>();
		}
		Collections.addAll(this.state, state);
		return this;
	}

	public synchronized DiversityTreeFilter crop(String... crop) {
		if (this.crop == null) {
			this.crop = new HashSet<>();
		}
		Collections.addAll(this.crop, crop);
		return this;
	}

	public synchronized DiversityTreeFilter crop(Set<String> crop) {
		this.crop = crop;
		return this;
	}

	@Override
	public String get_text() {
		return _text;
	}
}