Search in sources :

Example 1 with PagedResult

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

the class SpannerOperationServiceImpl method searchImpl.

private <O> PagedResult<EntryData> searchImpl(TableMapping tableMapping, String key, ConvertedExpression expression, SearchScope scope, String[] attributes, Sort[] orderBy, SpannerBatchOperationWraper<O> batchOperationWraper, SearchReturnDataType returnDataType, int start, int count, int pageSize) throws SearchException {
    BatchOperation<O> batchOperation = null;
    if (batchOperationWraper != null) {
        batchOperation = (BatchOperation<O>) batchOperationWraper.getBatchOperation();
    }
    Table table = buildTable(tableMapping);
    PlainSelect sqlSelectQuery = new PlainSelect();
    sqlSelectQuery.setFromItem(table);
    List<SelectItem> selectItems = buildSelectAttributes(tableMapping, key, attributes);
    sqlSelectQuery.addSelectItems(selectItems);
    if (expression != null) {
        applyWhereExpression(sqlSelectQuery, expression);
    }
    if (orderBy != null) {
        OrderByElement[] orderByElements = new OrderByElement[orderBy.length];
        for (int i = 0; i < orderBy.length; i++) {
            Column column = new Column(orderBy[i].getName());
            orderByElements[i] = new OrderByElement();
            orderByElements[i].setExpression(column);
            if (orderBy[i].getSortOrder() != null) {
                orderByElements[i].setAscDescPresent(true);
                orderByElements[i].setAsc(SortOrder.ASCENDING == orderBy[i].getSortOrder());
            }
        }
        sqlSelectQuery.withOrderByElements(Arrays.asList(orderByElements));
    }
    List<EntryData> searchResultList = new LinkedList<EntryData>();
    if ((SearchReturnDataType.SEARCH == returnDataType) || (SearchReturnDataType.SEARCH_COUNT == returnDataType)) {
        List<EntryData> lastResult = null;
        if (pageSize > 0) {
            boolean collectSearchResult;
            Limit limit = new Limit();
            sqlSelectQuery.setLimit(limit);
            Offset offset = new Offset();
            sqlSelectQuery.setOffset(offset);
            int currentLimit;
            try {
                int resultCount = 0;
                int lastCountRows = 0;
                do {
                    collectSearchResult = true;
                    currentLimit = pageSize;
                    if (count > 0) {
                        currentLimit = Math.min(pageSize, count - resultCount);
                    }
                    // Change limit and offset
                    limit.setRowCount(new LongValue(currentLimit));
                    offset.setOffset(start + resultCount);
                    Statement.Builder statementBuilder = Statement.newBuilder(sqlSelectQuery.toString());
                    applyParametersBinding(statementBuilder, expression);
                    Statement statement = statementBuilder.build();
                    LOG.debug("Executing query: '{}'", statement);
                    try (ResultSet resultSet = databaseClient.singleUse().executeQuery(statement)) {
                        lastResult = getEntryDataList(tableMapping.getObjectClass(), 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)) {
                        break;
                    }
                } while (lastCountRows > 0);
            } catch (SpannerException | EntryConvertationException | IncompatibleTypeException ex) {
                LOG.error("Failed to execute query with expression: '{}'", expression);
                throw new SearchException(String.format("Failed to execute query '%s'  with key: '%s'", sqlSelectQuery, key), ex);
            }
        } else {
            try {
                long currentLimit = count;
                if (currentLimit <= 0) {
                    currentLimit = connectionProvider.getDefaultMaximumResultSize();
                }
                Limit limit = new Limit();
                limit.setRowCount(new LongValue(currentLimit));
                sqlSelectQuery.setLimit(limit);
                if (start > 0) {
                    Offset offset = new Offset();
                    offset.setOffset(start);
                    sqlSelectQuery.setOffset(offset);
                }
                Statement.Builder statementBuilder = Statement.newBuilder(sqlSelectQuery.toString());
                applyParametersBinding(statementBuilder, expression);
                Statement statement = statementBuilder.build();
                LOG.debug("Executing query: '{}'", statement);
                try (ResultSet resultSet = databaseClient.singleUse().executeQuery(statement)) {
                    lastResult = getEntryDataList(tableMapping.getObjectClass(), resultSet);
                    searchResultList.addAll(lastResult);
                }
            } catch (SpannerException | EntryConvertationException | IncompatibleTypeException ex) {
                LOG.error("Failed to execute query with expression: '{}'", expression);
                throw new SearchException(String.format("Failed to execute query '%s'  with key: '%s'", sqlSelectQuery, key), 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)) {
        PlainSelect sqlCountSelectQuery = new PlainSelect();
        sqlCountSelectQuery.setFromItem(table);
        Function countFunction = new Function();
        countFunction.setName("COUNT");
        countFunction.setAllColumns(true);
        SelectExpressionItem selectCountItem = new SelectExpressionItem(countFunction);
        selectCountItem.setAlias(new Alias("TOTAL", false));
        sqlCountSelectQuery.addSelectItems(selectCountItem);
        if (expression != null) {
            applyWhereExpression(sqlCountSelectQuery, expression);
        }
        try {
            Statement.Builder statementBuilder = Statement.newBuilder(sqlCountSelectQuery.toString());
            applyParametersBinding(statementBuilder, expression);
            Statement statement = statementBuilder.build();
            LOG.debug("Calculating count. Executing query: '{}'", statement);
            try (ResultSet countResult = databaseClient.singleUse().executeQuery(statement)) {
                if (!countResult.next()) {
                    throw new SearchException(String.format("Failed to calculate count entries. Query: '%s'", statement));
                }
                result.setTotalEntriesCount((int) countResult.getLong("TOTAL"));
            }
        } catch (SpannerException | IncompatibleTypeException ex) {
            LOG.error("Failed to execute query with expression: '{}'", expression);
            throw new SearchException(String.format("Failed to build count search entries query. Key: '%s', expression: '%s'", key, expression.expression()), ex);
        }
    }
    return result;
}
Also used : EntryData(io.jans.orm.model.EntryData) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SearchException(io.jans.orm.exception.operation.SearchException) Function(net.sf.jsqlparser.expression.Function) Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) ResultSet(com.google.cloud.spanner.ResultSet) IncompatibleTypeException(io.jans.orm.exception.operation.IncompatibleTypeException) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement) PagedResult(io.jans.orm.model.PagedResult) Table(net.sf.jsqlparser.schema.Table) Statement(com.google.cloud.spanner.Statement) LinkedList(java.util.LinkedList) Offset(net.sf.jsqlparser.statement.select.Offset) Alias(net.sf.jsqlparser.expression.Alias) LongValue(net.sf.jsqlparser.expression.LongValue) EntryConvertationException(io.jans.orm.exception.operation.EntryConvertationException) Limit(net.sf.jsqlparser.statement.select.Limit) SpannerException(com.google.cloud.spanner.SpannerException) Builder(com.google.cloud.spanner.Statement.Builder)

