Search in sources :

Example 21 with ConvertedExpression

use of io.jans.orm.sql.model.ConvertedExpression in project jans by JanssenProject.

the class SqlEntryManager method contains.

@Override
protected <T> boolean contains(String baseDN, String[] objectClasses, Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, Filter filter, String[] ldapReturnAttributes) {
    if (StringHelper.isEmptyString(baseDN)) {
        throw new MappingException("Base DN to check contain entries is null");
    }
    // Create filter
    Filter searchFilter;
    if (objectClasses.length > 0) {
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    // Prepare properties types to allow build filter properly
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    ConvertedExpression convertedExpression;
    try {
        convertedExpression = toSqlFilter(searchFilter, propertiesAnnotationsMap);
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to convert filter '%s' to expression", searchFilter));
    }
    PagedResult<EntryData> searchResult = null;
    try {
        ParsedKey keyWithInum = toSQLKey(baseDN);
        searchResult = searchImpl(keyWithInum.getKey(), objectClasses[0], convertedExpression, SearchScope.SUB, ldapReturnAttributes, null, null, SearchReturnDataType.SEARCH, 0, 1, 0);
        if (searchResult == null) {
            throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: '%s', filter: '%s'", baseDN, searchFilter));
        }
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entry with baseDN: '%s', filter: '%s'", baseDN, searchFilter), ex);
    }
    return (searchResult != null) && (searchResult.getEntriesCount() > 0);
}
Also used : EntryData(io.jans.orm.model.EntryData) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) ParsedKey(io.jans.orm.impl.model.ParsedKey) SearchException(io.jans.orm.exception.operation.SearchException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) MappingException(io.jans.orm.exception.MappingException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchException(io.jans.orm.exception.operation.SearchException) DateTimeParseException(java.time.format.DateTimeParseException) AuthenticationException(io.jans.orm.exception.AuthenticationException) MappingException(io.jans.orm.exception.MappingException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation)

Example 22 with ConvertedExpression

use of io.jans.orm.sql.model.ConvertedExpression in project jans by JanssenProject.

the class SqlEntryManager method removeImpl.

protected <T> int removeImpl(String dn, Class<T> entryClass, Filter filter, int count) {
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    Filter searchFilter;
    if (objectClasses.length > 0) {
        LOG.trace("Filter: {}", filter);
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        throw new EntryDeleteException(String.format("Failed to delete entries with DN: '%s', filter: '%s' because objectClass is not specified", dn, filter));
    }
    // Find entries
    LOG.trace("-------------------------------------------------------");
    LOG.trace("Filter: {}", filter);
    LOG.trace("objectClasses count: {} ", objectClasses.length);
    LOG.trace("objectClasses: {}", objectClasses.toString());
    LOG.trace("Search filter: {}", searchFilter);
    // Prepare properties types to allow build filter properly
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    ParsedKey keyWithInum = toSQLKey(dn);
    ConvertedExpression convertedExpression;
    try {
        convertedExpression = toSqlFilterWithEmptyAlias(searchFilter, propertiesAnnotationsMap);
    } catch (SearchException ex) {
        throw new EntryDeleteException(String.format("Failed to convert filter '%s' to expression", searchFilter), ex);
    }
    try {
        int processed = (int) getOperationService().delete(keyWithInum.getKey(), objectClasses[0], convertedExpression, count);
        return processed;
    } catch (Exception ex) {
        throw new EntryDeleteException(String.format("Failed to delete entries with key: '%s', expression: '%s'", keyWithInum.getKey(), convertedExpression), ex);
    }
}
Also used : Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) ParsedKey(io.jans.orm.impl.model.ParsedKey) SearchException(io.jans.orm.exception.operation.SearchException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) MappingException(io.jans.orm.exception.MappingException) EntryPersistenceException(io.jans.orm.exception.EntryPersistenceException) EntryDeleteException(io.jans.orm.exception.EntryDeleteException) SearchException(io.jans.orm.exception.operation.SearchException) DateTimeParseException(java.time.format.DateTimeParseException) AuthenticationException(io.jans.orm.exception.AuthenticationException) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation)

