Java Code Examples for org.apache.cxf.jaxrs.ext.search.ConditionType#EQUALS

The following examples show how to use org.apache.cxf.jaxrs.ext.search.ConditionType#EQUALS . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SearchCondVisitor.java    From syncope with Apache License 2.0 6 votes vote down vote up
protected static ConditionType getConditionType(final SearchCondition<SearchBean> sc) {
    ConditionType ct = sc.getConditionType();
    if (sc instanceof SyncopeFiqlSearchCondition && sc.getConditionType() == ConditionType.CUSTOM) {
        SyncopeFiqlSearchCondition<SearchBean> sfsc = (SyncopeFiqlSearchCondition<SearchBean>) sc;
        switch (sfsc.getOperator()) {
            case SyncopeFiqlParser.IEQ:
                ct = ConditionType.EQUALS;
                break;

            case SyncopeFiqlParser.NIEQ:
                ct = ConditionType.NOT_EQUALS;
                break;

            default:
                throw new IllegalArgumentException(
                        String.format("Condition type %s is not supported", sfsc.getOperator()));
        }
    }

    return ct;
}
 
Example 2
Source File: LuceneQueryVisitor.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Query createRangeQuery(Class<?> cls, String name, Object value, ConditionType type) {

        boolean minInclusive = type == ConditionType.GREATER_OR_EQUALS || type == ConditionType.EQUALS;
        boolean maxInclusive = type == ConditionType.LESS_OR_EQUALS || type == ConditionType.EQUALS;

        if (String.class.isAssignableFrom(cls) || Number.class.isAssignableFrom(cls)) {
            Query query = null;

            if (Double.class.isAssignableFrom(cls)) {
                query = createDoubleRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else if (Float.class.isAssignableFrom(cls)) {
                query = createFloatRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else if (Long.class.isAssignableFrom(cls)) {
                query = createLongRangeQuery(name, value, type, minInclusive, maxInclusive);
            } else {
                query = createIntRangeQuery(name, value, type, minInclusive, maxInclusive);
            }

            return query;
        } else if (Date.class.isAssignableFrom(cls)) {
            final Date date = getValue(Date.class, getFieldTypeConverter(), value.toString());
            final String luceneDateValue = getString(Date.class, getFieldTypeConverter(), date);

            if (type == ConditionType.LESS_THAN || type == ConditionType.LESS_OR_EQUALS) {
                return TermRangeQuery.newStringRange(name, null, luceneDateValue, minInclusive, maxInclusive);
            }
            return TermRangeQuery.newStringRange(name, luceneDateValue,
                DateTools.dateToString(new Date(), Resolution.MILLISECOND), minInclusive, maxInclusive);
        } else {
            return null;
        }
    }
 
Example 3
Source File: SearchUtils.java    From syncope with Apache License 2.0 4 votes vote down vote up
private static SearchClause getPrimitiveSearchClause(final SearchCondition<SearchBean> sc) {
    SearchClause clause = new SearchClause();

    String property = sc.getCondition().getKeySet().iterator().next();
    clause.setProperty(property);

    String value = ENCODINGS.values().stream().
            reduce(sc.getCondition().get(property), (s, v) -> s.replace(v, ENCODINGS.getKey(v)));
    clause.setValue(value);

    LOG.debug("Condition: " + sc.getCondition());

    if (SpecialAttr.ROLES.toString().equals(property)) {
        clause.setType(SearchClause.Type.ROLE_MEMBERSHIP);
        clause.setProperty(value);
    } else if (SpecialAttr.PRIVILEGES.toString().equals(property)) {
        clause.setType(SearchClause.Type.PRIVILEGE);
        clause.setProperty(value);
    } else if (SpecialAttr.RELATIONSHIPS.toString().equals(property)) {
        clause.setType(SearchClause.Type.RELATIONSHIP);
        clause.setProperty(value);
    } else if (SpecialAttr.RELATIONSHIP_TYPES.toString().equals(property)) {
        clause.setType(SearchClause.Type.RELATIONSHIP);
        clause.setProperty(value);
    } else if (SpecialAttr.GROUPS.toString().equals(property)) {
        clause.setType(SearchClause.Type.GROUP_MEMBERSHIP);
        clause.setProperty(value);
    } else if (SpecialAttr.RESOURCES.toString().equals(property)) {
        clause.setType(SearchClause.Type.RESOURCE);
        clause.setProperty(value);
    } else if (SpecialAttr.MEMBER.toString().equals(property)) {
        clause.setType(SearchClause.Type.GROUP_MEMBER);
        clause.setProperty(value);
    } else if (property.startsWith("$")) {
        clause.setType(SearchClause.Type.CUSTOM);
        clause.setProperty(value);
    } else {
        clause.setType(SearchClause.Type.ATTRIBUTE);
    }

    ConditionType ct = sc.getConditionType();
    if (sc instanceof SyncopeFiqlSearchCondition && sc.getConditionType() == ConditionType.CUSTOM) {
        SyncopeFiqlSearchCondition<SearchBean> sfsc = (SyncopeFiqlSearchCondition<SearchBean>) sc;
        if (SyncopeFiqlParser.IEQ.equals(sfsc.getOperator())) {
            ct = ConditionType.EQUALS;
        } else if (SyncopeFiqlParser.NIEQ.equals(sfsc.getOperator())) {
            ct = ConditionType.NOT_EQUALS;
        }
    }
    switch (ct) {
        case EQUALS:
            if (SpecialAttr.RELATIONSHIP_TYPES.toString().equals(property)) {
                clause.setComparator(SpecialAttr.NULL.toString().equals(value)
                        ? SearchClause.Comparator.EQUALS : SearchClause.Comparator.IS_NULL);
            } else {
                clause.setComparator(SpecialAttr.NULL.toString().equals(value)
                        ? SearchClause.Comparator.IS_NULL : SearchClause.Comparator.EQUALS);
            }
            break;

        case NOT_EQUALS:
            if (SpecialAttr.RELATIONSHIP_TYPES.toString().equals(property)) {
                clause.setComparator(SpecialAttr.NULL.toString().equals(value)
                        ? SearchClause.Comparator.NOT_EQUALS : SearchClause.Comparator.IS_NOT_NULL);
            } else {
                clause.setComparator(SpecialAttr.NULL.toString().equals(value)
                        ? SearchClause.Comparator.IS_NOT_NULL : SearchClause.Comparator.NOT_EQUALS);
            }
            break;

        case GREATER_OR_EQUALS:
            clause.setComparator(SearchClause.Comparator.GREATER_OR_EQUALS);
            break;

        case GREATER_THAN:
            clause.setComparator(SearchClause.Comparator.GREATER_THAN);
            break;

        case LESS_OR_EQUALS:
            clause.setComparator(SearchClause.Comparator.LESS_OR_EQUALS);
            break;

        case LESS_THAN:
            clause.setComparator(SearchClause.Comparator.LESS_THAN);
            break;

        default:
            break;
    }

    return clause;
}
 
Example 4
Source File: FilterVisitor.java    From syncope with Apache License 2.0 4 votes vote down vote up
private Filter visitPrimitive(final SearchCondition<SearchBean> sc) {
    String name = getRealPropertyName(sc.getStatement().getProperty());
    Optional<SpecialAttr> specialAttrName = SpecialAttr.fromString(name);

    String value = SearchUtils.toSqlWildcardString(
            URLDecoder.decode(sc.getStatement().getValue().toString(), StandardCharsets.UTF_8), false).
            replaceAll("\\\\_", "_");
    Optional<SpecialAttr> specialAttrValue = SpecialAttr.fromString(value);

    ConditionType ct = sc.getConditionType();
    if (sc instanceof SyncopeFiqlSearchCondition && sc.getConditionType() == ConditionType.CUSTOM) {
        SyncopeFiqlSearchCondition<SearchBean> sfsc = (SyncopeFiqlSearchCondition<SearchBean>) sc;
        switch (sfsc.getOperator()) {
            case SyncopeFiqlParser.IEQ:
                ct = ConditionType.EQUALS;
                break;

            case SyncopeFiqlParser.NIEQ:
                ct = ConditionType.NOT_EQUALS;
                break;

            default:
                throw new IllegalArgumentException(
                        String.format("Condition type %s is not supported", sfsc.getOperator()));
        }
    }

    Attribute attr = AttributeBuilder.build(name, value);
    attrs.add(name);

    Filter leaf;
    switch (ct) {
        case EQUALS:
        case NOT_EQUALS:
            if (!specialAttrName.isPresent()) {
                if (specialAttrValue.isPresent() && specialAttrValue.get() == SpecialAttr.NULL) {
                    Filter empty = FilterBuilder.startsWith(AttributeBuilder.build(name, StringUtils.EMPTY));
                    if (ct == ConditionType.NOT_EQUALS) {
                        leaf = empty;
                    } else {
                        leaf = FilterBuilder.not(empty);
                        attrs.remove(name);
                    }
                } else {
                    if (value.indexOf('%') == -1) {
                        leaf = sc.getConditionType() == ConditionType.CUSTOM
                                ? FilterBuilder.equalsIgnoreCase(attr)
                                : FilterBuilder.equalTo(attr);
                    } else if (sc.getConditionType() != ConditionType.CUSTOM && value.startsWith("%")) {
                        leaf = FilterBuilder.endsWith(
                                AttributeBuilder.build(name, value.substring(1)));
                    } else if (sc.getConditionType() != ConditionType.CUSTOM && value.endsWith("%")) {
                        leaf = FilterBuilder.startsWith(
                                AttributeBuilder.build(name, value.substring(0, value.length() - 1)));
                    } else {
                        throw new IllegalArgumentException(
                                String.format("Unsupported search value %s", value));
                    }

                    if (ct == ConditionType.NOT_EQUALS) {
                        leaf = FilterBuilder.not(leaf);
                    }
                }
            } else {
                throw new IllegalArgumentException(
                        String.format("Special attr name %s is not supported", specialAttrName));
            }
            break;

        case GREATER_OR_EQUALS:
            leaf = FilterBuilder.greaterThanOrEqualTo(attr);
            break;

        case GREATER_THAN:
            leaf = FilterBuilder.greaterThan(attr);
            break;

        case LESS_OR_EQUALS:
            leaf = FilterBuilder.lessThanOrEqualTo(attr);
            break;

        case LESS_THAN:
            leaf = FilterBuilder.lessThan(attr);
            break;

        default:
            throw new IllegalArgumentException(String.format("Condition type %s is not supported", ct.name()));
    }

    return leaf;
}
 
Example 5
Source File: ODataParser.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object visitBinary(BinaryExpression binaryExpression, BinaryOperator operator,
        Object leftSide, Object rightSide) {

    // AND / OR operate on search conditions
    if (operator == BinaryOperator.AND || operator == BinaryOperator.OR) {
        if (leftSide instanceof SearchCondition && rightSide instanceof SearchCondition) {
            final List< SearchCondition< T > > conditions = new ArrayList<>(2);
            conditions.add((SearchCondition< T >)leftSide);
            conditions.add((SearchCondition< T >)rightSide);

            if (operator == BinaryOperator.AND) {
                return new AndSearchCondition< T >(conditions);
            } else if (operator == BinaryOperator.OR) {
                return new OrSearchCondition< T >(conditions);
            }
        } else {
            throw new SearchParseException(
                "Unsupported binary operation arguments (SearchCondition expected): "
                    + leftSide + ", " + rightSide);
        }
    }

    // Property could be either on left side (Name eq 'Tom') or
    // right side ('Tom' eq Name)
    TypedValue value = null;
    TypedProperty property = null;

    if (leftSide instanceof TypedProperty && rightSide instanceof TypedValue) {
        property = (TypedProperty)leftSide;
        value = (TypedValue)rightSide;
    } else if (rightSide instanceof TypedProperty && leftSide instanceof TypedValue) {
        property = (TypedProperty)rightSide;
        value = (TypedValue)leftSide;
    } else {
        throw new SearchParseException(
            "Unsupported binary operation arguments (TypedValue or TypedProperty expected): "
                + leftSide + ", " + rightSide);
    }

    ConditionType conditionType = null;
    switch (operator) {
    case EQ:
        conditionType = ConditionType.EQUALS;
        break;
    case NE:
        conditionType = ConditionType.NOT_EQUALS;
        break;
    case LT:
        conditionType = ConditionType.LESS_THAN;
        break;
    case LE:
        conditionType = ConditionType.LESS_OR_EQUALS;
        break;
    case GT:
        conditionType = ConditionType.GREATER_THAN;
        break;
    case GE:
        conditionType = ConditionType.GREATER_OR_EQUALS;
        break;
    default:
        throw new SearchParseException("Unsupported binary operation: " + operator);
    }

    Object typedValue = null;
    // If property type and value type are compatible, just use them
    if (property.typeInfo.getWrappedTypeClass().isAssignableFrom(value.typeClass)) {
        typedValue = value.value;
    } else { // Property type and value type are not compatible and convert / cast are required
        String valueStr = value.literal;
        if (isDecodeQueryValues()) {
            valueStr = UrlUtils.urlDecode(valueStr);
        }
        typedValue = parseType(property.propertyName, null, null, property.propertyName,
            property.typeInfo, valueStr);
    }

    final CollectionCheckInfo checkInfo = property.typeInfo.getCollectionCheckInfo();
    if (checkInfo != null) {
        return new CollectionCheckCondition< T >(property.propertyName, typedValue,
            property.typeInfo.getGenericType(), conditionType, condition, checkInfo);
    }

    return new PrimitiveSearchCondition< T >(property.propertyName, typedValue,
        property.typeInfo.getGenericType(), conditionType, condition);
}