DescriptorListFilter.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 static org.genesys.server.model.traits.QDescriptorList.descriptorList;

import java.util.HashSet;
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.genesys.blocks.model.filters.UuidModelFilter;
import org.genesys.server.model.PublishState;
import org.genesys.server.model.traits.DescriptorList;
import org.genesys.server.model.traits.QDescriptorList;
import org.genesys.server.service.filter.IFullTextFilter;
import org.genesys.server.service.filter.InstituteFilter;

import com.querydsl.core.types.Predicate;

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

	private static final long serialVersionUID = -5524243000542552690L;

	/** 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 bibliographic citation. */
	public StringFilter bibliographicCitation;

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

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

	/** The publisher. */
	public StringFilter publisher;
	
	/** The descriptor. */
	public DescriptorFilter descriptor;

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

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

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

		if (crop != null && !crop.isEmpty()) {
			predicates.add(descriptorList.crop.isNotNull().and(descriptorList.crop.in(crop)));
		}
		if (state != null && !state.isEmpty()) {
			predicates.add(descriptorList.state.in(state));
		}
		if (publisher != null) {
			predicates.add(publisher.buildQuery(descriptorList.publisher));
		}
		if (title != null) {
			predicates.add(title.buildQuery(descriptorList.title));
		}
		if (description != null) {
			predicates.add(description.buildQuery(descriptorList.description));
		}
		if (bibliographicCitation != null) {
			predicates.add(bibliographicCitation.buildQuery(descriptorList.bibliographicCitation));
		}
		if (owner != null) {
			predicates.addAll(owner.collectPredicates(descriptorList.owner()));
		}
		if (institute != null) {
			predicates.addAll(institute.collectPredicates(descriptorList.owner().institutes.any()));
		}
		if (descriptor != null) {
			predicates.addAll(descriptor.collectPredicates(descriptorList.descriptors.any()));
		}
		if (CollectionUtils.isNotEmpty(originalLanguageTag)) {
			predicates.add(descriptorList.originalLanguageTag.in(originalLanguageTag));
		}


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

		return predicates;
	}

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

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

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

	public synchronized 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;
	}

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