org.apache.cxf.jaxrs.ext.search.PrimitiveStatement Java Examples

The following examples show how to use org.apache.cxf.jaxrs.ext.search.PrimitiveStatement. 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: ConfigurationEndpointUtils.java    From carbon-identity-framework with Apache License 2.0 6 votes vote down vote up
public static Condition buildSearchCondition(SearchCondition searchCondition) {

        if (!(searchCondition.getStatement() == null)) {
            PrimitiveStatement primitiveStatement = searchCondition.getStatement();
            if (!(primitiveStatement.getProperty() == null)) {
                return new PrimitiveCondition(
                        primitiveStatement.getProperty(),
                        getPrimitiveOperatorFromOdata(primitiveStatement.getCondition()),
                        primitiveStatement.getValue()
                );
            }
            return null;
        } else {
            List<Condition> conditions = new ArrayList<>();
            for (Object condition : searchCondition.getSearchConditions()) {
                Condition buildCondition = buildSearchCondition((SearchCondition) condition);
                if (buildCondition != null) {
                    conditions.add(buildCondition);
                }
            }
            return new ComplexCondition(
                    getComplexOperatorFromOdata(searchCondition.getConditionType()),
                    conditions
            );
        }
    }
 
Example #2
Source File: AbstractJPATypedQueryVisitor.java    From cxf with Apache License 2.0 6 votes vote down vote up
private Predicate buildPredicate(PrimitiveStatement ps) {
    String name = ps.getProperty();
    Object propertyValue = ps.getValue();
    validatePropertyValue(name, propertyValue);

    name = super.getRealPropertyName(name);
    ClassValue cv = getPrimitiveFieldClass(ps,
                                           name,
                                           ps.getValue().getClass(),
                                           ps.getValueType(),
                                           propertyValue);
    CollectionCheckInfo collInfo = cv.getCollectionCheckInfo();
    Path<?> path = getPath(root, name, cv, collInfo);

    return collInfo == null
        ? doBuildPredicate(ps.getCondition(), path, cv.getCls(), cv.getValue())
        : doBuildCollectionPredicate(ps.getCondition(), path, collInfo);
}
 
Example #3
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 #4
Source File: FiqlParserTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseComplex1() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==ami*;level=gt=10");
    assertEquals(ConditionType.AND, filter.getConditionType());

    List<SearchCondition<Condition>> conditions = filter.getSearchConditions();
    assertEquals(2, conditions.size());
    PrimitiveStatement st1 = conditions.get(0).getStatement();
    PrimitiveStatement st2 = conditions.get(1).getStatement();
    assertTrue((ConditionType.EQUALS.equals(st1.getCondition())
        && ConditionType.GREATER_THAN.equals(st2.getCondition()))
        || (ConditionType.EQUALS.equals(st2.getCondition())
            && ConditionType.GREATER_THAN.equals(st1.getCondition())));

    assertTrue(filter.isMet(new Condition("amichalec", 12, new Date())));
    assertTrue(filter.isMet(new Condition("ami", 12, new Date())));
    assertFalse(filter.isMet(new Condition("ami", 8, null)));
    assertFalse(filter.isMet(new Condition("am", 20, null)));
}
 
Example #5
Source File: FiqlParserTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseComplex2() throws SearchParseException {
    SearchCondition<Condition> filter = parser.parse("name==ami*,level=gt=10");
    assertEquals(ConditionType.OR, filter.getConditionType());

    List<SearchCondition<Condition>> conditions = filter.getSearchConditions();
    assertEquals(2, conditions.size());

    PrimitiveStatement st1 = conditions.get(0).getStatement();
    PrimitiveStatement st2 = conditions.get(1).getStatement();
    assertTrue((ConditionType.EQUALS.equals(st1.getCondition())
        && ConditionType.GREATER_THAN.equals(st2.getCondition()))
        || (ConditionType.EQUALS.equals(st2.getCondition())
            && ConditionType.GREATER_THAN.equals(st1.getCondition())));

    assertTrue(filter.isMet(new Condition("ami", 0, new Date())));
    assertTrue(filter.isMet(new Condition("foo", 20, null)));
    assertFalse(filter.isMet(new Condition("foo", 0, null)));
}
 
