Search in sources :

Example 11 with KeysetPage

use of com.blazebit.persistence.KeysetPage in project blaze-persistence by Blazebit.

the class KeysetPageableParamConverter method fromString.

@Override
public Pageable fromString(String value) {
    MultivaluedMap<String, String> queryParameters = requestUriInfo.getQueryParameters();
    Pageable defaultOrFallback = pageableConfiguration.getFallbackPageable();
    String offsetString = queryParameters.getFirst(pageableConfiguration.getPrefix() + pageableConfiguration.getOffsetParameterName());
    String pageString = queryParameters.getFirst(pageableConfiguration.getPrefix() + pageableConfiguration.getPageParameterName());
    String pageSizeString = queryParameters.getFirst(pageableConfiguration.getPrefix() + pageableConfiguration.getSizeParameterName());
    boolean pageAndSizeGiven = (!StringUtils.isEmpty(pageString) || !StringUtils.isEmpty(offsetString)) && !StringUtils.isEmpty(pageSizeString);
    if (!pageAndSizeGiven && defaultOrFallback == null) {
        return null;
    }
    int maxPageSize = pageableConfiguration.getMaxPageSize();
    int pageSize = !StringUtils.isEmpty(pageSizeString) ? parseAndApplyBoundaries(pageSizeString, maxPageSize, false) : defaultOrFallback.getPageSize();
    // Limit lower bound
    pageSize = pageSize < 1 ? defaultOrFallback.getPageSize() : pageSize;
    // Limit upper bound
    pageSize = pageSize > maxPageSize ? maxPageSize : pageSize;
    int offset;
    if (!StringUtils.isEmpty(offsetString)) {
        offset = parseAndApplyBoundaries(pageString, Integer.MAX_VALUE, false);
    } else if (!StringUtils.isEmpty(pageString)) {
        offset = pageSize * parseAndApplyBoundaries(pageString, Integer.MAX_VALUE, true);
    } else {
        offset = pageSize * defaultOrFallback.getPageNumber();
    }
    Sort sort = resolveSort(queryParameters.get(pageableConfiguration.getSortParameterName()));
    // Default if necessary and default configured
    sort = sort == null && defaultOrFallback != null ? defaultOrFallback.getSort() : sort;
    KeysetPage keysetPage = null;
    Iterator<Sort.Order> iterator;
    if (keysetClass != null) {
        if (sort != null && (iterator = sort.iterator()).hasNext()) {
            String previousOffsetString = queryParameters.getFirst(keysetPageableConfiguration.getPrefix() + keysetPageableConfiguration.getPreviousOffsetParameterName());
            String previousPageString = queryParameters.getFirst(keysetPageableConfiguration.getPrefix() + keysetPageableConfiguration.getPreviousPageParameterName());
            if (!StringUtils.isEmpty(previousOffsetString) || !StringUtils.isEmpty(previousPageString)) {
                String previousPageSizeString = queryParameters.getFirst(keysetPageableConfiguration.getPrefix() + keysetPageableConfiguration.getPreviousSizeParameterName());
                int previousPageSize = StringUtils.isEmpty(previousPageSizeString) ? pageSize : parseAndApplyBoundaries(previousPageSizeString, maxPageSize, false);
                int previousOffset;
                if (!StringUtils.isEmpty(previousOffsetString)) {
                    previousOffset = parseAndApplyBoundaries(previousOffsetString, Integer.MAX_VALUE, false);
                } else {
                    int previousPage = parseAndApplyBoundaries(previousPageString, Integer.MAX_VALUE, true);
                    previousOffset = previousPage * previousPageSize;
                }
                String lowestString = queryParameters.getFirst(keysetPageableConfiguration.getPrefix() + keysetPageableConfiguration.getLowestParameterName());
                String highestString = queryParameters.getFirst(keysetPageableConfiguration.getPrefix() + keysetPageableConfiguration.getHighestParameterName());
                if (!StringUtils.isEmpty(lowestString) && !StringUtils.isEmpty(highestString)) {
                    List<Serializable> lowest = new ArrayList<>();
                    List<Serializable> highest = new ArrayList<>();
                    JsonNode lowestObject;
                    JsonNode highestObject;
                    try {
                        lowestObject = mapper.readTree(lowestString);
                    } catch (IOException ex) {
                        throw new IllegalArgumentException("Invalid lowest object!", ex);
                    }
                    try {
                        highestObject = mapper.readTree(highestString);
                    } catch (IOException ex) {
                        throw new IllegalArgumentException("Invalid highest object!", ex);
                    }
                    while (iterator.hasNext()) {
                        Sort.Order o = iterator.next();
                        JsonNode low = lowestObject;
                        JsonNode high = highestObject;
                        String[] propertyParts = o.getPath().split("\\.");
                        Class<? extends Serializable> propertyType = getPropertyType(keysetClass, o.getPath());
                        for (int i = 0; i < propertyParts.length; i++) {
                            low = low == null ? null : low.get(propertyParts[i]);
                            high = high == null ? null : high.get(propertyParts[i]);
                        }
                        lowest.add(low == null ? null : convert(low, propertyType));
                        highest.add(high == null ? null : convert(high, propertyType));
                    }
                    keysetPage = new DefaultKeysetPage(previousOffset, previousPageSize, new DefaultKeyset(lowest.toArray(new Serializable[lowest.size()])), new DefaultKeyset(highest.toArray(new Serializable[highest.size()])));
                }
            }
        }
        return new KeysetPageRequest(keysetPage, sort, offset, pageSize);
    }
    return new PageRequest(sort, offset, pageSize);
}
Also used : Serializable(java.io.Serializable) DefaultKeysetPage(com.blazebit.persistence.DefaultKeysetPage) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) KeysetPage(com.blazebit.persistence.KeysetPage) DefaultKeysetPage(com.blazebit.persistence.DefaultKeysetPage) KeysetPageRequest(com.blazebit.persistence.deltaspike.data.KeysetPageRequest) PageRequest(com.blazebit.persistence.deltaspike.data.PageRequest) Pageable(com.blazebit.persistence.deltaspike.data.Pageable) DefaultKeyset(com.blazebit.persistence.DefaultKeyset) KeysetPageRequest(com.blazebit.persistence.deltaspike.data.KeysetPageRequest) Sort(com.blazebit.persistence.deltaspike.data.Sort)

