1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.search;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.search.Document;
20  import com.liferay.portal.kernel.search.Field;
21  import com.liferay.portal.kernel.search.Hits;
22  import com.liferay.portal.kernel.search.Indexer;
23  import com.liferay.portal.kernel.search.SearchException;
24  import com.liferay.portal.kernel.search.Summary;
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.InstancePool;
27  import com.liferay.portal.kernel.util.StringBundler;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.kernel.xml.Element;
31  import com.liferay.portal.model.Layout;
32  import com.liferay.portal.model.Portlet;
33  import com.liferay.portal.service.CompanyLocalServiceUtil;
34  import com.liferay.portal.service.LayoutLocalServiceUtil;
35  import com.liferay.portal.service.PortletLocalServiceUtil;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.PortletKeys;
39  import com.liferay.portal.util.WebKeys;
40  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
41  
42  import java.util.Date;
43  import java.util.List;
44  
45  import javax.portlet.PortletURL;
46  
47  import javax.servlet.http.HttpServletRequest;
48  
49  /**
50   * <a href="PortalOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Charles May
53   * @author Brian Wing Shun Chan
54   */
55  public class PortalOpenSearchImpl extends BaseOpenSearchImpl {
56  
57      public static final String SEARCH_PATH = "/c/search/open_search";
58  
59      public String search(
60              HttpServletRequest request, long groupId, long userId,
61              String keywords, int startPage, int itemsPerPage, String format)
62          throws SearchException {
63  
64          try {
65              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
66                  WebKeys.THEME_DISPLAY);
67  
68              int start = (startPage * itemsPerPage) - itemsPerPage;
69              int end = startPage * itemsPerPage;
70  
71              Hits results = CompanyLocalServiceUtil.search(
72                  themeDisplay.getCompanyId(), userId, keywords, start, end);
73  
74              String[] queryTerms = results.getQueryTerms();
75  
76              int total = results.getLength();
77  
78              Object[] values = addSearchResults(
79                  queryTerms, keywords, startPage, itemsPerPage, total, start,
80                  "Liferay Portal Search: " + keywords, SEARCH_PATH, format,
81                  themeDisplay);
82  
83              com.liferay.portal.kernel.xml.Document doc =
84                  (com.liferay.portal.kernel.xml.Document)values[0];
85              Element root = (Element)values[1];
86  
87              for (int i = 0; i < results.getDocs().length; i++) {
88                  Document result = results.doc(i);
89  
90                  String portletId = result.get(Field.PORTLET_ID);
91  
92                  Portlet portlet = PortletLocalServiceUtil.getPortletById(
93                      themeDisplay.getCompanyId(), portletId);
94  
95                  if (portlet == null) {
96                      continue;
97                  }
98  
99                  String portletTitle = PortalUtil.getPortletTitle(
100                     portletId, themeDisplay.getUser());
101 
102                 long resultGroupId = GetterUtil.getLong(
103                     result.get(Field.GROUP_ID));
104 
105                 String title = StringPool.BLANK;
106 
107                 PortletURL portletURL = getPortletURL(
108                     request, portletId, resultGroupId);
109 
110                 String url = portletURL.toString();
111 
112                 Date modifedDate = result.getDate(Field.MODIFIED);
113 
114                 String content = StringPool.BLANK;
115 
116                 if (Validator.isNotNull(portlet.getIndexerClass())) {
117                     Indexer indexer = (Indexer)InstancePool.get(
118                         portlet.getIndexerClass());
119 
120                     String snippet = results.snippet(i);
121 
122                     Summary summary = indexer.getSummary(
123                         result, snippet, portletURL);
124 
125                     title = summary.getTitle();
126                     url = portletURL.toString();
127                     content = summary.getContent();
128 
129                     if (portlet.getPortletId().equals(PortletKeys.JOURNAL)) {
130                         url = getJournalURL(
131                             themeDisplay, resultGroupId, result);
132                     }
133                 }
134 
135                 double score = results.score(i);
136 
137                 addSearchResult(
138                     root, portletTitle + " &raquo; " + title, url, modifedDate,
139                     content, score, format);
140             }
141 
142             if (_log.isDebugEnabled()) {
143                 _log.debug("Return\n" + doc.asXML());
144             }
145 
146             return doc.asXML();
147 
148         }
149         catch (Exception e) {
150             throw new SearchException(e);
151         }
152     }
153 
154     protected String getJournalURL(
155             ThemeDisplay themeDisplay, long groupId, Document result)
156         throws Exception {
157 
158         Layout layout = themeDisplay.getLayout();
159 
160         String articleId = result.get(Field.ENTRY_CLASS_PK);
161         String version = result.get("version");
162 
163         List<Long> hitLayoutIds =
164             JournalContentSearchLocalServiceUtil.getLayoutIds(
165                 layout.getGroupId(), layout.isPrivateLayout(), articleId);
166 
167         if (hitLayoutIds.size() > 0) {
168             Long hitLayoutId = hitLayoutIds.get(0);
169 
170             Layout hitLayout = LayoutLocalServiceUtil.getLayout(
171                 layout.getGroupId(), layout.isPrivateLayout(),
172                 hitLayoutId.longValue());
173 
174             return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
175         }
176         else {
177             StringBundler sb = new StringBundler(7);
178 
179             sb.append(themeDisplay.getPathMain());
180             sb.append("/journal/view_article_content?groupId=");
181             sb.append(groupId);
182             sb.append("&articleId=");
183             sb.append(articleId);
184             sb.append("&version=");
185             sb.append(version);
186 
187             return sb.toString();
188         }
189     }
190 
191     private static Log _log = LogFactoryUtil.getLog(PortalOpenSearchImpl.class);
192 
193 }