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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations