001
014
015 package com.liferay.portal.search;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.portlet.PortletProvider;
020 import com.liferay.portal.kernel.portlet.PortletProviderUtil;
021 import com.liferay.portal.kernel.search.BaseOpenSearchImpl;
022 import com.liferay.portal.kernel.search.Document;
023 import com.liferay.portal.kernel.search.Field;
024 import com.liferay.portal.kernel.search.Hits;
025 import com.liferay.portal.kernel.search.Indexer;
026 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
027 import com.liferay.portal.kernel.search.SearchException;
028 import com.liferay.portal.kernel.search.Summary;
029 import com.liferay.portal.kernel.util.CharPool;
030 import com.liferay.portal.kernel.util.GetterUtil;
031 import com.liferay.portal.kernel.util.StringPool;
032 import com.liferay.portal.kernel.util.WebKeys;
033 import com.liferay.portal.kernel.xml.Element;
034 import com.liferay.portal.model.Portlet;
035 import com.liferay.portal.service.CompanyLocalServiceUtil;
036 import com.liferay.portal.service.PortletLocalServiceUtil;
037 import com.liferay.portal.theme.ThemeDisplay;
038 import com.liferay.portal.util.PortalUtil;
039
040 import java.util.Date;
041
042 import javax.portlet.PortletURL;
043
044 import javax.servlet.http.HttpServletRequest;
045
046
050 public class PortalOpenSearchImpl extends BaseOpenSearchImpl {
051
052 public PortalOpenSearchImpl(
053 String openSearchURL, String openSearchDescriptionURL) {
054
055 super(openSearchURL, openSearchDescriptionURL);
056 }
057
058 @Override
059 public String search(
060 HttpServletRequest request, long groupId, long userId,
061 String keywords, int startPage, int itemsPerPage, String format)
062 throws SearchException {
063
064 try {
065 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
066 WebKeys.THEME_DISPLAY);
067
068 int start = (startPage * itemsPerPage) - itemsPerPage;
069 int end = startPage * itemsPerPage;
070
071 Hits results = CompanyLocalServiceUtil.search(
072 themeDisplay.getCompanyId(), userId, keywords, start, end);
073
074 String[] queryTerms = results.getQueryTerms();
075
076 int total = results.getLength();
077
078 Object[] values = addSearchResults(
079 queryTerms, keywords, startPage, itemsPerPage, total, start,
080 "Liferay Portal Search: " + keywords, StringPool.BLANK, format,
081 themeDisplay);
082
083 com.liferay.portal.kernel.xml.Document doc =
084 (com.liferay.portal.kernel.xml.Document)values[0];
085 Element root = (Element)values[1];
086
087 for (int i = 0; i < results.getDocs().length; i++) {
088 Document result = results.doc(i);
089
090 String className = result.get(Field.ENTRY_CLASS_NAME);
091
092 String portletId = PortletProviderUtil.getPortletId(
093 className, PortletProvider.Action.VIEW);
094
095 Portlet portlet = PortletLocalServiceUtil.getPortletById(
096 themeDisplay.getCompanyId(), portletId);
097
098 if (portlet == null) {
099 continue;
100 }
101
102 String portletTitle = PortalUtil.getPortletTitle(
103 portletId, themeDisplay.getUser());
104
105 long resultGroupId = GetterUtil.getLong(
106 result.get(Field.GROUP_ID));
107
108 long resultScopeGroupId = GetterUtil.getLong(
109 result.get(Field.SCOPE_GROUP_ID));
110
111 if (resultScopeGroupId == 0) {
112 resultScopeGroupId = themeDisplay.getScopeGroupId();
113 }
114
115 String entryClassName = GetterUtil.getString(
116 result.get(Field.ENTRY_CLASS_NAME));
117
118 long entryClassPK = GetterUtil.getLong(
119 result.get(Field.ENTRY_CLASS_PK));
120
121 String title = StringPool.BLANK;
122
123 PortletURL portletURL = getPortletURL(
124 request, portletId, resultScopeGroupId);
125
126 String url = portletURL.toString();
127
128 Date modifiedDate = result.getDate(Field.MODIFIED_DATE);
129
130 String content = StringPool.BLANK;
131
132 Indexer<?> indexer = IndexerRegistryUtil.getIndexer(
133 entryClassName);
134
135 if (indexer != null) {
136 String snippet = result.get(Field.SNIPPET);
137
138 Summary summary = indexer.getSummary(
139 result, snippet, null, null);
140
141 if (summary == null) {
142 continue;
143 }
144
145 title = summary.getTitle();
146 url = portletURL.toString();
147 content = summary.getContent();
148 }
149
150 double score = results.score(i);
151
152 addSearchResult(
153 root, resultGroupId, resultScopeGroupId, entryClassName,
154 entryClassPK,
155 portletTitle + " " + CharPool.RAQUO + " " + title, url,
156 modifiedDate, content, score, format);
157 }
158
159 if (_log.isDebugEnabled()) {
160 _log.debug("Return\n" + doc.asXML());
161 }
162
163 return doc.asXML();
164 }
165 catch (Exception e) {
166 throw new SearchException(e);
167 }
168 }
169
170 private static final Log _log = LogFactoryUtil.getLog(
171 PortalOpenSearchImpl.class);
172
173 }