1   /**
2    * Copyright (c) 2000-2007 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.Hits;
28  import com.liferay.portal.kernel.search.Indexer;
29  import com.liferay.portal.kernel.search.SearchException;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.InstancePool;
32  import com.liferay.portal.kernel.util.StringMaker;
33  import com.liferay.portal.kernel.util.StringPool;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.lucene.LuceneFields;
36  import com.liferay.portal.model.Layout;
37  import com.liferay.portal.model.Portlet;
38  import com.liferay.portal.service.CompanyLocalServiceUtil;
39  import com.liferay.portal.service.LayoutLocalServiceUtil;
40  import com.liferay.portal.service.PortletLocalServiceUtil;
41  import com.liferay.portal.theme.ThemeDisplay;
42  import com.liferay.portal.util.PortalUtil;
43  import com.liferay.portal.util.PortletKeys;
44  import com.liferay.portal.util.WebKeys;
45  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
46  
47  import java.util.Date;
48  import java.util.List;
49  
50  import javax.portlet.PortletURL;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  import org.apache.commons.logging.Log;
55  import org.apache.commons.logging.LogFactory;
56  import org.apache.lucene.document.DateTools;
57  
58  import org.dom4j.Element;
59  
60  /**
61   * <a href="PortalOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Charles May
64   * @author Brian Wing Shun Chan
65   *
66   */
67  public class PortalOpenSearchImpl extends BaseOpenSearchImpl {
68  
69      public static final String SEARCH_PATH = "/c/search/open_search";
70  
71      public String search(
72              HttpServletRequest req, String keywords, int startPage,
73              int itemsPerPage)
74          throws SearchException {
75  
76          Hits hits = null;
77  
78          try {
79              ThemeDisplay themeDisplay =
80                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
81  
82              hits = CompanyLocalServiceUtil.search(
83                  themeDisplay.getCompanyId(), keywords);
84  
85              Object[] values = addSearchResults(
86                  keywords, startPage, itemsPerPage, hits,
87                  "Liferay Portal Search: " + keywords, SEARCH_PATH,
88                  themeDisplay);
89  
90              Hits results = (Hits)values[0];
91              org.dom4j.Document doc = (org.dom4j.Document)values[1];
92              Element root = (Element)values[2];
93  
94              for (int i = 0; i < results.getLength(); i++) {
95                  Document result = results.doc(i);
96  
97                  String portletId = (String)result.get(LuceneFields.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(
110                     (String)result.get(LuceneFields.GROUP_ID));
111 
112                 String title = StringPool.BLANK;
113 
114                 PortletURL portletURL = getPortletURL(req, portletId, groupId);
115 
116                 String url = portletURL.toString();
117 
118                 Date modifedDate = DateTools.stringToDate(
119                     (String)result.get(LuceneFields.MODIFIED));
120 
121                 String content = StringPool.BLANK;
122 
123                 if (Validator.isNotNull(portlet.getIndexerClass())) {
124                     Indexer indexer = (Indexer)InstancePool.get(
125                         portlet.getIndexerClass());
126 
127                     DocumentSummary docSummary = indexer.getDocumentSummary(
128                         result, portletURL);
129 
130                     title = docSummary.getTitle();
131                     url = portletURL.toString();
132                     content = docSummary.getContent();
133 
134                     if (portlet.getPortletId().equals(PortletKeys.JOURNAL)) {
135                         url = getJournalURL(themeDisplay, groupId, result);
136                     }
137                 }
138 
139                 double score = hits.score(i);
140 
141                 addSearchResult(
142                     root, portletTitle + " &raquo; " + title, url, modifedDate,
143                     content, score);
144             }
145 
146             if (_log.isDebugEnabled()) {
147                 _log.debug("Return\n" + doc.asXML());
148             }
149 
150             return doc.asXML();
151 
152         }
153         catch (Exception e) {
154             throw new SearchException(e);
155         }
156         finally {
157             if (hits != null) {
158                 hits.closeSearcher();
159             }
160         }
161     }
162 
163     protected String getJournalURL(
164             ThemeDisplay themeDisplay, long groupId, Document result)
165         throws Exception {
166 
167         Layout layout = themeDisplay.getLayout();
168 
169         String articleId = result.get("articleId");
170         String version = result.get("version");
171 
172         List hitLayoutIds =
173             JournalContentSearchLocalServiceUtil.getLayoutIds(
174                 layout.getGroupId(), layout.isPrivateLayout(), articleId);
175 
176         if (hitLayoutIds.size() > 0) {
177             Long hitLayoutId = (Long)hitLayoutIds.get(0);
178 
179             Layout hitLayout = LayoutLocalServiceUtil.getLayout(
180                 layout.getGroupId(), layout.isPrivateLayout(),
181                 hitLayoutId.longValue());
182 
183             return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
184         }
185         else {
186             StringMaker sm = new StringMaker();
187 
188             sm.append(themeDisplay.getPathMain());
189             sm.append("/journal/view_article_content?groupId=");
190             sm.append(groupId);
191             sm.append("&articleId=");
192             sm.append(articleId);
193             sm.append("&version=");
194             sm.append(version);
195 
196             return sm.toString();
197         }
198     }
199 
200     private static Log _log = LogFactory.getLog(PortalOpenSearchImpl.class);
201 
202 }