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.portlet.journal.util;
21  
22  import com.liferay.portal.kernel.search.Document;
23  import com.liferay.portal.kernel.search.Field;
24  import com.liferay.portal.kernel.search.Hits;
25  import com.liferay.portal.model.Layout;
26  import com.liferay.portal.search.HitsOpenSearchImpl;
27  import com.liferay.portal.service.LayoutLocalServiceUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
31  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
32  
33  import java.util.List;
34  
35  import javax.portlet.PortletURL;
36  
37  /**
38   * <a href="JournalOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class JournalOpenSearchImpl extends HitsOpenSearchImpl {
44  
45      public static final String SEARCH_PATH = "/c/journal/open_search";
46  
47      public static final String TITLE = "Liferay Journal Search: ";
48  
49      public Hits getHits(
50              long companyId, String keywords, int start, int end)
51          throws Exception {
52  
53          return JournalArticleLocalServiceUtil.search(
54              companyId, 0, keywords, start, end);
55      }
56  
57      public String getSearchPath() {
58          return SEARCH_PATH;
59      }
60  
61      public String getTitle(String keywords) {
62          return TITLE + keywords;
63      }
64  
65      protected String getURL(
66              ThemeDisplay themeDisplay, long groupId, Document result,
67              PortletURL portletURL)
68          throws Exception {
69  
70          Layout layout = themeDisplay.getLayout();
71  
72          String articleId = result.get(Field.ENTRY_CLASS_PK);
73          String version = result.get("version");
74  
75          List<Long> hitLayoutIds =
76              JournalContentSearchLocalServiceUtil.getLayoutIds(
77                  layout.getGroupId(), layout.isPrivateLayout(), articleId);
78  
79          if (hitLayoutIds.size() > 0) {
80              Long hitLayoutId = hitLayoutIds.get(0);
81  
82              Layout hitLayout = LayoutLocalServiceUtil.getLayout(
83                  layout.getGroupId(), layout.isPrivateLayout(),
84                  hitLayoutId.longValue());
85  
86              return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
87          }
88          else {
89              StringBuilder sb = new StringBuilder();
90  
91              sb.append(themeDisplay.getPathMain());
92              sb.append("/journal/view_article_content?groupId=");
93              sb.append(groupId);
94              sb.append("&articleId=");
95              sb.append(articleId);
96              sb.append("&version=");
97              sb.append(version);
98  
99              return sb.toString();
100         }
101     }
102 
103 }