org.javalite.activejdbc
Class Paginator

java.lang.Object
  extended by org.javalite.activejdbc.Paginator
All Implemented Interfaces:
Serializable

public class Paginator
extends Object
implements Serializable

This class supports pagination of result sets in ActiveJDBC. This is useful for paging through tables. This class does not cache resultsets, rather it will make requests to DB each time getPage(int) method is called. 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 Model> modelClass, int pageSize, String query, Object... params)
          Paginator is created with parameters to jump to chunks of result sets (pages).
 
Method Summary
 long getCount()
          Total count of records based on provided criteria.
 int getCurrentPage()
          Returns index of current page, or -1 if this instance has not produced a page yet.
 boolean getNext()
          Synonym for hasNext().
<T extends Model>
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 Model> modelClass,
                 int pageSize,
                 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.
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 <T extends Model> 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 -1 if this instance has not produced a page yet.

Returns:
index of current page, or -1 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()
Total count of records based on provided criteria.

Returns:
total count of records based on provided criteria


Copyright © 2013. All Rights Reserved.