Search in sources :

Example 1 with FuzzyFunction

use of ddf.catalog.impl.filter.FuzzyFunction 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 FuzzyFunction

use of ddf.catalog.impl.filter.FuzzyFunction in project ddf by codice.

the class GeotoolsBuilder method build.

//
// /**
// * @param expression the expression to set
// */
// void setExpression(Expression expression) {
// this.expression = expression;
// }
protected Filter build() {
    LOGGER.debug("BUILDING attribute = {}, operator = {}, value = {}, secondaryValue = {}", attribute, operator, value, secondaryValue);
    Filter filter = null;
    String wkt = null;
    Date date = null;
    double distance = 0;
    switch(operator) {
        case AFTER:
            date = getValue(Date.class);
            if (date != null) {
                filter = factory.after(factory.property(attribute), factory.literal(new DefaultInstant(new DefaultPosition(date))));
            }
            break;
        case BEFORE:
            date = getValue(Date.class);
            if (date != null) {
                filter = factory.before(factory.property(attribute), factory.literal(new DefaultInstant(new DefaultPosition(date))));
            }
            break;
        case BETWEEN:
            filter = factory.between(factory.property(attribute), factory.literal(value), factory.literal(secondaryValue));
            break;
        case DURING:
            Date start = getValue(Date.class);
            Date end = getSecondaryValue(Date.class);
            if (start != null && end != null) {
                DefaultPosition defaultPosition = new DefaultPosition(start);
                Instant startInstant = new DefaultInstant(defaultPosition);
                Instant endInstant = new DefaultInstant(new DefaultPosition(end));
                Period period = new DefaultPeriod(startInstant, endInstant);
                filter = factory.during(factory.property(attribute), factory.literal(period));
            }
            break;
        case DURING_RELATIVE:
            Long longValue = getValue(Long.class);
            if (null != value) {
                filter = factory.during(factory.property(attribute), factory.literal(new DefaultPeriodDuration(longValue)));
            }
            break;
        case EQ:
            filter = factory.equals(factory.property(attribute), factory.literal(value));
            break;
        case GT:
            filter = factory.greater(factory.property(attribute), factory.literal(value));
            break;
        case GTE:
            filter = factory.greaterOrEqual(factory.property(attribute), factory.literal(value));
            break;
        case LT:
            filter = factory.less(factory.property(attribute), factory.literal(value));
            break;
        case LTE:
            filter = factory.lessOrEqual(factory.property(attribute), factory.literal(value));
            break;
        case NEQ:
            filter = factory.notEqual(factory.property(attribute), factory.literal(value));
            break;
        case NULL:
            filter = factory.isNull(factory.property(attribute));
            break;
        case TOVERLAPS:
            filter = factory.toverlaps(factory.property(attribute), factory.literal(value));
            break;
        case BEYOND:
            wkt = getValue(String.class);
            distance = getSecondaryValue(Double.class);
            if (wkt != null && wkt.length() > 0) {
                filter = factory.beyond(attribute, toGeometry(wkt), distance, METERS);
            }
            break;
        case CONTAINS:
            wkt = getValue(String.class);
            if (wkt != null && wkt.length() > 0) {
                filter = factory.contains(attribute, toGeometry(wkt));
            }
            break;
        case DWITHIN:
            wkt = getValue(String.class);
            distance = getSecondaryValue(Double.class);
            if (wkt != null && wkt.length() > 0) {
                filter = factory.dwithin(attribute, toGeometry(wkt), distance, METERS);
            }
            break;
        case INTERSECTS:
            wkt = getValue(String.class);
            if (wkt != null && wkt.length() > 0) {
                filter = factory.intersects(attribute, toGeometry(wkt));
            }
            break;
        case WITHIN:
            wkt = getValue(String.class);
            if (wkt != null && wkt.length() > 0) {
                filter = factory.within(attribute, toGeometry(wkt));
            }
            break;
        case LIKE:
            filter = factory.like(factory.property(attribute), getValue(String.class), "*", "%", "'", getSecondaryValue(Boolean.class));
            break;
        case FUZZY:
            Expression expression = factory.property(attribute);
            filter = factory.like(new FuzzyFunction(Arrays.asList(expression), factory.literal(Metacard.ANY_TEXT)), getValue(String.class), "*", "%", "'", getSecondaryValue(Boolean.class));
            break;
        default:
    }
    if (filter == null) {
        throw new IllegalArgumentException("Illegal argument for operation [" + operator.name() + "]");
    }
    return filter;
}
Also used : FuzzyFunction(ddf.catalog.impl.filter.FuzzyFunction) DefaultInstant(org.geotools.temporal.object.DefaultInstant) Instant(org.opengis.temporal.Instant) DefaultInstant(org.geotools.temporal.object.DefaultInstant) Period(org.opengis.temporal.Period) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) Date(java.util.Date) DefaultPeriodDuration(org.geotools.temporal.object.DefaultPeriodDuration) Filter(org.opengis.filter.Filter) DefaultPeriod(org.geotools.temporal.object.DefaultPeriod) Expression(org.opengis.filter.expression.Expression) DefaultPosition(org.geotools.temporal.object.DefaultPosition)

Example 3 with FuzzyFunction

