001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.util;
016    
017    import com.liferay.portal.kernel.search.Document;
018    import com.liferay.portal.kernel.search.Field;
019    import com.liferay.portal.kernel.search.HitsOpenSearchImpl;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.service.GroupLocalServiceUtil;
025    import com.liferay.portal.service.LayoutLocalServiceUtil;
026    import com.liferay.portal.service.permission.LayoutPermissionUtil;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portlet.journal.model.JournalContentSearch;
030    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
031    
032    import java.util.List;
033    
034    import javax.portlet.PortletURL;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Wesley Gong
039     */
040    public class JournalOpenSearchImpl extends HitsOpenSearchImpl {
041    
042            public static final String SEARCH_PATH = "/c/journal/open_search";
043    
044            public static final String TITLE = "Liferay Journal Search: ";
045    
046            public String getPortletId() {
047                    return JournalIndexer.PORTLET_ID;
048            }
049    
050            public String getSearchPath() {
051                    return SEARCH_PATH;
052            }
053    
054            public String getTitle(String keywords) {
055                    return TITLE + keywords;
056            }
057    
058            protected String getLayoutURL(ThemeDisplay themeDisplay, String articleId)
059                    throws Exception {
060    
061                    PermissionChecker permissionChecker =
062                            themeDisplay.getPermissionChecker();
063    
064                    List<JournalContentSearch> contentSearches =
065                            JournalContentSearchLocalServiceUtil.getArticleContentSearches(
066                                    articleId);
067    
068                    for (JournalContentSearch contentSearch : contentSearches) {
069                            if (LayoutPermissionUtil.contains(
070                                            permissionChecker, contentSearch.getGroupId(),
071                                            contentSearch.isPrivateLayout(),
072                                            contentSearch.getLayoutId(), ActionKeys.VIEW)) {
073    
074                                    if (contentSearch.isPrivateLayout()) {
075                                            if (!GroupLocalServiceUtil.hasUserGroup(
076                                                            themeDisplay.getUserId(),
077                                                            contentSearch.getGroupId())) {
078    
079                                                    continue;
080                                            }
081                                    }
082    
083                                    Layout hitLayout = LayoutLocalServiceUtil.getLayout(
084                                            contentSearch.getGroupId(), contentSearch.isPrivateLayout(),
085                                            contentSearch.getLayoutId());
086    
087                                    return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
088                            }
089                    }
090    
091                    return null;
092            }
093    
094            protected String getURL(
095                            ThemeDisplay themeDisplay, long groupId, Document result,
096                            PortletURL portletURL)
097                    throws Exception {
098    
099                    PermissionChecker permissionChecker =
100                            themeDisplay.getPermissionChecker();
101    
102                    Layout layout = themeDisplay.getLayout();
103    
104                    String articleId = result.get(Field.ENTRY_CLASS_PK);
105                    String version = result.get("version");
106    
107                    List<Long> hitLayoutIds =
108                            JournalContentSearchLocalServiceUtil.getLayoutIds(
109                                    layout.getGroupId(), layout.isPrivateLayout(), articleId);
110    
111                    for (Long hitLayoutId : hitLayoutIds) {
112                            if (LayoutPermissionUtil.contains(
113                                            permissionChecker, layout.getGroupId(),
114                                            layout.isPrivateLayout(), hitLayoutId, ActionKeys.VIEW)) {
115    
116                                    Layout hitLayout = LayoutLocalServiceUtil.getLayout(
117                                            layout.getGroupId(), layout.isPrivateLayout(),
118                                            hitLayoutId.longValue());
119    
120                                    return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
121                            }
122                    }
123    
124                    String layoutURL = getLayoutURL(themeDisplay, articleId);
125    
126                    if (layoutURL != null) {
127                            return layoutURL;
128                    }
129    
130                    StringBundler sb = new StringBundler(7);
131    
132                    sb.append(themeDisplay.getPathMain());
133                    sb.append("/journal/view_article_content?groupId=");
134                    sb.append(groupId);
135                    sb.append("&articleId=");
136                    sb.append(articleId);
137                    sb.append("&version=");
138                    sb.append(version);
139    
140                    return sb.toString();
141            }
142    
143    }