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.kernel.util.HttpUtil;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.search.HitsOpenSearchImpl;
23  import com.liferay.portal.security.permission.ActionKeys;
24  import com.liferay.portal.security.permission.PermissionChecker;
25  import com.liferay.portal.service.GroupLocalServiceUtil;
26  import com.liferay.portal.service.LayoutLocalServiceUtil;
27  import com.liferay.portal.service.permission.LayoutPermissionUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portlet.journal.model.JournalContentSearch;
31  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
32  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
33  
34  import java.util.List;
35  
36  import javax.portlet.PortletURL;
37  
38  /**
39   * <a href="JournalOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   * @author Wesley Gong
43   */
44  public class JournalOpenSearchImpl extends HitsOpenSearchImpl {
45  
46      public static final String SEARCH_PATH = "/c/journal/open_search";
47  
48      public static final String TITLE = "Liferay Journal Search: ";
49  
50      public Hits getHits(
51              long companyId, long groupId, long userId, String keywords,
52              int start, int end)
53          throws Exception {
54  
55          return JournalArticleLocalServiceUtil.search(
56              companyId, groupId, userId, keywords, start, end);
57      }
58  
59      public String getSearchPath() {
60          return SEARCH_PATH;
61      }
62  
63      public String getTitle(String keywords) {
64          return TITLE + keywords;
65      }
66  
67      protected String getLayoutURL(ThemeDisplay themeDisplay, String articleId)
68          throws Exception {
69  
70          PermissionChecker permissionChecker =
71              themeDisplay.getPermissionChecker();
72  
73          List<JournalContentSearch> contentSearches =
74              JournalContentSearchLocalServiceUtil.getArticleContentSearches(
75                  articleId);
76  
77          for (JournalContentSearch contentSearch : contentSearches) {
78              if (LayoutPermissionUtil.contains(
79                      permissionChecker, contentSearch.getGroupId(),
80                      contentSearch.isPrivateLayout(),
81                      contentSearch.getLayoutId(), ActionKeys.VIEW)) {
82  
83                  if (contentSearch.isPrivateLayout()) {
84                      if (!GroupLocalServiceUtil.hasUserGroup(
85                              themeDisplay.getUserId(),
86                              contentSearch.getGroupId())) {
87  
88                          continue;
89                      }
90                  }
91  
92                  Layout hitLayout = LayoutLocalServiceUtil.getLayout(
93                      contentSearch.getGroupId(), contentSearch.isPrivateLayout(),
94                      contentSearch.getLayoutId());
95  
96                  String url = PortalUtil.getLayoutURL(hitLayout, themeDisplay);
97  
98                  url = HttpUtil.setParameter(url, "articleId", articleId);
99  
100                 return url;
101             }
102         }
103 
104         return null;
105     }
106 
107     protected String getURL(
108             ThemeDisplay themeDisplay, long groupId, Document result,
109             PortletURL portletURL)
110         throws Exception {
111 
112         PermissionChecker permissionChecker =
113             themeDisplay.getPermissionChecker();
114 
115         Layout layout = themeDisplay.getLayout();
116 
117         String articleId = result.get(Field.ENTRY_CLASS_PK);
118         String version = result.get("version");
119 
120         List<Long> hitLayoutIds =
121             JournalContentSearchLocalServiceUtil.getLayoutIds(
122                 layout.getGroupId(), layout.isPrivateLayout(), articleId);
123 
124         for (Long hitLayoutId : hitLayoutIds) {
125             if (LayoutPermissionUtil.contains(
126                     permissionChecker, layout.getGroupId(),
127                     layout.isPrivateLayout(), hitLayoutId, ActionKeys.VIEW)) {
128 
129                 Layout hitLayout = LayoutLocalServiceUtil.getLayout(
130                     layout.getGroupId(), layout.isPrivateLayout(),
131                     hitLayoutId.longValue());
132 
133                 String url = PortalUtil.getLayoutURL(hitLayout, themeDisplay);
134 
135                 url = HttpUtil.setParameter(url, "groupId", groupId);
136                 url = HttpUtil.setParameter(url, "articleId", articleId);
137 
138                 return url;
139             }
140         }
141 
142         String layoutURL = getLayoutURL(themeDisplay, articleId);
143 
144         if (layoutURL != null) {
145             return layoutURL;
146         }
147 
148         StringBuilder sb = new StringBuilder();
149 
150         sb.append(themeDisplay.getPathMain());
151         sb.append("/journal/view_article_content?groupId=");
152         sb.append(groupId);
153         sb.append("&articleId=");
154         sb.append(articleId);
155         sb.append("&version=");
156         sb.append(version);
157 
158         return sb.toString();
159     }
160 
161 }