com.scooterframework.orm.misc
Class Paginator

java.lang.Object
  extended by com.scooterframework.orm.misc.Paginator

public class Paginator
extends java.lang.Object

Paginator class manages pagination of records of a model object.

Any information on a URL link can be passed to this object through controlOptions parameter.

Specifying Paging controlOptions

The following keys have an impact on the result of pagination:

The following keys are for information only:

Notes:

  1. When both npage and offset exists, the latter is ignored as it will be derived from npage.
  2. Either use key_order_by or use key_sort and key_order together.
  3. If a key/value pair is not used by the paginator, it will reappear in query string outputs.
  4. In addition, all SQL related information can be passed to the paginator through its PageListSource instance.

It is easier to specify paging controlOptions as a string:

    //Skip the first 250 records and returns the next 50 records.
    String controlOptions = "limit=50, offset=250";
 

Usage example:

    Paginator page = new Paginator(new JdbcPageListSource(modelClass), controlOptions);
    List pagedRecords = page.getRecordList();
 


Field Summary
protected  java.util.Map<java.lang.String,java.lang.String> controlOptions
           
protected  int cpage
          Current page number
static int DEFAULT_PAGE_LIMIT
           
protected  java.lang.String excludedKeys
           
static java.lang.String key_cpage
           
static java.lang.String key_group_by
          Key group_by represents GROUP BY clause in SQL.
static java.lang.String key_having
          Key having represents HAVING clause in SQL.
static java.lang.String key_limit
           
static java.lang.String key_link
           
static java.lang.String key_npage
           
static java.lang.String key_offset
           
static java.lang.String key_order
          Key order represents direction of sort.
static java.lang.String key_order_by
          Key order_by represents ORDER BY clause in SQL.
static java.lang.String key_sort
          Key sort indicates column names to sort.
protected  int limit
          Maximum number of records per page
static java.lang.String link_value_first
           
static java.lang.String link_value_last
           
static java.lang.String link_value_next
           
static java.lang.String link_value_page
           
static java.lang.String link_value_previous
           
protected  int npage
          New page number
protected  int offset
          Number of records to skip
protected  int opage
          Origin page number
protected  java.lang.String order
          Sort order direction: up or down
protected  int pageCount
          Total number of pages
protected  PageListSource pls
           
protected  java.util.List<?> recordList
          paged record list
protected  java.lang.String ref
          Link reference
protected  java.lang.String sort
          Sort column name
protected  int totalCount
          Total number of records
 
Constructor Summary
Paginator(PageListSource pls, java.util.Map<java.lang.String,?> controlOptions)
          Constructs a Paginator object that manages a model entity's pagination.
Paginator(PageListSource pls, java.lang.String controlOptions)
          Constructs a Paginator object that manages a model entity's pagination.
 
Method Summary
protected  int countPages(int totalRecords)
           
 int getCurrentPage()
          Return current page number.
 int getCurrentPageSize()
          Return the number of records on the current page
 int getEndIndex()
          Return index number of the last record on the current page
 int getLimit()
          Return maximum number of records per page
 int getOffset()
          Return offset
 int getOriginPage()
          Return origin page number.
 int getPageCount()
          Return total number of pages
 java.lang.String getQueryStringFirst()
          Return query string for link "first"
 java.lang.String getQueryStringLast()
          Return query string for link "last"
 java.lang.String getQueryStringNext()
          Return query string for link "next"
 java.lang.String getQueryStringOrigin()
          Return query string for link of the origin page
 java.lang.String getQueryStringPage(int pageNumber)
          Return query string for link of a page pageNumber starts from 1 to total page count.
 java.lang.String getQueryStringPrevious()
          Return query string for link "previous"
 java.util.List<?> getRecordList()
           
 int getStartIndex()
          Return index number of the first record on the current page
 int getTotalCount()
          Return total number of records
 boolean hasLastPage()
          Check if there is link on last page.
 boolean hasPreviousPage()
          Check if there is link on previous page.
protected  void initialize(PageListSource pls, java.util.Map<java.lang.String,java.lang.String> options)
           
 boolean isFirstPage()
          Check if the paginator is on the first page.
 boolean isLastPage()
          Check if the paginator is on the last page.
 Paginator nextPage()
          Returns a Paginator instance for the next page.
 void setExcludedKeys(java.lang.String excludedKeys)
          Sets html keys that do not have to appear in url.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

key_link

public static final java.lang.String key_link
See Also:
Constant Field Values

link_value_page

public static final java.lang.String link_value_page
See Also:
Constant Field Values

link_value_first

public static final java.lang.String link_value_first
See Also:
Constant Field Values

link_value_previous

public static final java.lang.String link_value_previous
See Also:
Constant Field Values

link_value_next

public static final java.lang.String link_value_next
See Also:
Constant Field Values

link_value_last

public static final java.lang.String link_value_last
See Also:
Constant Field Values

key_limit

public static final java.lang.String key_limit
See Also:
Constant Field Values

key_offset

public static final java.lang.String key_offset
See Also:
Constant Field Values

key_cpage

