DescriptorFilter.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.model.filters;

import static org.genesys.server.model.traits.QDescriptor.descriptor;

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

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

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.genesys.blocks.model.filters.UuidModelFilter;
import org.genesys.server.model.PublishState;
import org.genesys.server.model.traits.Descriptor;
import org.genesys.server.model.traits.QDescriptor;
import org.genesys.server.service.filter.IFullTextFilter;
import org.genesys.server.service.filter.InstituteFilter;

import com.querydsl.core.types.Predicate;

/**
 * The Class DescriptorFilter.
 *
 * @author Andrey Lugovskoy
 * @author Matija Obreza
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class DescriptorFilter extends UuidModelFilter<DescriptorFilter, Descriptor> implements IFullTextFilter {

	private static final long serialVersionUID = -7996157718282752063L;

	public static final String[] ES_BOOSTED_FIELDS = { "columnName", "title" };

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

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

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

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

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

	/** The column name. */
	public Set<String> columnName;

	/** The version tag. */
	public Set<String> versionTag;

	/** The bibliographic citation. */
	public StringFilter bibliographicCitation;

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

	/** The category. */
	public Set<Descriptor.Category> category;

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

	/** The key. */
	public Boolean key;

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

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

	/** The integer only. */
	public Boolean integerOnly;

	/** The min value. */
	public NumberFilter<Double> minValue;

	/** The max value. */
	public NumberFilter<Double> maxValue;

	/** Descriptor list filter */
	public DescriptorListFilter descriptorLists;

	/** Descriptor is in use: referenced in Dataset or DescriptorList. */
	public Boolean used;

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

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

	public List<Predicate> collectPredicates(final QDescriptor descriptorPath) {
		final List<Predicate> predicates = super.collectPredicates(descriptorPath, descriptorPath._super._super);

		if (crop != null && !crop.isEmpty()) {
			predicates.add(descriptorPath.crop.isNotNull().and(descriptorPath.crop.in(crop)));
		}
		if (category != null && !category.isEmpty()) {
			predicates.add(descriptorPath.category.isNotNull().and(descriptorPath.category.in(category)));
		}
		if (key != null) {
			predicates.add(descriptorPath.key.eq(key));
		}
		if (publisher != null && !publisher.isEmpty()) {
			predicates.add(descriptorPath.publisher.isNotNull().and(descriptorPath.publisher.in(publisher)));
		}
		if (state != null && !state.isEmpty()) {
			predicates.add(descriptorPath.state.in(state));
		}
		if (integerOnly != null) {
			predicates.add(descriptorPath.integerOnly.eq(integerOnly));
		}
		if (title != null) {
			predicates.add(title.buildQuery(descriptorPath.title));
		}
		if (description != null) {
			predicates.add(descriptorPath.description.isNotNull().and(description.buildQuery(descriptorPath.description)));
		}
		if (columnName != null && !columnName.isEmpty()) {
			predicates.add(descriptorPath.columnName.isNotNull().and(descriptorPath.columnName.in(columnName)));
		}
		if (versionTag != null && !versionTag.isEmpty()) {
			predicates.add(descriptorPath.versionTag.in(versionTag));
		}
		if (uom != null && !uom.isEmpty()) {
			predicates.add(descriptorPath.uom.isNotNull().and(descriptorPath.uom.in(uom)));
		}
		if (bibliographicCitation != null) {
			predicates.add(descriptorPath.bibliographicCitation.isNotNull().and(bibliographicCitation.buildQuery(descriptorPath.bibliographicCitation)));
		}
		if (owner != null) {
			predicates.addAll(owner.collectPredicates(descriptorPath.owner()));
		}
		if (institute != null) {
			predicates.addAll(institute.collectPredicates(descriptorPath.owner().institutes.any()));
		}
		if (minValue != null) {
			predicates.add(minValue.buildQuery(descriptorPath.minValue));
		}
		if (maxValue != null) {
			predicates.add(maxValue.buildQuery(descriptorPath.maxValue));
		}
		if (descriptorLists != null) {
			predicates.add(descriptorPath.descriptorLists.isNotEmpty());
			predicates.addAll(descriptorLists.collectPredicates(descriptorPath.descriptorLists.any()));
		}
		if (used != null) {
			if (used.booleanValue()==true) {
				predicates.add(descriptorPath.descriptorLists.isNotEmpty().or(descriptorPath.datasets.isNotEmpty()));
			} else {
				predicates.add(descriptorPath.descriptorLists.isEmpty().and(descriptorPath.datasets.isEmpty()));
			}
		}
		if (CollectionUtils.isNotEmpty(originalLanguageTag)) {
			predicates.add(descriptorPath.originalLanguageTag.in(originalLanguageTag));
		}

//		if (StringUtils.isNotBlank(_text)) {
//			/*@formatter:off*/
//			and.andAnyOf(
//				ArrayUtils.addAll(
//					FilterHelpers.equalsAny(_text,
//						descriptorPath.crop, descriptorPath.versionTag, descriptorPath.publisher,
//						descriptorPath.owner.shortName,
//						descriptorPath.descriptorLists.any().publisher, descriptorPath.descriptorLists.any().versionTag, descriptorPath.descriptorLists.any().crop
//					),
//					FilterHelpers.containsAll(_text,
//						descriptorPath.title, descriptorPath.description, descriptorPath.bibliographicCitation,
//						descriptorPath.owner.name,
//						descriptorPath.descriptorLists.any().title
//					)
//				)
//			);
//			/*@formatter:on*/
//		}

		return predicates;
	}

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

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

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

	@Override
	public String[] boostedFields() {
		return ES_BOOSTED_FIELDS;
	}

	/**
	 * Title filter.
	 *
	 * @return the string filter
	 */
	public synchronized StringFilter title() {
		return this.title == null ? this.title = new StringFilter() : this.title;
	}
}