use of ddf.catalog.impl.filter.FuzzyFunction in project ddf by codice.

the class CswRecordMapperFilterVisitor method visit.

@Override
public Object visit(Function function, Object extraData) {
    if (function instanceof FuzzyFunction) {
        //FuzzyFunction has 1 parameter to visit
        Expression expr1 = visit(function.getParameters().get(0), null);
        ((FuzzyFunction) function).setParameters(Arrays.asList(expr1));
        return function;
    } else {
        return super.visit(function, extraData);
    }
}
Also used : FuzzyFunction(ddf.catalog.impl.filter.FuzzyFunction) Expression(org.opengis.filter.expression.Expression)

Example 4 with FuzzyFunction

use of ddf.catalog.impl.filter.FuzzyFunction in project ddf by codice.

the class SubscriptionFilterVisitor method visit.

/**
     * PropertyIsLike filter maps to a Contextual search criteria.
     */
@Override
public Object visit(PropertyIsLike filter, Object data) {
    LOGGER.debug("ENTERING: PropertyIsLike filter");
    String wildcard = filter.getWildCard();
    String escape = filter.getEscape();
    String single = filter.getSingleChar();
    boolean isFuzzy = false;
    List<String> textPathList = null;
    LikeFilterImpl likeFilter = (LikeFilterImpl) filter;
    Expression expression = likeFilter.getExpression();
    // ContentTypePredicate
    if (expression instanceof PropertyName) {
        PropertyName propertyName = (PropertyName) expression;
        if (Metacard.CONTENT_TYPE.equals(propertyName.getPropertyName())) {
            LOGGER.debug("Expression is ContentType.");
            String typeValue = likeFilter.getLiteral();
            ContentTypePredicate predicate = new ContentTypePredicate(typeValue, null);
            return predicate;
        } else if (Metacard.CONTENT_TYPE_VERSION.equals(propertyName.getPropertyName())) {
            LOGGER.debug("Expression is ContentTypeVersion.");
            String versionValue = likeFilter.getLiteral();
            ContentTypePredicate predicate = new ContentTypePredicate(null, versionValue);
            return predicate;
        }
    }
    if (expression instanceof AttributeExpressionImpl) {
        AttributeExpressionImpl textPathExpression = (AttributeExpressionImpl) expression;
        textPathList = extractXpathSelectors(textPathExpression);
    } else if (expression instanceof FuzzyFunction) {
        FuzzyFunction fuzzyFunction = (FuzzyFunction) expression;
        LOGGER.debug("fuzzy search");
        isFuzzy = true;
        List<Expression> expressions = fuzzyFunction.getParameters();
        AttributeExpressionImpl firstExpression = (AttributeExpressionImpl) expressions.get(0);
        if (!Metacard.ANY_TEXT.equals(firstExpression.getPropertyName())) {
            LOGGER.debug("fuzzy search has a text path section");
            textPathList = extractXpathSelectors(firstExpression);
        }
    }
    String searchPhrase = likeFilter.getLiteral();
    LOGGER.debug("raw searchPhrase = [{}]", searchPhrase);
    String sterilizedSearchPhrase = sterilize(searchPhrase, wildcard, escape, single);
    LOGGER.debug("sterilizedSearchPhrase = [{}]", sterilizedSearchPhrase);
    ContextualPredicate contextPred = new ContextualPredicate(sterilizedSearchPhrase, isFuzzy, likeFilter.isMatchingCase(), textPathList);
    LOGGER.debug("EXITING: PropertyIsLike filter");
    return contextPred;
}
Also used : PropertyName(org.opengis.filter.expression.PropertyName) FuzzyFunction(ddf.catalog.impl.filter.FuzzyFunction) LikeFilterImpl(org.geotools.filter.LikeFilterImpl) Expression(org.opengis.filter.expression.Expression) AttributeExpressionImpl(org.geotools.filter.AttributeExpressionImpl) ContextualPredicate(ddf.catalog.pubsub.predicate.ContextualPredicate) ContentTypePredicate(ddf.catalog.pubsub.predicate.ContentTypePredicate) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

FuzzyFunction (ddf.catalog.impl.filter.FuzzyFunction)4 Expression (org.opengis.filter.expression.Expression)3 PropertyName (org.opengis.filter.expression.PropertyName)2 FilterDelegate (ddf.catalog.filter.FilterDelegate)1 ContentTypePredicate (ddf.catalog.pubsub.predicate.ContentTypePredicate)1 ContextualPredicate (ddf.catalog.pubsub.predicate.ContextualPredicate)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 AttributeExpressionImpl (org.geotools.filter.AttributeExpressionImpl)1 LikeFilterImpl (org.geotools.filter.LikeFilterImpl)1 DefaultInstant (org.geotools.temporal.object.DefaultInstant)1 DefaultPeriod (org.geotools.temporal.object.DefaultPeriod)1 DefaultPeriodDuration (org.geotools.temporal.object.DefaultPeriodDuration)1 DefaultPosition (org.geotools.temporal.object.DefaultPosition)1 Filter (org.opengis.filter.Filter)1 Instant (org.opengis.temporal.Instant)1 Period (org.opengis.temporal.Period)1