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.kernel.util.StringMaker;
031: import com.liferay.portal.kernel.util.StringPool;
032: import com.liferay.portal.kernel.util.Validator;
033: import com.liferay.portal.lucene.LuceneFields;
034: import com.liferay.portal.model.Layout;
035: import com.liferay.portal.model.Portlet;
036: import com.liferay.portal.service.CompanyLocalServiceUtil;
037: import com.liferay.portal.service.LayoutLocalServiceUtil;
038: import com.liferay.portal.service.PortletLocalServiceUtil;
039: import com.liferay.portal.theme.ThemeDisplay;
040: import com.liferay.portal.util.PortalUtil;
041: import com.liferay.portal.util.PortletKeys;
042: import com.liferay.portal.util.WebKeys;
043: import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
044:
045: import java.util.Date;
046: import java.util.List;
047:
048: import javax.portlet.PortletURL;
049:
050: import javax.servlet.http.HttpServletRequest;
051:
052: import org.apache.commons.logging.Log;
053: import org.apache.commons.logging.LogFactory;
054: import org.apache.lucene.document.DateTools;
055:
056: import org.dom4j.Element;
057:
058: /**
059: * <a href="PortalOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
060: *
061: * @author Charles May
062: * @author Brian Wing Shun Chan
063: *
064: */
065: public class PortalOpenSearchImpl extends BaseOpenSearchImpl {
066:
067: public static final String SEARCH_PATH = "/c/search/open_search";
068:
069: public String search(HttpServletRequest req, String keywords,
070: int startPage, int itemsPerPage) throws SearchException {
071:
072: Hits hits = null;
073:
074: try {
075: ThemeDisplay themeDisplay = (ThemeDisplay) req
076: .getAttribute(WebKeys.THEME_DISPLAY);
077:
078: hits = CompanyLocalServiceUtil.search(themeDisplay
079: .getCompanyId(), keywords);
080:
081: Object[] values = addSearchResults(keywords, startPage,
082: itemsPerPage, hits, "Liferay Portal Search: "
083: + keywords, SEARCH_PATH, themeDisplay);
084:
085: Hits results = (Hits) values[0];
086: org.dom4j.Document doc = (org.dom4j.Document) values[1];
087: Element root = (Element) values[2];
088:
089: for (int i = 0; i < results.getLength(); i++) {
090: Document result = results.doc(i);
091:
092: String portletId = (String) result
093: .get(LuceneFields.PORTLET_ID);
094:
095: Portlet portlet = PortletLocalServiceUtil
096: .getPortletById(themeDisplay.getCompanyId(),
097: portletId);
098:
099: if (portlet == null) {
100: continue;
101: }
102:
103: String portletTitle = PortalUtil.getPortletTitle(
104: portletId, themeDisplay.getUser());
105:
106: long groupId = GetterUtil.getLong((String) result
107: .get(LuceneFields.GROUP_ID));
108:
109: String title = StringPool.BLANK;
110:
111: PortletURL portletURL = getPortletURL(req, portletId,
112: groupId);
113:
114: String url = portletURL.toString();
115:
116: Date modifedDate = DateTools
117: .stringToDate((String) result
118: .get(LuceneFields.MODIFIED));
119:
120: String content = StringPool.BLANK;
121:
122: if (Validator.isNotNull(portlet.getIndexerClass())) {
123: Indexer indexer = (Indexer) InstancePool
124: .get(portlet.getIndexerClass());
125:
126: DocumentSummary docSummary = indexer
127: .getDocumentSummary(result, portletURL);
128:
129: title = docSummary.getTitle();
130: url = portletURL.toString();
131: content = docSummary.getContent();
132:
133: if (portlet.getPortletId().equals(
134: PortletKeys.JOURNAL)) {
135: url = getJournalURL(themeDisplay, groupId,
136: result);
137: }
138: }
139:
140: double score = hits.score(i);
141:
142: addSearchResult(root, portletTitle + " » "
143: + title, url, modifedDate, content, score);
144: }
145:
146: if (_log.isDebugEnabled()) {
147: _log.debug("Return\n" + doc.asXML());
148: }
149:
150: return doc.asXML();
151:
152: } catch (Exception e) {
153: throw new SearchException(e);
154: } finally {
155: if (hits != null) {
156: hits.closeSearcher();
157: }
158: }
159: }
160:
161: protected String getJournalURL(ThemeDisplay themeDisplay,
162: long groupId, Document result) throws Exception {
163:
164: Layout layout = themeDisplay.getLayout();
165:
166: String articleId = result.get("articleId");
167: String version = result.get("version");
168:
169: List hitLayoutIds = JournalContentSearchLocalServiceUtil
170: .getLayoutIds(layout.getGroupId(), layout
171: .isPrivateLayout(), articleId);
172:
173: if (hitLayoutIds.size() > 0) {
174: Long hitLayoutId = (Long) hitLayoutIds.get(0);
175:
176: Layout hitLayout = LayoutLocalServiceUtil.getLayout(layout
177: .getGroupId(), layout.isPrivateLayout(),
178: hitLayoutId.longValue());
179:
180: return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
181: } else {
182: StringMaker sm = new StringMaker();
183:
184: sm.append(themeDisplay.getPathMain());
185: sm.append("/journal/view_article_content?groupId=");
186: sm.append(groupId);
187: sm.append("&articleId=");
188: sm.append(articleId);
189: sm.append("&version=");
190: sm.append(version);
191:
192: return sm.toString();
193: }
194: }
195:
196: private static Log _log = LogFactory
197: .getLog(PortalOpenSearchImpl.class);
198:
199: }
|