001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.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.util.GetterUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.kernel.util.WebKeys;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portlet.ratings.model.RatingsStats;
028    import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
029    
030    import java.util.Date;
031    import java.util.Locale;
032    
033    import javax.portlet.PortletURL;
034    
035    import javax.servlet.http.HttpServletRequest;
036    
037    /**
038     * @author Charles May
039     * @author Brian Wing Shun Chan
040     */
041    public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
042    
043            public Indexer<?> getIndexer() {
044                    if (_log.isWarnEnabled()) {
045                            _log.warn(getClass() + " does not implement getIndexer()");
046                    }
047    
048                    return null;
049            }
050    
051            public abstract String getSearchPath();
052    
053            public Summary getSummary(
054                            Indexer<?> indexer, Document document, Locale locale,
055                            String snippet)
056                    throws SearchException {
057    
058                    return indexer.getSummary(document, snippet, null, null);
059            }
060    
061            public abstract String getTitle(String keywords);
062    
063            @Override
064            public String search(
065                            HttpServletRequest request, long groupId, long userId,
066                            String keywords, int startPage, int itemsPerPage, String format)
067                    throws SearchException {
068    
069                    try {
070                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
071                                    WebKeys.THEME_DISPLAY);
072    
073                            SearchContext searchContext = SearchContextFactory.getInstance(
074                                    request);
075    
076                            searchContext.setAttribute("paginationType", "more");
077    
078                            if (groupId == 0) {
079                                    searchContext.setGroupIds(null);
080                            }
081                            else {
082                                    searchContext.setGroupIds(new long[] {groupId});
083                            }
084    
085                            int end = startPage * itemsPerPage;
086    
087                            searchContext.setEnd(end);
088    
089                            Layout layout = themeDisplay.getLayout();
090    
091                            Group layoutGroup = layout.getGroup();
092    
093                            if (!layoutGroup.isStagingGroup() &&
094                                    !layoutGroup.isControlPanel()) {
095    
096                                    searchContext.setIncludeStagingGroups(false);
097                            }
098    
099                            searchContext.setKeywords(keywords);
100                            searchContext.setScopeStrict(false);
101    
102                            int start = (startPage * itemsPerPage) - itemsPerPage;
103    
104                            searchContext.setStart(start);
105    
106                            searchContext.setUserId(userId);
107    
108                            Indexer<?> indexer = getIndexer();
109    
110                            Hits results = indexer.search(searchContext);
111    
112                            String[] queryTerms = results.getQueryTerms();
113    
114                            int total = results.getLength();
115    
116                            Object[] values = addSearchResults(
117                                    queryTerms, keywords, startPage, itemsPerPage, total, start,
118                                    getTitle(keywords), getSearchPath(), format, themeDisplay);
119    
120                            com.liferay.portal.kernel.xml.Document doc =
121                                    (com.liferay.portal.kernel.xml.Document)values[0];
122                            Element root = (Element)values[1];
123    
124                            for (int i = 0; i < results.getDocs().length; i++) {
125                                    Document result = results.doc(i);
126    
127                                    String snippet = result.get(Field.SNIPPET);
128    
129                                    long resultGroupId = GetterUtil.getLong(
130                                            result.get(Field.GROUP_ID));
131    
132                                    if (resultGroupId == 0) {
133                                            resultGroupId = themeDisplay.getScopeGroupId();
134                                    }
135    
136                                    long resultScopeGroupId = GetterUtil.getLong(
137                                            result.get(Field.SCOPE_GROUP_ID));
138    
139                                    if (resultScopeGroupId == 0) {
140                                            resultScopeGroupId = themeDisplay.getScopeGroupId();
141                                    }
142    
143                                    String className = indexer.getClassName();
144    
145                                    if (Validator.isNull(className)) {
146                                            className = result.get(Field.ENTRY_CLASS_NAME);
147                                    }
148    
149                                    PortletURL portletURL = getPortletURL(
150                                            request, className, PortletProvider.Action.VIEW,
151                                            resultScopeGroupId);
152    
153                                    Summary summary = getSummary(
154                                            indexer, result, themeDisplay.getLocale(), snippet);
155    
156                                    String title = summary.getTitle();
157                                    String url = getURL(
158                                            themeDisplay, resultScopeGroupId, result, portletURL);
159                                    Date modifiedDate = result.getDate(Field.MODIFIED_DATE);
160                                    String content = summary.getContent();
161    
162                                    String[] tags = new String[0];
163    
164                                    Field assetTagNamesField = result.getFields().get(
165                                            Field.ASSET_TAG_NAMES);
166    
167                                    if (assetTagNamesField != null) {
168                                            tags = assetTagNamesField.getValues();
169                                    }
170    
171                                    double ratings = 0.0;
172    
173                                    String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
174                                    long entryClassPK = GetterUtil.getLong(
175                                            result.get(Field.ENTRY_CLASS_PK));
176    
177                                    if (Validator.isNotNull(entryClassName) && (entryClassPK > 0)) {
178                                            RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
179                                                    entryClassName, entryClassPK);
180    
181                                            ratings = stats.getTotalScore();
182                                    }
183    
184                                    double score = results.score(i);
185    
186                                    addSearchResult(
187                                            root, resultGroupId, resultScopeGroupId, entryClassName,
188                                            entryClassPK, title, url, modifiedDate, content, tags,
189                                            ratings, score, format);
190                            }
191    
192                            if (_log.isDebugEnabled()) {
193                                    _log.debug("Return\n" + doc.asXML());
194                            }
195    
196                            return doc.asXML();
197                    }
198                    catch (Exception e) {
199                            throw new SearchException(e);
200                    }
201            }
202    
203            protected String getURL(
204                            ThemeDisplay themeDisplay, long groupId, Document result,
205                            PortletURL portletURL)
206                    throws Exception {
207    
208                    return portletURL.toString();
209            }
210    
211            private static final Log _log = LogFactoryUtil.getLog(
212                    HitsOpenSearchImpl.class);
213    
214    }