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.lucene.LuceneFields;
33  import com.liferay.portal.model.Portlet;
34  import com.liferay.portal.service.PortletLocalServiceUtil;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.WebKeys;
37  
38  import java.util.Date;
39  
40  import javax.portlet.PortletURL;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  import org.apache.lucene.document.DateTools;
47  
48  import org.dom4j.Element;
49  
50  /**
51   * <a href="HitsOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Charles May
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public abstract class HitsOpenSearchImpl extends BaseOpenSearchImpl {
58  
59      public abstract Hits getHits(long companyId, String keywords)
60          throws Exception;
61  
62      public abstract String getSearchPath();
63  
64      public abstract String getTitle(String keywords);
65  
66      public String search(
67              HttpServletRequest req, String keywords, int startPage,
68              int itemsPerPage)
69          throws SearchException {
70  
71          Hits hits = null;
72  
73          try {
74              ThemeDisplay themeDisplay =
75                  (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
76  
77              hits = getHits(themeDisplay.getCompanyId(), keywords);
78  
79              Object[] values = addSearchResults(
80                  keywords, startPage, itemsPerPage, hits, getTitle(keywords),
81                  getSearchPath(), themeDisplay);
82  
83              Hits results = (Hits)values[0];
84              org.dom4j.Document doc = (org.dom4j.Document)values[1];
85              Element root = (Element)values[2];
86  
87              for (int i = 0; i < results.getLength(); i++) {
88                  Document result = results.doc(i);
89  
90                  String portletId = (String)result.get(LuceneFields.PORTLET_ID);
91  
92                  Portlet portlet = PortletLocalServiceUtil.getPortletById(
93                      themeDisplay.getCompanyId(), portletId);
94  
95                  //String portletTitle = PortalUtil.getPortletTitle(
96                  //  portletId, themeDisplay.getUser());
97  
98                  long groupId = GetterUtil.getLong(
99                      (String)result.get(LuceneFields.GROUP_ID));
100 
101                 PortletURL portletURL = getPortletURL(req, portletId, groupId);
102 
103                 Indexer indexer = (Indexer)InstancePool.get(
104                     portlet.getIndexerClass());
105 
106                 DocumentSummary docSummary = indexer.getDocumentSummary(
107                     result, portletURL);
108 
109                 String title = docSummary.getTitle();
110                 String url = getURL(themeDisplay, groupId, result, portletURL);
111                 Date modifedDate = DateTools.stringToDate(
112                     (String)result.get(LuceneFields.MODIFIED));
113                 String content = docSummary.getContent();
114                 double score = hits.score(i);
115 
116                 addSearchResult(root, title, url, modifedDate, content, score);
117             }
118 
119             if (_log.isDebugEnabled()) {
120                 _log.debug("Return\n" + doc.asXML());
121             }
122 
123             return doc.asXML();
124         }
125         catch (Exception e) {
126             throw new SearchException(e);
127         }
128         finally {
129             if (hits != null) {
130                 hits.closeSearcher();
131             }
132         }
133     }
134 
135     protected String getURL(
136             ThemeDisplay themeDisplay, long groupId, Document result,
137             PortletURL portletURL)
138         throws Exception {
139 
140         return portletURL.toString();
141     }
142 
143     private static Log _log = LogFactory.getLog(HitsOpenSearchImpl.class);
144 
145 }