org.apache.velocity.tools.view
Class AbstractSearchTool

java.lang.Object
  extended by org.apache.velocity.tools.view.PagerTool
      extended by org.apache.velocity.tools.view.AbstractSearchTool
Direct Known Subclasses:
AbstractSearchTool

@DefaultKey(value="search")
@InvalidScope(value={"application","session"})
public abstract class AbstractSearchTool
extends PagerTool

Abstract view 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.

Usage:
To use this class, you must extend it and implement the executeQuery(Object) method.

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);
 }
 

Here's an example of how your subclass would be used in a template:

   <form name="search" method="get" action="$link.setRelative('search.vm')">
     <input type="text"name="find" value="$!search.criteria">
     <input type="submit" value="Find">
   </form>
   #if( $search.hasItems() )
   Showing $!search.pageDescription<br>
     #set( $i = $search.index )
     #foreach( $item in $search.page )
       ${i}. $!item <br>
       #set( $i = $i + 1 )
     #end
     <br>
     #if ( $search.pagesAvailable > 1 )
       #set( $pagelink = $link.setRelative('search.vm').addQueryData("find",$!search.criteria).addQueryData("show",$!search.itemsPerPage) )
       #if( $search.prevIndex )
           <a href="$pagelink.addQueryData('index',$!search.prevIndex)">Prev</a>
       #end
       #foreach( $index in $search.slip )
         #if( $index == $search.index )
           <b>$search.pageNumber</b>
         #else
           <a href="$pagelink.addQueryData('index',$!index)">$!search.getPageNumber($index)</a>
         #end
       #end
       #if( $search.nextIndex )
           <a href="$pagelink.addQueryData('index',$!search.nextIndex)">Next</a>
       #end
     #end
   #elseif( $search.criteria )
   Sorry, no matches were found for "$!search.criteria".
   #else
   Please enter a search term
   #end
 
The output of this might look like:

Showing 1-5 of 8
1. foo
2. bar
3. blah
4. woogie
5. baz

1 2 Next

Example toolbox.xml configuration:

 <tools>
   <toolbox scope="request">
     <tool class="com.foo.tools.MySearchTool"/>
   </toolbox>
 </tools>
 

Since:
VelocityTools 2.0
Version:
$Revision: 591088 $ $Date: 2007-11-01 10:11:41 -0700 (Thu, 01 Nov 2007) $
Author:
Nathan Bubna

Nested Class Summary
static class AbstractSearchTool.StoredResults
          Simple utility class to hold a criterion and its result list.
 
Field Summary
private  java.lang.Object criteria
           
private  java.lang.String criteriaKey
           
static java.lang.String DEFAULT_CRITERIA_KEY
           
protected  org.apache.velocity.runtime.log.Log LOG
           
protected static java.lang.String STORED_RESULTS_KEY
          the key under which StoredResults are kept in session
 
Fields inherited from class org.apache.velocity.tools.view.PagerTool
DEFAULT_INDEX_KEY, DEFAULT_ITEMS_PER_PAGE, DEFAULT_ITEMS_PER_PAGE_KEY, DEFAULT_NEW_ITEMS_KEY, DEFAULT_SLIP_SIZE, DEFAULT_SLIP_SIZE_KEY, session, STORED_ITEMS_KEY
 
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.
 java.lang.String getCriteriaKey()
           
 java.util.List getItems()
          Gets the results for the given criteria either in memory or by performing a new query for them.
protected  java.util.List getStoredItems()
          Retrieves stored search items (if any) from the user's session attributes.
protected  AbstractSearchTool.StoredResults getStoredResults()
          Retrieves stored search results (if any) from the user's session attributes.
 void reset()
          Sets the criteria and results to null, page index to zero, and items per page to the default.
 void setCriteria(java.lang.Object criteria)
          Sets the criteria for this search.
 void setCriteriaKey(java.lang.String key)
           
 void setLog(org.apache.velocity.runtime.log.Log log)
           
protected  void setStoredItems(java.util.List items)
          Stores current search items in the user's session attributes (if one currently exists) in order to do efficient result pagination.
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.
 void setup(javax.servlet.http.HttpServletRequest request)
          Sets the criteria *if* it is set in the request parameters.
 
Methods inherited from class org.apache.velocity.tools.view.PagerTool
getCreateSession, getFirstIndex, getIndex, getIndexKey, getItemsPerPage, getItemsPerPageKey, getLastIndex, getNewItemsKey, getNextIndex, getPage, getPageDescription, getPageNumber, getPageNumber, getPagesAvailable, getPrevIndex, getSlip, getSlipSize, getSlipSizeKey, getTotal, hasItems, setCreateSession, setIndex, setIndexKey, setItems, setItemsPerPage, setItemsPerPageKey, setNewItemsKey, setRequest, setSlipSize, setSlipSizeKey
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CRITERIA_KEY

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

STORED_RESULTS_KEY

protected static final java.lang.String STORED_RESULTS_KEY
the key under which StoredResults are kept in session


LOG

protected org.apache.velocity.runtime.log.Log LOG

criteriaKey

private java.lang.String criteriaKey

criteria

private java.lang.Object criteria
Constructor Detail

AbstractSearchTool

public AbstractSearchTool()
Method Detail

setLog

public void setLog(org.apache.velocity.runtime.log.Log log)

setup

public void setup(javax.servlet.http.HttpServletRequest request)
Sets the criteria *if* it is set in the request parameters.

Overrides:
setup in class PagerTool
Parameters:
request - the current HttpServletRequest

setCriteriaKey

public void setCriteriaKey(java.lang.String key)

getCriteriaKey

public java.lang.String getCriteriaKey()

reset

public void reset()
Sets the criteria and results to null, page index to zero, and items per page to the default.

Overrides:
reset in class PagerTool

setCriteria

public void setCriteria(java.lang.Object criteria)
Sets the criteria for this search.

Parameters:
criteria - - the criteria used for this search

getCriteria

public java.lang.Object getCriteria()
Return the criteria object for this request. (for a simple search mechanism, this will typically be just a java.lang.String)

Returns:
criteria object

getItems

public java.util.List getItems()
Gets the results for the given criteria either in memory or by performing a new query for them. If the criteria is null, an empty list will be returned.

Overrides:
getItems in class PagerTool
Returns:
List of all items for the criteria

getStoredItems

protected java.util.List getStoredItems()
Description copied from class: PagerTool
Retrieves stored search items (if any) from the user's session attributes.

Overrides:
getStoredItems in class PagerTool
Returns:
the List retrieved from memory

setStoredItems

protected void setStoredItems(java.util.List items)
Description copied from class: PagerTool
Stores current search items in the user's session attributes (if one currently exists) in order to do efficient result pagination.

Override this to store search items somewhere besides the HttpSession or to prevent storage of items across requests. In the former situation, you must also override getStoredItems().

Overrides:
setStoredItems in class PagerTool
Parameters:
items - the List to be stored

executeQuery

protected abstract java.util.List executeQuery(java.lang.Object criteria)
Executes a query for the specified criteria.

This method must be implemented! A simple implementation might be something like:

 protected List executeQuery(Object crit)
 {
     return MyDbUtils.getFooBarsMatching((String)crit);
 }
 

Returns:
a List of results for this query

getStoredResults

protected AbstractSearchTool.StoredResults getStoredResults()
Retrieves stored search results (if any) from the user's session attributes.

Returns:
the AbstractSearchTool.StoredResults retrieved from memory

setStoredResults

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.

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().

Parameters:
results - the AbstractSearchTool.StoredResults to be stored


Copyright (c) 2003-2007 Apache Software Foundation