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.search;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.search.BaseOpenSearchImpl;
020    import com.liferay.portal.kernel.search.Document;
021    import com.liferay.portal.kernel.search.Field;
022    import com.liferay.portal.kernel.search.Hits;
023    import com.liferay.portal.kernel.search.Indexer;
024    import com.liferay.portal.kernel.search.SearchException;
025    import com.liferay.portal.kernel.search.Summary;
026    import com.liferay.portal.kernel.util.GetterUtil;
027    import com.liferay.portal.kernel.util.InstancePool;
028    import com.liferay.portal.kernel.util.StringBundler;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.Validator;
031    import com.liferay.portal.kernel.xml.Element;
032    import com.liferay.portal.model.Layout;
033    import com.liferay.portal.model.Portlet;
034    import com.liferay.portal.service.CompanyLocalServiceUtil;
035    import com.liferay.portal.service.LayoutLocalServiceUtil;
036    import com.liferay.portal.service.PortletLocalServiceUtil;
037    import com.liferay.portal.theme.ThemeDisplay;
038    import com.liferay.portal.util.PortalUtil;
039    import com.liferay.portal.util.PortletKeys;
040    import com.liferay.portal.util.WebKeys;
041    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
042    
043    import java.util.Date;
044    import java.util.List;
045    
046    import javax.portlet.PortletURL;
047    
048    import javax.servlet.http.HttpServletRequest;
049    
050    /**
051     * @author Charles May
052     * @author Brian Wing Shun Chan
053     */
054    public class PortalOpenSearchImpl extends BaseOpenSearchImpl {
055    
056            public static final String SEARCH_PATH = "/c/search/open_search";
057    
058            public String search(
059                            HttpServletRequest request, long groupId, long userId,
060                            String keywords, int startPage, int itemsPerPage, String format)
061                    throws SearchException {
062    
063                    try {
064                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
065                                    WebKeys.THEME_DISPLAY);
066    
067                            int start = (startPage * itemsPerPage) - itemsPerPage;
068                            int end = startPage * itemsPerPage;
069    
070                            Hits results = CompanyLocalServiceUtil.search(
071                                    themeDisplay.getCompanyId(), userId, keywords, start, end);
072    
073                            String[] queryTerms = results.getQueryTerms();
074    
075                            int total = results.getLength();
076    
077                            Object[] values = addSearchResults(
078                                    queryTerms, keywords, startPage, itemsPerPage, total, start,
079                                    "Liferay Portal Search: " + keywords, SEARCH_PATH, format,
080                                    themeDisplay);
081    
082                            com.liferay.portal.kernel.xml.Document doc =
083                                    (com.liferay.portal.kernel.xml.Document)values[0];
084                            Element root = (Element)values[1];
085    
086                            for (int i = 0; i < results.getDocs().length; i++) {
087                                    Document result = results.doc(i);
088    
089                                    String portletId = result.get(Field.PORTLET_ID);
090    
091                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
092                                            themeDisplay.getCompanyId(), portletId);
093    
094                                    if (portlet == null) {
095                                            continue;
096                                    }
097    
098                                    String portletTitle = PortalUtil.getPortletTitle(
099                                            portletId, themeDisplay.getUser());
100    
101                                    long resultGroupId = GetterUtil.getLong(
102                                            result.get(Field.GROUP_ID));
103    
104                                    String entryClassName = GetterUtil.getString(
105                                            result.get(Field.ENTRY_CLASS_NAME));
106    
107                                    long entryClassPK = GetterUtil.getLong(
108                                            result.get(Field.ENTRY_CLASS_PK));
109    
110                                    String title = StringPool.BLANK;
111    
112                                    PortletURL portletURL = getPortletURL(
113                                            request, portletId, resultGroupId);
114    
115                                    String url = portletURL.toString();
116    
117                                    Date modifedDate = result.getDate(Field.MODIFIED);
118    
119                                    String content = StringPool.BLANK;
120    
121                                    if (Validator.isNotNull(portlet.getIndexerClass())) {
122                                            Indexer indexer = (Indexer)InstancePool.get(
123                                                    portlet.getIndexerClass());
124    
125                                            String snippet = results.snippet(i);
126    
127                                            Summary summary = indexer.getSummary(
128                                                    result, snippet, portletURL);
129    
130                                            title = summary.getTitle();
131                                            url = portletURL.toString();
132                                            content = summary.getContent();
133    
134                                            if (portlet.getPortletId().equals(PortletKeys.JOURNAL)) {
135                                                    url = getJournalURL(
136                                                            themeDisplay, resultGroupId, result);
137                                            }
138                                    }
139    
140                                    double score = results.score(i);
141    
142                                    addSearchResult(
143                                            root, groupId, entryClassName, entryClassPK,
144                                            portletTitle + " &raquo; " + title, url, modifedDate,
145                                            content, score, format);
146                            }
147    
148                            if (_log.isDebugEnabled()) {
149                                    _log.debug("Return\n" + doc.asXML());
150                            }
151    
152                            return doc.asXML();
153    
154                    }
155                    catch (Exception e) {
156                            throw new SearchException(e);
157                    }
158            }
159    
160            protected String getJournalURL(
161                            ThemeDisplay themeDisplay, long groupId, Document result)
162                    throws Exception {
163    
164                    Layout layout = themeDisplay.getLayout();
165    
166                    String articleId = result.get(Field.ENTRY_CLASS_PK);
167                    String version = result.get("version");
168    
169                    List<Long> hitLayoutIds =
170                            JournalContentSearchLocalServiceUtil.getLayoutIds(
171                                    layout.getGroupId(), layout.isPrivateLayout(), articleId);
172    
173                    if (hitLayoutIds.size() > 0) {
174                            Long hitLayoutId = hitLayoutIds.get(0);
175    
176                            Layout hitLayout = LayoutLocalServiceUtil.getLayout(
177                                    layout.getGroupId(), layout.isPrivateLayout(),
178                                    hitLayoutId.longValue());
179    
180                            return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
181                    }
182                    else {
183                            StringBundler sb = new StringBundler(7);
184    
185                            sb.append(themeDisplay.getPathMain());
186                            sb.append("/journal/view_article_content?groupId=");
187                            sb.append(groupId);
188                            sb.append("&articleId=");
189                            sb.append(articleId);
190                            sb.append("&version=");
191                            sb.append(version);
192    
193                            return sb.toString();
194                    }
195            }
196    
197            private static Log _log = LogFactoryUtil.getLog(PortalOpenSearchImpl.class);
198    
199    }