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                            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 = result.get(Field.PORTLET_ID);
102    
103                                    String snippet = results.snippet(i);
104    
105                                    long resultGroupId = GetterUtil.getLong(
106                                            result.get(Field.GROUP_ID));
107    
108                                    PortletURL portletURL = getPortletURL(
109                                            request, portletId, resultGroupId);
110    
111                                    Summary summary = getSummary(
112                                            indexer, result, snippet, portletURL);
113    
114                                    String title = summary.getTitle();
115                                    String url = getURL(
116                                            themeDisplay, resultGroupId, result, portletURL);
117                                    Date modifedDate = result.getDate(Field.MODIFIED);
118                                    String content = summary.getContent();
119    
120                                    String[] tags = new String[0];
121    
122                                    Field assetTagNamesField = result.getFields().get(
123                                            Field.ASSET_TAG_NAMES);
124    
125                                    if (assetTagNamesField != null) {
126                                            tags = assetTagNamesField.getValues();
127                                    }
128    
129                                    double ratings = 0.0;
130    
131                                    String entryClassName = result.get(Field.ENTRY_CLASS_NAME);
132                                    long entryClassPK = GetterUtil.getLong(
133                                            result.get(Field.ENTRY_CLASS_PK));
134    
135                                    if ((Validator.isNotNull(entryClassName)) &&
136                                            (entryClassPK > 0)) {
137    
138                                            RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
139                                                    entryClassName, entryClassPK);
140    
141                                            ratings = stats.getTotalScore();
142                                    }
143    
144                                    double score = results.score(i);
145    
146                                    addSearchResult(
147                                            root, resultGroupId, entryClassName, entryClassPK, title,
148                                            url, modifedDate, content, tags, ratings, score, format);
149                            }
150    
151                            if (_log.isDebugEnabled()) {
152                                    _log.debug("Return\n" + doc.asXML());
153                            }
154    
155                            return doc.asXML();
156                    }
157                    catch (Exception e) {
158                            throw new SearchException(e);
159                    }
160            }
161    
162            protected void addSearchAttributes(
163                    long companyId, SearchContext searchContext, String keywords) {
164            }
165    
166            protected String getURL(
167                            ThemeDisplay themeDisplay, long groupId, Document result,
168                            PortletURL portletURL)
169                    throws Exception {
170    
171                    return portletURL.toString();
172            }
173    
174            private static Log _log = LogFactoryUtil.getLog(HitsOpenSearchImpl.class);
175    
176    }