X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.app.geo%2Fsrc%2Forg%2Fargeo%2Fapp%2Fgeo%2FCqlUtils.java;fp=org.argeo.app.geo%2Fsrc%2Forg%2Fargeo%2Fapp%2Fgeo%2FCqlUtils.java;h=8d6c138f4591ebed9156f9559dce5254cb8a230c;hb=2c1a7dd0af92287d3239a21dcec90de7313842a9;hp=0000000000000000000000000000000000000000;hpb=9728ee7133bc787a1796670b7622d0cc4c62b5e0;p=gpl%2Fargeo-suite.git diff --git a/org.argeo.app.geo/src/org/argeo/app/geo/CqlUtils.java b/org.argeo.app.geo/src/org/argeo/app/geo/CqlUtils.java new file mode 100644 index 0000000..8d6c138 --- /dev/null +++ b/org.argeo.app.geo/src/org/argeo/app/geo/CqlUtils.java @@ -0,0 +1,60 @@ +package org.argeo.app.geo; + +import org.argeo.api.acr.NamespaceUtils; +import org.argeo.api.acr.search.AndFilter; +import org.argeo.api.acr.search.BasicSearch; +import org.argeo.api.acr.search.ContentFilter; +import org.geotools.filter.text.cql2.CQL; +import org.geotools.filter.text.cql2.CQLException; +import org.opengis.filter.And; +import org.opengis.filter.Filter; +import org.opengis.filter.PropertyIsEqualTo; +import org.opengis.filter.expression.Literal; +import org.opengis.filter.expression.PropertyName; + +public class CqlUtils { + + public final static String CQL_FILTER = "cql_filter"; + + public static void filter(BasicSearch search, String cql) { + try { + filter(search, CQL.toFilter(cql)); + } catch (CQLException e) { + throw new IllegalArgumentException("Cannot parse CQL: " + cql, e); + } + } + + public static void filter(BasicSearch search, Filter filter) { + search.where((where) -> { + if (filter instanceof And and) { + processAnd(where, and); + } else if (filter instanceof PropertyIsEqualTo propertyIsEqualTo) { + processIsEqualTo(where, propertyIsEqualTo); + } else { + throw new IllegalArgumentException("Unsupported filter " + filter.getClass()); + } + }); + } + + private static void processAnd(AndFilter contentFilter, And filter) { + for (Filter child : filter.getChildren()) { + if (child instanceof PropertyIsEqualTo propertyIsEqualTo) { + processIsEqualTo(contentFilter, propertyIsEqualTo); + } + } + + } + + private static void processIsEqualTo(ContentFilter contentFilter, PropertyIsEqualTo propertyIsEqualTo) { + // TODO properly deal with types etc. + // see GeoTools org.geotools.filter.text.commons.ExpressionToText + PropertyName propertyName = (PropertyName) propertyIsEqualTo.getExpression1(); + Literal value = (Literal) propertyIsEqualTo.getExpression2(); + // String escaped = literal.toString().replaceAll("'", "''"); + contentFilter.eq(NamespaceUtils.parsePrefixedName(propertyName.toString()), value.toString()); + } + + /** singleton */ + private CqlUtils() { + } +}