001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.search;
022:
023: import com.liferay.portal.kernel.search.Document;
024: import com.liferay.portal.kernel.search.DocumentSummary;
025: import com.liferay.portal.kernel.search.Hits;
026: import com.liferay.portal.kernel.search.Indexer;
027: import com.liferay.portal.kernel.search.SearchException;
028: import com.liferay.portal.kernel.util.GetterUtil;
029: import com.liferay.portal.kernel.util.InstancePool;
030: import com.liferay.portal.lucene.LuceneFields;
031: import com.liferay.portal.model.Portlet;
032: import com.liferay.portal.service.PortletLocalServiceUtil;
033: import com.liferay.portal.theme.ThemeDisplay;
034: import com.liferay.portal.util.WebKeys;
035:
036: import java.util.Date;
037:
038: import javax.portlet.PortletURL;
039:
040: import javax.servlet.http.HttpServletRequest;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044: import org.apache.lucene.document.DateTools;
045:
046: import org.dom4j.Element;
047:
048: /**
049: * <a href="HitsOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
050: *
051: * @author Charles May
052: * @author Brian Wing Shun Chan
053: *
054: */
055: public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
056:
057: public abstract Hits getHits(long companyId, String keywords)
058: throws Exception;
059:
060: public abstract String getSearchPath();
061:
062: public abstract String getTitle(String keywords);
063:
064: public String search(HttpServletRequest req, String keywords,
065: int startPage, int itemsPerPage) throws SearchException {
066:
067: Hits hits = null;
068:
069: try {
070: ThemeDisplay themeDisplay = (ThemeDisplay) req
071: .getAttribute(WebKeys.THEME_DISPLAY);
072:
073: hits = getHits(themeDisplay.getCompanyId(), keywords);
074:
075: Object[] values = addSearchResults(keywords, startPage,
076: itemsPerPage, hits, getTitle(keywords),
077: getSearchPath(), themeDisplay);
078:
079: Hits results = (Hits) values[0];
080: org.dom4j.Document doc = (org.dom4j.Document) values[1];
081: Element root = (Element) values[2];
082:
083: for (int i = 0; i < results.getLength(); i++) {
084: Document result = results.doc(i);
085:
086: String portletId = (String) result
087: .get(LuceneFields.PORTLET_ID);
088:
089: Portlet portlet = PortletLocalServiceUtil
090: .getPortletById(themeDisplay.getCompanyId(),
091: portletId);
092:
093: //String portletTitle = PortalUtil.getPortletTitle(
094: // portletId, themeDisplay.getUser());
095:
096: long groupId = GetterUtil.getLong((String) result
097: .get(LuceneFields.GROUP_ID));
098:
099: PortletURL portletURL = getPortletURL(req, portletId,
100: groupId);
101:
102: Indexer indexer = (Indexer) InstancePool.get(portlet
103: .getIndexerClass());
104:
105: DocumentSummary docSummary = indexer
106: .getDocumentSummary(result, portletURL);
107:
108: String title = docSummary.getTitle();
109: String url = getURL(themeDisplay, groupId, result,
110: portletURL);
111: Date modifedDate = DateTools
112: .stringToDate((String) result
113: .get(LuceneFields.MODIFIED));
114: String content = docSummary.getContent();
115: double score = hits.score(i);
116:
117: addSearchResult(root, title, url, modifedDate, content,
118: score);
119: }
120:
121: if (_log.isDebugEnabled()) {
122: _log.debug("Return\n" + doc.asXML());
123: }
124:
125: return doc.asXML();
126: } catch (Exception e) {
127: throw new SearchException(e);
128: } finally {
129: if (hits != null) {
130: hits.closeSearcher();
131: }
132: }
133: }
134:
135: protected String getURL(ThemeDisplay themeDisplay, long groupId,
136: Document result, PortletURL portletURL) throws Exception {
137:
138: return portletURL.toString();
139: }
140:
141: private static Log _log = LogFactory
142: .getLog(HitsOpenSearchImpl.class);
143:
144: }
|