CitationFilter.java

/*
 * Copyright 2024 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 com.querydsl.core.types.Predicate;
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.bib.Citation;
import org.genesys.server.model.bib.CitationType;
import org.genesys.server.model.bib.QCitation;
import org.genesys.server.model.bib.RelevanceType;
import org.genesys.server.model.bib.SourceType;
import org.genesys.server.service.filter.IFullTextFilter;

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

import javax.validation.constraints.Pattern;

@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class CitationFilter extends UuidModelFilter<CitationFilter, Citation> implements IFullTextFilter {

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

	/** Any text. */
	public String _text;
	
	/** The title. */
	public StringFilter title;

	/** The author name. */
	public StringFilter authorLastName;

	/** The citation year. */
	public NumberFilter<Integer> publicationYear;

	/** The journal. */
	public StringFilter publication;

	/** The type. */
	public Set<CitationType> type;

	/** The doi reference. */
	public Set<String> doi;

	/** Language. */
	public Set<String> language;

	/** Keywords. */
	public Set<String> keywords;

	/** The source type. */
	public Set<SourceType> sourceType;

	/** The relevance. */
	public Set<RelevanceType> relevance;

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

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

	/** The status text. */
	public StringFilter statusText;

	/**
	 * The collection
	 * @deprecated Use {@link #collections}
	 */
	@Deprecated
	public Set<String> collection;

	/** The collection. */
	public Set<String> collections;

	/** The taxonomy families */
	public Set<String> taxonomyFamilies;

	/** The taxonomy species */
	public Set<String> taxonomySpecies;

	/** The institute codes */
	public Set<@Pattern(regexp = "[A-Z]{3}\\d{3,4}") String> instituteCodes;

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

	/**
	 * Collect predicates.
	 *
	 * @param citation the citation path
	 * @return the list
	 */
	public List<Predicate> collectPredicates(final QCitation citation) {
		final List<Predicate> predicates = super.collectPredicates(citation, citation._super);
		
		if (title != null) {
			predicates.add(title.buildQuery(citation.title));
		}
		 if (authorLastName != null) {
		 	predicates.add(authorLastName.buildQuery(citation.authors.any().lastName));
		 }
		if (publicationYear != null) {
			predicates.add(publicationYear.buildQuery(citation.publicationYear));
		}
		if (publication != null) {
			predicates.add(publication.buildQuery(citation.publication));
		}
		if (CollectionUtils.isNotEmpty(type)) {
			predicates.add(citation.type.in(type));
		}
		if (CollectionUtils.isNotEmpty(doi)) {
			predicates.add(citation.doi.in(doi));
		}
		if (CollectionUtils.isNotEmpty(language)) {
			predicates.add(citation.language.in(language));
		}
		if (CollectionUtils.isNotEmpty(keywords)) {
			predicates.add(citation.keywords.any().in(keywords));
		}
		// if (abstractText != null) {
		// 	predicates.add(abstractText.buildQuery(citation.abstractText));
		// }
		if (CollectionUtils.isNotEmpty(sourceType)) {
			predicates.add(citation.sourceType.in(sourceType));
		}
		if (CollectionUtils.isNotEmpty(relevance)) {
			predicates.add(citation.relevance.in(relevance));
		}
		if (owner != null) {
			predicates.addAll(owner.collectPredicates(citation.owner()));
		}
		if (CollectionUtils.isNotEmpty(state)) {
			predicates.add(citation.state.in(state));
		}
		if (statusText != null) {
			predicates.add(statusText.buildQuery(citation.statusText));
		}
		if (CollectionUtils.isNotEmpty(collections)) {
			predicates.add(citation.collections.any().in(collections));
		} else if (CollectionUtils.isNotEmpty(collection)) {
			predicates.add(citation.collections.any().in(collection));
		}
		if (CollectionUtils.isNotEmpty(taxonomyFamilies)) {
			predicates.add(citation.taxonomyFamilies.any().in(taxonomyFamilies));
		}
		if (CollectionUtils.isNotEmpty(taxonomySpecies)) {
			predicates.add(citation.taxonomySpecies.any().in(taxonomySpecies));
		}
		if (CollectionUtils.isNotEmpty(instituteCodes)) {
			predicates.add(citation.instituteCodes.any().in(instituteCodes));
		}
		return predicates;
	}

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

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