Example 2 with PagedResult

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

the class FidoDeviceWebService method searchDevices.

private PagedResult<BaseScimResource> searchDevices(String userId, String filter, String sortBy, SortOrder sortOrder, int startIndex, int count) throws Exception {
    Filter ldapFilter = scimFilterParserService.createFilter(filter, Filter.createPresenceFilter("jansId"), FidoDeviceResource.class);
    log.info("Executing search for fido devices using: ldapfilter '{}', sortBy '{}', sortOrder '{}', startIndex '{}', count '{}', userId '{}'", ldapFilter.toString(), sortBy, sortOrder.getValue(), startIndex, count, userId);
    // Currently, searching with SUB scope in Couchbase requires some help (beyond use of baseDN)
    if (StringUtils.isNotEmpty(userId)) {
        ldapFilter = Filter.createANDFilter(ldapFilter, Filter.createEqualityFilter("personInum", userId));
    }
    PagedResult<GluuCustomFidoDevice> list;
    try {
        list = entryManager.findPagedEntries(fidoDeviceService.getDnForFidoDevice(userId, null), GluuCustomFidoDevice.class, ldapFilter, null, sortBy, sortOrder, startIndex - 1, count, getMaxCount());
    } catch (Exception e) {
        log.info("Returning an empty listViewReponse");
        log.error(e.getMessage(), e);
        list = new PagedResult<>();
        list.setEntries(new ArrayList<>());
    }
    List<BaseScimResource> resources = new ArrayList<>();
    for (GluuCustomFidoDevice device : list.getEntries()) {
        FidoDeviceResource scimDev = new FidoDeviceResource();
        transferAttributesToFidoResource(device, scimDev, endpointUrl, userPersistenceHelper.getUserInumFromDN(device.getDn()));
        resources.add(scimDev);
    }
    log.info("Found {} matching entries - returning {}", list.getTotalEntriesCount(), list.getEntries().size());
    PagedResult<BaseScimResource> result = new PagedResult<>();
    result.setEntries(resources);
    result.setTotalEntriesCount(list.getTotalEntriesCount());
    return result;
}
Also used : GluuCustomFidoDevice(io.jans.scim.model.fido.GluuCustomFidoDevice) Filter(io.jans.orm.search.filter.Filter) FidoDeviceResource(io.jans.scim.model.scim2.fido.FidoDeviceResource) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) SCIMException(io.jans.scim.model.exception.SCIMException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) PagedResult(io.jans.orm.model.PagedResult)

Example 3 with PagedResult

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

the class Scim2GroupService method searchGroups.

