use of org.apache.syncope.ext.scimv2.api.type.Resource in project syncope by apache.
the class AbstractService method doSearch.
@SuppressWarnings("unchecked")
protected ListResponse<R> doSearch(final Resource type, final SCIMSearchRequest request) {
if (type == null) {
throw new UnsupportedOperationException();
}
if (request.getCount() > confManager().get().getFilterMaxResults()) {
throw new BadRequestException(ErrorType.tooMany, "Too many results requested");
}
SearchCondVisitor visitor = new SearchCondVisitor(type, confManager().get());
int startIndex = request.getStartIndex() <= 1 ? 1 : (request.getStartIndex() / AnyDAO.DEFAULT_PAGE_SIZE) + 1;
int itemsPerPage = request.getCount() <= 1 ? AnyDAO.DEFAULT_PAGE_SIZE : request.getCount();
List<OrderByClause> sort;
if (request.getSortBy() == null) {
sort = Collections.<OrderByClause>emptyList();
} else {
OrderByClause clause = new OrderByClause();
clause.setField(visitor.createAttributeCond(request.getSortBy()).getSchema());
clause.setDirection(request.getSortOrder() == null || request.getSortOrder() == SortOrder.ascending ? OrderByClause.Direction.ASC : OrderByClause.Direction.DESC);
sort = Collections.singletonList(clause);
}
Pair<Integer, ? extends List<? extends AnyTO>> result = anyLogic(type).search(StringUtils.isBlank(request.getFilter()) ? null : SearchCondConverter.convert(visitor, request.getFilter()), startIndex, itemsPerPage, sort, SyncopeConstants.ROOT_REALM, false);
if (result.getLeft() > confManager().get().getFilterMaxResults()) {
throw new BadRequestException(ErrorType.tooMany, "Too many results found");
}
ListResponse<R> response = new ListResponse<>(result.getLeft(), startIndex == 1 ? 1 : startIndex - 1, itemsPerPage);
result.getRight().forEach(anyTO -> {
SCIMResource resource = null;
if (anyTO instanceof UserTO) {
resource = binder().toSCIMUser((UserTO) anyTO, uriInfo.getAbsolutePathBuilder().path(anyTO.getKey()).build().toASCIIString(), request.getAttributes(), request.getExcludedAttributes());
} else if (anyTO instanceof GroupTO) {
resource = binder().toSCIMGroup((GroupTO) anyTO, uriInfo.getAbsolutePathBuilder().path(anyTO.getKey()).build().toASCIIString(), request.getAttributes(), request.getExcludedAttributes());
}
if (resource != null) {
response.getResources().add((R) resource);
}
});
return response;
}
Aggregations