001: package ru.emdev.EmForge.wiki.web.bean;
002:
003: import java.io.IOException;
004: import java.util.ArrayList;
005: import java.util.Collection;
006:
007: import org.apache.commons.lang.StringUtils;
008:
009: import ru.emdev.EmForge.web.bean.MainMenuController.MainMenuItem;
010:
011: import com.ecyrd.jspwiki.SearchResult;
012: import com.ecyrd.jspwiki.WikiContext;
013: import com.ecyrd.jspwiki.WikiPage;
014: import com.ecyrd.jspwiki.providers.ProviderException;
015:
016: public class SearchController extends BaseWikiControllerImpl {
017:
018: private String m_query = "Search...";
019:
020: public static final String SEARCH = "Search";
021: public static final String SEARCH_RESULT_PAGE = "search";
022: public static final String QUERY_PARAM = "query";
023:
024: @Override
025: protected void init() {
026: if (getParam(QUERY_PARAM) != null) {
027: m_query = getParam(QUERY_PARAM);
028: }
029: }
030:
031: @Override
032: public MainMenuItem getSelectionItemOnMainMenu() {
033: return null;
034: }
035:
036: @Override
037: public String getTitleImpl() {
038: return SEARCH;
039: }
040:
041: @Override
042: public Crumb getTrailCrumbInfo() {
043: return null;
044: }
045:
046: public void setQuery(String i_query) {
047: m_query = i_query;
048: }
049:
050: public String getQuery() {
051: return m_query;
052: }
053:
054: public String go() {
055: if (StringUtils.isNotEmpty(m_query)) {
056: redirect(SEARCH_RESULT_PAGE + "/" + m_query);
057: }
058:
059: return null;
060: }
061:
062: public boolean isSearchResultEmpty() {
063: return m_isEmpty;
064: }
065:
066: private boolean m_isEmpty = false;
067:
068: @SuppressWarnings("unchecked")
069: public Collection<EmForgeSearchResult> getSearchResults() {
070: Collection result = new ArrayList<Object>();
071: if (StringUtils.isNotEmpty(m_query)) {
072: try {
073: Collection<SearchResult> luceneResults = m_wikiEngine
074: .findPages(m_query);
075: //
076: // Conversion the lucene result to own result.
077: // It necessary that each result stores 'view' that is required to make a navigation link to either a page or a process.
078: // for a page: "wiki/pageName";
079: // for a process "process/processId";
080: //
081: for (SearchResult luceneResult : luceneResults) {
082: result.add(new EmForgeSearchResult(luceneResult));
083: }
084:
085: // addFoundedProcessToResult(result);
086:
087: } catch (ProviderException e) {
088: logger.error(e);
089: return result;
090: } catch (IOException e) {
091: logger.error(e);
092: return result;
093: }
094: }
095: m_isEmpty = result.isEmpty();
096: return result;
097: }
098:
099: /** adds a process if exists to the result
100: *
101: * @param io_searchResult
102: * @param i_query
103: */
104: /* seems it is not required
105: private void addFoundedProcessToResult(Collection<EmForgeSearchResult> io_searchResult) {
106: if (Helper.isNumber(m_query)) {
107: WikiPage processPage = m_wikiEngine.getPage(m_query);
108: if (processPage != null) {
109: EmForgeSearchResult procPageResult = new EmForgeSearchResult(processPage);
110: io_searchResult.add(procPageResult);
111: }
112: }
113: }
114: */
115:
116: public class EmForgeSearchResult {
117: WikiPage m_page;
118: String[] m_fragments = new String[0];
119: int m_score;
120: String m_view;
121:
122: public EmForgeSearchResult(SearchResult i_searchResult) {
123: m_page = i_searchResult.getPage();
124: m_fragments = i_searchResult.getContexts();
125: m_score = i_searchResult.getScore();
126: m_view = m_urlConstructor.makeURL(WikiContext.VIEW, m_page
127: .getName(), false, null);
128: }
129:
130: public EmForgeSearchResult(WikiPage i_page) {
131: m_page = i_page;
132: m_fragments = new String[1];
133: m_view = "process";
134: }
135:
136: public void setView(String i_view) {
137: m_view = i_view;
138: }
139:
140: public String getView() {
141: return m_view;
142: }
143:
144: public WikiPage getPage() {
145: return m_page;
146: }
147:
148: public int getScore() {
149: return m_score;
150: }
151:
152: public String getFragment() {
153: if (m_fragments.length > 0) {
154: return m_fragments[0];
155: }
156: return null;
157: }
158: }
159: }
|