001    /**
002     * Copyright (c) 2000-2010 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.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                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
077                                    themeDisplay.getCompanyId(), getPortletId());
078    
079                            Indexer indexer = portlet.getIndexerInstance();
080    
081                            Hits results = indexer.search(searchContext);
082    
083                            String[] queryTerms = results.getQueryTerms();
084    
085                            int total = results.getLength();
086    
087                            Object[] values = addSearchResults(
088                                    queryTerms, keywords, startPage, itemsPerPage, total, start,
089                                    getTitle(keywords), getSearchPath(), format, themeDisplay);
090    
091                            com.liferay.portal.kernel.xml.Document doc =
092                                    (com.liferay.portal.kernel.xml.Document)values[0];
093                            Element root = (Element)values[1];
094    
095                            for (int i = 0; i < results.getDocs().length; i++) {
096                                    Document result = results.doc(i);
097    
098                                    String portletId = result.get(Field.PORTLET_ID);
099    
100                                    String snippet = results.snippet(i);
101    
102                                    long resultGroupId = GetterUtil.getLong(
103                                            result.get(Field.GROUP_ID));
104    
105                                    PortletURL portletURL = getPortletURL(
106                                            request, portletId, resultGroupId);
107    
108                                    Summary summary = getSummary(
109                                            indexer, result, snippet, portletURL);
110    
111                                    String title = summary.getTitle();
112                                    String url = getURL(
113                                            themeDisplay, resultGroupId, result, portletURL);
114                                    Date modifedDate = result.getDate(Field.MODIFIED);
115                                    String content = summary.getContent();
116    
117                                    String[] tags = new String[0];
118    
119                                    Field assetTagNamesField = result.getFields().get(
120                                            Field.ASSET_TAG_NAMES);
121    
122                                    if (assetTagNamesField != null) {
123                                            tags = assetTagNamesField.getValues();
124                                    }
125    
126                                    double ratings = 0.0;
127    
128                                    String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
129                                    long entryClassPK = GetterUtil.getLong(
130                                            result.get(Field.ENTRY_CLASS_PK));
131    
132                                    if ((Validator.isNotNull(entryClassName)) &&
133                                            (entryClassPK > 0)) {
134    
135                                            RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
136                                                    entryClassName, entryClassPK);
137    
138                                            ratings = stats.getTotalScore();
139                                    }
140    
141                                    double score = results.score(i);
142    
143                                    addSearchResult(
144                                            root, resultGroupId, entryClassName, entryClassPK, title,
145                                            url, modifedDate, content, tags, ratings, score, format);
146                            }
147    
148                            if (_log.isDebugEnabled()) {
149                                    _log.debug("Return\n" + doc.asXML());
150                            }
151    
152                            return doc.asXML();
153                    }
154                    catch (Exception e) {
155                            throw new SearchException(e);
156                    }
157            }
158    
159            protected String getURL(
160                            ThemeDisplay themeDisplay, long groupId, Document result,
161                            PortletURL portletURL)
162                    throws Exception {
163    
164                    return portletURL.toString();
165            }
166    
167            private static Log _log = LogFactoryUtil.getLog(HitsOpenSearchImpl.class);
168    
169    }