SubsetFilter.java

/*
 * Copyright 2018 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.time.Instant;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.TemporalFilter;
import org.genesys.blocks.model.filters.UuidModelFilter;
import org.genesys.server.model.PublishState;
import org.genesys.server.model.filters.PartnerFilter;
import org.genesys.server.model.impl.QSubset;
import org.genesys.server.model.impl.Subset;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.querydsl.core.types.Predicate;

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


/**
 * The Class SubsetFilter.
 *
 * @author Maxym Borodenko
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class SubsetFilter extends UuidModelFilter<SubsetFilter, Subset> implements IFullTextFilter {

	private static final long serialVersionUID = 7779104030650048760L;

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

	/** The owner. */
	public PartnerFilter owner;

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

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

	/** The publisher. */
	public Set<String> publisher;

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

	/**
	 * Institutes.
	 * @deprecated Replaced with {@link #institute}
	 */
	@Deprecated
	public Set<String> institutes;

	/** The institute. */
	public InstituteFilter institute;

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

	public Set<String> crops;

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

	/** The subset type */
	public Set<Subset.SubsetType> subsetType;

	/** The first published date */
	public TemporalFilter<Instant> firstPublishedDate;
	
	/**
	 * Builds the query.
	 *
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates() {
		return collectPredicates(QSubset.subset);
	}
	/**
	 * Builds the query.
	 *
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates(QSubset subset) {
		final List<Predicate> predicates = super.collectPredicates(subset, subset._super._super);

		if (owner != null) {
			predicates.addAll(owner.collectPredicates(subset.owner()));
		}
		if (institute != null) {
			predicates.addAll(institute.collectPredicates(subset.institute()));
		} else if (CollectionUtils.isNotEmpty(institutes())) {
			// TODO To be removed
			predicates.add(subset.institute().code.in(institutes()));
		}
		if (title != null) {
			predicates.add(title.buildQuery(subset.title));
		}
		if (description != null) {
			predicates.add(description.buildQuery(subset.description));
		}
		if (CollectionUtils.isNotEmpty(publisher)) {
			predicates.add(subset.publisher.isNotNull().and(subset.publisher.in(publisher)));
		}
		if (dateCreated != null) {
			predicates.add(dateCreated.buildQuery(subset.dateCreated));
		}
		if (state != null && !state.isEmpty()) {
			predicates.add(subset.state.in(state));
		}
		if (CollectionUtils.isNotEmpty(crops)) {
			// predicates.add(subset.crops.isNotEmpty()); // is an element collection!
			predicates.add(subset.crops.any().in(crops));
		}
		if (current != null) {
			predicates.add(subset.current.eq(current));
		}
		if (subsetType != null && !subsetType.isEmpty()) {
			predicates.add(subset.subsetType.isNotNull().and(subset.subsetType.in(subsetType)));
		}
		if (firstPublishedDate != null) {
			predicates.add(firstPublishedDate.buildQuery(subset.firstPublishedDate));
		}
		return predicates;
	}

	public SubsetFilter state(PublishState ... state) {
		if (this.state == null) {
			this.state = new HashSet<>();
		}
		for (PublishState s : state) {
			this.state.add(s);
		}
		return this;
	}

	public SubsetFilter state(Set<PublishState> state) {
		this.state = state;
		return this;
	}

	/**
	 * Current.
	 *
	 * @param current the current
	 * @return the subset filter
	 */
	public SubsetFilter current(Boolean current) {
		this.current = current;
		return this;
	}

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

	public PartnerFilter owner() {
		if (this.owner == null) {
			return this.owner = new PartnerFilter();
		}
		return this.owner;
	}

	@JsonGetter("institutes")
	public synchronized Set<String> institutes() {
		if (this.institutes == null) {
			return this.institutes = new HashSet<>();
		} else {
			return this.institutes.stream().map(String::toUpperCase).collect(Collectors.toSet());
		}
	}
}