Search in sources :

Example 6 with EntryData

use of io.jans.orm.model.EntryData in project jans by JanssenProject.

the class SqlOperationServiceImpl method searchImpl.

private <O> PagedResult<EntryData> searchImpl(TableMapping tableMapping, String key, ConvertedExpression expression, SearchScope scope, String[] attributes, OrderSpecifier<?>[] orderBy, SqlBatchOperationWraper<O> batchOperationWraper, SearchReturnDataType returnDataType, int start, int count, int pageSize) throws SearchException {
    BatchOperation<O> batchOperation = null;
    if (batchOperationWraper != null) {
        batchOperation = (BatchOperation<O>) batchOperationWraper.getBatchOperation();
    }
    RelationalPathBase<Object> tableRelationalPath = buildTableRelationalPath(tableMapping);
    Expression<?> attributesExp = buildSelectAttributes(attributes);
    SQLQuery<?> sqlSelectQuery;
    if (expression == null) {
        sqlSelectQuery = sqlQueryFactory.select(attributesExp).from(tableRelationalPath);
    } else {
        Predicate whereExp = (Predicate) expression.expression();
        sqlSelectQuery = sqlQueryFactory.select(attributesExp).from(tableRelationalPath).where(whereExp);
    }
    SQLQuery<?> baseQuery = sqlSelectQuery;
    if (orderBy != null) {
        baseQuery = sqlSelectQuery.orderBy(orderBy);
    }
    List<EntryData> searchResultList = new LinkedList<EntryData>();
    String queryStr = null;
    if ((SearchReturnDataType.SEARCH == returnDataType) || (SearchReturnDataType.SEARCH_COUNT == returnDataType)) {
        List<EntryData> lastResult = null;
        if (pageSize > 0) {
            boolean collectSearchResult;
            SQLQuery<?> query;
            int currentLimit;
            try {
                int resultCount = 0;
                int lastCountRows = 0;
                do {
                    collectSearchResult = true;
                    currentLimit = pageSize;
                    if (count > 0) {
                        currentLimit = Math.min(pageSize, count - resultCount);
                    }
                    query = baseQuery.limit(currentLimit).offset(start + resultCount);
                    queryStr = query.getSQL().getSQL();
                    LOG.debug("Executing query: '" + queryStr + "'");
                    try (ResultSet resultSet = query.getResults()) {
                        lastResult = getEntryDataList(tableMapping, resultSet);
                    }
                    lastCountRows = lastResult.size();
                    if (batchOperation != null) {
                        collectSearchResult = batchOperation.collectSearchResult(lastCountRows);
                    }
                    if (collectSearchResult) {
                        searchResultList.addAll(lastResult);
                    }
                    if (batchOperation != null) {
                        List<O> entries = batchOperationWraper.createEntities(lastResult);
                        batchOperation.performAction(entries);
                    }
                    resultCount += lastCountRows;
                    if (((count > 0) && (resultCount >= count)) || (lastCountRows < currentLimit)) {
                        break;
                    }
                } while (lastCountRows > 0);
            } catch (QueryException ex) {
                throw new SearchException(String.format("Failed to build search entries query. Key: '%s', expression: '%s'", key, expression.expression()), ex);
            } catch (SQLException | EntryConvertationException ex) {
                throw new SearchException(String.format("Failed to execute query '%s'  with key: '%s'", queryStr, key), ex);
            }
        } else {
            try {
                SQLQuery<?> query = baseQuery;
                if (count > 0) {
                    query = query.limit(count);
                }
                if (start > 0) {
                    query = query.offset(start);
                }
                queryStr = query.getSQL().getSQL();
                LOG.debug("Execution query: '" + queryStr + "'");
                try (ResultSet resultSet = query.getResults()) {
                    lastResult = getEntryDataList(tableMapping, resultSet);
                    searchResultList.addAll(lastResult);
                }
            } catch (QueryException ex) {
                String sqlExpression = queryStr;
                if (StringHelper.isNotEmpty(sqlExpression)) {
                    sqlExpression = expression.expression().toString();
                }
                throw new SearchException(String.format("Failed to build search entries query. Key: '%s', expression: '%s'", key, sqlExpression), ex);
            } catch (SQLException | EntryConvertationException ex) {
                throw new SearchException("Failed to search entries. Query: '" + queryStr + "'", ex);
            }
        }
    }
    PagedResult<EntryData> result = new PagedResult<EntryData>();
    result.setEntries(searchResultList);
    result.setEntriesCount(searchResultList.size());
    result.setStart(start);
    if ((SearchReturnDataType.COUNT == returnDataType) || (SearchReturnDataType.SEARCH_COUNT == returnDataType)) {
        SQLQuery<?> sqlCountSelectQuery;
        if (expression == null) {
            sqlCountSelectQuery = sqlQueryFactory.select(Expressions.as(ExpressionUtils.count(Wildcard.all), "TOTAL")).from(tableRelationalPath);
        } else {
            Predicate whereExp = (Predicate) expression.expression();
            sqlCountSelectQuery = sqlQueryFactory.select(Expressions.as(ExpressionUtils.count(Wildcard.all), "TOTAL")).from(tableRelationalPath).where(whereExp);
        }
        try {
            queryStr = sqlCountSelectQuery.getSQL().getSQL();
            LOG.debug("Calculating count. Execution query: '" + queryStr + "'");
            try (ResultSet countResult = sqlCountSelectQuery.getResults()) {
                if (!countResult.next()) {
                    throw new SearchException("Failed to calculate count entries. Query: '" + queryStr + "'");
                }
                result.setTotalEntriesCount(countResult.getInt("TOTAL"));
            }
        } catch (QueryException ex) {
            throw new SearchException(String.format("Failed to build count search entries query. Key: '%s', expression: '%s'", key, expression.expression()), ex);
        } catch (SQLException ex) {
            throw new SearchException("Failed to calculate count entries. Query: '" + queryStr + "'", ex);
        }
    }
    return result;
}
Also used : EntryData(io.jans.orm.model.EntryData) SQLException(java.sql.SQLException) SearchException(io.jans.orm.exception.operation.SearchException) LinkedList(java.util.LinkedList) Predicate(com.querydsl.core.types.Predicate) QueryException(com.querydsl.core.QueryException) ResultSet(java.sql.ResultSet) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) PagedResult(io.jans.orm.model.PagedResult)

