1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.search.Document;
28  import com.liferay.portal.kernel.search.DocumentSummary;
29  import com.liferay.portal.kernel.search.Field;
30  import com.liferay.portal.kernel.search.Hits;
31  import com.liferay.portal.kernel.search.Indexer;
32  import com.liferay.portal.kernel.search.SearchException;
33  import com.liferay.portal.kernel.util.GetterUtil;
34  import com.liferay.portal.kernel.util.InstancePool;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.kernel.xml.Element;
38  import com.liferay.portal.model.Layout;
39  import com.liferay.portal.model.Portlet;
40  import com.liferay.portal.service.CompanyLocalServiceUtil;
41  import com.liferay.portal.service.LayoutLocalServiceUtil;
42  import com.liferay.portal.service.PortletLocalServiceUtil;
43  import com.liferay.portal.theme.ThemeDisplay;
44  import com.liferay.portal.util.PortalUtil;
45  import com.liferay.portal.util.PortletKeys;
46  import com.liferay.portal.util.WebKeys;
47  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
48  
49  import java.util.Date;
50  import java.util.List;
51  
52  import javax.portlet.PortletURL;
53  
54  import javax.servlet.http.HttpServletRequest;
55  
56  /**
57   * <a href="PortalOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Charles May
60   * @author Brian Wing Shun Chan
61   */
62  public class PortalOpenSearchImpl extends BaseOpenSearchImpl {
63  
64      public static final String SEARCH_PATH = "/c/search/open_search";
65  
66      public String search(
67              HttpServletRequest request, long groupId, long userId,
68              String keywords, int startPage, int itemsPerPage, String format)
69          throws SearchException {
70  
71          try {
72              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
73                  WebKeys.THEME_DISPLAY);
74  
75              int start = (startPage * itemsPerPage) - itemsPerPage;
76              int end = startPage * itemsPerPage;
77  
78              Hits results = CompanyLocalServiceUtil.search(
79                  themeDisplay.getCompanyId(), userId, keywords, start, end);
80  
81              String[] queryTerms = results.getQueryTerms();
82  
83              int total = results.getLength();
84  
85              Object[] values = addSearchResults(
86                  queryTerms, keywords, startPage, itemsPerPage, total, start,
87                  "Liferay Portal Search: " + keywords, SEARCH_PATH, format,
88                  themeDisplay);
89  
90              com.liferay.portal.kernel.xml.Document doc =
91                  (com.liferay.portal.kernel.xml.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 resultGroupId = GetterUtil.getLong(
110                     result.get(Field.GROUP_ID));
111 
112                 String title = StringPool.BLANK;
113 
114                 PortletURL portletURL = getPortletURL(
115                     request, portletId, resultGroupId);
116 
117                 String url = portletURL.toString();
118 
119                 Date modifedDate = result.getDate(Field.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                     String snippet = results.snippet(i);
128 
129                     DocumentSummary docSummary = indexer.getDocumentSummary(
130                         result, snippet, portletURL);
131 
132                     title = docSummary.getTitle();
133                     url = portletURL.toString();
134                     content = docSummary.getContent();
135 
136                     if (portlet.getPortletId().equals(PortletKeys.JOURNAL)) {
137                         url = getJournalURL(
138                             themeDisplay, resultGroupId, result);
139                     }
140                 }
141 
142                 double score = results.score(i);
143 
144                 addSearchResult(
145                     root, portletTitle + " &raquo; " + title, url, modifedDate,
146                     content, score, format);
147             }
148 
149             if (_log.isDebugEnabled()) {
150                 _log.debug("Return\n" + doc.asXML());
151             }
152 
153             return doc.asXML();
154 
155         }
156         catch (Exception e) {
157             throw new SearchException(e);
158         }
159     }
160 
161     protected String getJournalURL(
162             ThemeDisplay themeDisplay, long groupId, Document result)
163         throws Exception {
164 
165         Layout layout = themeDisplay.getLayout();
166 
167         String articleId = result.get(Field.ENTRY_CLASS_PK);
168         String version = result.get("version");
169 
170         List<Long> hitLayoutIds =
171             JournalContentSearchLocalServiceUtil.getLayoutIds(
172                 layout.getGroupId(), layout.isPrivateLayout(), articleId);
173 
174         if (hitLayoutIds.size() > 0) {
175             Long hitLayoutId = hitLayoutIds.get(0);
176 
177             Layout hitLayout = LayoutLocalServiceUtil.getLayout(
178                 layout.getGroupId(), layout.isPrivateLayout(),
179                 hitLayoutId.longValue());
180 
181             return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
182         }
183         else {
184             StringBuilder sb = new StringBuilder();
185 
186             sb.append(themeDisplay.getPathMain());
187             sb.append("/journal/view_article_content?groupId=");
188             sb.append(groupId);
189             sb.append("&articleId=");
190             sb.append(articleId);
191             sb.append("&version=");
192             sb.append(version);
193 
194             return sb.toString();
195         }
196     }
197 
198     private static Log _log = LogFactoryUtil.getLog(PortalOpenSearchImpl.class);
199 
200 }