Search in sources :

Example 1 with KeysetPageRequest

use of com.blazebit.persistence.spring.data.repository.KeysetPageRequest in project blaze-persistence by Blazebit.

the class ReadOnlyDocumentRepositoryTest method testFindByNameInKeysetPaginated.

@Test
public void testFindByNameInKeysetPaginated() {
    // ignored with EclipseLink due to IN collection rendering bug
    Assume.assumeFalse(isEntityRepository() && isEclipseLink());
    // Given
    final Document d1 = createDocument("d1");
    final Document d2 = createDocument("d2");
    final Document d3 = createDocument("d3");
    // When
    KeysetAwarePage<DocumentAccessor> actual = DocumentAccessors.of(readOnlyDocumentRepository.findByNameIn(new KeysetPageRequest(null, Sort.asc("id"), 0, 1), d2.getName(), d3.getName()));
    List<Long> actualIds = getIdsFromViews(actual);
    // Then
    assertEquals(2, actual.getTotalPages());
    assertEquals(0, actual.getNumber());
    assertEquals(1, actual.getNumberOfElements());
    assertEquals(1, actual.getSize());
    assertTrue(actualIds.contains(d2.getId()));
    actual = DocumentAccessors.of(readOnlyDocumentRepository.findByNameIn(actual.nextPageable(), d2.getName(), d3.getName()));
    actualIds = getIdsFromViews(actual);
    assertEquals(2, actual.getTotalPages());
    assertEquals(1, actual.getNumber());
    assertEquals(1, actual.getNumberOfElements());
    assertEquals(1, actual.getSize());
    assertTrue(actualIds.contains(d3.getId()));
}
Also used : DocumentAccessor(com.blazebit.persistence.spring.data.testsuite.webmvc.accessor.DocumentAccessor) KeysetPageRequest(com.blazebit.persistence.spring.data.repository.KeysetPageRequest) Document(com.blazebit.persistence.spring.data.testsuite.webmvc.entity.Document) Test(org.junit.Test)

Example 2 with KeysetPageRequest

use of com.blazebit.persistence.spring.data.repository.KeysetPageRequest in project blaze-persistence by Blazebit.

the class KeysetPageableHandlerMethodArgumentResolver method getDefaultPageRequestFrom.

private static KeysetPageable getDefaultPageRequestFrom(MethodParameter parameter) {
    PageableDefault defaults = parameter.getParameterAnnotation(PageableDefault.class);
    Integer defaultPageNumber = defaults.page();
    Integer defaultPageSize = getSpecificPropertyOrDefaultFromValue(defaults, "size");
    if (defaultPageSize < 1) {
        Method annotatedMethod = parameter.getMethod();
        throw new IllegalStateException(String.format(INVALID_DEFAULT_PAGE_SIZE, annotatedMethod));
    }
    if (defaults.sort().length == 0) {
        return new KeysetPageRequest(defaultPageNumber, defaultPageSize, null, null);
    }
    return new KeysetPageRequest(defaultPageNumber, defaultPageSize, null, Sort.of(defaults.direction(), defaults.sort()));
}
Also used : BigInteger(java.math.BigInteger) PageableDefault(org.springframework.data.web.PageableDefault) KeysetPageRequest(com.blazebit.persistence.spring.data.repository.KeysetPageRequest) Method(java.lang.reflect.Method)

Example 3 with KeysetPageRequest

use of com.blazebit.persistence.spring.data.repository.KeysetPageRequest in project blaze-persistence by Blazebit.

the class KeysetPageableHandlerMethodArgumentResolver method resolveArgument.

