1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.search;
24  
25  import com.liferay.portal.kernel.search.Document;
26  import com.liferay.portal.kernel.search.DocumentSummary;
27  import com.liferay.portal.kernel.search.Field;
28  import com.liferay.portal.kernel.search.Hits;
29  import com.liferay.portal.kernel.search.Indexer;
30  import com.liferay.portal.kernel.search.SearchException;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.InstancePool;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.model.Layout;
36  import com.liferay.portal.model.Portlet;
37  import com.liferay.portal.service.CompanyLocalServiceUtil;
38  import com.liferay.portal.service.LayoutLocalServiceUtil;
39  import com.liferay.portal.service.PortletLocalServiceUtil;
40  import com.liferay.portal.theme.ThemeDisplay;
41  import com.liferay.portal.util.PortalUtil;
42  import com.liferay.portal.util.PortletKeys;
43  import com.liferay.portal.util.WebKeys;
44  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
45  
46  import java.util.Date;
47  import java.util.List;
48  
49  import javax.portlet.PortletURL;
50  
51  import javax.servlet.http.HttpServletRequest;
52  
53  import org.apache.commons.logging.Log;
54  import org.apache.commons.logging.LogFactory;
55  
56  import org.dom4j.Element;
57  
58  /**
59   * <a href="PortalOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
60   *
61   * @author Charles May
62   * @author Brian Wing Shun Chan
63   *
64   */
65  public class PortalOpenSearchImpl extends BaseOpenSearchImpl {
66  
67      public static final String SEARCH_PATH = "/c/search/open_search";
68  
69      public String search(
70              HttpServletRequest request, String keywords, int startPage,
71              int itemsPerPage)
72          throws SearchException {
73  
74          try {
75              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
76                  WebKeys.THEME_DISPLAY);
77  
78              int start = (startPage * itemsPerPage) - itemsPerPage;
79              int end = startPage * itemsPerPage;
80  
81              Hits results = CompanyLocalServiceUtil.search(
82                  themeDisplay.getCompanyId(), keywords, start, end);
83  
84              int total = results.getLength();
85  
86              Object[] values = addSearchResults(
87                  keywords, startPage, itemsPerPage, total, start,
88                  "Liferay Portal Search: " + keywords, SEARCH_PATH,
89                  themeDisplay);
90  
91              org.dom4j.Document doc = (org.dom4j.Document)values[0];
92              Element root = (Element)values[1];
93  
94              for (int i = 0; i < results.getDocs().length; i++) {
95                  Document result = results.doc(i);
96  
97                  String portletId = result.get(Field.PORTLET_ID);
98  
99                  Portlet portlet = PortletLocalServiceUtil.getPortletById(
100                     themeDisplay.getCompanyId(), portletId);
101 
102                 if (portlet == null) {
103                     continue;
104                 }
105 
106                 String portletTitle = PortalUtil.getPortletTitle(
107                     portletId, themeDisplay.getUser());
108 
109                 long groupId = GetterUtil.getLong(result.get(Field.GROUP_ID));
110 
111                 String title = StringPool.BLANK;
112 
113                 PortletURL portletURL = getPortletURL(
114                     request, portletId, groupId);
115 
116                 String url = portletURL.toString();
117 
118                 Date modifedDate = result.getDate(Field.MODIFIED);
119 
120                 String content = StringPool.BLANK;
121 
122                 if (Validator.isNotNull(portlet.getIndexerClass())) {
123                     Indexer indexer = (Indexer)InstancePool.get(
124                         portlet.getIndexerClass());
125 
126                     DocumentSummary docSummary = indexer.getDocumentSummary(
127                         result, portletURL);
128 
129                     title = docSummary.getTitle();
130                     url = portletURL.toString();
131                     content = docSummary.getContent();
132 
133                     if (portlet.getPortletId().equals(PortletKeys.JOURNAL)) {
134                         url = getJournalURL(themeDisplay, groupId, result);
135                     }
136                 }
137 
138                 double score = results.score(i);
139 
140                 addSearchResult(
141                     root, portletTitle + " &raquo; " + title, url, modifedDate,
142                     content, score);
143             }
144 
145             if (_log.isDebugEnabled()) {
146                 _log.debug("Return\n" + doc.asXML());
147             }
148 
149             return doc.asXML();
150 
151         }
152         catch (Exception e) {
153             throw new SearchException(e);
154         }
155     }
156 
157     protected String getJournalURL(
158             ThemeDisplay themeDisplay, long groupId, Document result)
159         throws Exception {
160 
161         Layout layout = themeDisplay.getLayout();
162 
163         String articleId = result.get(Field.ENTRY_CLASS_PK);
164         String version = result.get("version");
165 
166         List<Long> hitLayoutIds =
167             JournalContentSearchLocalServiceUtil.getLayoutIds(
168                 layout.getGroupId(), layout.isPrivateLayout(), articleId);
169 
170         if (hitLayoutIds.size() > 0) {
171             Long hitLayoutId = hitLayoutIds.get(0);
172 
173             Layout hitLayout = LayoutLocalServiceUtil.getLayout(
174                 layout.getGroupId(), layout.isPrivateLayout(),
175                 hitLayoutId.longValue());
176 
177             return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
178         }
179         else {
180             StringBuilder sb = new StringBuilder();
181 
182             sb.append(themeDisplay.getPathMain());
183             sb.append("/journal/view_article_content?groupId=");
184             sb.append(groupId);
185             sb.append("&articleId=");
186             sb.append(articleId);
187             sb.append("&version=");
188             sb.append(version);
189 
190             return sb.toString();
191         }
192     }
193 
194     private static Log _log = LogFactory.getLog(PortalOpenSearchImpl.class);
195 
196 }