AccessionRefFilter.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.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.genesys.blocks.model.filters.Filter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.util.FilterUtils;
import org.genesys.server.model.dataset.DatasetAccessionRef;
import org.genesys.server.model.dataset.QDatasetAccessionRef;
import org.genesys.server.service.filter.AccessionFilter;

import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.ListPath;

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

/**
 * The Class AccessionRefFilter.
 *
 * @author Viacheslav Pavlov
 * @author Matija Obreza
 */
@Getter
@Setter
@EqualsAndHashCode
@Accessors(fluent = true)
public class AccessionRefFilter implements Filter {

	private static final long serialVersionUID = 7898002699557658474L;

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

	/** The inst code. */
	public Set<String> instCode;

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

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

	/** The acce numb. */
	public StringFilter acceNumb;
	
	/** Matched accession filter */
	public AccessionFilter accession;

	@Override
	public boolean isEmpty() {
		return FilterUtils.isEmpty(doi, instCode, genus, species) && FilterUtils.isEmpty(acceNumb, accession);
	}

	/**
	 * Builds the query.
	 *
	 * @param accessionRefs the accession identifiers
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates(final ListPath<DatasetAccessionRef, QDatasetAccessionRef> accessionRefs) {
		final List<Predicate> predicates = new LinkedList<>();

		final QDatasetAccessionRef accessionRef = accessionRefs.any();
		if (doi != null && !doi.isEmpty()) {
			predicates.add(accessionRef.doi.isNotNull().and(accessionRef.doi.in(doi)));
		}
		if (instCode != null && !instCode.isEmpty()) {
			predicates.add(accessionRef.instCode.in(instCode));
		}
		if (genus != null && !genus.isEmpty()) {
			predicates.add(accessionRef.genus.in(genus));
		}
		if (species != null && !species.isEmpty()) {
			predicates.add(accessionRef.species.isNotNull().and(accessionRef.species.in(species)));
		}
		if (acceNumb != null) {
			predicates.add(acceNumb.buildQuery(accessionRef.acceNumb));
		}
		if (accession != null) {
			predicates.addAll(accession.collectPredicates(accessionRef.accession()));
		}

		return predicates;
	}

}