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