Commit 9d15f47f authored by Matija Obreza's avatar Matija Obreza

Updated CountryFilter with region codes

- Accepts region codes and loads country iso3 codes from GeoRegionService
- ES: Fixed nesting of Region in Country
- Updated GeoRegion#isoCode and #name column definitions
- QDSL was null because of http://www.querydsl.com/static/querydsl/3.5.0/reference/html/ch03s03.html#d0e2181
parent 75cbac61
...@@ -81,6 +81,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU ...@@ -81,6 +81,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU
@JsonView({ JsonViews.Minimal.class }) @JsonView({ JsonViews.Minimal.class })
@Field(type = FieldType.Object) @Field(type = FieldType.Object)
@JsonIgnoreProperties({"settings"}) @JsonIgnoreProperties({"settings"})
// @QueryInit({ "country.region.*" })
private FaoInstitute institute; private FaoInstitute institute;
@Column(name = "acceNumb", nullable = false, length = 128) @Column(name = "acceNumb", nullable = false, length = 128)
...@@ -117,6 +118,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU ...@@ -117,6 +118,7 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU
@JoinColumn(name = "orgCtyId", nullable = true) @JoinColumn(name = "orgCtyId", nullable = true)
@JsonView({ JsonViews.Public.class }) @JsonView({ JsonViews.Public.class })
@Field(type = FieldType.Object) @Field(type = FieldType.Object)
// @QueryInit({ "region.*" })
private Country countryOfOrigin; private Country countryOfOrigin;
@Column(name = "sampStat", length = 3) @Column(name = "sampStat", length = 3)
......
...@@ -74,7 +74,7 @@ public class Country extends BasicModel { ...@@ -74,7 +74,7 @@ public class Country extends BasicModel {
@JoinColumn(name = "regionId") @JoinColumn(name = "regionId")
@JsonView({ JsonViews.Public.class }) @JsonView({ JsonViews.Public.class })
@JsonIgnoreProperties({ "countries", "parentRegion" }) @JsonIgnoreProperties({ "countries", "parentRegion" })
@Field(type = FieldType.Nested) @Field(type = FieldType.Object)
private GeoRegion region; private GeoRegion region;
@Column(name = "minLongitude") @Column(name = "minLongitude")
......
...@@ -21,6 +21,8 @@ import org.genesys.blocks.model.JsonViews; ...@@ -21,6 +21,8 @@ import org.genesys.blocks.model.JsonViews;
import org.genesys.blocks.model.VersionedModel; import org.genesys.blocks.model.VersionedModel;
import org.genesys.custom.elasticsearch.IgnoreField; import org.genesys.custom.elasticsearch.IgnoreField;
import org.hibernate.annotations.Type; import org.hibernate.annotations.Type;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
import com.fasterxml.jackson.annotation.JsonIdentityReference; import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
...@@ -35,10 +37,11 @@ public class GeoRegion extends VersionedModel { ...@@ -35,10 +37,11 @@ public class GeoRegion extends VersionedModel {
private static final long serialVersionUID = -1L; private static final long serialVersionUID = -1L;
@Column @Column(length = 3, nullable = false, unique = true)
@Field(index = FieldIndex.not_analyzed)
private String isoCode; private String isoCode;
@Column @Column(nullable = false, length = 250)
private String name; private String name;
@ManyToOne() @ManyToOne()
......
...@@ -4,8 +4,9 @@ import org.genesys2.server.model.impl.Country; ...@@ -4,8 +4,9 @@ import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.GeoRegion; import org.genesys2.server.model.impl.GeoRegion;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
public interface GeoRegionRepository extends JpaRepository<GeoRegion, Long>{ public interface GeoRegionRepository extends JpaRepository<GeoRegion, Long>, QueryDslPredicateExecutor<GeoRegion> {
@Query("select distinct gr from GeoRegion gr where gr.isoCode = ?1") @Query("select distinct gr from GeoRegion gr where gr.isoCode = ?1")
GeoRegion findByIsoCode(String isoCode); GeoRegion findByIsoCode(String isoCode);
......
...@@ -6,29 +6,39 @@ import org.xml.sax.SAXException; ...@@ -6,29 +6,39 @@ import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException; import java.io.IOException;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Set;
public interface GeoRegionService { public interface GeoRegionService {
GeoRegion find(String regionIsoCode); GeoRegion find(String regionIsoCode);
void save(GeoRegion geoRegion); void save(GeoRegion geoRegion);
void delete(GeoRegion geoRegion); void delete(GeoRegion geoRegion);
List<GeoRegion> findAll(); List<GeoRegion> findAll();
List<GeoRegion> findAll(Locale locale); List<GeoRegion> findAll(Locale locale);
void updateGeoRegionData() throws IOException, ParserConfigurationException, SAXException; void updateGeoRegionData() throws IOException, ParserConfigurationException, SAXException;
GeoRegion getRegion(Country country); GeoRegion getRegion(Country country);
List<GeoRegion> getGeoRegionsForFilter(); List<GeoRegion> getGeoRegionsForFilter();
List<GeoRegion> conversionToSubRegionsList(GeoRegion parentGeo); List<GeoRegion> conversionToSubRegionsList(GeoRegion parentGeo);
List<GeoRegion> getChildren(String regionIsoCode); List<GeoRegion> getChildren(String regionIsoCode);
/**
* Get all ISO3 country codes for specified region codes
*
* @param regionCodes set of region codes
* @return a set of ISO3 country codes of countries in all specified regions
*/
Collection<String> countryCodesFor(Set<String> regionCodes);
} }
...@@ -15,8 +15,6 @@ ...@@ -15,8 +15,6 @@
*/ */
package org.genesys2.server.service.filter; package org.genesys2.server.service.filter;
import static org.genesys2.server.model.genesys.QAccession.accession;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
...@@ -24,6 +22,7 @@ import org.genesys.blocks.model.filters.NumberFilter; ...@@ -24,6 +22,7 @@ import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter; import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.blocks.model.filters.UuidModelFilter; import org.genesys.blocks.model.filters.UuidModelFilter;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters; import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import com.hazelcast.util.CollectionUtil; import com.hazelcast.util.CollectionUtil;
...@@ -87,12 +86,16 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession> ...@@ -87,12 +86,16 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
/** acce.lists uuid */ /** acce.lists uuid */
public Set<UUID> list; public Set<UUID> list;
public Predicate buildQuery() {
return buildQuery(QAccession.accession);
}
/** /**
* Builds the query. * Builds the query.
* *
* @return the predicate * @return the predicate
*/ */
public Predicate buildQuery() { public Predicate buildQuery(QAccession accession) {
final BooleanBuilder and = new BooleanBuilder(); final BooleanBuilder and = new BooleanBuilder();
super.buildQuery(accession, accession._super._super, and); super.buildQuery(accession, accession._super._super, and);
...@@ -174,4 +177,40 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession> ...@@ -174,4 +177,40 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
return this; return this;
} }
public InstituteFilter holder() {
if (this.holder==null) {
return this.holder = new InstituteFilter();
} else {
return this.holder;
}
}
public CountryFilter origin() {
if (this.origin == null) {
return this.origin = new CountryFilter();
}
return this.origin;
}
public AccessionGeoFilter geo() {
if (this.geo == null) {
return this.geo = new AccessionGeoFilter();
}
return this.geo;
}
public AccessionCollectFilter coll() {
if (this.coll == null) {
return this.coll = new AccessionCollectFilter();
}
return this.coll;
}
public TaxonomyFilter taxa() {
if (this.taxa == null) {
return this.taxa = new TaxonomyFilter();
}
return this.taxa;
}
} }
...@@ -118,49 +118,46 @@ public class AppliedFiltersConverter { ...@@ -118,49 +118,46 @@ public class AppliedFiltersConverter {
} }
LOG.trace("Converting {}", pretty.writeValueAsString(af)); LOG.trace("Converting {}", pretty.writeValueAsString(af));
switch (filterName) {
case "regionHoldInst": {
filterName = "institute.country.region";
break;
}
case "regionOrigin": {
filterName = "orgCty.region";
break;
}
default:
}
if (filterName.contains(".")) { if (filterName.contains(".")) {
int lastDot = filterName.lastIndexOf('.'); int firstDot = filterName.indexOf('.');
String base = filterName.substring(0, lastDot); String base = filterName.substring(0, firstDot);
String newFilterName = filterName.substring(lastDot + 1); String newFilterName = filterName.substring(firstDot + 1);
LOG.debug("Doing accession.{} for {}", base, af.getFilterName()); LOG.debug("Doing accession.{} for {}", base, af.getFilterName());
switch (base) { switch (base) {
case "institute": { case "institute": {
InstituteFilter ff = f.holder; InstituteFilter ff = f.holder();
if (ff == null) {
ff = f.holder = new InstituteFilter();
}
apply(ff, newFilterName, af); apply(ff, newFilterName, af);
break; break;
} }
case "taxonomy": { case "taxonomy": {
TaxonomyFilter ff = f.taxa; TaxonomyFilter ff = f.taxa();
if (ff == null) {
ff = f.taxa = new TaxonomyFilter();
}
apply(ff, newFilterName, af); apply(ff, newFilterName, af);
break; break;
} }
case "coll": { case "coll": {
AccessionCollectFilter ff = f.coll; AccessionCollectFilter ff = f.coll();
if (ff == null) {
ff = f.coll = new AccessionCollectFilter();
}
apply(ff, newFilterName, af); apply(ff, newFilterName, af);
break; break;
} }
case "geo": { case "geo": {
AccessionGeoFilter ff = f.geo; AccessionGeoFilter ff = f.geo();
if (ff == null) {
ff = f.geo = new AccessionGeoFilter();
}
apply(ff, newFilterName, af); apply(ff, newFilterName, af);
break; break;
} }
case "orgCty": { case "orgCty": {
CountryFilter ff = f.origin; CountryFilter ff = f.origin();
if (ff == null) {
ff = f.origin = new CountryFilter();
}
apply(ff, newFilterName, af); apply(ff, newFilterName, af);
break; break;
} }
...@@ -427,16 +424,22 @@ public class AppliedFiltersConverter { ...@@ -427,16 +424,22 @@ public class AppliedFiltersConverter {
return nf; return nf;
} }
private static void apply(CountryFilter ff, String filterName, AppliedFilter af) { private static void apply(CountryFilter countryFilter, String filterName, AppliedFilter af) {
switch (filterName) { switch (filterName) {
case "iso3": { case "iso3": {
Set<String> iso3 = ff.iso3; Set<String> iso3 = countryFilter.iso3;
if (iso3 == null) { if (iso3 == null) {
iso3 = ff.iso3 = new HashSet<>(); iso3 = countryFilter.iso3 = new HashSet<>();
} }
iso3.addAll(toStringSet(af)); iso3.addAll(toStringSet(af));
break; break;
} }
case "region": {
Set<String> codes = toStringSet(af);
LOG.debug("Got country region codes {}", codes);
countryFilter.region = codes;
break;
}
default: default:
throw new RuntimeException("Unhandled Institute filter property=" + filterName); throw new RuntimeException("Unhandled Institute filter property=" + filterName);
} }
...@@ -450,22 +453,37 @@ public class AppliedFiltersConverter { ...@@ -450,22 +453,37 @@ public class AppliedFiltersConverter {
return af.getValues().stream().map(fv -> (UUID) ((LiteralValueFilter) fv).getValue()).collect(Collectors.toSet()); return af.getValues().stream().map(fv -> (UUID) ((LiteralValueFilter) fv).getValue()).collect(Collectors.toSet());
} }
private static void apply(InstituteFilter ff, String filterName, AppliedFilter af) { private static void apply(InstituteFilter instituteFilter, String filterName, AppliedFilter af) {
switch (filterName) { switch (filterName) {
case "code": { case "code": {
Set<String> code = ff.code; Set<String> code = instituteFilter.code;
if (code == null) { if (code == null) {
code = ff.code = new HashSet<>(); code = instituteFilter.code = new HashSet<>();
} }
Set<String> codes = toStringSet(af); Set<String> codes = toStringSet(af);
LOG.debug("Got Institute codes {}", codes); LOG.debug("Got Institute codes {}", codes);
code.addAll(codes); code.addAll(codes);
break; break;
} }
case "country.iso3": {
CountryFilter countryFilter = instituteFilter.country();
Set<String> codes = toStringSet(af);
LOG.debug("Got countries codes {}", codes);
countryFilter.iso3 = new HashSet<>();
countryFilter.iso3.addAll(codes);
break;
}
case "country.region": {
Set<String> codes = toStringSet(af);
LOG.debug("Got country region codes {}", codes);
CountryFilter countryFilter = instituteFilter.country();
countryFilter.region = codes;
break;
}
case "networks": { case "networks": {
Set<String> networks = ff.networks; Set<String> networks = instituteFilter.networks;
if (networks == null) { if (networks == null) {
networks = ff.networks = new HashSet<>(); networks = instituteFilter.networks = new HashSet<>();
} }
Set<String> codes = toStringSet(af); Set<String> codes = toStringSet(af);
LOG.debug("Got Institute networks {}", codes); LOG.debug("Got Institute networks {}", codes);
...@@ -477,24 +495,4 @@ public class AppliedFiltersConverter { ...@@ -477,24 +495,4 @@ public class AppliedFiltersConverter {
} }
} }
// private static void apply(CountryFilter country, AppliedFilter af) {
// String filterName = af.getFilterName();
// switch (filterName) {
// case "code": {
// Set<String> code = country.iso3;
// if (code == null) {
// code = country.iso3 = new HashSet<>();
// }
// Set<String> codes = af.getValues().stream().map(fv -> (String)
// ((LiteralValueFilter) fv).getValue()).collect(Collectors.toSet());
// LOG.debug("Got origin iso3 codes {}", codes);
// code.addAll(codes);
// break;
// }
// default:
// throw new RuntimeException("Unhandled country filter property=" +
// filterName);
// }
// }
} }
...@@ -17,24 +17,29 @@ package org.genesys2.server.service.filter; ...@@ -17,24 +17,29 @@ package org.genesys2.server.service.filter;
import static org.genesys2.server.model.impl.QCountry.country; import static org.genesys2.server.model.impl.QCountry.country;
import java.util.Collection;
import java.util.Set; import java.util.Set;
import org.genesys.blocks.model.filters.BasicModelFilter; import org.genesys.blocks.model.filters.BasicModelFilter;
import org.genesys.blocks.util.CurrentApplicationContext;
import org.genesys2.server.model.impl.Country; import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.QCountry; import org.genesys2.server.model.impl.QCountry;
import org.genesys2.server.service.GeoRegionService;
import com.querydsl.core.BooleanBuilder; import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate; import com.querydsl.core.types.Predicate;
// TODO: Auto-generated Javadoc
/** /**
* The Class CountryFilter. * The filter for {@link Country}
*/ */
public class CountryFilter extends BasicModelFilter<CountryFilter, Country> { public class CountryFilter extends BasicModelFilter<CountryFilter, Country> {
/** The iso 3. */ /** The iso 3. */
public Set<String> iso3; public Set<String> iso3;
/** The region codes. */
public Set<String> region;
/** /**
* Builds the query. * Builds the query.
* *
...@@ -57,8 +62,16 @@ public class CountryFilter extends BasicModelFilter<CountryFilter, Country> { ...@@ -57,8 +62,16 @@ public class CountryFilter extends BasicModelFilter<CountryFilter, Country> {
if (iso3 != null && !iso3.isEmpty()) { if (iso3 != null && !iso3.isEmpty()) {
and.and(country.code3.in(iso3)); and.and(country.code3.in(iso3));
} }
if (region != null && ! region.isEmpty()) {
and.and(country.code3.in(getCountriesInRegions(region)));
}
return and; return and;
} }
private Collection<String> getCountriesInRegions(Set<String> regionCodes) {
GeoRegionService geoRegionService = CurrentApplicationContext.getContext().getBean(GeoRegionService.class);
return geoRegionService.countryCodesFor(regionCodes);
}
} }
/*
* 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.genesys2.server.service.filter;
import java.util.Set;
import org.genesys.blocks.model.filters.BasicModelFilter;
import org.genesys2.server.model.impl.GeoRegion;
import org.genesys2.server.model.impl.QGeoRegion;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
/**
* The filter for {@link GeoRegion}
*/
public class GeoRegionFilter extends BasicModelFilter<GeoRegionFilter, GeoRegion> {
/** The region code. */
public Set<String> code;
/**
* Builds the query.
*
* @return the predicate
*/
public Predicate buildQuery() {
return buildQuery(QGeoRegion.geoRegion);
}
/**
* Builds the query.
*
* @param georegion the georegion
* @return the predicate
*/