use of com.blazebit.persistence.deltaspike.data.Sort 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);
}
use of com.blazebit.persistence.deltaspike.data.Sort in project blaze-persistence by Blazebit.
the class KeysetPageableParamConverterProvider method apply.
private void apply(PageableConfiguration pageableConfiguration, PageableDefault pageableDefault, List<SortDefault> sortDefaults) {
int page = pageableConfiguration.getFallbackPageable().getPageNumber();
int pageSize = pageableConfiguration.getFallbackPageable().getPageSize();
Sort sort = pageableConfiguration.getFallbackPageable().getSort();
if (pageableDefault != null) {
page = pageableDefault.page();
pageSize = pageableDefault.size();
if (!pageableDefault.pageName().isEmpty()) {
pageableConfiguration.setPageParameterName(pageableDefault.pageName());
}
if (!pageableDefault.pageSizeName().isEmpty()) {
pageableConfiguration.setSizeParameterName(pageableDefault.pageSizeName());
}
if (!pageableDefault.sortName().isEmpty()) {
pageableConfiguration.setSortParameterName(pageableDefault.sortName());
}
pageableConfiguration.setOneIndexedParameters(pageableDefault.oneIndexed());
if (pageableDefault.sort().length != 0) {
if (!sortDefaults.isEmpty()) {
throw new IllegalArgumentException("Annotating both, @PageableDefault with sort and @SortDefault isn't allowed!");
}
sort = new Sort(pageableDefault.direction(), pageableDefault.sort());
}
}
if (!sortDefaults.isEmpty()) {
List<Sort.Order> orders = new ArrayList<>(sortDefaults.size());
for (int i = 0; i < sortDefaults.size(); i++) {
SortDefault sortDefault = sortDefaults.get(i);
String[] sortPaths = sortDefault.sort();
for (int j = 0; j < sortPaths.length; j++) {
orders.add(new Sort.Order(sortDefault.direction(), sortPaths[j], sortDefault.nulls()));
}
}
sort = new Sort(orders);
}
int offset;
if (page == 0) {
offset = 0;
} else {
offset = page * pageSize;
}
pageableConfiguration.setFallbackPageable(new PageRequest(sort, offset, pageSize));
}
use of com.blazebit.persistence.deltaspike.data.Sort in project blaze-persistence by Blazebit.
the class FullEntityViewRepositoryTest method testFindAllWithKeysetExtraction.
@Test
public void testFindAllWithKeysetExtraction() {
Pageable pageable = new KeysetPageRequest(null, new Sort("id"), 0, 5, true, true);
Page<PersonView> page = personViewRepository.findAll(pageable);
KeysetAwarePage<PersonView> keysetAwarePage = (KeysetAwarePage<PersonView>) page;
assertEquals(5, keysetAwarePage.getKeysetPage().getKeysets().size());
}
Aggregations