|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.apache.velocity.tools.view.tools.AbstractSearchTool
Abstract context tool for doing "searching" and robust pagination of search results. The goal here is to provide a simple and uniform API for "search tools" that can be used in velocity templates (or even a standard Search.vm template). In particular, this class provides good support for result pagination and some very simple result caching.
To use this class, you must extend it and implement the setup(HttpServletRequest) and executeQuery(Object) methods.
The setup(HttpServletRequest) method ought to extract from the current request the search criteria, the current list index, and optionally, the number of items to display per page of results. Upon extracting these parameters, they should be set using the provided setCriteria(Object), setIndex(int), and setItemsPerPage(int) methods. A simple implementation would be:
public void setup(HttpServletRequest req) { ParameterParser pp = new ParameterParser(req); setCriteria(pp.getString("find")); setIndex(pp.getInt("index", 0)); setItemsPerPage(pp.getInt("show", DEFAULT_ITEMS_PER_PAGE)); }
The setCriteria(Object) method takes an Object in order to allow the search criteria to meet your needs. Your criteria may be as simple as a single string, an array of strings, or whatever you like. The value passed into this method is that which will ultimately be passed into executeQuery(Object) to perform the search and return a list of results. A simple implementation might be like:
protected List executeQuery(Object crit) { return MyDbUtils.getFooBarsMatching((String)crit); }
Nested Class Summary | |
class |
AbstractSearchTool.StoredResults
Simple utility class to hold a criteria and its result list This class is by default stored in a user's session, so it implements Serializable, but its members are transient. |
Field Summary | |
private java.lang.Object |
criteria
|
static int |
DEFAULT_ITEMS_PER_PAGE
the default number of results shown per page |
static int |
DEFAULT_SLIP_SIZE
the default max number of result page indices to list |
private int |
index
|
private int |
itemsPerPage
|
private java.util.List |
results
|
protected javax.servlet.http.HttpSession |
session
|
private int |
slipSize
|
protected static java.lang.String |
STORED_RESULTS_KEY
the key under which StoredResults are kept in session |
Constructor Summary | |
AbstractSearchTool()
|
Method Summary | |
protected abstract java.util.List |
executeQuery(java.lang.Object criteria)
Executes a query for the specified criteria. |
java.lang.Object |
getCriteria()
Return the criteria object for this request. |
int |
getIndex()
Return the current search result index. |
int |
getItemsPerPage()
Return the set number of items to be displayed per page of results |
java.lang.Integer |
getNextIndex()
Return the index for the next page of results (as determined by the current index, items per page, and the number of results). |
java.util.List |
getPage()
Return the current "page" of search results. |
java.lang.String |
getPageDescription()
Returns a description of the current page. |
java.lang.Integer |
getPageNumber()
Returns the "page number" for the current index. |
java.lang.Integer |
getPageNumber(int i)
Returns the "page number" for the specified index. |
int |
getPagesAvailable()
Return the number of pages that can be made from this list given the set number of items per page. |
java.lang.Integer |
getPrevIndex()
Return the index for the previous page of results (as determined by the current index, items per page, and the number of results). |
java.util.List |
getResults()
Return the results for the given criteria. |
java.util.List |
getSlip()
Return a Sliding List of Indices for Pages of search results. |
int |
getSlipSize()
Returns the number of result page indices getSlip()
will return per request (if available). |
protected AbstractSearchTool.StoredResults |
getStoredResults()
Retrieves stored search results (if any) from the user's session attributes. |
boolean |
hasResults()
Checks whether or not the result list is empty. |
void |
init(java.lang.Object obj)
Initializes this instance by grabbing the request and session objects from the current ViewContext. |
void |
reset()
Sets the criteria and results to null, page index to zero, and items per page to the default. |
private java.util.List |
retrieveResults()
Gets the results for the given criteria either in memory or by performing a new query for them. |
void |
setCriteria(java.lang.Object criteria)
Sets the criteria for this search and clears existing results. |
void |
setIndex(int index)
Sets the index of the first result in the current page |
void |
setItemsPerPage(int itemsPerPage)
Sets the number of items returned in a page of results |
void |
setSlipSize(int slipSize)
Sets the number of result page indices for getSlip() to list. |
protected void |
setStoredResults(AbstractSearchTool.StoredResults results)
Stores current search results in the user's session attributes (if one currently exists) in order to do efficient result pagination. |
abstract void |
setup(javax.servlet.http.HttpServletRequest request)
Abstract method to make it as obvious as possible just where implementing classes should be retrieving and configuring search/display parameters. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int DEFAULT_ITEMS_PER_PAGE
public static final int DEFAULT_SLIP_SIZE
protected static final java.lang.String STORED_RESULTS_KEY
private java.util.List results
private java.lang.Object criteria
private int index
private int slipSize
private int itemsPerPage
protected javax.servlet.http.HttpSession session
Constructor Detail |
public AbstractSearchTool()
Method Detail |
public void init(java.lang.Object obj)
init
in interface ViewTool
obj
- the current ViewContext
java.lang.ClassCastException
- if the param is not a ViewContextpublic abstract void setup(javax.servlet.http.HttpServletRequest request)
A simple implementation would be:
public void setup(HttpServletRequest req) { ParameterParser pp = new ParameterParser(req); setCriteria(pp.getString("find")); setIndex(pp.getInt("index", 0)); setItemsPerPage(pp.getInt("show", DEFAULT_ITEMS_PER_PAGE)); }
request
- the current HttpServletRequestpublic void reset()
public void setCriteria(java.lang.Object criteria)
criteria
- - the criteria used for this searchpublic void setIndex(int index)
index
- - the result index to start the current page withpublic void setItemsPerPage(int itemsPerPage)
itemsPerPage
- - the number of items to be returned per pagepublic void setSlipSize(int slipSize)
getSlip()
to list.
(for google-ish result page links).
slipSize
- - the number of result page indices to listgetSlip()
public java.lang.Object getCriteria()
public int getItemsPerPage()
public int getSlipSize()
getSlip()
will return per request (if available).
getSlip()
will try to returnpublic int getIndex()
public boolean hasResults()
true
if the result list is not empty.public java.util.List getResults()
null
.
List
of all results for the criteriapublic java.lang.Integer getNextIndex()
null
if none existspublic java.lang.Integer getPrevIndex()
null
if none existspublic int getPagesAvailable()
public java.util.List getPage()
List
of results for the "current page"public java.lang.Integer getPageNumber(int i)
i
- the index that you want the page number for
null
if there are no resultspublic java.lang.Integer getPageNumber()
null
if there are no resultspublic java.lang.String getPageDescription()
Returns a description of the current page. This implementation displays a 1-based range of result indices and the total number of results. (e.g. "1 - 10 of 42" or "7 of 7")
Sub-classes may override this to provide a customized description (such as one in another language).
public java.util.List getSlip()
Essentially, this returns a list of result indices that correspond to available pages of search results (as based on the set items-per-page). This makes it relativly easy to do a google-ish set of links to result pages.
Note that this list of Integers is 0-based to correspond with the
underlying result indices and not the displayed page numbers (see
getPageNumber(int)
).
List
of Integers representing the indices of result
pages or empty list if there's one or less pages availableprivate java.util.List retrieveResults()
protected abstract java.util.List executeQuery(java.lang.Object criteria)
This method must be implemented! A simple implementation might be something like:
protected List executeQuery(Object crit) { return MyDbUtils.getFooBarsMatching((String)crit); }
List
of results for this queryprotected AbstractSearchTool.StoredResults getStoredResults()
AbstractSearchTool.StoredResults
retrieved from memoryprotected void setStoredResults(AbstractSearchTool.StoredResults results)
Override this to store search results somewhere besides the HttpSession or to prevent storage of results across requests. In the former situation, you must also override getStoredResults().
results
- the AbstractSearchTool.StoredResults
to be stored
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |