Search in sources :

Example 1 with OrderCriteria

use of org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria in project dhis2-core by dhis2.

the class DimensionFilteringAndPagingService method sortedAndPagedStream.

private Stream<DimensionResponse> sortedAndPagedStream(Stream<DimensionResponse> dimensions, DimensionsCriteria pagingAndSortingCriteria) {
    if (Objects.nonNull(pagingAndSortingCriteria.getOrder()) && !pagingAndSortingCriteria.getOrder().isEmpty()) {
        OrderCriteria orderCriteria = pagingAndSortingCriteria.getOrder().get(0);
        Comparator<DimensionResponse> comparator = ORDERING_MAP.keySet().stream().filter(key -> key.equalsIgnoreCase(orderCriteria.getField())).map(ORDERING_MAP::get).findFirst().orElse(DEFAULT_COMPARATOR);
        if (Objects.nonNull(orderCriteria.getDirection()) && !orderCriteria.getDirection().isAscending()) {
            dimensions = dimensions.sorted(comparator.reversed());
        } else {
            dimensions = dimensions.sorted(comparator);
        }
    } else {
        dimensions = dimensions.sorted(DEFAULT_COMPARATOR);
    }
    if (pagingAndSortingCriteria.isPaging()) {
        dimensions = dimensions.skip(pagingAndSortingCriteria.getFirstResult()).limit(pagingAndSortingCriteria.getPageSize());
    }
    return dimensions;
}
Also used : FieldFilterService(org.hisp.dhis.fieldfiltering.FieldFilterService) Comparator.nullsFirst(java.util.Comparator.nullsFirst) Comparator.naturalOrder(java.util.Comparator.naturalOrder) AnalyticsDimensionsPagingWrapper(org.hisp.dhis.analytics.dimensions.AnalyticsDimensionsPagingWrapper) NonNull(lombok.NonNull) Collection(java.util.Collection) Pager(org.hisp.dhis.common.Pager) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Collectors(java.util.stream.Collectors) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Objects(java.util.Objects) List(java.util.List) OrderCriteria(org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria) Stream(java.util.stream.Stream) FieldFilterParams(org.hisp.dhis.fieldfiltering.FieldFilterParams) Service(org.springframework.stereotype.Service) Map(java.util.Map) Optional(java.util.Optional) DimensionsCriteria(org.hisp.dhis.common.DimensionsCriteria) Comparator.comparing(java.util.Comparator.comparing) Comparator(java.util.Comparator) OrderCriteria(org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria)

Aggregations

ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Collection (java.util.Collection)1 Comparator (java.util.Comparator)1 Comparator.comparing (java.util.Comparator.comparing)1 Comparator.naturalOrder (java.util.Comparator.naturalOrder)1 Comparator.nullsFirst (java.util.Comparator.nullsFirst)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 NonNull (lombok.NonNull)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 AnalyticsDimensionsPagingWrapper (org.hisp.dhis.analytics.dimensions.AnalyticsDimensionsPagingWrapper)1 DimensionsCriteria (org.hisp.dhis.common.DimensionsCriteria)1 Pager (org.hisp.dhis.common.Pager)1 FieldFilterParams (org.hisp.dhis.fieldfiltering.FieldFilterParams)1 FieldFilterService (org.hisp.dhis.fieldfiltering.FieldFilterService)1 OrderCriteria (org.hisp.dhis.webapi.controller.event.webrequest.OrderCriteria)1