001: /*
002: JSPWiki - a JSP-based WikiWiki clone.
003:
004: Copyright (C) 2005 Janne Jalkanen (Janne.Jalkanen@iki.fi)
005:
006: This program is free software; you can redistribute it and/or modify
007: it under the terms of the GNU Lesser General Public License as published by
008: the Free Software Foundation; either version 2.1 of the License, or
009: (at your option) any later version.
010:
011: This program is distributed in the hope that it will be useful,
012: but WITHOUT ANY WARRANTY; without even the implied warranty of
013: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: GNU Lesser General Public License for more details.
015:
016: You should have received a copy of the GNU Lesser General Public License
017: along with this program; if not, write to the Free Software
018: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package com.ecyrd.jspwiki.plugin;
021:
022: import java.io.IOException;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import org.apache.ecs.xhtml.*;
028: import org.apache.log4j.Logger;
029:
030: import com.ecyrd.jspwiki.SearchResult;
031: import com.ecyrd.jspwiki.WikiContext;
032: import com.ecyrd.jspwiki.WikiEngine;
033: import com.ecyrd.jspwiki.providers.ProviderException;
034:
035: /**
036: * @author jalkanen
037: *
038: * @since
039: */
040: public class Search implements WikiPlugin {
041: static Logger log = Logger.getLogger(Search.class);
042:
043: public static final String PARAM_QUERY = "query";
044: public static final String PARAM_SET = "set";
045: public static final String DEFAULT_SETNAME = "_defaultSet";
046: public static final String PARAM_MAX = "max";
047:
048: /* (non-Javadoc)
049: * @see com.ecyrd.jspwiki.plugin.WikiPlugin#execute(com.ecyrd.jspwiki.WikiContext, java.util.Map)
050: */
051: public String execute(WikiContext context, Map params)
052: throws PluginException {
053: int maxItems = Integer.MAX_VALUE;
054: Collection results = null;
055:
056: String queryString = (String) params.get(PARAM_QUERY);
057: String set = (String) params.get(PARAM_SET);
058: String max = (String) params.get(PARAM_MAX);
059:
060: if (set == null)
061: set = DEFAULT_SETNAME;
062: if (max != null)
063: maxItems = Integer.parseInt(max);
064:
065: if (queryString == null) {
066: results = (Collection) context.getVariable(set);
067: } else {
068: try {
069: results = doBasicQuery(context, queryString);
070: context.setVariable(set, results);
071: } catch (Exception e) {
072: return "<div class='error'>" + e.getMessage()
073: + "</div>\n";
074: }
075: }
076:
077: String res = "";
078:
079: if (results != null) {
080: res = renderResults(results, context, maxItems);
081: }
082:
083: return res;
084: }
085:
086: private Collection doBasicQuery(WikiContext context, String query)
087: throws ProviderException, IOException {
088: log.debug("Searching for string " + query);
089:
090: Collection list = context.getEngine().findPages(query);
091:
092: return list;
093: }
094:
095: private String renderResults(Collection results,
096: WikiContext context, int maxItems) {
097: WikiEngine engine = context.getEngine();
098: table t = new table();
099: t.setBorder(0);
100: t.setCellPadding(4);
101:
102: tr row = new tr();
103: t.addElement(row);
104:
105: row.addElement(new th().setWidth("30%").setAlign("left")
106: .addElement("Page"));
107: row.addElement(new th().setAlign("left").addElement("Score"));
108:
109: int idx = 0;
110: for (Iterator i = results.iterator(); i.hasNext()
111: && idx++ <= maxItems;) {
112: SearchResult sr = (SearchResult) i.next();
113: row = new tr();
114:
115: td name = new td().setWidth("30%");
116: name.addElement("<a href=\""
117: + context.getURL(WikiContext.VIEW, sr.getPage()
118: .getName()) + "\">"
119: + engine.beautifyTitle(sr.getPage().getName())
120: + "</a>");
121: row.addElement(name);
122:
123: row.addElement(new td().addElement("" + sr.getScore()));
124:
125: t.addElement(row);
126: }
127:
128: if (results.isEmpty()) {
129: row = new tr();
130:
131: row.addElement(new td().setColSpan(2).addElement(
132: new b().addElement("No results")));
133:
134: t.addElement(row);
135: }
136:
137: return t.toString();
138: }
139: }
|