public PagedResult<BaseScimResource> searchGroups(String filter, String sortBy, SortOrder sortOrder, int startIndex, int count, String groupsUrl, String usersUrl, int maxCount, boolean fillMembersDisplay) throws Exception {
    Filter ldapFilter = scimFilterParserService.createFilter(filter, Filter.createPresenceFilter("inum"), GroupResource.class);
    log.info("Executing search for groups using: ldapfilter '{}', sortBy '{}', sortOrder '{}', startIndex '{}', count '{}'", ldapFilter.toString(), sortBy, sortOrder.getValue(), startIndex, count);
    PagedResult<GluuGroup> list = ldapEntryManager.findPagedEntries(groupService.getDnForGroup(null), GluuGroup.class, ldapFilter, null, sortBy, sortOrder, startIndex - 1, count, maxCount);
    List<BaseScimResource> resources = new ArrayList<>();
    if (externalScimService.isEnabled() && !externalScimService.executeScimPostSearchGroupsMethods(list)) {
        throw new WebApplicationException("Failed to execute SCIM script successfully", Status.PRECONDITION_FAILED);
    }
    for (GluuGroup group : list.getEntries()) {
        GroupResource scimGroup = new GroupResource();
        transferAttributesToGroupResource(group, scimGroup, fillMembersDisplay, groupsUrl, usersUrl);
        resources.add(scimGroup);
    }
    log.info("Found {} matching entries - returning {}", list.getTotalEntriesCount(), list.getEntries().size());
    PagedResult<BaseScimResource> result = new PagedResult<>();
    result.setEntries(resources);
    result.setTotalEntriesCount(list.getTotalEntriesCount());
    return result;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) Filter(io.jans.orm.search.filter.Filter) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) ArrayList(java.util.ArrayList) GluuGroup(io.jans.scim.model.GluuGroup) GroupResource(io.jans.scim.model.scim2.group.GroupResource) PagedResult(io.jans.orm.model.PagedResult)

Example 4 with PagedResult

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

the class Scim2UserService method searchUsers.

public PagedResult<BaseScimResource> searchUsers(String filter, String sortBy, SortOrder sortOrder, int startIndex, int count, String url, int maxCount) throws Exception {
    Filter ldapFilter = scimFilterParserService.createFilter(filter, Filter.createPresenceFilter("inum"), UserResource.class);
    log.info("Executing search for users using: ldapfilter '{}', sortBy '{}', sortOrder '{}', startIndex '{}', count '{}'", ldapFilter.toString(), sortBy, sortOrder.getValue(), startIndex, count);
    PagedResult<ScimCustomPerson> list = ldapEntryManager.findPagedEntries(personService.getDnForPerson(null), ScimCustomPerson.class, ldapFilter, null, sortBy, sortOrder, startIndex - 1, count, maxCount);
    List<BaseScimResource> resources = new ArrayList<>();
    if (externalScimService.isEnabled() && !externalScimService.executeScimPostSearchUsersMethods(list)) {
        throw new WebApplicationException("Failed to execute SCIM script successfully", Status.PRECONDITION_FAILED);
    }
    for (ScimCustomPerson person : list.getEntries()) {
        UserResource scimUsr = new UserResource();
        transferAttributesToUserResource(person, scimUsr, url);
        resources.add(scimUsr);
    }
    log.info("Found {} matching entries - returning {}", list.getTotalEntriesCount(), list.getEntries().size());
    PagedResult<BaseScimResource> result = new PagedResult<>();
    result.setEntries(resources);
    result.setTotalEntriesCount(list.getTotalEntriesCount());
    return result;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) Filter(io.jans.orm.search.filter.Filter) ScimCustomPerson(io.jans.scim.model.scim.ScimCustomPerson) BaseScimResource(io.jans.scim.model.scim2.BaseScimResource) ArrayList(java.util.ArrayList) UserResource(io.jans.scim.model.scim2.user.UserResource) PagedResult(io.jans.orm.model.PagedResult)

Example 5 with PagedResult

use of io.jans.orm.model.PagedResult 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)

Aggregations

PagedResult (io.jans.orm.model.PagedResult)8 ArrayList (java.util.ArrayList)6 Filter (io.jans.orm.search.filter.Filter)5 SearchException (io.jans.orm.exception.operation.SearchException)4 BaseScimResource (io.jans.scim.model.scim2.BaseScimResource)3 AuthenticationException (io.jans.orm.exception.AuthenticationException)2 ConnectionException (io.jans.orm.exception.operation.ConnectionException)2 EntryConvertationException (io.jans.orm.exception.operation.EntryConvertationException)2 EntryData (io.jans.orm.model.EntryData)2 SCIMException (io.jans.scim.model.exception.SCIMException)2 URISyntaxException (java.net.URISyntaxException)2 LinkedList (java.util.LinkedList)2 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 CouchbaseException (com.couchbase.client.core.CouchbaseException)1 Bucket (com.couchbase.client.java.Bucket)1 JsonObject (com.couchbase.client.java.document.json.JsonObject)1 N1qlQueryResult (com.couchbase.client.java.query.N1qlQueryResult)1 N1qlQueryRow (com.couchbase.client.java.query.N1qlQueryRow)1 Statement (com.couchbase.client.java.query.Statement)1