Search in sources :

Example 1 with Resource

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;
}
Also used : SCIMResource(org.apache.syncope.ext.scimv2.api.data.SCIMResource) ListResponse(org.apache.syncope.ext.scimv2.api.data.ListResponse) GroupTO(org.apache.syncope.common.lib.to.GroupTO) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) UserTO(org.apache.syncope.common.lib.to.UserTO) BadRequestException(org.apache.syncope.ext.scimv2.api.BadRequestException) SearchCondVisitor(org.apache.syncope.core.logic.scim.SearchCondVisitor)

Aggregations

GroupTO (org.apache.syncope.common.lib.to.GroupTO)1 UserTO (org.apache.syncope.common.lib.to.UserTO)1 SearchCondVisitor (org.apache.syncope.core.logic.scim.SearchCondVisitor)1 OrderByClause (org.apache.syncope.core.persistence.api.dao.search.OrderByClause)1 BadRequestException (org.apache.syncope.ext.scimv2.api.BadRequestException)1 ListResponse (org.apache.syncope.ext.scimv2.api.data.ListResponse)1 SCIMResource (org.apache.syncope.ext.scimv2.api.data.SCIMResource)1