1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.util;
24  
25  import com.liferay.portal.kernel.search.Document;
26  import com.liferay.portal.kernel.search.Field;
27  import com.liferay.portal.kernel.search.Hits;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.search.HitsOpenSearchImpl;
30  import com.liferay.portal.security.permission.ActionKeys;
31  import com.liferay.portal.security.permission.PermissionChecker;
32  import com.liferay.portal.service.GroupLocalServiceUtil;
33  import com.liferay.portal.service.LayoutLocalServiceUtil;
34  import com.liferay.portal.service.permission.LayoutPermissionUtil;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portlet.journal.model.JournalContentSearch;
38  import com.liferay.portlet.journal.service.JournalArticleLocalServiceUtil;
39  import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
40  
41  import java.util.List;
42  
43  import javax.portlet.PortletURL;
44  
45  /**
46   * <a href="JournalOpenSearchImpl.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   * @author Wesley Gong
50   */
51  public class JournalOpenSearchImpl extends HitsOpenSearchImpl {
52  
53      public static final String SEARCH_PATH = "/c/journal/open_search";
54  
55      public static final String TITLE = "Liferay Journal Search: ";
56  
57      public Hits getHits(
58              long companyId, long groupId, long userId, String keywords,
59              int start, int end)
60          throws Exception {
61  
62          return JournalArticleLocalServiceUtil.search(
63              companyId, groupId, userId, keywords, start, end);
64      }
65  
66      public String getSearchPath() {
67          return SEARCH_PATH;
68      }
69  
70      public String getTitle(String keywords) {
71          return TITLE + keywords;
72      }
73  
74      protected String getLayoutURL(ThemeDisplay themeDisplay, String articleId)
75          throws Exception {
76  
77          PermissionChecker permissionChecker =
78              themeDisplay.getPermissionChecker();
79  
80          List<JournalContentSearch> contentSearches =
81              JournalContentSearchLocalServiceUtil.getArticleContentSearches(
82                  articleId);
83  
84          for (JournalContentSearch contentSearch : contentSearches) {
85              if (LayoutPermissionUtil.contains(
86                      permissionChecker, contentSearch.getGroupId(),
87                      contentSearch.isPrivateLayout(),
88                      contentSearch.getLayoutId(), ActionKeys.VIEW)) {
89  
90                  if (contentSearch.isPrivateLayout()) {
91                      if (!GroupLocalServiceUtil.hasUserGroup(
92                              themeDisplay.getUserId(),
93                              contentSearch.getGroupId())) {
94  
95                          continue;
96                      }
97                  }
98  
99                  Layout hitLayout = LayoutLocalServiceUtil.getLayout(
100                     contentSearch.getGroupId(), contentSearch.isPrivateLayout(),
101                     contentSearch.getLayoutId());
102 
103                 return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
104             }
105         }
106 
107         return null;
108     }
109 
110     protected String getURL(
111             ThemeDisplay themeDisplay, long groupId, Document result,
112             PortletURL portletURL)
113         throws Exception {
114 
115         PermissionChecker permissionChecker =
116             themeDisplay.getPermissionChecker();
117 
118         Layout layout = themeDisplay.getLayout();
119 
120         String articleId = result.get(Field.ENTRY_CLASS_PK);
121         String version = result.get("version");
122 
123         List<Long> hitLayoutIds =
124             JournalContentSearchLocalServiceUtil.getLayoutIds(
125                 layout.getGroupId(), layout.isPrivateLayout(), articleId);
126 
127         for (Long hitLayoutId : hitLayoutIds) {
128             if (LayoutPermissionUtil.contains(
129                     permissionChecker, layout.getGroupId(),
130                     layout.isPrivateLayout(), hitLayoutId, ActionKeys.VIEW)) {
131 
132                 Layout hitLayout = LayoutLocalServiceUtil.getLayout(
133                     layout.getGroupId(), layout.isPrivateLayout(),
134                     hitLayoutId.longValue());
135 
136                 return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
137             }
138         }
139 
140         String layoutURL = getLayoutURL(themeDisplay, articleId);
141 
142         if (layoutURL != null) {
143             return layoutURL;
144         }
145 
146         StringBuilder sb = new StringBuilder();
147 
148         sb.append(themeDisplay.getPathMain());
149         sb.append("/journal/view_article_content?groupId=");
150         sb.append(groupId);
151         sb.append("&articleId=");
152         sb.append(articleId);
153         sb.append("&version=");
154         sb.append(version);
155 
156         return sb.toString();
157     }
158 
159 }