Example 7 with EntryData

use of io.jans.orm.model.EntryData in project jans by JanssenProject.

the class SqlOperationServiceImpl method search.

@Override
public <O> PagedResult<EntryData> search(String key, String objectClass, ConvertedExpression expression, SearchScope scope, String[] attributes, OrderSpecifier<?>[] orderBy, SqlBatchOperationWraper<O> batchOperationWraper, SearchReturnDataType returnDataType, int start, int count, int pageSize) throws SearchException {
    Instant startTime = OperationDurationUtil.instance().now();
    TableMapping tableMapping = connectionProvider.getTableMappingByKey(key, objectClass);
    PagedResult<EntryData> result = searchImpl(tableMapping, key, expression, scope, attributes, orderBy, batchOperationWraper, returnDataType, start, count, pageSize);
    Duration duration = OperationDurationUtil.instance().duration(startTime);
    OperationDurationUtil.instance().logDebug("SQL operation: search, duration: {}, table: {}, key: {}, expression: {}, scope: {}, attributes: {}, orderBy: {}, batchOperationWraper: {}, returnDataType: {}, start: {}, count: {}, pageSize: {}", duration, tableMapping.getTableName(), key, expression, scope, attributes, orderBy, batchOperationWraper, returnDataType, start, count, pageSize);
    return result;
}
Also used : EntryData(io.jans.orm.model.EntryData) Instant(java.time.Instant) TableMapping(io.jans.orm.sql.model.TableMapping) Duration(java.time.Duration)

Example 8 with EntryData

use of io.jans.orm.model.EntryData in project jans by JanssenProject.

the class SqlEntryManager method findEntriesImpl.

