Java Code Examples for org.apache.cxf.jaxrs.ext.search.SearchCondition#getConditionType()

The following examples show how to use org.apache.cxf.jaxrs.ext.search.SearchCondition#getConditionType() . 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: FilterVisitor.java    From syncope with Apache License 2.0 6 votes vote down vote up
private Filter visitCompount(final SearchCondition<SearchBean> sc) {
    List<Filter> searchConds = new ArrayList<>();
    sc.getSearchConditions().forEach(searchCond -> {
        searchConds.add(searchCond.getStatement() == null
                ? visitCompount(searchCond)
                : visitPrimitive(searchCond));
    });

    Filter compound;
    switch (sc.getConditionType()) {
        case AND:
            compound = FilterBuilder.and(searchConds);
            break;

        case OR:
            compound = FilterBuilder.or(searchConds);
            break;

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

    return compound;
}
 
Example 2
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 3
Source File: SearchCondVisitor.java    From syncope with Apache License 2.0 6 votes vote down vote up
protected SearchCond visitCompount(final SearchCondition<SearchBean> sc) {
    List<SearchCond> searchConds = new ArrayList<>();
    sc.getSearchConditions().forEach(searchCond -> {
        searchConds.add(searchCond.getStatement() == null
                ? visitCompount(searchCond)
                : visitPrimitive(searchCond));
    });

    SearchCond compound;
    switch (sc.getConditionType()) {
        case AND:
            compound = SearchCond.getAnd(searchConds);
            break;

        case OR:
            compound = SearchCond.getOr(searchConds);
            break;

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

    return compound;
}
 
Example 4
Source File: LuceneQueryVisitor.java    From cxf with Apache License 2.0 6 votes vote down vote up
public void visit(SearchCondition<T> sc) {
    if (state.get() == null) {
        reset();
    }
    PrimitiveStatement statement = sc.getStatement();
    if (statement != null) {
        if (statement.getProperty() != null) {
            state.get().peek().add(buildSimpleQuery(sc.getConditionType(),
                                     statement.getProperty(),
                                     statement.getValue()));
        }
    } else {
        state.get().push(new ArrayList<>());
        for (SearchCondition<T> condition : sc.getSearchConditions()) {
            condition.accept(this);
        }
        boolean orCondition = sc.getConditionType() == ConditionType.OR;
        List<Query> queries = state.get().pop();
        state.get().peek().add(createCompositeQuery(queries, orCondition));
    }
}
 
Example 5
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 6
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 7
Source File: LdapQueryVisitor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void visit(SearchCondition<T> sc) {

        StringBuilder sb = getStringBuilder();
        if (sb == null) {
            sb = new StringBuilder();
        }

        PrimitiveStatement statement = sc.getStatement();
        if (statement != null) {
            if (statement.getProperty() != null) {
                String name = getRealPropertyName(statement.getProperty());
                String rvalStr = getPropertyValue(name, statement.getValue());
                validatePropertyValue(name, rvalStr);

                sb.append('(');
                if (sc.getConditionType() == ConditionType.NOT_EQUALS) {
                    sb.append('!');
                }

                String ldapOperator = conditionTypeToLdapOperator(sc.getConditionType());
                String encodedRValStr = encodeQueryValues ? Util.doRFC2254Encoding(rvalStr) : rvalStr;
                sb.append(name).append(ldapOperator).append(encodedRValStr);

                sb.append(')');
            }
        } else {
            sb.append('(');
            if (sc.getConditionType() == ConditionType.AND) {
                sb.append('&');
            } else {
                sb.append('|');
            }

            for (SearchCondition<T> condition : sc.getSearchConditions()) {
                saveStringBuilder(sb);
                condition.accept(this);
                sb = getStringBuilder();
            }
            sb.append(')');
        }
        saveStringBuilder(sb);
    }