Example 12 with KeysetPage

use of com.blazebit.persistence.KeysetPage in project blaze-persistence by Blazebit.

the class GraphQLEntityViewSupport method createRelayConnection.

/**
 * Returns a relay connection from the given result list.
 *
 * @param list the result list
 * @return the relay connection
 */
public <T> Connection<T> createRelayConnection(List<T> list) {
    List<Edge<T>> edges = new ArrayList<>(list.size());
    boolean hasPreviousPage;
    boolean hasNextPage;
    KeysetPage keysetPage;
    if (list instanceof PagedList<?>) {
        PagedList<T> data = (PagedList<T>) list;
        hasPreviousPage = data.getFirstResult() != 0;
        hasNextPage = data.getTotalSize() == -1 || data.getFirstResult() + data.getMaxResults() < data.getTotalSize();
        keysetPage = data.getKeysetPage();
    } else {
        hasPreviousPage = true;
        hasNextPage = true;
        keysetPage = null;
    }
    if (keysetPage == null) {
        for (int i = 0; i < list.size(); i++) {
            edges.add(new DefaultEdge<>(list.get(i), new DefaultConnectionCursor(Integer.toString(i + 1))));
        }
    } else {
        PagedList<T> data = (PagedList<T>) list;
        List<Keyset> keysets = keysetPage.getKeysets();
        int listSize = list.size();
        if (listSize != 0 && keysets.size() != listSize) {
            int end = listSize - 1;
            edges.add(new DefaultEdge<>(list.get(0), new DefaultConnectionCursor(Base64.getEncoder().encodeToString(serializeCursor(data.getFirstResult(), data.getMaxResults(), keysetPage.getLowest().getTuple())))));
            for (int i = 1; i < end; i++) {
                T node = list.get(i);
                edges.add(new DefaultEdge<>(node, new DefaultConnectionCursor(Integer.toString(i + 1))));
            }
            edges.add(new DefaultEdge<>(list.get(end), new DefaultConnectionCursor(Base64.getEncoder().encodeToString(serializeCursor(data.getFirstResult(), data.getMaxResults(), keysetPage.getHighest().getTuple())))));
        } else {
            for (int i = 0; i < list.size(); i++) {
                T node = list.get(i);
                edges.add(new DefaultEdge<>(node, new DefaultConnectionCursor(Base64.getEncoder().encodeToString(serializeCursor(data.getFirstResult(), data.getMaxResults(), keysets.get(i).getTuple())))));
            }
        }
    }
    PageInfo pageInfo;
    if (edges.isEmpty()) {
        pageInfo = new DefaultPageInfo(null, null, hasPreviousPage, hasNextPage);
    } else {
        pageInfo = new DefaultPageInfo(edges.get(0).getCursor(), edges.get(edges.size() - 1).getCursor(), hasPreviousPage, hasNextPage);
    }
    return new DefaultConnection<>(edges, pageInfo);
}
Also used : Keyset(com.blazebit.persistence.Keyset) DefaultKeyset(com.blazebit.persistence.DefaultKeyset) PagedList(com.blazebit.persistence.PagedList) ArrayList(java.util.ArrayList) KeysetPage(com.blazebit.persistence.KeysetPage) DefaultKeysetPage(com.blazebit.persistence.DefaultKeysetPage) PageInfo(graphql.relay.PageInfo) DefaultPageInfo(graphql.relay.DefaultPageInfo) DefaultConnection(graphql.relay.DefaultConnection) DefaultEdge(graphql.relay.DefaultEdge) Edge(graphql.relay.Edge) DefaultPageInfo(graphql.relay.DefaultPageInfo) DefaultConnectionCursor(graphql.relay.DefaultConnectionCursor)

