Search in sources :

Example 1 with PropertyName

use of org.opengis.filter.expression.PropertyName in project ddf by codice.

the class GeotoolsFilterAdapterImpl method visit.

public Object visit(PropertyIsLike filter, Object delegate) {
    String propertyName;
    String wildcard = filter.getWildCard();
    String singleChar = filter.getSingleChar();
    String escapeChar = filter.getEscape();
    if (filter.getExpression() == null || filter.getLiteral() == null) {
        throw new UnsupportedOperationException("Expression and Literal must not be null for PropertyIsLike.");
    }
    if (wildcard.length() > 1 || singleChar.length() > 1 || escapeChar.length() > 1) {
        throw new UnsupportedOperationException("Wildcard, single, and escape characters must be a single character for PropertyIsLike.");
    }
    if (wildcard.equals(singleChar) || wildcard.equals(escapeChar) || singleChar.equals(escapeChar)) {
        throw new UnsupportedOperationException("Wildcard, single, and escape characters must be different for PropertyIsLike.");
    }
    String pattern = normalizePattern(filter.getLiteral(), wildcard, singleChar, escapeChar);
    boolean matchCase = filter.isMatchingCase();
    boolean isFuzzy = false;
    if (filter.getExpression() instanceof FuzzyFunction) {
        FuzzyFunction fuzzy = (FuzzyFunction) filter.getExpression();
        propertyName = ((PropertyName) (fuzzy.getParameters().get(0))).getPropertyName();
        isFuzzy = true;
    } else if (filter.getExpression() instanceof PropertyName) {
        PropertyName expression = (PropertyName) filter.getExpression();
        propertyName = expression.getPropertyName();
    } else {
        throw new UnsupportedOperationException("Only support PropertyName expression for PropertyIsLike filter.");
    }
    boolean isXpathSearch = (propertyName.indexOf('/') != -1 || propertyName.indexOf('@') != -1);
    if (!isFuzzy && !isXpathSearch) {
        return ((FilterDelegate<?>) delegate).propertyIsLike(propertyName, pattern, matchCase);
    } else if (isFuzzy && !isXpathSearch) {
        // TODO check if wildcards are escaped
        return ((FilterDelegate<?>) delegate).propertyIsFuzzy(propertyName, pattern);
    } else if (!isFuzzy && isXpathSearch) {
        if (pattern.trim().isEmpty() || pattern.trim().equals(FilterDelegate.WILDCARD_CHAR)) {
            return ((FilterDelegate<?>) delegate).xpathExists(propertyName);
        } else {
            return ((FilterDelegate<?>) delegate).xpathIsLike(propertyName, pattern, matchCase);
        }
    } else if (isFuzzy && isXpathSearch) {
        // TODO check if wildcards are escaped
        return ((FilterDelegate<?>) delegate).xpathIsFuzzy(propertyName, pattern);
    } else {
        throw new UnsupportedOperationException("Unsupported operands for PropertyIsLike.");
    }
}
Also used : FuzzyFunction(ddf.catalog.impl.filter.FuzzyFunction) PropertyName(org.opengis.filter.expression.PropertyName) FilterDelegate(ddf.catalog.filter.FilterDelegate)

Example 2 with PropertyName

use of org.opengis.filter.expression.PropertyName in project ddf by codice.

the class FilterBuilderTest method andOrNull.