Example 23 with ConvertedExpression

use of io.jans.orm.sql.model.ConvertedExpression in project jans by JanssenProject.

the class SqlFilterConverterTest method checkLeFilters.

@Test
public void checkLeFilters() throws SearchException {
    // LE -- String
    Filter filterLe1 = Filter.createLessOrEqualFilter("uid", "test");
    ConvertedExpression expressionLe1 = simpleConverter.convertToSqlFilter(filterLe1, null, null);
    String queryLe1 = toSelectSQL(expressionLe1);
    assertEquals(queryLe1, "select doc.`*` from `table` as doc where doc.uid <= 'test'");
    // LE -- Integer
    Filter filterLe2 = Filter.createLessOrEqualFilter("age", 23);
    ConvertedExpression expressionLe2 = simpleConverter.convertToSqlFilter(filterLe2, null, null);
    String queryLe2 = toSelectSQL(expressionLe2);
    assertEquals(queryLe2, "select doc.`*` from `table` as doc where doc.age <= 23");
    // LE -- Long
    Filter filterLe3 = Filter.createLessOrEqualFilter("age", 23L);
    ConvertedExpression expressionLe3 = simpleConverter.convertToSqlFilter(filterLe3, null, null);
    String queryLe3 = toSelectSQL(expressionLe3);
    assertEquals(queryLe3, "select doc.`*` from `table` as doc where doc.age <= 23");
    // LE -- Date
    Filter filterLe4 = Filter.createLessOrEqualFilter("added", getUtcDateFromMillis(1608130698398L));
    ConvertedExpression expressionLe4 = simpleConverter.convertToSqlFilter(filterLe4, null, null);
    String queryLe4 = toSelectSQL(expressionLe4);
    assertEquals(queryLe4, "select doc.`*` from `table` as doc where doc.added <= '2020-12-16T14:58:18.398'");
}
Also used : Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) Test(org.testng.annotations.Test)

Example 24 with ConvertedExpression

use of io.jans.orm.sql.model.ConvertedExpression in project jans by JanssenProject.

the class SqlFilterConverter method convertToSqlFilterImpl.

