]> git.argeo.org Git - gpl/argeo-suite.git/blob - geo/CqlUtils.java
Prepare next development cycle
[gpl/argeo-suite.git] / geo / CqlUtils.java
1 package org.argeo.app.geo;
2
3 import org.argeo.api.acr.NamespaceUtils;
4 import org.argeo.api.acr.search.AndFilter;
5 import org.argeo.api.acr.search.BasicSearch;
6 import org.argeo.api.acr.search.ContentFilter;
7 import org.geotools.api.filter.And;
8 import org.geotools.api.filter.Filter;
9 import org.geotools.api.filter.PropertyIsEqualTo;
10 import org.geotools.api.filter.expression.Literal;
11 import org.geotools.api.filter.expression.PropertyName;
12 import org.geotools.filter.text.cql2.CQL;
13 import org.geotools.filter.text.cql2.CQLException;
14
15 /** Utilities around the CQL query format. */
16 public class CqlUtils {
17
18 public static void filter(BasicSearch search, String cql) {
19 try {
20 filter(search, CQL.toFilter(cql));
21 } catch (CQLException e) {
22 throw new IllegalArgumentException("Cannot parse CQL: " + cql, e);
23 }
24 }
25
26 public static void filter(BasicSearch search, Filter filter) {
27 search.where((where) -> {
28 if (filter instanceof And and) {
29 processAnd(where, and);
30 } else if (filter instanceof PropertyIsEqualTo propertyIsEqualTo) {
31 processIsEqualTo(where, propertyIsEqualTo);
32 } else {
33 throw new IllegalArgumentException("Unsupported filter " + filter.getClass());
34 }
35 });
36 }
37
38 private static void processAnd(AndFilter contentFilter, And filter) {
39 for (Filter child : filter.getChildren()) {
40 if (child instanceof PropertyIsEqualTo propertyIsEqualTo) {
41 processIsEqualTo(contentFilter, propertyIsEqualTo);
42 }
43 }
44
45 }
46
47 private static void processIsEqualTo(ContentFilter<?> contentFilter, PropertyIsEqualTo propertyIsEqualTo) {
48 // TODO properly deal with types etc.
49 // see GeoTools org.geotools.filter.text.commons.ExpressionToText
50 PropertyName propertyName = (PropertyName) propertyIsEqualTo.getExpression1();
51 Literal value = (Literal) propertyIsEqualTo.getExpression2();
52 // String escaped = literal.toString().replaceAll("'", "''");
53 contentFilter.eq(NamespaceUtils.parsePrefixedName(propertyName.toString()), value.toString());
54 }
55
56 /** singleton */
57 private CqlUtils() {
58 }
59 }