Example #6
Source File: ServerApplicationManagementService.java    From identity-api-server with Apache License 2.0 5 votes vote down vote up
private Condition buildSearchCondition(SearchCondition searchCondition) {

        if (!(searchCondition.getStatement() == null)) {
            PrimitiveStatement primitiveStatement = searchCondition.getStatement();

            if (ApplicationManagementConstants.TemplateProperties.SEARCH_KEYS.contains(primitiveStatement.getProperty
                    ())) {
                List<Condition> list = new ArrayList<>();
                Condition attrKeyCondition = new PrimitiveCondition(ApplicationManagementConstants.TemplateProperties
                        .ATTR_KEY, EQUALS, primitiveStatement.getProperty());
                Condition attrValueCondition = new PrimitiveCondition(ApplicationManagementConstants
                        .TemplateProperties.ATTR_VALUE, getPrimitiveOperatorFromOdata(primitiveStatement.getCondition
                        ()), primitiveStatement.getValue());
                list.add(attrKeyCondition);
                list.add(attrValueCondition);
                return new ComplexCondition(getComplexOperatorFromOdata(ConditionType.AND),
                        list);
            } else if (ApplicationManagementConstants.TemplateProperties.SEARCH_KEY_NAME.equals(primitiveStatement
                    .getProperty())) {
                return new PrimitiveCondition(ApplicationManagementConstants.TemplateProperties
                        .SEARCH_KEY_NAME_INTERNAL, getPrimitiveOperatorFromOdata(primitiveStatement
                        .getCondition()), primitiveStatement.getValue());
            } else {
                throw buildClientError(ApplicationManagementConstants.ErrorMessage
                        .ERROR_CODE_ERROR_INVALID_SEARCH_FILTER, null);
            }
        } else {
            List<Condition> conditions = new ArrayList<>();
            for (Object condition : searchCondition.getSearchConditions()) {
                Condition buildCondition = buildSearchCondition((SearchCondition) condition);
                conditions.add(buildCondition);
            }
            return new ComplexCondition(getComplexOperatorFromOdata(searchCondition.getConditionType()), conditions);
        }
    }
 
Example #7
Source File: CollectionCheckCondition.java    From cxf with Apache License 2.0 5 votes vote down vote up
public PrimitiveStatement getStatement() {
    return new CollectionCheckStatement(getPropertyName(),
                                        getPropertyValue(),
                                        getPropertyType(),
                                        getConditionType(),
                                        checkInfo);
}
 
Example #8
Source File: ServerIdpManagementService.java    From identity-api-server with Apache License 2.0 4 votes vote down vote up
private Condition buildSearchCondition(SearchCondition searchCondition) {

        if (!(searchCondition.getStatement() == null)) {
            PrimitiveStatement primitiveStatement = searchCondition.getStatement();

            if (Constants.SEARCH_KEY_NAME.equals(primitiveStatement.getProperty())) {
                return new PrimitiveCondition(
                        Constants.SEARCH_KEY_NAME_INTERNAL, getPrimitiveOperatorFromOdata(primitiveStatement
                        .getCondition()), primitiveStatement.getValue());
            } else if (Constants.SEARCH_KEY_SERVICES.equals(primitiveStatement.getProperty()) &&
                    getPrimitiveOperatorFromOdata(primitiveStatement.getCondition()) == EQUALS) {
                Condition servicesAttrKeyCondition = new PrimitiveCondition(Constants.ATTR_KEY, EQUALS, Constants
                        .SEARCH_KEY_SERVICES);
                Condition servicesAttrValueCondition = new PrimitiveCondition(Constants.ATTR_VALUE, EQUALS,
                        primitiveStatement.getValue());
                Condition servicesCondition = new ComplexCondition(getComplexOperatorFromOdata(ConditionType.AND),
                        Arrays.asList(servicesAttrKeyCondition, servicesAttrValueCondition));

                Condition servicesCombinedAttrKeyCondition = new PrimitiveCondition(Constants.ATTR_KEY, EQUALS,
                        Constants.SEARCH_KEY_SERVICES);
                Condition servicesCombinedAttrValueCondition = new PrimitiveCondition(Constants.ATTR_VALUE, EQUALS,
                        Constants.SEARCH_VALUE_AUTHENTICATION_PROVISIONING);
                Condition servicesCombinedCondition = new ComplexCondition(getComplexOperatorFromOdata(ConditionType
                        .AND), Arrays.asList(servicesCombinedAttrKeyCondition, servicesCombinedAttrValueCondition));

                return new ComplexCondition(getComplexOperatorFromOdata(ConditionType.OR), Arrays.asList
                        (servicesCondition, servicesCombinedCondition));
            } else if (Constants.SEARCH_KEYS.contains(primitiveStatement.getProperty())) {
                List<Condition> list = new ArrayList<>();
                Condition attrKeyCondition = new PrimitiveCondition(Constants.ATTR_KEY, EQUALS, primitiveStatement
                        .getProperty());
                Condition attrValueCondition = new PrimitiveCondition(Constants.ATTR_VALUE,
                        getPrimitiveOperatorFromOdata(primitiveStatement.getCondition()), primitiveStatement
                        .getValue());
                list.add(attrKeyCondition);
                list.add(attrValueCondition);
                return new ComplexCondition(getComplexOperatorFromOdata(ConditionType.AND),
                        list);
            } else {
                throw handleException(Response.Status.BAD_REQUEST,
                        Constants.ErrorMessage.ERROR_CODE_ERROR_INVALID_SEARCH_FILTER, null);
            }
        } else {
            List<Condition> conditions = new ArrayList<>();
            for (Object condition : searchCondition.getSearchConditions()) {
                Condition buildCondition = buildSearchCondition((SearchCondition) condition);
                conditions.add(buildCondition);
            }
            return new ComplexCondition(getComplexOperatorFromOdata(searchCondition.getConditionType()), conditions);
        }
    }
 