private ConvertedExpression convertToSqlFilterImpl(Filter genericFilter, Map<String, PropertyAnnotation> propertiesAnnotationsMap, Map<String, Class<?>> jsonAttributes, Function<? super Filter, Boolean> processor, boolean skipAlias) throws SearchException {
    if (genericFilter == null) {
        return null;
    }
    Filter currentGenericFilter = genericFilter;
    FilterType type = currentGenericFilter.getType();
    if (FilterType.RAW == type) {
        LOG.warn("RAW Ldap filter to SQL convertion will be removed in new version!!!");
        currentGenericFilter = ldapFilterConverter.convertRawLdapFilterToFilter(currentGenericFilter.getFilterString());
        type = currentGenericFilter.getType();
    }
    if (processor != null) {
        processor.apply(currentGenericFilter);
    }
    if ((FilterType.NOT == type) || (FilterType.AND == type) || (FilterType.OR == type)) {
        Filter[] genericFilters = currentGenericFilter.getFilters();
        Predicate[] expFilters = new Predicate[genericFilters.length];
        if (genericFilters != null) {
            // We can replace only multiple OR with IN
            boolean canJoinOrFilters = FilterType.OR == type;
            List<Filter> joinOrFilters = new ArrayList<Filter>();
            String joinOrAttributeName = null;
            for (int i = 0; i < genericFilters.length; i++) {
                Filter tmpFilter = genericFilters[i];
                expFilters[i] = (Predicate) convertToSqlFilterImpl(tmpFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias).expression();
                // Check if we can replace OR with IN
                if (!canJoinOrFilters) {
                    continue;
                }
                if (tmpFilter.getMultiValued() != null) {
                    canJoinOrFilters = false;
                    continue;
                }
                if ((FilterType.EQUALITY != tmpFilter.getType()) || (tmpFilter.getFilters() != null)) {
                    canJoinOrFilters = false;
                    continue;
                }
                Boolean isMultiValuedDetected = determineMultiValuedByType(tmpFilter.getAttributeName(), propertiesAnnotationsMap);
                if (!Boolean.FALSE.equals(isMultiValuedDetected)) {
                    if (!Boolean.FALSE.equals(currentGenericFilter.getMultiValued())) {
                        canJoinOrFilters = false;
                        continue;
                    }
                }
                if (joinOrAttributeName == null) {
                    joinOrAttributeName = tmpFilter.getAttributeName();
                    joinOrFilters.add(tmpFilter);
                    continue;
                }
                if (!joinOrAttributeName.equals(tmpFilter.getAttributeName())) {
                    canJoinOrFilters = false;
                    continue;
                }
                joinOrFilters.add(tmpFilter);
            }
            if (FilterType.NOT == type) {
                return ConvertedExpression.build(ExpressionUtils.predicate(Ops.NOT, expFilters[0]), jsonAttributes);
            } else if (FilterType.AND == type) {
                return ConvertedExpression.build(ExpressionUtils.allOf(expFilters), jsonAttributes);
            } else if (FilterType.OR == type) {
                if (canJoinOrFilters) {
                    List<Object> rightObjs = new ArrayList<>(joinOrFilters.size());
                    Filter lastEqFilter = null;
                    for (Filter eqFilter : joinOrFilters) {
                        lastEqFilter = eqFilter;
                        Object assertionValue = eqFilter.getAssertionValue();
                        if (assertionValue instanceof AttributeEnum) {
                            assertionValue = ((AttributeEnum) assertionValue).getValue();
                        }
                        rightObjs.add(assertionValue);
                    }
                    return ConvertedExpression.build(ExpressionUtils.in(buildTypedPath(lastEqFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), rightObjs), jsonAttributes);
                } else {
                    return ConvertedExpression.build(ExpressionUtils.anyOf(expFilters), jsonAttributes);
                }
            }
        }
    }
    if (FilterType.EQUALITY == type) {
        if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
            Expression expression = buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
            Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_CONTAINS, expression, buildTypedExpression(currentGenericFilter, true), Expressions.constant("$.v"));
            return ConvertedExpression.build(operation, jsonAttributes);
        } else {
            Filter usedFilter = currentGenericFilter;
            Expression expression = buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
            return ConvertedExpression.build(ExpressionUtils.eq(expression, buildTypedExpression(usedFilter)), jsonAttributes);
        }
    }
    if (FilterType.LESS_OR_EQUAL == type) {
        if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
            if (currentGenericFilter.getMultiValuedCount() > 1) {
                Collection<Predicate> expressions = new ArrayList<>(currentGenericFilter.getMultiValuedCount());
                for (int i = 0; i < currentGenericFilter.getMultiValuedCount(); i++) {
                    Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[" + i + "]"));
                    Predicate predicate = Expressions.asComparable(operation).loe(buildTypedExpression(currentGenericFilter));
                    expressions.add(predicate);
                }
                Expression expression = ExpressionUtils.anyOf(expressions);
                return ConvertedExpression.build(expression, jsonAttributes);
            }
            Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[0]"));
            Expression expression = Expressions.asComparable(operation).loe(buildTypedExpression(currentGenericFilter));
            return ConvertedExpression.build(expression, jsonAttributes);
        } else {
            return ConvertedExpression.build(Expressions.asComparable(buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias)).loe(buildTypedExpression(currentGenericFilter)), jsonAttributes);
        }
    }
    if (FilterType.GREATER_OR_EQUAL == type) {
        if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
            if (currentGenericFilter.getMultiValuedCount() > 1) {
                Collection<Predicate> expressions = new ArrayList<>(currentGenericFilter.getMultiValuedCount());
                for (int i = 0; i < currentGenericFilter.getMultiValuedCount(); i++) {
                    Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[" + i + "]"));
                    Predicate predicate = Expressions.asComparable(operation).goe(buildTypedExpression(currentGenericFilter));
                    expressions.add(predicate);
                }
                Expression expression = ExpressionUtils.anyOf(expressions);
                return ConvertedExpression.build(expression, jsonAttributes);
            }
            Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[0]"));
            Expression expression = Expressions.asComparable(operation).goe(buildTypedExpression(currentGenericFilter));
            return ConvertedExpression.build(expression, jsonAttributes);
        } else {
            return ConvertedExpression.build(Expressions.asComparable(buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias)).goe(buildTypedExpression(currentGenericFilter)), jsonAttributes);
        }
    }
    if (FilterType.PRESENCE == type) {
        Expression expression;
        if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
            if (currentGenericFilter.getMultiValuedCount() > 1) {
                Collection<Predicate> expressions = new ArrayList<>(currentGenericFilter.getMultiValuedCount());
                for (int i = 0; i < currentGenericFilter.getMultiValuedCount(); i++) {
                    Predicate predicate = ExpressionUtils.isNotNull(ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[" + i + "]")));
                    expressions.add(predicate);
                }
                Predicate predicate = ExpressionUtils.anyOf(expressions);
                return ConvertedExpression.build(predicate, jsonAttributes);
            }
            expression = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[0]"));
        } else {
            expression = buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
        }
        return ConvertedExpression.build(ExpressionUtils.isNotNull(expression), jsonAttributes);
    }
    if (FilterType.APPROXIMATE_MATCH == type) {
        throw new SearchException("Convertion from APPROXIMATE_MATCH LDAP filter to SQL filter is not implemented");
    }
    if (FilterType.SUBSTRING == type) {
        StringBuilder like = new StringBuilder();
        if (currentGenericFilter.getSubInitial() != null) {
            like.append(currentGenericFilter.getSubInitial());
        }
        like.append("%");
        String[] subAny = currentGenericFilter.getSubAny();
        if ((subAny != null) && (subAny.length > 0)) {
            for (String any : subAny) {
                like.append(any);
                like.append("%");
            }
        }
        if (currentGenericFilter.getSubFinal() != null) {
            like.append(currentGenericFilter.getSubFinal());
        }
        Expression expression;
        if (isMultiValue(currentGenericFilter, propertiesAnnotationsMap)) {
            if (currentGenericFilter.getMultiValuedCount() > 1) {
                Collection<Predicate> expressions = new ArrayList<>(currentGenericFilter.getMultiValuedCount());
                for (int i = 0; i < currentGenericFilter.getMultiValuedCount(); i++) {
                    Operation<Boolean> operation = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[" + i + "]"));
                    Predicate predicate = Expressions.booleanOperation(Ops.LIKE, operation, Expressions.constant(like.toString()));
                    expressions.add(predicate);
                }
                Predicate predicate = ExpressionUtils.anyOf(expressions);
                return ConvertedExpression.build(predicate, jsonAttributes);
            }
            expression = ExpressionUtils.predicate(SqlOps.JSON_EXTRACT, buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias), Expressions.constant("$.v[0]"));
        } else {
            expression = buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias);
        }
        return ConvertedExpression.build(Expressions.booleanOperation(Ops.LIKE, expression, Expressions.constant(like.toString())), jsonAttributes);
    }
    if (FilterType.LOWERCASE == type) {
        return ConvertedExpression.build(ExpressionUtils.toLower(buildTypedPath(currentGenericFilter, propertiesAnnotationsMap, jsonAttributes, processor, skipAlias)), jsonAttributes);
    }
    throw new SearchException(String.format("Unknown filter type '%s'", type));
}
Also used : AttributeEnum(io.jans.orm.annotation.AttributeEnum) ArrayList(java.util.ArrayList) SearchException(io.jans.orm.exception.operation.SearchException) Predicate(com.querydsl.core.types.Predicate) FilterType(io.jans.orm.search.filter.FilterType) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) Expression(com.querydsl.core.types.Expression)

