001    /**
002     * Copyright (c) 2000-2013 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.HitsOpenSearchImpl;
019    import com.liferay.portal.kernel.search.Indexer;
020    import com.liferay.portal.kernel.search.IndexerRegistryUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.security.permission.ActionKeys;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.service.GroupLocalServiceUtil;
026    import com.liferay.portal.service.LayoutLocalServiceUtil;
027    import com.liferay.portal.service.permission.LayoutPermissionUtil;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portlet.journal.model.JournalArticle;
031    import com.liferay.portlet.journal.model.JournalArticleConstants;
032    import com.liferay.portlet.journal.model.JournalContentSearch;
033    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
034    import com.liferay.portlet.journal.service.JournalContentSearchLocalServiceUtil;
035    
036    import java.util.List;
037    
038    import javax.portlet.PortletURL;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Wesley Gong
043     */
044    public class JournalOpenSearchImpl extends HitsOpenSearchImpl {
045    
046            public static final String SEARCH_PATH = "/c/journal/open_search";
047    
048            public static final String TITLE = "Liferay Journal Search: ";
049    
050            @Override
051            public Indexer getIndexer() {
052                    return IndexerRegistryUtil.getIndexer(JournalArticle.class);
053            }
054    
055            @Override
056            public String getPortletId() {
057                    return JournalIndexer.PORTLET_ID;
058            }
059    
060            @Override
061            public String getSearchPath() {
062                    return SEARCH_PATH;
063            }
064    
065            @Override
066            public String getTitle(String keywords) {
067                    return TITLE + keywords;
068            }
069    
070            protected String getLayoutURL(ThemeDisplay themeDisplay, String articleId)
071                    throws Exception {
072    
073                    PermissionChecker permissionChecker =
074                            themeDisplay.getPermissionChecker();
075    
076                    List<JournalContentSearch> contentSearches =
077                            JournalContentSearchLocalServiceUtil.getArticleContentSearches(
078                                    articleId);
079    
080                    for (JournalContentSearch contentSearch : contentSearches) {
081                            if (LayoutPermissionUtil.contains(
082                                            permissionChecker, contentSearch.getGroupId(),
083                                            contentSearch.isPrivateLayout(),
084                                            contentSearch.getLayoutId(), ActionKeys.VIEW)) {
085    
086                                    if (contentSearch.isPrivateLayout()) {
087                                            if (!GroupLocalServiceUtil.hasUserGroup(
088                                                            themeDisplay.getUserId(),
089                                                            contentSearch.getGroupId())) {
090    
091                                                    continue;
092                                            }
093                                    }
094    
095                                    Layout hitLayout = LayoutLocalServiceUtil.getLayout(
096                                            contentSearch.getGroupId(), contentSearch.isPrivateLayout(),
097                                            contentSearch.getLayoutId());
098    
099                                    return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
100                            }
101                    }
102    
103                    return null;
104            }
105    
106            @Override
107            protected String getURL(
108                            ThemeDisplay themeDisplay, long groupId, Document result,
109                            PortletURL portletURL)
110                    throws Exception {
111    
112                    String articleId = result.get("articleId");
113    
114                    JournalArticle article = JournalArticleServiceUtil.getArticle(
115                            groupId, articleId);
116    
117                    if (Validator.isNotNull(article.getLayoutUuid())) {
118                            String groupFriendlyURL = PortalUtil.getGroupFriendlyURL(
119                                    GroupLocalServiceUtil.getGroup(article.getGroupId()), false,
120                                    themeDisplay);
121    
122                            return groupFriendlyURL.concat(
123                                    JournalArticleConstants.CANONICAL_URL_SEPARATOR).concat(
124                                            article.getUrlTitle());
125                    }
126    
127                    Layout layout = themeDisplay.getLayout();
128    
129                    List<Long> hitLayoutIds =
130                            JournalContentSearchLocalServiceUtil.getLayoutIds(
131                                    layout.getGroupId(), layout.isPrivateLayout(), articleId);
132    
133                    for (Long hitLayoutId : hitLayoutIds) {
134                            PermissionChecker permissionChecker =
135                                    themeDisplay.getPermissionChecker();
136    
137                            if (LayoutPermissionUtil.contains(
138                                            permissionChecker, layout.getGroupId(),
139                                            layout.isPrivateLayout(), hitLayoutId, ActionKeys.VIEW)) {
140    
141                                    Layout hitLayout = LayoutLocalServiceUtil.getLayout(
142                                            layout.getGroupId(), layout.isPrivateLayout(),
143                                            hitLayoutId.longValue());
144    
145                                    return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
146                            }
147                    }
148    
149                    String layoutURL = getLayoutURL(themeDisplay, articleId);
150    
151                    if (layoutURL != null) {
152                            return layoutURL;
153                    }
154    
155                    portletURL.setParameter("struts_action", "/journal/view_article");
156                    portletURL.setParameter("groupId", String.valueOf(groupId));
157                    portletURL.setParameter("articleId", articleId);
158    
159                    String version = result.get("version");
160    
161                    portletURL.setParameter("version", version);
162    
163                    return portletURL.toString();
164            }
165    
166    }