@Test
public void andOrNull() {
    FilterVisitor visitor = spy(new DefaultFilterVisitor() {
    });
    // FilterBuilder builder = new GeotoolsFilterBuilder();
    //
    //
    FilterBuilder builder = new GeotoolsFilterBuilder();
    Filter filter = builder.allOf(builder.anyOf(builder.attribute(FOO_ATTRIBUTE).is().empty()));
    filter.accept(visitor, null);
    InOrder inOrder = inOrder(visitor);
    inOrder.verify(visitor, times(1)).visit(isA(And.class), anyObject());
    inOrder.verify(visitor).visit(isA(Or.class), anyObject());
    ArgumentCaptor<PropertyIsNull> propertyIsNullArgument = ArgumentCaptor.forClass(PropertyIsNull.class);
    verify(visitor).visit(propertyIsNullArgument.capture(), anyObject());
    ExpressionVisitor expVisitor = spy(new DefaultExpressionVisitor() {
    });
    propertyIsNullArgument.getValue().getExpression().accept(expVisitor, null);
    ArgumentCaptor<PropertyName> propertyNameArgument = ArgumentCaptor.forClass(PropertyName.class);
    verify(expVisitor).visit(propertyNameArgument.capture(), anyObject());
    assertEquals(FOO_ATTRIBUTE, propertyNameArgument.getValue().getPropertyName());
}
Also used : PropertyName(org.opengis.filter.expression.PropertyName) InOrder(org.mockito.InOrder) Or(org.opengis.filter.Or) DefaultFilterVisitor(org.geotools.filter.visitor.DefaultFilterVisitor) DefaultExpressionVisitor(org.geotools.filter.visitor.DefaultExpressionVisitor) ExpressionVisitor(org.opengis.filter.expression.ExpressionVisitor) PropertyIsNull(org.opengis.filter.PropertyIsNull) DefaultFilterVisitor(org.geotools.filter.visitor.DefaultFilterVisitor) FilterVisitor(org.opengis.filter.FilterVisitor) Filter(org.opengis.filter.Filter) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) And(org.opengis.filter.And) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) DefaultExpressionVisitor(org.geotools.filter.visitor.DefaultExpressionVisitor) Test(org.junit.Test)

Example 3 with PropertyName

use of org.opengis.filter.expression.PropertyName in project ddf by codice.

the class FilterBuilderTest method afterDate.

@Test
public void afterDate() {
    final Date date = new Date();
    FilterVisitor visitor = spy(new DefaultFilterVisitor() {
    });
    FilterBuilder builder = new GeotoolsFilterBuilder();
    Filter filter = builder.attribute(FOO_ATTRIBUTE).is().after().date(date);
    filter.accept(visitor, null);
    ArgumentCaptor<After> expressionArgument = ArgumentCaptor.forClass(After.class);
    verify(visitor).visit(expressionArgument.capture(), anyObject());
    ExpressionVisitor expVisitor = spy(new DefaultExpressionVisitor() {
    });
    expressionArgument.getValue().getExpression1().accept(expVisitor, null);
    ArgumentCaptor<PropertyName> propertyNameArgument = ArgumentCaptor.forClass(PropertyName.class);
    verify(expVisitor).visit(propertyNameArgument.capture(), anyObject());
    assertEquals(FOO_ATTRIBUTE, propertyNameArgument.getValue().getPropertyName());
    expressionArgument.getValue().getExpression2().accept(expVisitor, null);
    ArgumentCaptor<Literal> literalArgument = ArgumentCaptor.forClass(Literal.class);
    verify(expVisitor).visit(literalArgument.capture(), anyObject());
    assertEquals(date, ((Instant) literalArgument.getValue().getValue()).getPosition().getDate());
}
Also used : PropertyName(org.opengis.filter.expression.PropertyName) DefaultFilterVisitor(org.geotools.filter.visitor.DefaultFilterVisitor) Instant(org.opengis.temporal.Instant) DefaultExpressionVisitor(org.geotools.filter.visitor.DefaultExpressionVisitor) ExpressionVisitor(org.opengis.filter.expression.ExpressionVisitor) Date(java.util.Date) DefaultFilterVisitor(org.geotools.filter.visitor.DefaultFilterVisitor) FilterVisitor(org.opengis.filter.FilterVisitor) Filter(org.opengis.filter.Filter) FilterBuilder(ddf.catalog.filter.FilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) GeotoolsFilterBuilder(ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder) Literal(org.opengis.filter.expression.Literal) After(org.opengis.filter.temporal.After) DefaultExpressionVisitor(org.geotools.filter.visitor.DefaultExpressionVisitor) Test(org.junit.Test)

Example 4 with PropertyName

use of org.opengis.filter.expression.PropertyName in project ddf by codice.

the class QueryRunnable method getResultComparator.

