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);
}
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());
}
Aggregations