I have an existing REST service that accepts PAGE and SIZE parameters
/fetchrecords?page=0&size=10
which in turn, creates a Spring Pageable to be used with Spring Repository.
Pageable pageRequest = new PageRequest(page, size, Sort.Direction.DESC, "created");
I want to now use Vaadin 8 CallbackDataProvider, however it produces OFFSET and LIMIT to be used for the BackendDataProvider.
dataProvider = new CallbackDataProvider<MyPojo, Void>(
query -> service.fetchrecords(query.getOffset(), query.getLimit()).stream(),
query -> service.getCount());
Of course this won't work as offset != page, and limit value will change based on how many records are left, according to the offset position.
Without rewriting the rest/service, how can I go from the DataProvider OFFSET and LIMIT to PAGE and SIZE, correctly?
This parameter can be thought of as the page size. If no limit is specified, the system defaults to a limit of 15 results per request. The maximum valid limit value is 100. The offset parameter controls the starting point within the collection of resource results.
IMO offset based paginations have wierd consistency problems what if someone added a new record, so the offset is shifted by 1 (if sorted by new), and you will get 1 record twice. People are used to that in pagination. Go to ebay and sort by "ending soonest" ad page through.
For translating limit and offset into a Pageable, we create a new class implementing Pageable. private Sort sort = new Sort ( Sort. Direction. DESC, "id"); throw new IllegalArgumentException ( "Limit must not be less than one!"); throw new IllegalArgumentException ( "Offset index must not be less than zero!"); // The integers are positive.
The maximum valid limit value is 100. The offset parameter controls the starting point within the collection of resource results.
I had the same problem. The Vaadin paging with offset and limit is more powerful than paging with page and page size (as in your REST service and in spring class PageRequest). So, the answer to your question is: you can't easily. If you could change your REST service parameters to offset and limit then you can implement your own Pageable
like:
public class Range implements Pageable, Serializable {
private static final long serialVersionUID = 0L;
private int limit;
private long offset;
private Sort sort;
public Range(long offset, int limit, Sort sort) {
...
}
@Override
public long getOffset() {
return offset;
}
...
}
I wonder why this isn't available out-of-the-box.
Another option is to map the offset/limit to possibly multiple pages that include the required range of data, then get these pages and take only the necessary data.
Please note: This is not working for the last page requested by Vaadin when accessing Neo4j via Spring Data Neo4j. SDN v5.2.8 will use the following code to apply "skip" to query
parameters.put(SKIP, pageable.getPageNumber() * pageable.getPageSize());
The use of getPageNumber
is a problem, e.g. offset 100 and limit 23 results in skip of 92. Filed an issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With