2 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
\r
4 * This library is free software; you can redistribute it and/or modify it under
\r
5 * the terms of the GNU Lesser General Public License as published by the Free
\r
6 * Software Foundation; either version 2.1 of the License, or (at your option)
\r
9 * This library is distributed in the hope that it will be useful, but WITHOUT
\r
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
\r
15 package com.liferay.portlet.documentlibrary.util;
\r
17 import java.util.Date;
\r
18 import java.util.Locale;
\r
20 import javax.portlet.PortletURL;
\r
21 import javax.servlet.http.HttpServletRequest;
\r
23 import com.liferay.portal.kernel.dao.orm.QueryUtil;
\r
24 import com.liferay.portal.kernel.log.Log;
\r
25 import com.liferay.portal.kernel.log.LogFactoryUtil;
\r
26 import com.liferay.portal.kernel.search.BaseOpenSearchImpl;
\r
27 import com.liferay.portal.kernel.search.Document;
\r
28 import com.liferay.portal.kernel.search.Field;
\r
29 import com.liferay.portal.kernel.search.Hits;
\r
30 import com.liferay.portal.kernel.search.HitsOpenSearchImpl;
\r
31 import com.liferay.portal.kernel.search.Indexer;
\r
32 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
\r
33 import com.liferay.portal.kernel.search.SearchContext;
\r
34 import com.liferay.portal.kernel.search.SearchException;
\r
35 import com.liferay.portal.kernel.search.Summary;
\r
36 import com.liferay.portal.kernel.util.GetterUtil;
\r
37 import com.liferay.portal.kernel.util.Validator;
\r
38 import com.liferay.portal.kernel.util.WebKeys;
\r
39 import com.liferay.portal.kernel.xml.Element;
\r
40 import com.liferay.portal.model.Portlet;
\r
41 import com.liferay.portal.service.PortletLocalServiceUtil;
\r
42 import com.liferay.portal.theme.ThemeDisplay;
\r
43 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
\r
44 import com.liferay.portlet.documentlibrary.store.CustomDLStoreUtil;
\r
45 import com.liferay.portlet.ratings.model.RatingsStats;
\r
46 import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
\r
49 * @author Brian Wing Shun Chan
\r
51 public class DLOpenSearchImpl extends HitsOpenSearchImpl {
\r
53 public static final String SEARCH_PATH = "/c/document_library/open_search";
\r
55 public static final String TITLE = "Liferay Document Library Search: ";
\r
57 public Indexer getIndexer() {
\r
58 return IndexerRegistryUtil.getIndexer(DLFileEntry.class);
\r
61 public String getPortletId() {
\r
62 return DLIndexer.PORTLET_ID;
\r
65 public String getSearchPath() {
\r
69 public Summary getSummary(
\r
70 Indexer indexer, Document document, Locale locale, String snippet,
\r
71 PortletURL portletURL) throws SearchException {
\r
73 return indexer.getSummary(document, locale, snippet, portletURL);
\r
76 public String getTitle(String keywords) {
\r
77 return TITLE + keywords;
\r
80 public Hits getHits(
\r
81 long companyId, long userId, String portletId, long groupId,
\r
82 long[] repositoryIds, String keywords, int start, int end)
\r
85 return CustomDLStoreUtil.search(companyId, userId, portletId, groupId, repositoryIds, keywords, start, end);
\r
88 public String search(HttpServletRequest request, long groupId,
\r
89 long folderId, long userId, String keywords, int startPage,
\r
90 int itemsPerPage, String format) throws SearchException {
\r
92 return search(request, userId, groupId, new long[]{folderId}, keywords, startPage, itemsPerPage, format);
\r
95 public String search(
\r
96 HttpServletRequest request, long groupId, long userId,
\r
97 String keywords, int startPage, int itemsPerPage, String format)
\r
98 throws SearchException {
\r
99 return search(request, groupId, 0, userId, keywords, startPage, itemsPerPage, format);
\r
102 public String search(
\r
103 HttpServletRequest request, long groupId, long folderId, long userId, long accountId,
\r
104 String keywords, int startPage, int itemsPerPage, String format)
\r
105 throws SearchException {
\r
106 return search(request, groupId, folderId, userId, keywords, startPage, itemsPerPage, format);
\r
109 public String search(
\r
110 HttpServletRequest request, long userId, long groupId,
\r
111 long[] repositoryIds, String keywords, int startPage, int itemsPerPage, String format)
\r
112 throws SearchException {
\r
115 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
\r
116 WebKeys.THEME_DISPLAY);
\r
118 int start = (startPage * itemsPerPage) - itemsPerPage;
\r
119 Hits results = null;
\r
121 if((startPage == QueryUtil.ALL_POS)||(itemsPerPage == QueryUtil.ALL_POS)){
\r
123 themeDisplay.getCompanyId(), userId, getPortletId(), groupId, repositoryIds, keywords, QueryUtil.ALL_POS,
\r
124 QueryUtil.ALL_POS);
\r
126 int end = startPage * itemsPerPage;
\r
128 themeDisplay.getCompanyId(), userId, getPortletId(), groupId, repositoryIds, keywords, start,
\r
131 String[] queryTerms = results.getQueryTerms();
\r
133 int total = results.getLength();
\r
135 Portlet portlet = PortletLocalServiceUtil.getPortletById(
\r
136 themeDisplay.getCompanyId(), getPortletId());
\r
138 Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class);
\r
140 Object[] values = addSearchResults(
\r
141 queryTerms, keywords, startPage, itemsPerPage, total, start,
\r
142 getTitle(keywords), getSearchPath(), format, themeDisplay);
\r
144 com.liferay.portal.kernel.xml.Document doc =
\r
145 (com.liferay.portal.kernel.xml.Document)values[0];
\r
146 Element root = (Element)values[1];
\r
148 for (int i = 0; i < results.getDocs().length; i++) {
\r
149 Document result = results.doc(i);
\r
151 String portletId = result.get(Field.PORTLET_ID);
\r
153 String snippet = results.snippet(i);
\r
155 long resultGroupId = GetterUtil.getLong(
\r
156 result.get(Field.GROUP_ID));
\r
158 PortletURL portletURL = getPortletURL(
\r
159 request, portletId, resultGroupId);
\r
161 Summary summary = getSummary(
\r
162 indexer, result, themeDisplay.getLocale(), snippet, portletURL);
\r
164 String title = summary.getTitle();
\r
165 String url = getURL(
\r
166 themeDisplay, resultGroupId, result, portletURL);
\r
167 Date modifedDate = result.getDate(Field.MODIFIED_DATE);
\r
168 String content = summary.getContent();
\r
170 String[] tags = new String[0];
\r
172 Field assetTagNamesField = result.getFields().get(
\r
173 Field.ASSET_TAG_NAMES);
\r
175 if (assetTagNamesField != null) {
\r
176 tags = assetTagNamesField.getValues();
\r
179 double ratings = 0.0;
\r
181 String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
\r
182 long entryClassPK = GetterUtil.getLong(
\r
183 result.get(Field.ENTRY_CLASS_PK));
\r
185 if ((Validator.isNotNull(entryClassName)) &&
\r
186 (entryClassPK > 0)) {
\r
188 RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
\r
189 entryClassName, entryClassPK);
\r
191 ratings = stats.getTotalScore();
\r
194 double score = results.score(i);
\r
197 root, resultGroupId, groupId, entryClassName, entryClassPK, title,
\r
198 url, modifedDate, content, tags, ratings, score, format);
\r
201 if (_log.isDebugEnabled()) {
\r
202 _log.debug("Return\n" + doc.asXML());
\r
205 return doc.asXML();
\r
207 catch (Exception e) {
\r
208 throw new SearchException(e);
\r
212 protected void addSearchAttributes(
\r
213 long companyId, SearchContext searchContext, String keywords) {
\r
216 protected String getURL(
\r
217 ThemeDisplay themeDisplay, long groupId, Document result,
\r
218 PortletURL portletURL)
\r
221 return portletURL.toString();
\r
224 private static Log _log = LogFactoryUtil.getLog(BaseOpenSearchImpl.class);
\r