Example 13 with KeysetPage

use of com.blazebit.persistence.KeysetPage in project blaze-persistence by Blazebit.

the class KeysetPageableHandlerMethodArgumentResolver method resolveArgument.

@Override
public KeysetPageable resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) {
    assertPageableUniqueness(methodParameter);
    Pageable defaultOrFallback = getDefaultFromAnnotationOrFallback(methodParameter);
    String pageString = webRequest.getParameter(getParameterNameToUse(getPageParameterName(), methodParameter));
    String offsetString = webRequest.getParameter(getParameterNameToUse(getOffsetParameterName(), methodParameter));
    String pageSizeString = webRequest.getParameter(getParameterNameToUse(getSizeParameterName(), methodParameter));
    boolean pageAndSizeGiven = (StringUtils.hasText(pageString) || StringUtils.hasText(offsetString)) && StringUtils.hasText(pageSizeString);
    if (!pageAndSizeGiven && defaultOrFallback == null) {
        return null;
    }
    int pageSize = StringUtils.hasText(pageSizeString) ? parseAndApplyBoundaries(pageSizeString, getMaxPageSize(), false) : defaultOrFallback.getPageSize();
    // Limit lower bound
    pageSize = pageSize < 1 ? defaultOrFallback.getPageSize() : pageSize;
    // Limit upper bound
    pageSize = pageSize > getMaxPageSize() ? getMaxPageSize() : pageSize;
    int offset;
    if (StringUtils.hasText(offsetString)) {
        offset = parseAndApplyBoundaries(offsetString, Integer.MAX_VALUE, false);
    } else if (StringUtils.hasText(pageString)) {
        offset = pageSize * parseAndApplyBoundaries(pageString, Integer.MAX_VALUE, true);
    } else {
        offset = pageSize * defaultOrFallback.getPageNumber();
    }
    org.springframework.data.domain.Sort sort = sortResolver.resolveArgument(methodParameter, mavContainer, webRequest, binderFactory);
    // Default if necessary and default configured
    sort = sort == UNSORTED && defaultOrFallback != null ? defaultOrFallback.getSort() : sort;
    KeysetPage keysetPage = null;
    Iterator<org.springframework.data.domain.Sort.Order> iterator;
    if (sort != null && (iterator = sort.iterator()).hasNext()) {
        KeysetConfig keysetConfig = methodParameter.getParameterAnnotation(KeysetConfig.class);
        Class<?> domainClass = keysetConfig.keysetClass();
        if (domainClass == void.class) {
            domainClass = keysetConfig.value();
        }
        if (domainClass == void.class) {
            Method annotatedMethod = methodParameter.getMethod();
            throw new IllegalStateException(String.format(INVALID_KEYSET_DOMAIN_CLASS, annotatedMethod));
        }
        String previousOffsetName = getParameterName(keysetConfig.previousOffsetName(), getParameterNameToUse(getPreviousOffsetParameterName(), methodParameter));
        String previousOffsetString = webRequest.getParameter(previousOffsetName);
        String previousPageName = getParameterName(keysetConfig.previousPageName(), getParameterNameToUse(getPreviousPageParameterName(), methodParameter));
        String previousPageString = webRequest.getParameter(previousPageName);
        if (StringUtils.hasText(previousOffsetString) || StringUtils.hasText(previousPageString)) {
            String previousPageSizeName = getParameterName(keysetConfig.previousPageSizeName(), getParameterNameToUse(getPreviousSizeParameterName(), methodParameter));
            String previousPageSizeString = webRequest.getParameter(previousPageSizeName);
            int previousPageSize = StringUtils.hasText(previousPageSizeString) ? parseAndApplyBoundaries(previousPageSizeString, getMaxPageSize(), false) : pageSize;
            int previousOffset;
            if (StringUtils.hasText(previousOffsetString)) {
                previousOffset = parseAndApplyBoundaries(previousOffsetString, Integer.MAX_VALUE, false);
            } else {
                int previousPage = parseAndApplyBoundaries(previousPageString, Integer.MAX_VALUE, true);
                previousOffset = previousPage * previousPageSize;
            }
            String lowestName = getParameterName(keysetConfig.lowestName(), getParameterNameToUse(getLowestParameterName(), methodParameter));
            String lowestString = webRequest.getParameter(lowestName);
            String highestName = getParameterName(keysetConfig.highestName(), getParameterNameToUse(getHighestParameterName(), methodParameter));
            String highestString = webRequest.getParameter(highestName);
            if (StringUtils.hasText(lowestString) && StringUtils.hasText(highestString)) {
                List<Serializable> lowest = new ArrayList<>();
                List<Serializable> highest = new ArrayList<>();
                JsonNode lowestObject;
                JsonNode highestObject;
                try {
                    lowestObject = mapper.readTree(lowestString);
                } catch (IOException ex) {
                    throw new IllegalArgumentException("Invalid lowest object!", ex);
                }
                try {
                    highestObject = mapper.readTree(highestString);
                } catch (IOException ex) {
                    throw new IllegalArgumentException("Invalid highest object!", ex);
                }
                while (iterator.hasNext()) {
                    org.springframework.data.domain.Sort.Order o = iterator.next();
                    JsonNode low = lowestObject;
                    JsonNode high = highestObject;
                    String[] propertyParts = o.getProperty().split("\\.");
                    Class<? extends Serializable> propertyType = getPropertyType(domainClass, o.getProperty());
                    for (int i = 0; i < propertyParts.length; i++) {
                        low = low == null ? null : low.get(propertyParts[i]);
                        high = high == null ? null : high.get(propertyParts[i]);
                    }
                    lowest.add(low == null ? null : convert(low, propertyType));
                    highest.add(high == null ? null : convert(high, propertyType));
                }
                keysetPage = new DefaultKeysetPage(previousOffset, previousPageSize, new DefaultKeyset(lowest.toArray(new Serializable[lowest.size()])), new DefaultKeyset(highest.toArray(new Serializable[highest.size()])));
            }
        }
    }
    return new KeysetPageRequest(keysetPage, sort, offset, pageSize);
}
Also used : Serializable(java.io.Serializable) DefaultKeysetPage(com.blazebit.persistence.DefaultKeysetPage) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) KeysetPageable(com.blazebit.persistence.spring.data.repository.KeysetPageable) Pageable(org.springframework.data.domain.Pageable) DefaultKeyset(com.blazebit.persistence.DefaultKeyset) Method(java.lang.reflect.Method) IOException(java.io.IOException) KeysetConfig(com.blazebit.persistence.spring.data.webmvc.KeysetConfig) KeysetPage(com.blazebit.persistence.KeysetPage) DefaultKeysetPage(com.blazebit.persistence.DefaultKeysetPage) KeysetPageRequest(com.blazebit.persistence.spring.data.repository.KeysetPageRequest)

Aggregations

KeysetPage (com.blazebit.persistence.KeysetPage)13 DefaultKeysetPage (com.blazebit.persistence.DefaultKeysetPage)8 DefaultKeyset (com.blazebit.persistence.DefaultKeyset)5 PagedList (com.blazebit.persistence.PagedList)5 ArrayList (java.util.ArrayList)5 Serializable (java.io.Serializable)4 List (java.util.List)4 ObjectBuilder (com.blazebit.persistence.ObjectBuilder)3 SelectBuilder (com.blazebit.persistence.SelectBuilder)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 KeysetPageRequest (com.blazebit.persistence.spring.data.repository.KeysetPageRequest)2 KeysetPageable (com.blazebit.persistence.spring.data.repository.KeysetPageable)2 Document (com.blazebit.persistence.testsuite.entity.Document)2 Method (java.lang.reflect.Method)2 Pageable (org.springframework.data.domain.Pageable)2 Keyset (com.blazebit.persistence.Keyset)1 PagedArrayList (com.blazebit.persistence.PagedArrayList)1 KeysetPageRequest (com.blazebit.persistence.deltaspike.data.KeysetPageRequest)1