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, long groupId, long userId, String keywords,
51              int start, int end)
52          throws Exception {
53  
54          return JournalArticleLocalServiceUtil.search(
55              companyId, groupId, userId, keywords, start, end);
56      }
57  
58      public String getSearchPath() {
59          return SEARCH_PATH;
60      }
61  
62      public String getTitle(String keywords) {
63          return TITLE + keywords;
64      }
65  
66      protected String getLayoutURL(ThemeDisplay themeDisplay, String articleId)
67          throws Exception {
68  
69          PermissionChecker permissionChecker =
70              themeDisplay.getPermissionChecker();
71  
72          List<JournalContentSearch> contentSearches =
73              JournalContentSearchLocalServiceUtil.getArticleContentSearches(
74                  articleId);
75  
76          for (JournalContentSearch contentSearch : contentSearches) {
77              if (LayoutPermissionUtil.contains(
78                      permissionChecker, contentSearch.getGroupId(),
79                      contentSearch.isPrivateLayout(),
80                      contentSearch.getLayoutId(), ActionKeys.VIEW)) {
81  
82                  if (contentSearch.isPrivateLayout()) {
83                      if (!GroupLocalServiceUtil.hasUserGroup(
84                              themeDisplay.getUserId(),
85                              contentSearch.getGroupId())) {
86  
87                          continue;
88                      }
89                  }
90  
91                  Layout hitLayout = LayoutLocalServiceUtil.getLayout(
92                      contentSearch.getGroupId(), contentSearch.isPrivateLayout(),
93                      contentSearch.getLayoutId());
94  
95                  return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
96              }
97          }
98  
99          return null;
100     }
101 
102     protected String getURL(
103             ThemeDisplay themeDisplay, long groupId, Document result,
104             PortletURL portletURL)
105         throws Exception {
106 
107         PermissionChecker permissionChecker =
108             themeDisplay.getPermissionChecker();
109 
110         Layout layout = themeDisplay.getLayout();
111 
112         String articleId = result.get(Field.ENTRY_CLASS_PK);
113         String version = result.get("version");
114 
115         List<Long> hitLayoutIds =
116             JournalContentSearchLocalServiceUtil.getLayoutIds(
117                 layout.getGroupId(), layout.isPrivateLayout(), articleId);
118 
119         for (Long hitLayoutId : hitLayoutIds) {
120             if (LayoutPermissionUtil.contains(
121                     permissionChecker, layout.getGroupId(),
122                     layout.isPrivateLayout(), hitLayoutId, ActionKeys.VIEW)) {
123 
124                 Layout hitLayout = LayoutLocalServiceUtil.getLayout(
125                     layout.getGroupId(), layout.isPrivateLayout(),
126                     hitLayoutId.longValue());
127 
128                 return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
129             }
130         }
131 
132         String layoutURL = getLayoutURL(themeDisplay, articleId);
133 
134         if (layoutURL != null) {
135             return layoutURL;
136         }
137 
138         StringBuilder sb = new StringBuilder();
139 
140         sb.append(themeDisplay.getPathMain());
141         sb.append("/journal/view_article_content?groupId=");
142         sb.append(groupId);
143         sb.append("&articleId=");
144         sb.append(articleId);
145         sb.append("&version=");
146         sb.append(version);
147 
148         return sb.toString();
149     }
150 
151 }