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, String keywords, int startPage,
66              int itemsPerPage)
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(), 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,
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 groupId = GetterUtil.getLong(result.get(Field.GROUP_ID));
106 
107                 String title = StringPool.BLANK;
108 
109                 PortletURL portletURL = getPortletURL(
110                     request, portletId, groupId);
111 
112                 String url = portletURL.toString();
113 
114                 Date modifedDate = result.getDate(Field.MODIFIED);
115 
116                 String content = StringPool.BLANK;
117 
118                 if (Validator.isNotNull(portlet.getIndexerClass())) {
119                     Indexer indexer = (Indexer)InstancePool.get(
120                         portlet.getIndexerClass());
121 
122                     DocumentSummary docSummary = indexer.getDocumentSummary(
123                         result, portletURL);
124 
125                     title = docSummary.getTitle();
126                     url = portletURL.toString();
127                     content = docSummary.getContent();
128 
129                     if (portlet.getPortletId().equals(PortletKeys.JOURNAL)) {
130                         url = getJournalURL(themeDisplay, groupId, result);
131                     }
132                 }
133 
134                 double score = results.score(i);
135 
136                 addSearchResult(
137                     root, portletTitle + " &raquo; " + title, url, modifedDate,
138                     content, score);
139             }
140 
141             if (_log.isDebugEnabled()) {
142                 _log.debug("Return\n" + doc.asXML());
143             }
144 
145             return doc.asXML();
146 
147         }
148         catch (Exception e) {
149             throw new SearchException(e);
150         }
151     }
152 
153     protected String getJournalURL(
154             ThemeDisplay themeDisplay, long groupId, Document result)
155         throws Exception {
156 
157         Layout layout = themeDisplay.getLayout();
158 
159         String articleId = result.get(Field.ENTRY_CLASS_PK);
160         String version = result.get("version");
161 
162         List<Long> hitLayoutIds =
163             JournalContentSearchLocalServiceUtil.getLayoutIds(
164                 layout.getGroupId(), layout.isPrivateLayout(), articleId);
165 
166         if (hitLayoutIds.size() > 0) {
167             Long hitLayoutId = hitLayoutIds.get(0);
168 
169             Layout hitLayout = LayoutLocalServiceUtil.getLayout(
170                 layout.getGroupId(), layout.isPrivateLayout(),
171                 hitLayoutId.longValue());
172 
173             return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
174         }
175         else {
176             StringBuilder sb = new StringBuilder();
177 
178             sb.append(themeDisplay.getPathMain());
179             sb.append("/journal/view_article_content?groupId=");
180             sb.append(groupId);
181             sb.append("&articleId=");
182             sb.append(articleId);
183             sb.append("&version=");
184             sb.append(version);
185 
186             return sb.toString();
187         }
188     }
189 
190     private static Log _log = LogFactoryUtil.getLog(PortalOpenSearchImpl.class);
191 
192 }