--- /dev/null
+/**\r
+ * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.\r
+ *\r
+ * This library is free software; you can redistribute it and/or modify it under\r
+ * the terms of the GNU Lesser General Public License as published by the Free\r
+ * Software Foundation; either version 2.1 of the License, or (at your option)\r
+ * any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more\r
+ * details.\r
+ */\r
+\r
+package com.liferay.portlet.documentlibrary.util;\r
+\r
+import java.util.Date;\r
+import java.util.Locale;\r
+\r
+import javax.portlet.PortletURL;\r
+import javax.servlet.http.HttpServletRequest;\r
+\r
+import com.liferay.portal.kernel.dao.orm.QueryUtil;\r
+import com.liferay.portal.kernel.log.Log;\r
+import com.liferay.portal.kernel.log.LogFactoryUtil;\r
+import com.liferay.portal.kernel.search.BaseOpenSearchImpl;\r
+import com.liferay.portal.kernel.search.Document;\r
+import com.liferay.portal.kernel.search.Field;\r
+import com.liferay.portal.kernel.search.Hits;\r
+import com.liferay.portal.kernel.search.HitsOpenSearchImpl;\r
+import com.liferay.portal.kernel.search.Indexer;\r
+import com.liferay.portal.kernel.search.IndexerRegistryUtil;\r
+import com.liferay.portal.kernel.search.SearchContext;\r
+import com.liferay.portal.kernel.search.SearchException;\r
+import com.liferay.portal.kernel.search.Summary;\r
+import com.liferay.portal.kernel.util.GetterUtil;\r
+import com.liferay.portal.kernel.util.Validator;\r
+import com.liferay.portal.kernel.util.WebKeys;\r
+import com.liferay.portal.kernel.xml.Element;\r
+import com.liferay.portal.model.Portlet;\r
+import com.liferay.portal.service.PortletLocalServiceUtil;\r
+import com.liferay.portal.theme.ThemeDisplay;\r
+import com.liferay.portlet.documentlibrary.model.DLFileEntry;\r
+import com.liferay.portlet.documentlibrary.store.CustomDLStoreUtil;\r
+import com.liferay.portlet.ratings.model.RatingsStats;\r
+import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;\r
+\r
+/**\r
+ * @author Brian Wing Shun Chan\r
+ */\r
+public class DLOpenSearchImpl extends HitsOpenSearchImpl {\r
+\r
+ public static final String SEARCH_PATH = "/c/document_library/open_search";\r
+\r
+ public static final String TITLE = "Liferay Document Library Search: ";\r
+\r
+ public Indexer getIndexer() {\r
+ return IndexerRegistryUtil.getIndexer(DLFileEntry.class);\r
+ }\r
+\r
+ public String getPortletId() {\r
+ return DLIndexer.PORTLET_ID;\r
+ }\r
+\r
+ public String getSearchPath() {\r
+ return SEARCH_PATH;\r
+ }\r
+ \r
+ public Summary getSummary(\r
+ Indexer indexer, Document document, Locale locale, String snippet,\r
+ PortletURL portletURL) throws SearchException {\r
+\r
+ return indexer.getSummary(document, locale, snippet, portletURL);\r
+ }\r
+\r
+ public String getTitle(String keywords) {\r
+ return TITLE + keywords;\r
+ }\r
+\r
+ public Hits getHits(\r
+ long companyId, long userId, String portletId, long groupId,\r
+ long[] repositoryIds, String keywords, int start, int end)\r
+ throws Exception {\r
+\r
+ return CustomDLStoreUtil.search(companyId, userId, portletId, groupId, repositoryIds, keywords, start, end);\r
+ }\r
+ \r
+ public String search(HttpServletRequest request, long groupId,\r
+ long folderId, long userId, String keywords, int startPage,\r
+ int itemsPerPage, String format) throws SearchException {\r
+ \r
+ return search(request, userId, groupId, new long[]{folderId}, keywords, startPage, itemsPerPage, format);\r
+ }\r
+\r
+ public String search(\r
+ HttpServletRequest request, long groupId, long userId,\r
+ String keywords, int startPage, int itemsPerPage, String format)\r
+ throws SearchException {\r
+ return search(request, groupId, 0, userId, keywords, startPage, itemsPerPage, format);\r
+ }\r
+\r
+ public String search(\r
+ HttpServletRequest request, long groupId, long folderId, long userId, long accountId,\r
+ String keywords, int startPage, int itemsPerPage, String format)\r
+ throws SearchException {\r
+ return search(request, groupId, folderId, userId, keywords, startPage, itemsPerPage, format);\r
+ }\r
+\r
+ public String search(\r
+ HttpServletRequest request, long userId, long groupId,\r
+ long[] repositoryIds, String keywords, int startPage, int itemsPerPage, String format)\r
+ throws SearchException {\r
+\r
+ try {\r
+ ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(\r
+ WebKeys.THEME_DISPLAY);\r
+\r
+ int start = (startPage * itemsPerPage) - itemsPerPage;\r
+ Hits results = null;\r
+ \r
+ if((startPage == QueryUtil.ALL_POS)||(itemsPerPage == QueryUtil.ALL_POS)){\r
+ results = getHits(\r
+ themeDisplay.getCompanyId(), userId, getPortletId(), groupId, repositoryIds, keywords, QueryUtil.ALL_POS,\r
+ QueryUtil.ALL_POS);\r
+ } else {\r
+ int end = startPage * itemsPerPage;\r
+ results = getHits(\r
+ themeDisplay.getCompanyId(), userId, getPortletId(), groupId, repositoryIds, keywords, start,\r
+ end);\r
+ }\r
+ String[] queryTerms = results.getQueryTerms();\r
+\r
+ int total = results.getLength();\r
+ \r
+ Portlet portlet = PortletLocalServiceUtil.getPortletById(\r
+ themeDisplay.getCompanyId(), getPortletId());\r
+ \r
+ Indexer indexer = IndexerRegistryUtil.getIndexer(DLFileEntry.class);\r
+\r
+ Object[] values = addSearchResults(\r
+ queryTerms, keywords, startPage, itemsPerPage, total, start,\r
+ getTitle(keywords), getSearchPath(), format, themeDisplay);\r
+\r
+ com.liferay.portal.kernel.xml.Document doc =\r
+ (com.liferay.portal.kernel.xml.Document)values[0];\r
+ Element root = (Element)values[1];\r
+\r
+ for (int i = 0; i < results.getDocs().length; i++) {\r
+ Document result = results.doc(i);\r
+\r
+ String portletId = result.get(Field.PORTLET_ID);\r
+\r
+ String snippet = results.snippet(i);\r
+\r
+ long resultGroupId = GetterUtil.getLong(\r
+ result.get(Field.GROUP_ID));\r
+\r
+ PortletURL portletURL = getPortletURL(\r
+ request, portletId, resultGroupId);\r
+\r
+ Summary summary = getSummary(\r
+ indexer, result, themeDisplay.getLocale(), snippet, portletURL);\r
+\r
+ String title = summary.getTitle();\r
+ String url = getURL(\r
+ themeDisplay, resultGroupId, result, portletURL);\r
+ Date modifedDate = result.getDate(Field.MODIFIED_DATE);\r
+ String content = summary.getContent();\r
+\r
+ String[] tags = new String[0];\r
+\r
+ Field assetTagNamesField = result.getFields().get(\r
+ Field.ASSET_TAG_NAMES);\r
+\r
+ if (assetTagNamesField != null) {\r
+ tags = assetTagNamesField.getValues();\r
+ }\r
+\r
+ double ratings = 0.0;\r
+\r
+ String entryClassName = result.get(Field.ENTRY_CLASS_NAME);\r
+ long entryClassPK = GetterUtil.getLong(\r
+ result.get(Field.ENTRY_CLASS_PK));\r
+\r
+ if ((Validator.isNotNull(entryClassName)) &&\r
+ (entryClassPK > 0)) {\r
+\r
+ RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(\r
+ entryClassName, entryClassPK);\r
+\r
+ ratings = stats.getTotalScore();\r
+ }\r
+\r
+ double score = results.score(i);\r
+\r
+ addSearchResult(\r
+ root, resultGroupId, groupId, entryClassName, entryClassPK, title,\r
+ url, modifedDate, content, tags, ratings, score, format);\r
+ }\r
+\r
+ if (_log.isDebugEnabled()) {\r
+ _log.debug("Return\n" + doc.asXML());\r
+ }\r
+\r
+ return doc.asXML();\r
+ }\r
+ catch (Exception e) {\r
+ throw new SearchException(e);\r
+ }\r
+ }\r
+ \r
+ protected void addSearchAttributes(\r
+ long companyId, SearchContext searchContext, String keywords) {\r
+ }\r
+\r
+ protected String getURL(\r
+ ThemeDisplay themeDisplay, long groupId, Document result,\r
+ PortletURL portletURL)\r
+ throws Exception {\r
+\r
+ return portletURL.toString();\r
+ }\r
+ \r
+ private static Log _log = LogFactoryUtil.getLog(BaseOpenSearchImpl.class);\r
+ \r
+}
\ No newline at end of file