CountryFilter.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 java.util.Collection;
import java.util.List;
import java.util.Set;

import org.genesys.blocks.model.filters.BasicModelFilter;
import org.genesys.blocks.util.CurrentApplicationContext;
import org.genesys.server.model.impl.Country;
import org.genesys.server.model.impl.QCountry;
import org.genesys.server.service.GeoRegionService;

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

import com.fasterxml.jackson.annotation.JsonProperty;
import com.querydsl.core.types.Predicate;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;

/**
 * The filter for {@link Country}
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
@Schema(description = "Filtering criteria to apply to the country records."
	+ "\n\nEach individual filter property can also be set to null or omitted, meaning that it should not be applied."
)
public class CountryFilter extends BasicModelFilter<CountryFilter, Country, QCountry> implements IFullTextFilter {

	private static final long serialVersionUID = 2709549285697195920L;

	public static final String[] ES_BOOSTED_FIELDS = { "code3", "name" };

	/** Any text. */
	@Schema(description = "Keywords to use in a full-text search across the country database. Whenever possible, use any of the other filtering options for better results. Full-text search is not available in nested filters!")
	@JsonProperty("_text")
	public String _text;

	/** The iso 3. */
	@ArraySchema(schema = @Schema(description = "ISO-3165 country code", example = "DEU"), arraySchema = @Schema(description = "Match countries by their three-letter ISO-3166 codes", example = "NGA, DEU, SVN"))
	public Set<String> code3;

	/** The region codes. */
	public Set<String> region;

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

	/**
	 * Builds the query.
	 *
	 * @param country the country
	 * @return the predicate
	 */
	public List<Predicate> collectPredicates(QCountry country) {
		final List<Predicate> predicates = super.collectSuperPredicates(country, country._super);

		if (code3 != null && !code3.isEmpty()) {
			predicates.add(country.code3.in(code3));
		}
		if (region != null && ! region.isEmpty()) {
			predicates.add(country.code3.in(getCountriesInRegions(region)));
		}

		return predicates;
	}

	private Collection<String> getCountriesInRegions(Set<String> regionCodes) {
		GeoRegionService geoRegionService = CurrentApplicationContext.getContext().getBean(GeoRegionService.class);
		return geoRegionService.countryCodesFor(regionCodes);
	}

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

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