Interface CursorPagination<TT>

Type Parameters:
TT - Entity type for pagination
All Superinterfaces:
Serializable

public interface CursorPagination<TT> extends Serializable
Interface defining cursor pagination behavior for PrimeFaces JPA LazyDataModel
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    Configuration class for cursor pagination, used to create a CursorPagination instance with specified supported fields and options.
    static final record 
    Used to configure columns for cursor pagination.
  • Method Summary

    Modifier and Type
    Method
    Description
    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());
        }
    }
    
    int
    cursorOffset(int offset)
    Calculate the adjusted offset based on cached cursor state
    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
    Creates a JPA sort Order to apply to the query when no explicit sort is requested by the client, based on the first supported column and configured sort direction
    boolean
    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>>
    Returns a no-op implementation of CursorPagination that can be used when cursor pagination is not supported or desired.
    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.
    void
    save(int offset, TT entity, Map<String,SortMeta> sortMeta)
    Caches the cursor value for the given offset and entity
  • Method Details

    • isSupported

      boolean isSupported(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. 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 filters
      sortMeta - 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

      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 client
      cb - the CriteriaBuilder to use for constructing the predicate
      root - the Root of the query, used to resolve the field for the predicate
      sortMeta - 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

      void save(int offset, TT entity, Map<String,SortMeta> sortMeta)
      Caches the cursor value for the given offset and entity
      Parameters:
      offset - the offset that was requested by the client
      entity - the entity being loaded at that offset, used to extract the cursor value
      sortMeta - the current sort metadata, used to determine which field is being sorted on for cursor extraction
    • defaultSort

      Order defaultSort(CriteriaBuilder cb, Root<TT> root)
      Creates a JPA sort Order to 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 Order
      root - the Root of the query, used to resolve the field for the Order
      Returns:
      the JPA Order to apply to the query for default sorting when no explicit sort is requested by the client
    • noop

      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. This is the default behavior of JPALazyDataModel
      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 a CursorPagination.Config with 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 client
      columns - the supported columns for cursor pagination, used to validate the requested sort field and determine the default field
      useDefault - 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