public static final java.lang.String key_cpage
See Also:
Constant Field Values

key_npage

public static final java.lang.String key_npage
See Also:
Constant Field Values

key_group_by

public static final java.lang.String key_group_by

Key group_by represents GROUP BY clause in SQL.

For example, "group_by=id, name" will be translated to SQL query as "GROUP BY id, name".

See Also:
Constant Field Values

key_having

public static final java.lang.String key_having

Key having represents HAVING clause in SQL. This is usually used with group_by together.

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

For example, "having=sum(price)<100" will be translated to sql query as "HAVING sum(price)<100".

See Also:
Constant Field Values

key_order_by

public static final java.lang.String key_order_by

Key order_by represents ORDER BY clause in SQL.

For example, "order_by=age desc" will be translated to SQL query as "ORDER BY age desc".

See Also:
Constant Field Values

key_sort

public static final java.lang.String key_sort

Key sort indicates column names to sort.

For example, "sort=first_name" will be translated to sql query as "order by first_name".

See Also:
Constant Field Values

key_order

public static final java.lang.String key_order

Key order represents direction of sort. If the query result set is in descending order, use value "Down". Otherwise by default the query results are in ascending order.

See Also:
Constant Field Values

pls

protected PageListSource pls

controlOptions

protected java.util.Map<java.lang.String,java.lang.String> controlOptions

DEFAULT_PAGE_LIMIT

public static final int DEFAULT_PAGE_LIMIT
See Also:
Constant Field Values

limit

protected int limit
Maximum number of records per page


offset

protected int offset
Number of records to skip


ref

protected java.lang.String ref
Link reference


opage

protected int opage
Origin page number


cpage

protected int cpage
Current page number


npage

protected int npage
New page number


sort

protected java.lang.String sort
Sort column name


order

protected java.lang.String order
Sort order direction: up or down


totalCount

protected int totalCount
Total number of records


pageCount

protected int pageCount
Total number of pages


recordList

protected java.util.List<?> recordList
paged record list


excludedKeys

protected java.lang.String excludedKeys
Constructor Detail

Paginator

public Paginator(PageListSource pls,
                 java.lang.String controlOptions)

Constructs a Paginator object that manages a model entity's pagination. Always recounts the total records.

String controlOptions is a string of name and value pairs separated by "=" sign. The default delimiter string to separate name-value pairs is ",|&".

String controlOptions may have the following format:

          cpage=2, limit=10,...
       or cpage=2|limit=10|...
       or cpage=2&limit=10&...
 

Parameters:
pls - PageListSource.
controlOptions - String of control information.

Paginator

public Paginator(PageListSource pls,
                 java.util.Map<java.lang.String,?> controlOptions)

Constructs a Paginator object that manages a model entity's pagination. Always recounts the total records.

Parameters:
pls - PageListSource.
controlOptions - Map of control information.
Method Detail

getLimit

public int getLimit()
Return maximum number of records per page


getOffset

public int getOffset()
Return offset


getCurrentPageSize

public int getCurrentPageSize()
Return the number of records on the current page


getTotalCount

public int getTotalCount()
Return total number of records


getPageCount

public int getPageCount()
Return total number of pages


getOriginPage

public int getOriginPage()
Return origin page number. Page number starts from 1.


getCurrentPage

public int getCurrentPage()
Return current page number. Page number starts from 1.


getStartIndex

public int getStartIndex()
Return index number of the first record on the current page


getEndIndex

public int getEndIndex()
Return index number of the last record on the current page


getQueryStringOrigin

public java.lang.String getQueryStringOrigin()
Return query string for link of the origin page


getQueryStringPage

public java.lang.String getQueryStringPage(int pageNumber)
Return query string for link of a page pageNumber starts from 1 to total page count.


getQueryStringFirst

public java.lang.String getQueryStringFirst()
Return query string for link "first"


getQueryStringPrevious

public java.lang.String getQueryStringPrevious()
Return query string for link "previous"


getQueryStringNext

public java.lang.String getQueryStringNext()
Return query string for link "next"


getQueryStringLast

public java.lang.String getQueryStringLast()
Return query string for link "last"


isFirstPage

public boolean isFirstPage()
Check if the paginator is on the first page.

Returns:
true for first page

isLastPage

public boolean isLastPage()
Check if the paginator is on the last page.

Returns:
true for last page

hasPreviousPage

public boolean hasPreviousPage()
Check if there is link on previous page.

Returns:
true for having link

hasLastPage

public boolean hasLastPage()
Check if there is link on last page.

Returns:
true for having link

getRecordList

public java.util.List<?> getRecordList()

setExcludedKeys

public void setExcludedKeys(java.lang.String excludedKeys)

Sets html keys that do not have to appear in url.

excludedKeys consists of comma separated keys.

Parameters:
excludedKeys -

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

nextPage

public Paginator nextPage()
Returns a Paginator instance for the next page.


initialize

protected void initialize(PageListSource pls,
                          java.util.Map<java.lang.String,java.lang.String> options)

countPages

protected int countPages(int totalRecords)