Skip to content

Commit

Permalink
CSW / Fix parsing date values for filters. Fixes #8034 (#8417)
Browse files Browse the repository at this point in the history
Co-authored-by: Jose García <josegar74@gmail.com>
  • Loading branch information
geonetworkbuild and josegar74 authored Oct 14, 2024
1 parent 57185ba commit e9f159a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.commons.text.StringEscapeUtils;
import org.fao.geonet.constants.Geonet;
import org.fao.geonet.kernel.csw.services.getrecords.IFieldMapper;
import org.fao.geonet.utils.DateUtil;
import org.fao.geonet.utils.Log;
import org.geotools.filter.visitor.AbstractFilterVisitor;
import org.locationtech.jts.geom.*;
Expand Down Expand Up @@ -338,7 +339,11 @@ public Object visitRange(BinaryComparisonOperator filter, String operator, Objec
String dataPropertyValue = stack.pop();
String dataPropertyName = stack.pop();

if (!NumberUtils.isNumber(dataPropertyValue)) {
boolean isDate = (DateUtil.parseBasicOrFullDateTime(dataPropertyValue) != null);

if (isDate) {
dataPropertyValue = CswFilter2Es.quoteString(dataPropertyValue);
} else if (!NumberUtils.isNumber(dataPropertyValue)) {
dataPropertyValue = StringEscapeUtils.escapeJson(CswFilter2Es.quoteString(dataPropertyValue));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,27 @@ void assertFilterEquals(JsonNode expected, String actual, String filterSpecVersi

assertEquals(expected, MAPPER.readTree(new StringReader(result)));
}


@Test
void testPropertyIsGreaterThanDateValue() throws IOException {

// INPUT:
final String input =
" <ogc:Filter xmlns:ogc=\"http://www.opengis.net/ogc\">\n" +
" <ogc:PropertyIsGreaterThan>\n" +
" <ogc:PropertyName>Modified</ogc:PropertyName>\n" +
" <ogc:Literal>1910-02-05</ogc:Literal>\n" +
" </ogc:PropertyIsGreaterThan>\n" +
" </ogc:Filter>";

// EXPECTED:
final ObjectNode expected = EsJsonHelper.boolbdr(). //
must(array(range("Modified", "gt", "1910-02-05"))). //
filter(queryStringPart()). //
bld();


assertFilterEquals(expected, input);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2001-2023 Food and Agriculture Organization of the
* Copyright (C) 2001-2024 Food and Agriculture Organization of the
* United Nations (FAO-UN), United Nations World Food Programme (WFP)
* and United Nations Environment Programme (UNEP)
*
Expand Down Expand Up @@ -62,6 +62,35 @@ public static ObjectNode match(String property, String matchString) {
return outer;
}


/**
* Returns a structure like
*
* <pre>
* { "range":
* {
* "gt": "value"
* }
* </pre>
*
* @param property
* @param operator
* @param matchString
* @return
*/
public static ObjectNode range(String property, String operator, String matchString) {
final ObjectNode rangeOperatorObject = MAPPER.createObjectNode();
rangeOperatorObject.put(operator, matchString);

final ObjectNode rangeObject = MAPPER.createObjectNode();
rangeObject.put(property, rangeOperatorObject);

final ObjectNode outer = MAPPER.createObjectNode();
outer.set("range", rangeObject);
return outer;
}


private static ArrayNode bound(double x, double y) {
final ArrayNode bound = MAPPER.createArrayNode();
bound.add(x);
Expand Down

0 comments on commit e9f159a

Please sign in to comment.