Search in sources :

Example 1 with KeysetAwarePage

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

the class KeysetAwarePagedResourcesAssembler method createLink.

private Link createLink(UriTemplate base, Page<?> page, Pageable pageable, LinkRelation relation) {
    UriComponentsBuilder builder;
    if (base.getVariables().isEmpty()) {
        builder = fromUriString(base.toString());
    } else {
        builder = fromUri(base.expand());
    }
    pageableResolver.enhance(builder, getMethodParameter(), pageable);
    String previousPagePropertyName = pageableResolver.getParameterNameToUse(pageableResolver.getPreviousPageParameterName(), getMethodParameter());
    String lowestPropertyName = pageableResolver.getParameterNameToUse(pageableResolver.getLowestParameterName(), getMethodParameter());
    String highestPropertyName = pageableResolver.getParameterNameToUse(pageableResolver.getHighestParameterName(), getMethodParameter());
    if ((relation == IanaLinkRelations.NEXT || relation == IanaLinkRelations.PREV) && page instanceof KeysetAwarePage<?>) {
        int pageNumber = page.getNumber();
        builder.replaceQueryParam(previousPagePropertyName, pageableResolver.isOneIndexedParameters() ? pageNumber + 1 : pageNumber);
        Sort sort = page.getSort();
        KeysetPage keysetPage = ((KeysetAwarePage<?>) page).getKeysetPage();
        if (relation == IanaLinkRelations.NEXT) {
            builder.replaceQueryParam(highestPropertyName, serialize(sort, keysetPage.getHighest()));
            builder.replaceQueryParam(lowestPropertyName);
        } else if (pageable.getOffset() != 0) {
            builder.replaceQueryParam(lowestPropertyName, serialize(sort, keysetPage.getLowest()));
            builder.replaceQueryParam(highestPropertyName);
        } else {
            builder.replaceQueryParam(previousPagePropertyName);
            builder.replaceQueryParam(lowestPropertyName);
            builder.replaceQueryParam(highestPropertyName);
        }
    } else {
        builder.replaceQueryParam(previousPagePropertyName);
        builder.replaceQueryParam(lowestPropertyName);
        builder.replaceQueryParam(highestPropertyName);
    }
    return link(UriTemplate.of(builder.build().toString()), relation);
}
Also used : KeysetAwarePage(com.blazebit.persistence.spring.data.repository.KeysetAwarePage) KeysetPage(com.blazebit.persistence.KeysetPage) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) Sort(org.springframework.data.domain.Sort) UriComponentsBuilder.fromUriString(org.springframework.web.util.UriComponentsBuilder.fromUriString)

Example 2 with KeysetAwarePage

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

the class ReadOnlyDocumentRepositoryTest method testFindAllBySpecWithKeysetExtraction.

@Test
public void testFindAllBySpecWithKeysetExtraction() {
    // Given
    final Document d3 = createDocument("d3", null, 3L, null);
    final Document d2 = createDocument("d2", null, 2L, null);
    final Document d1 = createDocument("d1", null, 1L, null);
    final String param = "Foo";
    Pageable pageable = new KeysetPageRequest(null, Sort.asc("id"), 0, 3, true, true);
    // When
    Page<?> actual = readOnlyDocumentRepository.findAll(new Specification<Document>() {

        @Override
        public Predicate toPredicate(Root<Document> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
            return criteriaBuilder.conjunction();
        }
    }, pageable);
    KeysetAwarePage<?> keysetAwarePage = (KeysetAwarePage<?>) actual;
    // Then
    assertEquals(3, keysetAwarePage.getKeysetPage().getKeysets().size());
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) KeysetAwarePage(com.blazebit.persistence.spring.data.repository.KeysetAwarePage) Document(com.blazebit.persistence.spring.data.testsuite.webmvc.entity.Document) Predicate(javax.persistence.criteria.Predicate) Pageable(org.springframework.data.domain.Pageable) KeysetPageRequest(com.blazebit.persistence.spring.data.repository.KeysetPageRequest) Test(org.junit.Test)

Aggregations

KeysetAwarePage (com.blazebit.persistence.spring.data.repository.KeysetAwarePage)2 KeysetPage (com.blazebit.persistence.KeysetPage)1 KeysetPageRequest (com.blazebit.persistence.spring.data.repository.KeysetPageRequest)1 Document (com.blazebit.persistence.spring.data.testsuite.webmvc.entity.Document)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 Test (org.junit.Test)1 Pageable (org.springframework.data.domain.Pageable)1 Sort (org.springframework.data.domain.Sort)1 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1 UriComponentsBuilder.fromUriString (org.springframework.web.util.UriComponentsBuilder.fromUriString)1