@Override
public Mono<Object> resolveArgument(MethodParameter methodParameter, BindingContext bindingContext, ServerWebExchange serverWebExchange) {
    assertPageableUniqueness(methodParameter);
    Pageable defaultOrFallback = getDefaultFromAnnotationOrFallback(methodParameter);
    String pageString = serverWebExchange.getRequest().getQueryParams().getFirst(getParameterNameToUse(getPageParameterName(), methodParameter));
    String offsetString = serverWebExchange.getRequest().getQueryParams().getFirst(getParameterNameToUse(getOffsetParameterName(), methodParameter));
    String pageSizeString = serverWebExchange.getRequest().getQueryParams().getFirst(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 = (org.springframework.data.domain.Sort) sortResolver.resolveArgument(methodParameter, bindingContext, serverWebExchange).block();
    // 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 = serverWebExchange.getRequest().getQueryParams().getFirst(previousOffsetName);
        String previousPageName = getParameterName(keysetConfig.previousPageName(), getParameterNameToUse(getPreviousPageParameterName(), methodParameter));
        String previousPageString = serverWebExchange.getRequest().getQueryParams().getFirst(previousPageName);
        if (StringUtils.hasText(previousOffsetString) || StringUtils.hasText(previousPageString)) {
            String previousPageSizeName = getParameterName(keysetConfig.previousPageSizeName(), getParameterNameToUse(getPreviousSizeParameterName(), methodParameter));
            String previousPageSizeString = serverWebExchange.getRequest().getQueryParams().getFirst(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 = serverWebExchange.getRequest().getQueryParams().getFirst(lowestName);
            String highestName = getParameterName(keysetConfig.highestName(), getParameterNameToUse(getHighestParameterName(), methodParameter));
            String highestString = serverWebExchange.getRequest().getQueryParams().getFirst(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 Mono.just(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.webflux.KeysetConfig) KeysetPage(com.blazebit.persistence.KeysetPage) DefaultKeysetPage(com.blazebit.persistence.DefaultKeysetPage) KeysetPageRequest(com.blazebit.persistence.spring.data.repository.KeysetPageRequest)

Example 4 with KeysetPageRequest

use of com.blazebit.persistence.spring.data.repository.KeysetPageRequest in project blaze-persistence by Blazebit.

the class KeysetPageableHandlerMethodArgumentResolver method getDefaultPageRequestFrom.

private static KeysetPageable getDefaultPageRequestFrom(MethodParameter parameter) {
    PageableDefault defaults = parameter.getParameterAnnotation(PageableDefault.class);
    Integer defaultPageNumber = defaults.page();
    Integer defaultPageSize = getSpecificPropertyOrDefaultFromValue(defaults, "size");
    if (defaultPageSize < 1) {
        Method annotatedMethod = parameter.getMethod();
        throw new IllegalStateException(String.format(INVALID_DEFAULT_PAGE_SIZE, annotatedMethod));
    }
    if (defaults.sort().length == 0) {
        return new KeysetPageRequest(defaultPageNumber, defaultPageSize, null, null);
    }
    return new KeysetPageRequest(defaultPageNumber, defaultPageSize, null, Sort.of(defaults.direction(), defaults.sort()));
}
Also used : BigInteger(java.math.BigInteger) PageableDefault(org.springframework.data.web.PageableDefault) KeysetPageRequest(com.blazebit.persistence.spring.data.repository.KeysetPageRequest) Method(java.lang.reflect.Method)

Example 5 with KeysetPageRequest

use of com.blazebit.persistence.spring.data.repository.KeysetPageRequest 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

KeysetPageRequest (com.blazebit.persistence.spring.data.repository.KeysetPageRequest)6 Method (java.lang.reflect.Method)4 Pageable (org.springframework.data.domain.Pageable)3 DefaultKeyset (com.blazebit.persistence.DefaultKeyset)2 DefaultKeysetPage (com.blazebit.persistence.DefaultKeysetPage)2 KeysetPage (com.blazebit.persistence.KeysetPage)2 KeysetPageable (com.blazebit.persistence.spring.data.repository.KeysetPageable)2 Document (com.blazebit.persistence.spring.data.testsuite.webmvc.entity.Document)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 PageableDefault (org.springframework.data.web.PageableDefault)2 KeysetAwarePage (com.blazebit.persistence.spring.data.repository.KeysetAwarePage)1 DocumentAccessor (com.blazebit.persistence.spring.data.testsuite.webmvc.accessor.DocumentAccessor)1 KeysetConfig (com.blazebit.persistence.spring.data.webflux.KeysetConfig)1 KeysetConfig (com.blazebit.persistence.spring.data.webmvc.KeysetConfig)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1