Example 25 with ConvertedExpression

use of io.jans.orm.sql.model.ConvertedExpression in project jans by JanssenProject.

the class SqlFilterConverterCheckExcludeFilterTest method checkObjectClassExcludeFilter.

@Test
public void checkObjectClassExcludeFilter() throws SearchException {
    Filter filterEq1 = Filter.createEqualityFilter("uid", "test");
    Filter filterEq2 = Filter.createEqualityFilter(Filter.createLowercaseFilter("uid"), "test");
    Filter filterEq3 = Filter.createEqualityFilter("objectClass", "jansPerson");
    Filter filterEq4 = Filter.createEqualityFilter("added", getUtcDateFromMillis(1608130698398L)).multiValued();
    Filter andFilter = Filter.createANDFilter(filterEq1, filterEq2, filterEq3, filterEq4);
    Filter orFilter = Filter.createANDFilter(filterEq1, filterEq2, filterEq3, andFilter, filterEq4);
    Filter filter1 = Filter.createANDFilter(filterEq3, orFilter);
    ConvertedExpression expression1 = simpleConverter.convertToSqlFilter(filter1, null, null);
    String query1 = toSelectSQL(expression1);
    assertEquals(query1, "select doc.`*` from `table` as doc where doc.objectClass = 'jansPerson' and (doc.uid = 'test' and lower(doc.uid) = 'test' and doc.objectClass = 'jansPerson' and (doc.uid = 'test' and lower(doc.uid) = 'test' and doc.objectClass = 'jansPerson' and JSON_CONTAINS(doc.added->'$.v', CAST('[\"2020-12-16T14:58:18.398\"]' AS JSON))) and JSON_CONTAINS(doc.added->'$.v', CAST('[\"2020-12-16T14:58:18.398\"]' AS JSON)))");
    Filter filter2 = filterProcessor.excludeFilter(filter1, filterEq3);
    ConvertedExpression expression2 = simpleConverter.convertToSqlFilter(filter2, null, null);
    String query2 = toSelectSQL(expression2);
    assertEquals(query2, "select doc.`*` from `table` as doc where doc.uid = 'test' and lower(doc.uid) = 'test' and (doc.uid = 'test' and lower(doc.uid) = 'test' and JSON_CONTAINS(doc.added->'$.v', CAST('[\"2020-12-16T14:58:18.398\"]' AS JSON))) and JSON_CONTAINS(doc.added->'$.v', CAST('[\"2020-12-16T14:58:18.398\"]' AS JSON))");
    Filter filter3 = filterProcessor.excludeFilter(filter1, Filter.createEqualityFilter("objectClass", null));
    ConvertedExpression expression3 = simpleConverter.convertToSqlFilter(filter3, null, null);
    String query3 = toSelectSQL(expression3);
    assertEquals(query3, "select doc.`*` from `table` as doc where doc.uid = 'test' and lower(doc.uid) = 'test' and (doc.uid = 'test' and lower(doc.uid) = 'test' and JSON_CONTAINS(doc.added->'$.v', CAST('[\"2020-12-16T14:58:18.398\"]' AS JSON))) and JSON_CONTAINS(doc.added->'$.v', CAST('[\"2020-12-16T14:58:18.398\"]' AS JSON))");
}
Also used : Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) Test(org.testng.annotations.Test)

Aggregations

ConvertedExpression (io.jans.orm.sql.model.ConvertedExpression)25 Filter (io.jans.orm.search.filter.Filter)24 Test (org.testng.annotations.Test)19 SearchException (io.jans.orm.exception.operation.SearchException)5 AuthenticationException (io.jans.orm.exception.AuthenticationException)4 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)4 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)4 MappingException (io.jans.orm.exception.MappingException)4 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)4 DateTimeParseException (java.time.format.DateTimeParseException)4 ParsedKey (io.jans.orm.impl.model.ParsedKey)3 EntryData (io.jans.orm.model.EntryData)3 Expression (com.querydsl.core.types.Expression)1 OrderSpecifier (com.querydsl.core.types.OrderSpecifier)1 Predicate (com.querydsl.core.types.Predicate)1 AttributeEnum (io.jans.orm.annotation.AttributeEnum)1 FilterType (io.jans.orm.search.filter.FilterType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1