Datasheet
Rendering the VIEW mode depends on whether we passed in a query mode RenderRequest parameter.
If there is a
queryMode parameter, we display the results; if not, we display the search box.
/* This method is overriden to specify
* the title programmatically
*/
protected String getTitle(RenderRequest request) {
return “Lucene Portlet”;
}
/* This method is the meat of the portlet
* manipulations of the portlet’s state are done
* through this method.
* <p>
* There are really two types of actions for this portlet
* depending on which mode we are in. In the VIEW mode,
* we have an action that searches a Lucene Index and then
* places the hits in a request attribute. In the EDIT mode,
* we allow users to change the location of the Lucene
* index.
*
*/
public void processAction(ActionRequest aReq, ActionResponse aRes)
throws PortletException, IOException {
PortletMode mode = new PortletMode(aReq.getParameter(“mode”));
aRes.setPortletMode(mode);
if (mode.equals(PortletMode.VIEW)) {
PortletPreferences prefs = aReq.getPreferences();
String indexPath = prefs.getValue(“indexPath”, null);
Searcher searcher = new IndexSearcher(indexPath);
Analyzer analyzer = new StandardAnalyzer();
StringBuffer queryBuffer = new StringBuffer();
String searchString = aReq.getParameter(“searchString”);
if (searchString != null) {
queryBuffer.append(searchString);
String line = queryBuffer.toString();
try {
Query query = QueryParser.parse(line, “contents”, analyzer);
Hits hits = searcher.search(query);
aReq.setAttribute(“hits”, hits);
} catch (ParseException pe) {
pe.printStackTrace();
}
} else {
aRes.setRenderParameter(“queryMode”, “begin”);
}
Our processAction behavior depends on the portlet’s mode. If we have a searchString object, then
we use it to query Lucene. If not, we set a render parameter.
} else if (mode.equals(PortletMode.EDIT)) {
/**
* If the Submit button is passed as a parameter, then we
34
Chapter 1
04 469513 Ch01.qxd 1/16/04 11:04 AM Page 34