protected Comparator<Result> getResultComparator(Query query) {
    Comparator<Result> sortComparator = new RelevanceResultComparator(SortOrder.DESCENDING);
    SortBy sortBy = query.getSortBy();
    if (sortBy != null && sortBy.getPropertyName() != null) {
        PropertyName sortingProp = sortBy.getPropertyName();
        String sortType = sortingProp.getPropertyName();
        SortOrder sortOrder = (sortBy.getSortOrder() == null) ? SortOrder.DESCENDING : sortBy.getSortOrder();
        // Temporal searches are currently sorted by the effective time
        if (Metacard.EFFECTIVE.equals(sortType) || Result.TEMPORAL.equals(sortType)) {
            sortComparator = new TemporalResultComparator(sortOrder);
        } else if (Metacard.CREATED.equals(sortType) || Metacard.MODIFIED.equals(sortType)) {
            sortComparator = new TemporalResultComparator(sortOrder, sortType);
        } else if (Result.DISTANCE.equals(sortType)) {
            sortComparator = new DistanceResultComparator(sortOrder);
        } else if (Result.RELEVANCE.equals(sortType)) {
            sortComparator = new RelevanceResultComparator(sortOrder);
        }
    }
    return sortComparator;
}
Also used : TemporalResultComparator(ddf.catalog.util.impl.TemporalResultComparator) PropertyName(org.opengis.filter.expression.PropertyName) SortBy(org.opengis.filter.sort.SortBy) SortOrder(org.opengis.filter.sort.SortOrder) RelevanceResultComparator(ddf.catalog.util.impl.RelevanceResultComparator) DistanceResultComparator(ddf.catalog.util.impl.DistanceResultComparator) Result(ddf.catalog.data.Result)

Example 5 with PropertyName

use of org.opengis.filter.expression.PropertyName in project ddf by codice.

the class TestCswRecordMapperFilterVisitor method testVisitWithMappedName.

@Test
public void testVisitWithMappedName() {
    AttributeExpressionImpl propName = new AttributeExpressionImpl(new NameImpl(new QName(CswConstants.DUBLIN_CORE_SCHEMA, CswConstants.CSW_ALTERNATIVE, CswConstants.DUBLIN_CORE_NAMESPACE_PREFIX)));
    CswRecordMapperFilterVisitor visitor = new CswRecordMapperFilterVisitor(metacardType, mockMetacardTypeList);
    PropertyName propertyName = (PropertyName) visitor.visit(propName, null);
    assertThat(propertyName.getPropertyName(), is(Core.TITLE));
    assertThat(propertyName.getPropertyName(), not(is(CswConstants.CSW_ALTERNATIVE)));
}
Also used : NameImpl(org.geotools.feature.NameImpl) PropertyName(org.opengis.filter.expression.PropertyName) AttributeExpressionImpl(org.geotools.filter.AttributeExpressionImpl) QName(javax.xml.namespace.QName) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Aggregations

PropertyName (org.opengis.filter.expression.PropertyName)14 Test (org.junit.Test)5 SortBy (org.opengis.filter.sort.SortBy)5 AttributeExpressionImpl (org.geotools.filter.AttributeExpressionImpl)4 Result (ddf.catalog.data.Result)3 ArrayList (java.util.ArrayList)3 CswQueryFactoryTest (org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest)3 NameImpl (org.geotools.feature.NameImpl)3 Filter (org.opengis.filter.Filter)3 FilterBuilder (ddf.catalog.filter.FilterBuilder)2 PropertyNameImpl (ddf.catalog.filter.impl.PropertyNameImpl)2 GeotoolsFilterBuilder (ddf.catalog.filter.proxy.builder.GeotoolsFilterBuilder)2 FuzzyFunction (ddf.catalog.impl.filter.FuzzyFunction)2 QueryRequest (ddf.catalog.operation.QueryRequest)2 QueryResponse (ddf.catalog.operation.QueryResponse)2 QueryImpl (ddf.catalog.operation.impl.QueryImpl)2 DistanceResultComparator (ddf.catalog.util.impl.DistanceResultComparator)2 RelevanceResultComparator (ddf.catalog.util.impl.RelevanceResultComparator)2 TemporalResultComparator (ddf.catalog.util.impl.TemporalResultComparator)2 Serializable (java.io.Serializable)2