Package org.javalite.activejdbc
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 bygetCount(), 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classPaginator.PaginatorBuilder<T extends Model>Provides a builder pattern to create new instances of paginator.
-
Constructor Summary
Constructors Constructor Description Paginator(Class<? extends T> modelClass, int pageSize, boolean suppressCounts, String query, Object... params)Convenience constructor.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).Paginator(Class<? extends T> modelClass, int pageSize, String query, Object... params)Convenience constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description LonggetCount()Returns total count of records based on provided criteria.intgetCurrentPage()Returns index of current page, or 0 if this instance has not produced a page yet.longgetFrom()Returns index of the first item in a current page.booleangetNext()Synonym forhasNext().LazyList<T>getPage()LazyList<T>getPage(int pageNumber)This method will return a list of records for a specific page.intgetPageSize()booleangetPrevious()Synonym forhasPrevious().longgetTo()Returns index of the last item in a current page.booleanhasNext()booleanhasPrevious()static <E extends Model>
Paginator.PaginatorBuilder<E>instance()Use to create a paginator instance, and provide arguments as needed.Paginator<T>orderBy(String orderBys)Use to set order by(s).longpageCount()voidsetCurrentPageIndex(int currentPageIndex, boolean skipCheck)Sets an index of a current page.
-
-
-
Constructor Detail
-
Paginator
public Paginator(Class<? extends T> modelClass, int pageSize, String query, Object... params)
Convenience constructor. CallsPaginator(Class, int, String, Object...)and passes true forsuppressCounts.
-
Paginator
public Paginator(Class<? extends T> modelClass, int pageSize, boolean suppressCounts, String query, Object... params)
Convenience constructor. CallsPaginator(Class, int, String, Object...)and passes null forcountQuery.
-
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
Full query example
- "select * from people where last_name like '%John%'"
- 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 timegetCount()is called fromhasNext()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<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 forhasPrevious().- Returns:
- true if a previous page is available.
-
hasPrevious
public boolean hasPrevious()
-
getNext
public boolean getNext()
Synonym forhasNext().- 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-trueto skip the upper boundary check (will not make a call to DB).
-
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 itemsir 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 itemsir something similar.- Returns:
- index of the last item in a current page.
-
-