protected <T> PagedResult<EntryData> findEntriesImpl(String baseDN, Class<T> entryClass, Filter filter, SearchScope scope, String[] ldapReturnAttributes, String sortBy, SortOrder sortOrder, BatchOperation<T> batchOperation, SearchReturnDataType returnDataType, int start, int count, int chunkSize) {
    // Check entry class
    checkEntryClass(entryClass, false);
    String[] objectClasses = getTypeObjectClasses(entryClass);
    List<PropertyAnnotation> propertiesAnnotations = getEntryPropertyAnnotations(entryClass);
    String[] currentLdapReturnAttributes = ldapReturnAttributes;
    if (ArrayHelper.isEmpty(currentLdapReturnAttributes)) {
        currentLdapReturnAttributes = getAttributes(null, propertiesAnnotations, false);
    }
    Filter searchFilter;
    if (objectClasses.length > 0) {
        LOG.trace("Filter: {}", filter);
        searchFilter = addObjectClassFilter(filter, objectClasses);
    } else {
        searchFilter = filter;
    }
    // Find entries
    LOG.trace("-------------------------------------------------------");
    LOG.trace("Filter: {}", filter);
    LOG.trace("objectClasses count: {} ", objectClasses.length);
    LOG.trace("objectClasses: {}", ArrayHelper.toString(objectClasses));
    LOG.trace("Search filter: {}", searchFilter);
    // Prepare default sort
    OrderSpecifier<?>[] defaultSort = getDefaultSort(entryClass);
    if (StringHelper.isNotEmpty(sortBy)) {
        OrderSpecifier<?> requestedSort = buildSort(sortBy, sortOrder);
        if (ArrayHelper.isEmpty(defaultSort)) {
            defaultSort = new OrderSpecifier[] { requestedSort };
        } else {
            defaultSort = ArrayHelper.arrayMerge(new OrderSpecifier[] { requestedSort }, defaultSort);
        }
    }
    // Prepare properties types to allow build filter properly
    Map<String, PropertyAnnotation> propertiesAnnotationsMap = prepareEntryPropertiesTypes(entryClass, propertiesAnnotations);
    ParsedKey keyWithInum = toSQLKey(baseDN);
    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 {
        SqlBatchOperationWraper<T> batchOperationWraper = null;
        if (batchOperation != null) {
            batchOperationWraper = new SqlBatchOperationWraper<T>(batchOperation, this, entryClass, propertiesAnnotations);
        }
        searchResult = searchImpl(keyWithInum.getKey(), objectClasses[0], convertedExpression, scope, currentLdapReturnAttributes, defaultSort, batchOperationWraper, returnDataType, start, count, chunkSize);
        if (searchResult == null) {
            throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s', expression: '%s'", keyWithInum.getKey(), convertedExpression));
        }
        return searchResult;
    } catch (SearchException ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s'", keyWithInum.getKey()), ex);
    } catch (Exception ex) {
        throw new EntryPersistenceException(String.format("Failed to find entries with key: '%s', expression: '%s'", keyWithInum.getKey(), convertedExpression), ex);
    }
}
Also used : EntryData(io.jans.orm.model.EntryData) 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) PropertyAnnotation(io.jans.orm.reflect.property.PropertyAnnotation) Filter(io.jans.orm.search.filter.Filter) ConvertedExpression(io.jans.orm.sql.model.ConvertedExpression) ParsedKey(io.jans.orm.impl.model.ParsedKey) OrderSpecifier(com.querydsl.core.types.OrderSpecifier)

Example 9 with EntryData

use of io.jans.orm.model.EntryData in project jans by JanssenProject.

the class SqlEntryManager method createEntities.

