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 static org.genesys.server.model.impl.QCountry.country;
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 com.querydsl.core.types.Predicate;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
/**
* The filter for {@link Country}
*/
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true)
public class CountryFilter extends BasicModelFilter<CountryFilter, Country> implements IFullTextFilter {
private static final long serialVersionUID = 2709549285697195920L;
public static final String[] ES_BOOSTED_FIELDS = { "code3", "name" };
/** Any text. */
public String _text;
/** The iso 3. */
public Set<String> code3;
/** The region codes. */
public Set<String> region;
/**
* Builds the query.
*
* @return the predicate
*/
public List<Predicate> collectPredicates() {
return collectPredicates(country);
}
/**
* Builds the query.
*
* @param country the country
* @return the predicate
*/
public List<Predicate> collectPredicates(QCountry country) {
final List<Predicate> predicates = super.collectPredicates(country);
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;
}
}