Example #9
Source File: AbstractSearchConditionVisitor.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected ClassValue getPrimitiveFieldClass(PrimitiveStatement ps, String name, Class<?> valueCls,
                                            Type type, Object value) {
    return doGetPrimitiveFieldClass(ps, name, valueCls, type, value, new HashSet<>());
}
 
Example #10
Source File: AbstractSearchConditionVisitor.java    From cxf with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
private ClassValue doGetPrimitiveFieldClass(PrimitiveStatement ps,
                                            String name, Class<?> valueCls, Type type, Object value,
                                            Set<String> set) {
    boolean isCollection = InjectionUtils.isSupportedCollectionOrArray(valueCls);
    Class<?> actualCls = isCollection ? InjectionUtils.getActualType(type) : valueCls;
    CollectionCheckInfo collInfo = null;
    int index = name.indexOf('.');
    if (index != -1) {
        String[] names = name.split("\\.");
        name = name.substring(index + 1);
        if (value != null && !InjectionUtils.isPrimitive(actualCls)) {
            try {
                String nextPart = StringUtils.capitalize(names[1]);

                Method m = actualCls.getMethod("get" + nextPart, new Class[]{});
                if (isCollection) {
                    value = ((Collection)value).iterator().next();
                    set.add(names[0]);
                }
                value = m.invoke(value, new Object[]{});
                valueCls = value.getClass();
                type = m.getGenericReturnType();
            } catch (Throwable ex) {
                throw new RuntimeException(ex);
            }
            return doGetPrimitiveFieldClass(ps, name, valueCls, type, value, set);
        }
    } else if (isCollection) {
        set.add(name);
        Collection coll = (Collection)value;
        value = coll.isEmpty() ? null : coll.iterator().next();
        valueCls = actualCls;
        if (ps instanceof CollectionCheckStatement) {
            collInfo = ((CollectionCheckStatement)ps).getCollectionCheckInfo();
        }
    }

    Class<?> cls = null;
    if (primitiveFieldTypeMap != null) {
        cls = primitiveFieldTypeMap.get(name);
    }
    if (cls == null) {
        cls = valueCls;
    }
    return new ClassValue(cls, value, collInfo, set);
}
 
Example #11
Source File: SQLPrinterVisitor.java    From cxf with Apache License 2.0 4 votes vote down vote up
public void visit(SearchCondition<T> sc) {
    StringBuilder sb = getStringBuilder();

    PrimitiveStatement statement = sc.getStatement();
    if (statement != null) {
        if (statement.getProperty() != null) {

            String property = statement.getProperty();
            String[] properties =  property.split("\\.");
            if (properties.length > 2) {
                throw new SearchParseException("SQL Visitor supports only a single JOIN");
            } else if (properties.length == 2) {
                if (joinDone) {
                    throw new SearchParseException("SQL Visitor has already created JOIN");
                }
                joinDone = true;
                String joinTable = getRealPropertyName(properties[0]);
                // Joining key can be pre-configured
                String joiningKey = primaryTable;
                if (joiningKey.endsWith("s")) {
                    joiningKey = joiningKey.substring(0, joiningKey.length() - 1);
                }
                joiningKey += "_id";

                topBuilder.append(" left join ").append(joinTable);
                topBuilder.append(" on ").append(primaryTable).append(".id").append(" = ")
                    .append(joinTable).append('.').append(joiningKey);

                property = joinTable + "." + getRealPropertyName(properties[1]);
            }

            String name = getRealPropertyName(property);
            String originalValue = getPropertyValue(name, statement.getValue());
            validatePropertyValue(name, originalValue);

            String value = SearchUtils.toSqlWildcardString(originalValue, isWildcardStringMatch());
            value = SearchUtils.duplicateSingleQuoteIfNeeded(value);

            if (tableAlias != null) {
                name = tableAlias + "." + name;
            }

            sb.append(name).append(' ').append(
                        SearchUtils.conditionTypeToSqlOperator(sc.getConditionType(), value,
                                                               originalValue))
                        .append(' ').append("'").append(value).append("'"); //NOPMD
        }
    } else {
        boolean first = true;
        for (SearchCondition<T> condition : sc.getSearchConditions()) {
            if (!first) {
                sb.append(' ').append(sc.getConditionType().toString()).append(' ');
            } else {
                first = false;
            }
            sb.append('(');
            saveStringBuilder(sb);
            condition.accept(this);
            sb = getStringBuilder();
            sb.append(')');
        }
    }

    saveStringBuilder(sb);
}
 
Example #12
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);
    }