001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.util.GetterUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.model.Portlet;
024    import com.liferay.portal.service.PortletLocalServiceUtil;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portlet.ratings.model.RatingsStats;
027    import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
028    
029    import java.util.Date;
030    
031    import javax.portlet.PortletURL;
032    
033    import javax.servlet.http.HttpServletRequest;
034    
035    /**
036     * @author Charles May
037     * @author Brian Wing Shun Chan
038     */
039    public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
040    
041            public abstract String getPortletId();
042    
043            public abstract String getSearchPath();
044    
045            public Summary getSummary(
046                    Indexer indexer, Document document, String snippet,
047                    PortletURL portletURL) {
048    
049                    return indexer.getSummary(document, snippet, portletURL);
050            }
051    
052            public abstract String getTitle(String keywords);
053    
054            public String search(
055                            HttpServletRequest request, long groupId, long userId,
056                            String keywords, int startPage, int itemsPerPage, String format)
057                    throws SearchException {
058    
059                    try {
060                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
061                                    WebKeys.THEME_DISPLAY);
062    
063                            int start = (startPage * itemsPerPage) - itemsPerPage;
064                            int end = startPage * itemsPerPage;
065    
066                            SearchContext searchContext = SearchContextFactory.getInstance(
067                                    request);
068    
069                            searchContext.setGroupIds(new long[] {groupId});
070                            searchContext.setEnd(end);
071                            searchContext.setKeywords(keywords);
072                            searchContext.setScopeStrict(false);
073                            searchContext.setStart(start);
074                            searchContext.setUserId(userId);
075    
076                            addSearchAttributes(
077                                    themeDisplay.getCompanyId(), searchContext, keywords);
078    
079                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
080                                    themeDisplay.getCompanyId(), getPortletId());
081    
082                            Indexer indexer = portlet.getIndexerInstance();
083    
084                            Hits results = indexer.search(searchContext);
085    
086                            String[] queryTerms = results.getQueryTerms();
087    
088                            int total = results.getLength();
089    
090                            Object[] values = addSearchResults(
091                                    queryTerms, keywords, startPage, itemsPerPage, total, start,
092                                    getTitle(keywords), getSearchPath(), format, themeDisplay);
093    
094                            com.liferay.portal.kernel.xml.Document doc =
095                                    (com.liferay.portal.kernel.xml.Document)values[0];
096                            Element root = (Element)values[1];
097    
098                            for (int i = 0; i < results.getDocs().length; i++) {
099                                    Document result = results.doc(i);
100    
101                                    String portletId = getPortletId();
102    
103                                    if (Validator.isNull(portletId)) {
104                                            portletId = result.get(Field.PORTLET_ID);
105                                    }
106    
107                                    String snippet = results.snippet(i);
108    
109                                    long resultGroupId = GetterUtil.getLong(
110                                            result.get(Field.GROUP_ID));
111    
112                                    long resultScopeGroupId = GetterUtil.getLong(
113                                            result.get(Field.SCOPE_GROUP_ID));
114    
115                                    PortletURL portletURL = getPortletURL(
116                                            request, portletId, resultGroupId);
117    
118                                    Summary summary = getSummary(
119                                            indexer, result, snippet, portletURL);
120    
121                                    String title = summary.getTitle();
122                                    String url = getURL(
123                                            themeDisplay, resultScopeGroupId, result, portletURL);
124                                    Date modifedDate = result.getDate(Field.MODIFIED);
125                                    String content = summary.getContent();
126    
127                                    String[] tags = new String[0];
128    
129                                    Field assetTagNamesField = result.getFields().get(
130                                            Field.ASSET_TAG_NAMES);
131    
132                                    if (assetTagNamesField != null) {
133                                            tags = assetTagNamesField.getValues();
134                                    }
135    
136                                    double ratings = 0.0;
137    
138                                    String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
139                                    long entryClassPK = GetterUtil.getLong(
140                                            result.get(Field.ENTRY_CLASS_PK));
141    
142                                    if ((Validator.isNotNull(entryClassName)) &&
143                                            (entryClassPK > 0)) {
144    
145                                            RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
146                                                    entryClassName, entryClassPK);
147    
148                                            ratings = stats.getTotalScore();
149                                    }
150    
151                                    double score = results.score(i);
152    
153                                    addSearchResult(
154                                            root, resultGroupId, resultScopeGroupId, entryClassName,
155                                            entryClassPK, title, url, modifedDate, content, tags,
156                                            ratings, 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            protected void addSearchAttributes(
171                    long companyId, SearchContext searchContext, String keywords) {
172            }
173    
174            protected String getURL(
175                            ThemeDisplay themeDisplay, long groupId, Document result,
176                            PortletURL portletURL)
177                    throws Exception {
178    
179                    return portletURL.toString();
180            }
181    
182            private static Log _log = LogFactoryUtil.getLog(HitsOpenSearchImpl.class);
183    
184    }