1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.search;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.search.Document;
25  import com.liferay.portal.kernel.search.DocumentSummary;
26  import com.liferay.portal.kernel.search.Field;
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.StringPool;
33  import com.liferay.portal.kernel.util.Validator;
34  import com.liferay.portal.kernel.xml.Element;
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  /**
54   * <a href="PortalOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Charles May
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class PortalOpenSearchImpl extends BaseOpenSearchImpl {
61  
62      public static final String SEARCH_PATH = "/c/search/open_search";
63  
64      public String search(
65              HttpServletRequest request, long groupId, long userId,
66              String keywords, int startPage, int itemsPerPage, String format)
67          throws SearchException {
68  
69          try {
70              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
71                  WebKeys.THEME_DISPLAY);
72  
73              int start = (startPage * itemsPerPage) - itemsPerPage;
74              int end = startPage * itemsPerPage;
75  
76              Hits results = CompanyLocalServiceUtil.search(
77                  themeDisplay.getCompanyId(), userId, keywords, start, end);
78  
79              int total = results.getLength();
80  
81              Object[] values = addSearchResults(
82                  keywords, startPage, itemsPerPage, total, start,
83                  "Liferay Portal Search: " + keywords, SEARCH_PATH, format,
84                  themeDisplay);
85  
86              com.liferay.portal.kernel.xml.Document doc =
87                  (com.liferay.portal.kernel.xml.Document)values[0];
88              Element root = (Element)values[1];
89  
90              for (int i = 0; i < results.getDocs().length; i++) {
91                  Document result = results.doc(i);
92  
93                  String portletId = result.get(Field.PORTLET_ID);
94  
95                  Portlet portlet = PortletLocalServiceUtil.getPortletById(
96                      themeDisplay.getCompanyId(), portletId);
97  
98                  if (portlet == null) {
99                      continue;
100                 }
101 
102                 String portletTitle = PortalUtil.getPortletTitle(
103                     portletId, themeDisplay.getUser());
104 
105                 long resultGroupId = GetterUtil.getLong(
106                     result.get(Field.GROUP_ID));
107 
108                 String title = StringPool.BLANK;
109 
110                 PortletURL portletURL = getPortletURL(
111                     request, portletId, resultGroupId);
112 
113                 String url = portletURL.toString();
114 
115                 Date modifedDate = result.getDate(Field.MODIFIED);
116 
117                 String content = StringPool.BLANK;
118 
119                 if (Validator.isNotNull(portlet.getIndexerClass())) {
120                     Indexer indexer = (Indexer)InstancePool.get(
121                         portlet.getIndexerClass());
122 
123                     DocumentSummary docSummary = indexer.getDocumentSummary(
124                         result, portletURL);
125 
126                     title = docSummary.getTitle();
127                     url = portletURL.toString();
128                     content = docSummary.getContent();
129 
130                     if (portlet.getPortletId().equals(PortletKeys.JOURNAL)) {
131                         url = getJournalURL(
132                             themeDisplay, resultGroupId, result);
133                     }
134                 }
135 
136                 double score = results.score(i);
137 
138                 addSearchResult(
139                     root, portletTitle + " &raquo; " + title, url, modifedDate,
140                     content, score, format);
141             }
142 
143             if (_log.isDebugEnabled()) {
144                 _log.debug("Return\n" + doc.asXML());
145             }
146 
147             return doc.asXML();
148 
149         }
150         catch (Exception e) {
151             throw new SearchException(e);
152         }
153     }
154 
155     protected String getJournalURL(
156             ThemeDisplay themeDisplay, long groupId, Document result)
157         throws Exception {
158 
159         Layout layout = themeDisplay.getLayout();
160 
161         String articleId = result.get(Field.ENTRY_CLASS_PK);
162         String version = result.get("version");
163 
164         List<Long> hitLayoutIds =
165             JournalContentSearchLocalServiceUtil.getLayoutIds(
166                 layout.getGroupId(), layout.isPrivateLayout(), articleId);
167 
168         if (hitLayoutIds.size() > 0) {
169             Long hitLayoutId = hitLayoutIds.get(0);
170 
171             Layout hitLayout = LayoutLocalServiceUtil.getLayout(
172                 layout.getGroupId(), layout.isPrivateLayout(),
173                 hitLayoutId.longValue());
174 
175             return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
176         }
177         else {
178             StringBuilder sb = new StringBuilder();
179 
180             sb.append(themeDisplay.getPathMain());
181             sb.append("/journal/view_article_content?groupId=");
182             sb.append(groupId);
183             sb.append("&articleId=");
184             sb.append(articleId);
185             sb.append("&version=");
186             sb.append(version);
187 
188             return sb.toString();
189         }
190     }
191 
192     private static Log _log = LogFactoryUtil.getLog(PortalOpenSearchImpl.class);
193 
194 }