001: /*
002: * regain - A file search engine providing plenty of formats
003: * Copyright (C) 2004 Til Schneider
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: Til Schneider, info@murfman.de
020: *
021: * CVS information:
022: * $RCSfile$
023: * $Source$
024: * $Date: 2005-08-07 12:51:09 +0200 (So, 07 Aug 2005) $
025: * $Author: til132 $
026: * $Revision: 145 $
027: */
028: package net.sf.regain.search.sharedlib;
029:
030: import net.sf.regain.RegainException;
031: import net.sf.regain.search.SearchConstants;
032: import net.sf.regain.search.SearchToolkit;
033: import net.sf.regain.search.results.SearchResults;
034: import net.sf.regain.util.sharedtag.PageRequest;
035: import net.sf.regain.util.sharedtag.PageResponse;
036: import net.sf.regain.util.sharedtag.SharedTag;
037:
038: import org.apache.lucene.document.Document;
039:
040: /**
041: * The list tag encloses the JSP code that should be repeated for every shown
042: * search hit.
043: * <p>
044: * Request Parameters:
045: * <ul>
046: * <li><code>fromresult</code>: The index of the first result to show.</li>
047: * <li><code>maxresults</code>: The maximum number of results to show.</li>
048: * </ul>
049: * <p>
050: * Tag Parameters:
051: * <ul>
052: * <li><code>msgNoResults</code>: The message to generate if the were no
053: * results.</li>
054: * </ul>
055: *
056: * @author Til Schneider, www.murfman.de
057: */
058: public class ListTag extends SharedTag implements SearchConstants {
059:
060: /** The index of the currently generated result. */
061: private int mCurrentResult;
062:
063: /** The index of the last generated index on this page. */
064: private int mToResult;
065:
066: /**
067: * Called when the parser reaches the start tag.
068: * <p>
069: * Initializes the list generation.
070: *
071: * @param request The page request.
072: * @param response The page response.
073: * @return {@link #EVAL_TAG_BODY} if you want the tag body to be evaluated or
074: * {@link #SKIP_TAG_BODY} if you want the tag body to be skipped.
075: * @throws RegainException If there was an exception.
076: */
077: public int printStartTag(PageRequest request, PageResponse response)
078: throws RegainException {
079: SearchResults results = SearchToolkit.getSearchResults(request);
080:
081: int fromResult = request
082: .getParameterAsInt(PARAM_FROM_RESULT, 0);
083: int maxResults = request.getParameterAsInt(PARAM_MAX_RESULTS,
084: SearchConstants.DEFAULT_MAX_RESULTS);
085:
086: if (results.getHitCount() == 0) {
087: String msgNoResults = getParameter("msgNoResults");
088: if (msgNoResults != null) {
089: response.print(msgNoResults);
090: }
091:
092: return SKIP_TAG_BODY;
093: } else {
094: mCurrentResult = fromResult;
095:
096: mToResult = fromResult + maxResults - 1;
097: if (mToResult >= results.getHitCount()) {
098: mToResult = results.getHitCount() - 1;
099: }
100:
101: writeHitToAttributes(mCurrentResult, results, request);
102:
103: return EVAL_TAG_BODY;
104: }
105: }
106:
107: /**
108: * Writes a hit to the page context, so it may be read by the hit tags.
109: *
110: * @param hitIndex The index of the hit.
111: * @param results The SearchResults to read the hit from.
112: * @param request The page request to write the hit to.
113: * @throws RegainException If the hit could not be read.
114: */
115: private void writeHitToAttributes(int hitIndex,
116: SearchResults results, PageRequest request)
117: throws RegainException {
118: try {
119: Document hit = results.getHitDocument(hitIndex);
120: request.setContextAttribute(ATTR_CURRENT_HIT, hit);
121: float score = results.getHitScore(hitIndex);
122: request.setContextAttribute(ATTR_CURRENT_HIT_SCORE,
123: new Float(score));
124: request.setContextAttribute(ATTR_CURRENT_HIT_INDEX,
125: new Integer(hitIndex));
126: } catch (RegainException exc) {
127: throw new RegainException("Getting hit #" + hitIndex
128: + " failed", exc);
129: }
130: }
131:
132: /**
133: * Called after the body content was evaluated.
134: * <p>
135: * Decides whether there are more hits to generate HTML for. If yes the next
136: * hit is put to the page attributes.
137: *
138: * @param request The page request.
139: * @param response The page response.
140: * @return {@link #EVAL_TAG_BODY} if you want the tag body to be evaluated
141: * once again or {@link #SKIP_TAG_BODY} if you want to print the
142: * end tag.
143: * @throws RegainException If there was an exception.
144: */
145: public int printAfterBody(PageRequest request, PageResponse response)
146: throws RegainException {
147: mCurrentResult++;
148:
149: if (mCurrentResult <= mToResult) {
150: SearchResults results = SearchToolkit
151: .getSearchResults(request);
152: writeHitToAttributes(mCurrentResult, results, request);
153:
154: return EVAL_TAG_BODY;
155: } else {
156: return SKIP_TAG_BODY;
157: }
158: }
159:
160: }
|