InstituteFilter.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.service.filter;

import static org.genesys.server.model.impl.QFaoInstitute.faoInstitute;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.genesys.blocks.model.filters.BasicModelFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.util.CurrentApplicationContext;
import org.genesys.server.model.filters.PartnerFilter;
import org.genesys.server.model.impl.FaoInstitute;
import org.genesys.server.model.impl.QFaoInstitute;
import org.genesys.server.service.PGRFANetworkService;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.querydsl.core.types.Predicate;

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

/**
 * Filter for {@link FaoInstitute}.
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class InstituteFilter extends BasicModelFilter<InstituteFilter, FaoInstitute> implements IFullTextFilter {

	private static final long serialVersionUID = 4754504385073612669L;

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

	/** The code. */
	public Set<String> code;
	
	/** The country. */
	public CountryFilter country;
	
	/** The name. */
	public StringFilter fullName;

	/** The accessions. */
	public Boolean accessions;

	/** The allowMaterialRequests. */
	public Boolean allowMaterialRequests;

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

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

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

	/**
	 * Builds the query.
	 *
	 * @param institute the institute
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates(QFaoInstitute institute) {
		final List<Predicate> predicates = super.collectPredicates(institute);

		if (code != null && !code.isEmpty()) {
			// predicates.add(institute.code.isNotNull());
			predicates.add(institute.code.in(code()));
		}
		if (country != null) {
			predicates.addAll(country.collectPredicates(institute.country()));
		}
		if (fullName != null) {
			predicates.add(fullName.buildQuery(institute.fullName));
		}
		if (accessions != null) {
			if (accessions) {
				predicates.add(institute.accessionCount.gt(0));
			} else {
				predicates.add(institute.accessionCount.eq(0L));
			}
		}
		if (allowMaterialRequests != null) {
			predicates.add(institute.allowMaterialRequests.eq(allowMaterialRequests));
		}
		if (owner != null) {
			predicates.addAll(owner.collectPredicates(institute.owner()));
		}
		
		if (CollectionUtils.isNotEmpty(networks)) {
			Set<Long> networkMembers = getNetworkMemberIds(networks);
			predicates.add(institute.id.in(networkMembers));
		}

		return predicates;
	}

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

	private Set<Long> getNetworkMemberIds(Set<String> slugs) {
		PGRFANetworkService networkService = CurrentApplicationContext.getContext().getBean(PGRFANetworkService.class);
		return networkService.getMembers(slugs).stream().map(FaoInstitute::getId).collect(Collectors.toSet());
	}

	public CountryFilter country() {
		if (this.country == null) {
			return this.country = new CountryFilter();
		} else {
			return this.country;
		}
	}
	
	@JsonGetter("code")
	public Set<String> code() {
		return code != null ? code.stream().map(String::toUpperCase).collect(Collectors.toSet()) : Collections.emptySet();
	}
}