Interface CursorPagination<TT>
- Type Parameters:
TT- Entity type for pagination
- All Superinterfaces:
Serializable
Interface defining cursor pagination behavior for PrimeFaces JPA LazyDataModel
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classConfiguration class for cursor pagination, used to create a CursorPagination instance with specified supported fields and options.static final recordUsed to configure columns for cursor pagination. -
Method Summary
Modifier and TypeMethodDescriptioncolumns()Returns columns that are supported for cursor pagination.static <TT> Lazy<CursorPagination<TT>> create(Function<com.flowlogix.jeedao.primefaces.CursorPagination.Config.ConfigBuilder<TT>, CursorPagination.Config<TT>> configConsumer) Creates a default implementation of cursor pagination
@Named @ViewScoped public class CursorDataModel implements Serializable { @Inject @Getter JPALazyDataModel<UserEntity> userModel; @PostConstruct void initialize() { // configure cursor pagination by id field userModel.initialize(builder -> builder .cursor(CursorPagination.create(config -> config.supportedFields(List.of( new Field<>(() -> UserEntity_.id.getName(), UserEntity::getId) )).build())).build()); } }intcursorOffset(int offset) Calculate the adjusted offset based on cached cursor statecursorPredicate(int offset, CriteriaBuilder cb, Root<TT> root, Map<String, SortMeta> sortMeta) Compute the JPA predicate to apply to the query for cursor pagination based on the cached cursor statedefaultSort(CriteriaBuilder cb, Root<TT> root) Creates a JPA sortOrderto apply to the query when no explicit sort is requested by the client, based on the first supported column and configured sort directionbooleanisSupported(Map<String, FilterMeta> filters, Map<String, SortMeta> sortMeta) Checks if cursor pagination is supported for the given filters and sort metadata, this method is called before every query execution and should return quickly.static <TT> Lazy<CursorPagination<TT>> noop()Returns a no-op implementation of CursorPagination that can be used when cursor pagination is not supported or desired.static <TT> StringrequestedSort(Map<String, SortMeta> sortMeta, Map<String, FunctionalInterfaces.SerializableFunction<TT, Comparable<?>>> columns, boolean useDefault) Calculates the requested sort field from the sort metadata and supported columns, with an option to fall back to the default column if no valid sort is requested.voidCaches the cursor value for the given offset and entity
-
Method Details
-
isSupported
Checks if cursor pagination is supported for the given filters and sort metadata, this method is called before every query execution and should return quickly. Implementations can use this method to detect changes in filters or sort order and clear cached cursor state as needed- Parameters:
filters- the current filter metadata, used to determine if cursor pagination is still valid based on supported filterssortMeta- the current sort metadata, used to determine if cursor pagination is still valid based on supported sort fields and order- Returns:
- true if cursor pagination is supported and can be applied to the query with the given filters and sort metadata, false if cursor pagination should not
-
columns
Map<String, FunctionalInterfaces.SerializableFunction<TT, Comparable<?>>> columns()Returns columns that are supported for cursor pagination. Implementation must preserve the order of the columns, as the default sort is applied to the first column.- Returns:
- a map of column names to functions that extract the comparable value from the entity for that column.
-
cursorOffset
int cursorOffset(int offset) Calculate the adjusted offset based on cached cursor state- Parameters:
offset- the original offset requested by the client- Returns:
- the adjusted offset to be used in the query based on the cached cursor state
-
cursorPredicate
Predicate cursorPredicate(int offset, CriteriaBuilder cb, Root<TT> root, Map<String, SortMeta> sortMeta) Compute the JPA predicate to apply to the query for cursor pagination based on the cached cursor state- Parameters:
offset- the original offset requested by the clientcb- the CriteriaBuilder to use for constructing the predicateroot- the Root of the query, used to resolve the field for the predicatesortMeta- the current sort metadata, used to determine which field is being sorted on for predicate construction- Returns:
- the JPA Predicate to apply to the query for cursor pagination, or null if no predicate is needed
-
save
Caches the cursor value for the given offset and entity- Parameters:
offset- the offset that was requested by the cliententity- the entity being loaded at that offset, used to extract the cursor valuesortMeta- the current sort metadata, used to determine which field is being sorted on for cursor extraction
-
defaultSort
Creates a JPA sortOrderto apply to the query when no explicit sort is requested by the client, based on the first supported column and configured sort direction- Parameters:
cb- the CriteriaBuilder to use for constructing the Orderroot- the Root of the query, used to resolve the field for the Order- Returns:
- the JPA
Orderto apply to the query for default sorting when no explicit sort is requested by the client
-
noop
Returns a no-op implementation of CursorPagination that can be used when cursor pagination is not supported or desired. This is the default behavior ofJPALazyDataModel- Returns:
- a Lazy containing a no-op implementation of CursorPagination
-
create
static <TT> Lazy<CursorPagination<TT>> create(Function<com.flowlogix.jeedao.primefaces.CursorPagination.Config.ConfigBuilder<TT>, CursorPagination.Config<TT>> configConsumer) Creates a default implementation of cursor pagination
@Named @ViewScoped public class CursorDataModel implements Serializable { @Inject @Getter JPALazyDataModel<UserEntity> userModel; @PostConstruct void initialize() { // configure cursor pagination by id field userModel.initialize(builder -> builder .cursor(CursorPagination.create(config -> config.supportedFields(List.of( new Field<>(() -> UserEntity_.id.getName(), UserEntity::getId) )).build())).build()); } }- Parameters:
configConsumer- a function that accepts a ConfigBuilder and returns aCursorPagination.Configwith the desired configuration for cursor pagination- Returns:
- a Lazy containing a CursorPagination instance configured via provided
CursorPagination.Config
-
requestedSort
static <TT> String requestedSort(Map<String, SortMeta> sortMeta, Map<String, FunctionalInterfaces.SerializableFunction<TT, Comparable<?>>> columns, boolean useDefault) Calculates the requested sort field from the sort metadata and supported columns, with an option to fall back to the default column if no valid sort is requested.- Parameters:
sortMeta- the current sort metadata, used to determine which field is being sorted on by the clientcolumns- the supported columns for cursor pagination, used to validate the requested sort field and determine the default fielduseDefault- flag to indicate whether to return the default column (first supported column) if no valid sort field is requested by the client- Returns:
- the name of the requested sort field if it is valid and supported otherwise the default column if useDefault is true, or an empty string if no valid sort field is requested and useDefault is false
-