Class Paginator<T extends Model>

java.lang.Object
org.javalite.activejdbc.Paginator<T>
All Implemented Interfaces:
Serializable

public class Paginator<T extends Model> extends Object implements Serializable
This class supports pagination of result sets in ActiveJDBC. This is useful for paging through tables. If the Model subclass is annotated with @Cached, then this class will cache the total count of records returned by getCount(), as LazyList will cache the result sets. You can generate an instance each time you need one, or you can cache an instance in a session or even servlet context.
Author:
Igor Polevoy
See Also:
Serialized Form
  • Constructor Details

    • Paginator

      public Paginator(Class<? extends T> modelClass, int pageSize, String query, Object... params)
      Convenience constructor. Calls Paginator(Class, int, String, Object...) and passes true for suppressCounts.
    • Paginator

      public Paginator(Class<? extends T> modelClass, int pageSize, boolean suppressCounts, String query, Object... params)
      Convenience constructor. Calls Paginator(Class, int, String, Object...) and passes null for countQuery.
    • Paginator

      public Paginator(Class<? extends T> modelClass, int pageSize, boolean suppressCounts, String query, String countQuery, Object... params)
      Paginator is created with parameters to jump to chunks of result sets (pages). This class is useful "paging" through result on a user interface (web page).

      Examples of a sub-query:

      • "last_name like '%John%'" - this is a sub-query, and the rest of the information will be filled out by this class
      • "*" - will search for all records, no filtering
      Sub-query is used in simple cases, when filtering is done against one table.

      Full query example

      • "select * from people where last_name like '%John%'"
      Full query is used in cases when select covers many tables. In this case, the selected columns need to include attributes of the model class.
      Parameters:
      modelClass - model class mapped to a table.
      pageSize - number of items per page.
      suppressCounts - suppress calling "select count(*)... " on a table each time. If set to true, it will call count only once. If set to false, it will call count each time getCount() is called from hasNext() as well.
      params - a set of parameters if a query is parametrized (has question marks '?').
      query - this is a query that will be applied every time a new page is requested; this query should not contain limit, offset or order by clauses of any kind, Paginator will do this automatically. This parameter can have two forms, a sub-query or a full query.
  • Method Details

    • orderBy

      public Paginator<T> orderBy(String orderBys)
      Use to set order by(s). Example: "category, created_at desc"
      Parameters:
      orderBys - a comma-separated list of field names followed by either "desc" or "asc"
      Returns:
      instance to self.
    • getPage

      public LazyList<T> getPage(int pageNumber)
      This method will return a list of records for a specific page.
      Parameters:
      pageNumber - page number to return. This is indexed at 1, not 0. Any value below 1 is illegal and will be rejected.
      Returns:
      list of records that match a query make up a "page".
    • getCurrentPage

      public int getCurrentPage()
      Returns index of current page, or 0 if this instance has not produced a page yet.
      Returns:
      index of current page, or 0 if this instance has not produced a page yet.
    • getPrevious

      public boolean getPrevious()
      Synonym for hasPrevious().
      Returns:
      true if a previous page is available.
    • hasPrevious

      public boolean hasPrevious()
    • getNext

      public boolean getNext()
      Synonym for hasNext().
      Returns:
      true if a next page is available.
    • hasNext

      public boolean hasNext()
    • pageCount

      public long pageCount()
      Returns:
      a number of pages
    • getCount

      public Long getCount()
      Returns total count of records based on provided criteria.
      Returns:
      total count of records based on provided criteria
    • getPageSize

      public int getPageSize()
    • instance

      public static <E extends Model> Paginator.PaginatorBuilder<E> instance()
      Use to create a paginator instance, and provide arguments as needed.
      Returns:
      self.
    • setCurrentPageIndex

      public void setCurrentPageIndex(int currentPageIndex, boolean skipCheck)
      Sets an index of a current page. This method will make a quick count query to check that the index you are setting is within the boundaries.
      Parameters:
      currentPageIndex - index of a current page.
      skipCheck - true to skip the upper boundary check (will not make a call to DB).
    • getPage

      public LazyList<T> getPage()
      Returns:
      records for the current page.
    • getFrom

      public long getFrom()
      Returns index of the first item in a current page. Use in UI where you need a message: Displaying 101 to 140 items ir something similar.
      Returns:
      index of the first item in a current page.
    • getTo

      public long getTo()
      Returns index of the last item in a current page. Use in UI where you need a message: Displaying 101 to 140 items ir something similar.
      Returns:
      index of the last item in a current page.