protected <T> List<T> createEntities(Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, ParsedKey baseDn, EntryData... searchResultEntries) {
    List<T> result = new ArrayList<T>(searchResultEntries.length);
    Map<String, List<AttributeData>> entriesAttributes = new LinkedHashMap<String, List<AttributeData>>(100);
    int count = 0;
    for (int i = 0; i < searchResultEntries.length; i++) {
        count++;
        EntryData entryData = searchResultEntries[i];
        AttributeData attributeDataDn = entryData.getAttributeDate(SqlOperationService.DN);
        if ((attributeDataDn == null) || (attributeDataDn.getValue() == null)) {
            throw new MappingException("Failed to convert EntryData to Entry because DN is missing");
        }
        entriesAttributes.put(attributeDataDn.getValue().toString(), entryData.getAttributeData());
        // Remove reference to allow java clean up object
        searchResultEntries[i] = null;
        // Allow java to clean up temporary objects
        if (count >= 100) {
            List<T> currentResult = createEntities(entryClass, propertiesAnnotations, entriesAttributes);
            result.addAll(currentResult);
            entriesAttributes = new LinkedHashMap<String, List<AttributeData>>(100);
            count = 0;
        }
    }
    List<T> currentResult = createEntities(entryClass, propertiesAnnotations, entriesAttributes);
    result.addAll(currentResult);
    return result;
}
Also used : EntryData(io.jans.orm.model.EntryData) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AttributeData(io.jans.orm.model.AttributeData) LinkedHashMap(java.util.LinkedHashMap) MappingException(io.jans.orm.exception.MappingException)

Example 10 with EntryData

use of io.jans.orm.model.EntryData in project jans by JanssenProject.

the class SpannerEntryManager method createEntities.

protected <T> List<T> createEntities(Class<T> entryClass, List<PropertyAnnotation> propertiesAnnotations, ParsedKey baseDn, EntryData... searchResultEntries) {
    List<T> result = new ArrayList<T>(searchResultEntries.length);
    Map<String, List<AttributeData>> entriesAttributes = new LinkedHashMap<String, List<AttributeData>>(100);
    int count = 0;
    for (int i = 0; i < searchResultEntries.length; i++) {
        count++;
        EntryData entryData = searchResultEntries[i];
        AttributeData attributeDataDn = entryData.getAttributeDate(SpannerOperationService.DN);
        if ((attributeDataDn == null) || (attributeDataDn.getValue() == null)) {
            throw new MappingException("Failed to convert EntryData to Entry because DN is missing");
        }
        entriesAttributes.put(attributeDataDn.getValue().toString(), entryData.getAttributeData());
        // Remove reference to allow java clean up object
        searchResultEntries[i] = null;
        // Allow java to clean up temporary objects
        if (count >= 100) {
            List<T> currentResult = createEntities(entryClass, propertiesAnnotations, entriesAttributes);
            result.addAll(currentResult);
            entriesAttributes = new LinkedHashMap<String, List<AttributeData>>(100);
            count = 0;
        }
    }
    List<T> currentResult = createEntities(entryClass, propertiesAnnotations, entriesAttributes);
    result.addAll(currentResult);
    return result;
}
Also used : EntryData(io.jans.orm.model.EntryData) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) AttributeData(io.jans.orm.model.AttributeData) LinkedHashMap(java.util.LinkedHashMap) MappingException(io.jans.orm.exception.MappingException)

Aggregations

EntryData (io.jans.orm.model.EntryData)16 MappingException (io.jans.orm.exception.MappingException)8 SearchException (io.jans.orm.exception.operation.SearchException)8 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)8 AuthenticationException (io.jans.orm.exception.AuthenticationException)6 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)6 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)6 Filter (io.jans.orm.search.filter.Filter)6 LinkedList (java.util.LinkedList)6 ParsedKey (io.jans.orm.impl.model.ParsedKey)4 AttributeData (io.jans.orm.model.AttributeData)4 ConvertedExpression (io.jans.orm.cloud.spanner.model.ConvertedExpression)3 ConvertedExpression (io.jans.orm.sql.model.ConvertedExpression)3 DateTimeParseException (java.time.format.DateTimeParseException)3 EntryConvertationException (io.jans.orm.exception.operation.EntryConvertationException)2 PagedResult (io.jans.orm.model.PagedResult)2 Duration (java.time.Duration)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2