1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portlet.journal.util;
16  
17  import com.liferay.portal.kernel.search.Document;
18  import com.liferay.portal.kernel.search.Field;
19  import com.liferay.portal.kernel.search.Hits;
20  import com.liferay.portal.model.Layout;
21  import com.liferay.portal.search.HitsOpenSearchImpl;
22  import com.liferay.portal.security.permission.ActionKeys;
23  import com.liferay.portal.security.permission.PermissionChecker;
24  import com.liferay.portal.service.GroupLocalServiceUtil;
25  import com.liferay.portal.service.LayoutLocalServiceUtil;
26  import com.liferay.portal.service.permission.LayoutPermissionUtil;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portlet.journal.model.JournalContentSearch;
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   * @author Wesley Gong
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 getLayoutURL(ThemeDisplay themeDisplay, String articleId)
66          throws Exception {
67  
68          PermissionChecker permissionChecker =
69              themeDisplay.getPermissionChecker();
70  
71          List<JournalContentSearch> contentSearches =
72              JournalContentSearchLocalServiceUtil.getArticleContentSearches(
73                  articleId);
74  
75          for (JournalContentSearch contentSearch : contentSearches) {
76              if (LayoutPermissionUtil.contains(
77                      permissionChecker, contentSearch.getGroupId(),
78                      contentSearch.isPrivateLayout(),
79                      contentSearch.getLayoutId(), ActionKeys.VIEW)) {
80  
81                  if (contentSearch.isPrivateLayout()) {
82                      if (!GroupLocalServiceUtil.hasUserGroup(
83                              themeDisplay.getUserId(),
84                              contentSearch.getGroupId())) {
85  
86                          continue;
87                      }
88                  }
89  
90                  Layout hitLayout = LayoutLocalServiceUtil.getLayout(
91                      contentSearch.getGroupId(), contentSearch.isPrivateLayout(),
92                      contentSearch.getLayoutId());
93  
94                  return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
95              }
96          }
97  
98          return null;
99      }
100 
101     protected String getURL(
102             ThemeDisplay themeDisplay, long groupId, Document result,
103             PortletURL portletURL)
104         throws Exception {
105 
106         PermissionChecker permissionChecker =
107             themeDisplay.getPermissionChecker();
108 
109         Layout layout = themeDisplay.getLayout();
110 
111         String articleId = result.get(Field.ENTRY_CLASS_PK);
112         String version = result.get("version");
113 
114         List<Long> hitLayoutIds =
115             JournalContentSearchLocalServiceUtil.getLayoutIds(
116                 layout.getGroupId(), layout.isPrivateLayout(), articleId);
117 
118         for (Long hitLayoutId : hitLayoutIds) {
119             if (LayoutPermissionUtil.contains(
120                     permissionChecker, layout.getGroupId(),
121                     layout.isPrivateLayout(), hitLayoutId, ActionKeys.VIEW)) {
122 
123                 Layout hitLayout = LayoutLocalServiceUtil.getLayout(
124                     layout.getGroupId(), layout.isPrivateLayout(),
125                     hitLayoutId.longValue());
126 
127                 return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
128             }
129         }
130 
131         String layoutURL = getLayoutURL(themeDisplay, articleId);
132 
133         if (layoutURL != null) {
134             return layoutURL;
135         }
136 
137         StringBuilder sb = new StringBuilder();
138 
139         sb.append(themeDisplay.getPathMain());
140         sb.append("/journal/view_article_content?groupId=");
141         sb.append(groupId);
142         sb.append("&articleId=");
143         sb.append(articleId);
144         sb.append("&version=");
145         sb.append(version);
146 
147         return sb.toString();
148     }
149 
150 }