Commit cd615bd3 authored by Maxym Borodenko's avatar Maxym Borodenko

Filter accessions by GeoRegion (changes for feedback)

parent cb7d2630
......@@ -41,4 +41,5 @@ public interface GenesysFilterService {
void listGeoTile(boolean distinct, AppliedFilters filters, Integer limit, int zoom, int xtile, int ytile, RowCallbackHandler rowHandler);
AppliedFilters transformFiltersIfNeed(AppliedFilters appliedFilters);
}
......@@ -25,6 +25,10 @@ public interface GeoRegionService {
GeoRegion getRegion(Country country);
List<GeoRegion> getGeoRegionsForFilter();
List<GeoRegion> conversionToSubRegionsList(GeoRegion parentGeo);
List<GeoRegion> getChildren(String regionIsoCode);
}
......@@ -99,6 +99,9 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ
@Autowired
private FilterHandler filterHandler;
@Autowired
private GenesysFilterService filterService;
private final Map<String, Class<?>> clazzMap;
@Autowired
......@@ -160,6 +163,7 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ
@Override
public String getBoundingBox(final AppliedFilters filters) {
filterService.transformFiltersIfNeed(filters);
AndFilterBuilder filterBuilder = getFilterBuilder(filters);
final FilterAggregationBuilder builder = AggregationBuilders
......@@ -223,6 +227,8 @@ public class ElasticsearchSearchServiceImpl implements ElasticService, Initializ
*/
@Override
public TermResult termStatisticsAuto(AppliedFilters appliedFilters, String term, int size) throws SearchException {
filterService.transformFiltersIfNeed(appliedFilters);
TermResult termResult = null;
int newSize = size;
do {
......
......@@ -137,6 +137,17 @@ public class FilterHandler {
return null;
}
public List<GenesysFilter> getAdditionalFilters(String[] selectedFilters) {
List<String> gmFilterList = new ArrayList<>();
for (String filter : selectedFilters) {
if (filter.startsWith("gm:")) {
gmFilterList.add(filter);
}
}
String[] gmFilters = gmFilterList.toArray(new String[gmFilterList.size()]);
return selectFilters(gmFilters);
}
public List<GenesysFilter> selectFilters(String[] selectedFilters) {
LOG.debug("Loading filter definitions sel=" + ArrayUtils.toString(selectedFilters));
final List<GenesysFilter> filters = new ArrayList<GenesysFilter>();
......
......@@ -20,6 +20,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
......@@ -33,6 +34,7 @@ import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.impl.GeoRegion;
import org.genesys2.server.persistence.domain.AccessionRepository;
import org.genesys2.server.persistence.domain.MethodRepository;
import org.genesys2.server.service.CropService;
......@@ -40,6 +42,7 @@ import org.genesys2.server.service.ElasticService;
import org.genesys2.server.service.FilterConstants;
import org.genesys2.server.service.GenesysFilterService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoRegionService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService;
......@@ -99,6 +102,9 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
@Autowired
private ElasticService elasticService;
@Autowired
private GeoRegionService geoRegionService;
@Autowired
public void setDataSource(final DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
......@@ -157,6 +163,8 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
}
}
transformFiltersIfNeed(filters);
if (pageable.getPageNumber() > PAGINATION_MAXPAGE_LIMIT) {
throw new RuntimeException("Genesys does not support requests for data beyond " + PAGINATION_MAXPAGE_LIMIT + "th page.");
}
......@@ -197,6 +205,7 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
LOG.debug("Autocomplete " + filter + " ac=" + ac);
}
transformFiltersIfNeed(filters);
Crop crop = null;
{
......@@ -415,4 +424,132 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
}, rowHandler);
}
@Override
public AppliedFilters transformFiltersIfNeed(final AppliedFilters appliedFilters) {
AppliedFilter regionOriginFilter = appliedFilters.stream()
.filter(filter -> filter.getFilterName().equals("regionOrigin")).findFirst().orElse(null);
AppliedFilter regionHoldInstFilter = appliedFilters.stream()
.filter(filter -> filter.getFilterName().equals("regionHoldInst")).findFirst().orElse(null);
AppliedFilter countryOriginFilter = appliedFilters.stream()
.filter(filter -> filter.getFilterName().equals("orgCty.iso3")).findFirst().orElse(null);
AppliedFilter countryHoldInstFilter = appliedFilters.stream()
.filter(filter -> filter.getFilterName().equals("institute.country.iso3")).findFirst().orElse(null);
List<String> regionHoldInstValues = new ArrayList<>();
if (regionOriginFilter !=null && countryOriginFilter!=null && !countryOriginFilter.getValues().isEmpty()) {
regionHoldInstValues.addAll(getFilterValues(countryOriginFilter));
}
List<String> instituteCountryValues = new ArrayList<>();
if (regionHoldInstFilter !=null && countryHoldInstFilter!=null && !countryHoldInstFilter.getValues().isEmpty()) {
instituteCountryValues.addAll(getFilterValues(countryHoldInstFilter));
}
conversionFiltersFromGeoRegions(appliedFilters);
transformFilterListIfExistsRegionAndCountryInFilters(appliedFilters, regionOriginFilter, countryOriginFilter, regionHoldInstValues);
transformFilterListIfExistsRegionAndCountryInFilters(appliedFilters, regionHoldInstFilter, countryHoldInstFilter, instituteCountryValues);
return appliedFilters;
}
private void transformFilterListIfExistsRegionAndCountryInFilters(final AppliedFilters appliedFilters,
final AppliedFilter regionFilter,
final AppliedFilter countryFilter,
final List<String> regionValues) {
if (regionFilter != null && countryFilter != null && !regionValues.isEmpty()) {
AppliedFilter newCountryFilter = new AppliedFilter();
newCountryFilter.setFilterName(countryFilter.getFilterName());
for (String value : regionValues) {
newCountryFilter.addFilterValue(new FilterHandler.LiteralValueFilter(value));
}
appliedFilters.removeIf(appliedFilter1 -> appliedFilter1.getFilterName().equals(countryFilter.getFilterName()));
appliedFilters.add(newCountryFilter);
}
}
private AppliedFilters conversionFiltersFromGeoRegions(final AppliedFilters appliedFilters) {
AppliedFilter regionOriginFilter = appliedFilters.stream()
.filter(filter -> filter.getFilterName().equals("regionOrigin")).findFirst().orElse(null);
AppliedFilter regionHoldInstFilter = appliedFilters.stream()
.filter(filter -> filter.getFilterName().equals("regionHoldInst")).findFirst().orElse(null);
if (regionOriginFilter != null && !regionOriginFilter.getValues().isEmpty()) {
conversionGeoRegionsFilterToAnotherFilter(appliedFilters, regionOriginFilter, "orgCty.iso3");
}
if (regionHoldInstFilter != null && !regionHoldInstFilter.getValues().isEmpty()) {
conversionGeoRegionsFilterToAnotherFilter(appliedFilters, regionHoldInstFilter, "institute.country.iso3");
}
return appliedFilters;
}
private AppliedFilters conversionGeoRegionsFilterToAnotherFilter(final AppliedFilters appliedFilters,
final AppliedFilter oldFilter,
final String anotherFilterName) {
List<GeoRegion> geoRegionList = new ArrayList<>();
List<Country> countryList = new ArrayList<>();
Set<String> countryIsoList = new HashSet<>();
for (String isoCode: getFilterValues(oldFilter)) {
GeoRegion geoRegion = geoRegionService.find(isoCode);
if (geoRegion != null) {
if (geoRegion.getParentRegion().getName().equals("Americas") ||
geoRegion.getParentRegion().getName().equals("World")) {
geoRegionList.addAll(geoRegionService.conversionToSubRegionsList(geoRegion));
} else {
geoRegionList.add(geoRegionService.find(isoCode));
}
}
}
if (!geoRegionList.isEmpty()) {
for (GeoRegion geoRegion: geoRegionList) {
countryList.addAll(geoRegion.getCountries());
}
}
if (!countryList.isEmpty()) {
for (Country country: countryList) {
countryIsoList.add(country.getCode3());
}
}
if (!countryIsoList.isEmpty()) {
AppliedFilter orgCtyFilter = appliedFilters.stream()
.filter(filter -> filter.getFilterName().equals(anotherFilterName))
.findFirst().orElse(new AppliedFilter().setFilterName(anotherFilterName));
if (!orgCtyFilter.getValues().isEmpty())
countryIsoList.addAll(getFilterValues(orgCtyFilter));
for (String isoCode: countryIsoList) {
orgCtyFilter.addFilterValue(new FilterHandler.LiteralValueFilter(isoCode));
}
appliedFilters.removeIf(appliedFilter1 -> appliedFilter1.getFilterName().equals(oldFilter.getFilterName()));
if (!appliedFilters.hasFilter(anotherFilterName))
appliedFilters.add(orgCtyFilter);
}
return appliedFilters;
}
private Set<String> getFilterValues(final AppliedFilter appliedFilter) {
Set<String> literals = new HashSet<>();
for (FilterHandler.FilterValue filterValue : appliedFilter.getValues()) {
if (filterValue instanceof FilterHandler.LiteralValueFilter) {
FilterHandler.LiteralValueFilter literal = (FilterHandler.LiteralValueFilter) filterValue;
literals.add(literal.getValue().toString());
}
}
return literals;
}
}
......@@ -20,6 +20,7 @@ import org.genesys2.server.service.worker.GeoRegionDataCLDR;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xml.sax.SAXException;
......@@ -140,6 +141,40 @@ public class GeoRegionServiceImpl implements GeoRegionService {
}
}
@Override
public List<GeoRegion> getGeoRegionsForFilter() {
List<GeoRegion> geoRegionList = findAll();
geoRegionList.removeIf(geoRegion -> geoRegion.getName().equals("World") || geoRegion.getName().equals("Americas"));
List<GeoRegion> resultList = new ArrayList<>();
for (GeoRegion geoRegion: geoRegionList) {
if (geoRegion.getParentRegion().getName().equals("World") || geoRegion.getParentRegion().getName().equals("Americas")) {
geoRegion.setParentRegion(null);
resultList.add(geoRegion);
}
else resultList.add(geoRegion);
}
Locale locale = LocaleContextHolder.getLocale();
resultList.sort(Comparator.comparing(o -> o.getName(locale)));
return resultList;
}
@Override
public List<GeoRegion> conversionToSubRegionsList(final GeoRegion parentGeo) {
List<GeoRegion> geoRegionList = findAll();
List<GeoRegion> subRegionsList = new ArrayList<>();
geoRegionList.removeIf(geoRegion -> geoRegion.getName().equals("World"));
subRegionsList.addAll(geoRegionList.stream().
filter(geoRegion -> parentGeo.getName().equals(geoRegion.getParentRegion().getName()))
.collect(Collectors.toList()));
return subRegionsList;
}
private boolean isChild(GeoRegion region, String parentIsoCode) {
return region.getParentRegion() != null && (region.getParentRegion().getIsoCode().equals(parentIsoCode) || isChild(region.getParentRegion(), parentIsoCode));
......
......@@ -154,6 +154,7 @@ public class MappingServiceImpl implements MappingService {
@Override
@Cacheable(value = "tileserver", key = "'tile-' + #zoom + '-' + #xtile + '-' + #ytile + '-' + #filters")
public byte[] getTile(AppliedFilters filters, final int zoom, final int xtile, final int ytile) {
filterService.transformFiltersIfNeed(filters);
final BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
// final int pixelSize = (int) Math.round(1.0 + (1 << (zoom / 2))) / 2;
......
......@@ -77,51 +77,38 @@ var BrowseUtil = {
},
applySuggestions: function (jsonData, messages) {
$.ajax({
url: '/explore/transformedFilters',
method: 'get',
data: {
filter: JSON.stringify(jsonData)
},
success: function (response) {
BrowseUtil.refreshSuggestions(response, messages);
}
});
},
refreshSuggestions: function (jsonData, messages) {
$.ajax({
url: '/explore/listFilterSuggestions',
method: 'get',
data: {
filter: JSON.stringify(jsonData)
},
success: function (response) {
BrowseUtil.renderListFilterSuggestions(response, messages);
}
});
$.ajax({
url: '/explore/booleanSuggestions',
method: 'get',
data: {
filter: JSON.stringify(jsonData)
},
success: function (response) {
BrowseUtil.renderBooleanSuggestions(response);
}
});
$.ajax({
url: '/explore/cropSuggestions',
method: 'get',
data: {
filter: JSON.stringify(jsonData)
},
success: function (response) {
BrowseUtil.renderCropSuggestions(response);
}
});
$.ajax({
url: '/explore/listFilterSuggestions',
method: 'get',
data: {
filter: JSON.stringify(jsonData)
},
success: function (response) {
BrowseUtil.renderListFilterSuggestions(response, messages);
}
});
$.ajax({
url: '/explore/booleanSuggestions',
method: 'get',
data: {
filter: JSON.stringify(jsonData)
},
success: function (response) {
BrowseUtil.renderBooleanSuggestions(response);
}
});
$.ajax({
url: '/explore/cropSuggestions',
method: 'get',
data: {
filter: JSON.stringify(jsonData)
},
success: function (response) {
BrowseUtil.renderCropSuggestions(response);
}
});
},
enableFilter: function (btn, jsonData) {
......
......@@ -61,6 +61,7 @@
<c:set var="index" value="${fn:indexOf(string, '\"' )}"/>
<c:set var="string" value="${fn:substring(string, 0, index)}" />
<optgroup label="${string}">
<option value="${geoRegion.isoCode}">${string}</option>
<c:forEach items="${geoRegions}" var="subRegion">
<c:remove var="string"/>
<c:if test="${subRegion.parentRegion.name eq geoRegion.name}">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment