org.javalite.activejdbc
Class Paginator<T extends Model>

java.lang.Object
  extended by 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. This class is thread safe and the same instance could be used across multiple web requests and even across multiple users/sessions. It is lightweight class, 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 Summary
Paginator(Class<? extends T> modelClass, int pageSize, boolean suppressCounts, String query, Object... params)
          Paginator is created with parameters to jump to chunks of result sets (pages).
Paginator(Class<? extends T> modelClass, int pageSize, String query, Object... params)
          Convenience constructor.
 
Method Summary
 Long getCount()
          Returns total count of records based on provided criteria.
 int getCurrentPage()
          Returns index of current page, or 0 if this instance has not produced a page yet.
 boolean getNext()
          Synonym for hasNext().
 LazyList<T> getPage(int pageNumber)
          This method will return a list of records for a specific page.
 boolean getPrevious()
          Synonym for hasPrevious().
 boolean hasNext()
           
 boolean hasPrevious()
           
 Paginator orderBy(String orderBys)
          Use to set order by(s).
 long pageCount()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

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)
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:

Sub-query is used in simple cases, when filtering is done against one table.

Full query example

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 Detail

orderBy

public Paginator 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()

getCount

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

Returns:
total count of records based on provided criteria


Copyright © 2015 JavaLite. All rights reserved.