DatasetFilter.java

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

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.dataset.Dataset;
import org.genesys.server.model.dataset.QDataset;
import org.genesys.server.service.filter.IFullTextFilter;
import org.genesys.server.service.filter.InstituteFilter;

import com.querydsl.core.types.Predicate;

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

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

	private static final long serialVersionUID = -3014463705200950518L;

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

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

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

	/** The accession reference. */
	public AccessionRefFilter accessionRefs;

	/** Dataset location filter */
	public DatasetLocationFilter locations;

	/** Descriptor filter */
	public DescriptorFilter descriptors;

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

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

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

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

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

	public Set<String> rights;

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

	/** The first published date */
	public TemporalFilter<Instant> firstPublishedDate;

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

	/**
	 * Collects filter predicates
	 *
	 * @return the list of predicates
	 */
	public List<Predicate> collectPredicates(QDataset dataset) {
		final List<Predicate> predicates = super.collectPredicates(dataset, dataset._super._super);

		if (CollectionUtils.isNotEmpty(state)) {
			predicates.add(dataset.state.in(state));
		}
		if (title != null) {
			predicates.add(title.buildQuery(dataset.title));
		}
		if (description != null) {
			predicates.add(dataset.description.isNotNull().and(description.buildQuery(dataset.description)));
		}
		if (owner != null) {
			predicates.addAll(owner.collectPredicates(dataset.owner()));
		}
		if (institute != null) {
			predicates.addAll(institute.collectPredicates(dataset.owner().institutes.any()));
		}
		if (accessionRefs != null) {
			predicates.add(dataset.accessionRefs.isNotEmpty());
			predicates.addAll(accessionRefs.collectPredicates(dataset.accessionRefs));
		}
		if (descriptors != null) {
			predicates.add(dataset.descriptors.isNotEmpty());
			predicates.addAll(descriptors.collectPredicates(dataset.descriptors.any()));
		}
		if (CollectionUtils.isNotEmpty(originalLanguageTag)) {
			predicates.add(dataset.originalLanguageTag.isNotNull().and(dataset.originalLanguageTag.in(originalLanguageTag)));
		}
		if (CollectionUtils.isNotEmpty(crops)) {
			// predicates.add(dataset.crops.isNotEmpty()); // is an element collection
			predicates.add(dataset.crops.any().in(crops));
		}
		if (locations != null) {
			// predicates.add(dataset.locations.isNotEmpty()); // is an element collection
			predicates.addAll(locations.collectPredicates(dataset.locations.any()));
		}

		if (CollectionUtils.isNotEmpty(rights)) {
			predicates.add(dataset.rights.isNotNull().and(dataset.rights.in(rights)));
		}
		if (current != null) {
			predicates.add(dataset.current.eq(current));
		}
		if (firstPublishedDate != null) {
			predicates.add(firstPublishedDate.buildQuery(dataset.firstPublishedDate));
		}
//		if (StringUtils.isNotBlank(_text)) {
//			/*@formatter:off*/
//			and.andAnyOf(
//				ArrayUtils.addAll(
//					FilterHelpers.equalsAny(_text,
//						dataset.versionTag,
//						dataset.owner.shortName,
//						dataset.crops.any(),
//						dataset.descriptors.any().crop,
//						dataset.accessionRefs.any().genus, dataset.accessionRefs.any().instCode, dataset.accessionRefs.any().acceNumb
//					),
//					FilterHelpers.containsAll(_text,
//						dataset.title, dataset.description,
//						dataset.creators.any().fullName,
//						dataset.locations.any().verbatimLocality,
//						dataset.repositoryFiles.any().title, dataset.repositoryFiles.any().originalFilename,
//						dataset.owner.name,
//						dataset.descriptors.any().title
//					)
//				)
//			);
//			/*@formatter:on*/
//		}

		return predicates;
	}

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

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

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

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

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