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.asset;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
019    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
020    import com.liferay.portal.kernel.util.HtmlUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portal.util.WebKeys;
028    import com.liferay.portlet.asset.model.BaseAssetRenderer;
029    import com.liferay.portlet.journal.model.JournalArticle;
030    import com.liferay.portlet.journal.model.JournalArticleConstants;
031    import com.liferay.portlet.journal.model.JournalArticleDisplay;
032    import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
033    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
034    
035    import javax.portlet.PortletURL;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    /**
040     * @author Julio Camarero
041     * @author Juan Fernández
042     */
043    public class JournalArticleAssetRenderer extends BaseAssetRenderer {
044    
045            public JournalArticleAssetRenderer(JournalArticle article) {
046                    _article = article;
047            }
048    
049            public String[] getAvailableLocales() {
050                    return _article.getAvailableLocales();
051            }
052    
053            public long getClassPK() {
054                    if ((_article.isDraft() || _article.isPending()) &&
055                            (_article.getVersion() !=
056                                    JournalArticleConstants.DEFAULT_VERSION)) {
057    
058                            return _article.getPrimaryKey();
059                    }
060                    else {
061                            return _article.getResourcePrimKey();
062                    }
063            }
064    
065            public String getDiscussionPath() {
066                    if (PropsValues.JOURNAL_ARTICLE_COMMENTS_ENABLED) {
067                            return "edit_article_discussion";
068                    }
069                    else {
070                            return null;
071                    }
072            }
073    
074            public long getGroupId() {
075                    return _article.getGroupId();
076            }
077    
078            public String getSummary() {
079                    return HtmlUtil.stripHtml(_article.getContent());
080            }
081    
082            public String getTitle() {
083                            return _article.getTitle();
084            }
085    
086            public PortletURL getURLEdit(
087                    LiferayPortletRequest liferayPortletRequest,
088                    LiferayPortletResponse liferayPortletResponse) {
089    
090                    PortletURL editPortletURL = liferayPortletResponse.createRenderURL(
091                            PortletKeys.JOURNAL);
092    
093                    editPortletURL.setParameter(
094                            "struts_action", "/journal/edit_article");
095                    editPortletURL.setParameter(
096                            "groupId", String.valueOf(_article.getGroupId()));
097                    editPortletURL.setParameter(
098                            "articleId", _article.getArticleId());
099                    editPortletURL.setParameter(
100                            "version", String.valueOf(_article.getVersion()));
101    
102                    return editPortletURL;
103            }
104    
105            public PortletURL getURLExport(
106                    LiferayPortletRequest liferayPortletRequest,
107                    LiferayPortletResponse liferayPortletResponse) {
108    
109                    PortletURL exportPortletURL = liferayPortletResponse.createActionURL();
110    
111                    exportPortletURL.setParameter(
112                            "struts_action", "/asset_publisher/export_journal_article");
113                    exportPortletURL.setParameter(
114                            "groupId", String.valueOf(_article.getGroupId()));
115                    exportPortletURL.setParameter("articleId", _article.getArticleId());
116    
117                    return exportPortletURL;
118            }
119    
120            public String getUrlTitle() {
121                    return _article.getUrlTitle();
122            }
123    
124            public String getURLViewInContext(
125                            LiferayPortletRequest liferayPortletRequest,
126                            LiferayPortletResponse liferayPortletResponse,
127                            String noSuchEntryRedirect)
128                    throws Exception {
129    
130                    ThemeDisplay themeDisplay =
131                            (ThemeDisplay)liferayPortletRequest.getAttribute(
132                                    WebKeys.THEME_DISPLAY);
133    
134                    String languageId = LanguageUtil.getLanguageId(liferayPortletRequest);
135    
136                    JournalArticleDisplay articleDisplay =
137                            JournalContentUtil.getDisplay(
138                                    _article.getGroupId(), _article.getArticleId(),
139                                    null, null, languageId, themeDisplay);
140    
141                    String viewURL = StringPool.BLANK;
142    
143                    if (articleDisplay != null) {
144    
145                            PortletURL viewPortletURL =
146                                    liferayPortletResponse.createRenderURL();
147    
148                            viewPortletURL.setParameter(
149                                    "struts_action", "/asset_publisher/view_content");
150                            viewPortletURL.setParameter("urlTitle", _article.getUrlTitle());
151                            viewPortletURL.setParameter(
152                                    "type", JournalArticleAssetRendererFactory.TYPE);
153    
154                            viewURL = viewPortletURL.toString();
155                    }
156    
157                    return viewURL;
158            }
159    
160            public long getUserId() {
161                    return _article.getUserId();
162            }
163    
164            public String getUuid() {
165                    return _article.getUuid();
166            }
167    
168            public String getViewInContextMessage() {
169                    return "view";
170            }
171    
172            public boolean hasEditPermission(PermissionChecker permissionChecker) {
173                    return JournalArticlePermission.contains(
174                            permissionChecker,_article, ActionKeys.UPDATE);
175            }
176    
177            public boolean hasViewPermission(PermissionChecker permissionChecker) {
178                    return JournalArticlePermission.contains(
179                            permissionChecker,_article, ActionKeys.VIEW);
180            }
181    
182            public boolean isConvertible() {
183                    return true;
184            }
185    
186            public boolean isLocalizable() {
187                    return true;
188            }
189    
190            public boolean isPrintable() {
191                    return true;
192            }
193    
194            public String render(
195                            RenderRequest renderRequest, RenderResponse renderResponse,
196                            String template)
197                    throws Exception {
198    
199                    if (template.equals(TEMPLATE_ABSTRACT) ||
200                            template.equals(TEMPLATE_FULL_CONTENT)) {
201    
202                            renderRequest.setAttribute(WebKeys.JOURNAL_ARTICLE, _article);
203    
204                            return "/html/portlet/journal/asset/" + template + ".jsp";
205                    }
206                    else {
207                            return null;
208                    }
209            }
210    
211            protected String getIconPath(ThemeDisplay themeDisplay) {
212                    return themeDisplay.getPathThemeImages() + "/common/history.png";
213            }
214    
215            private JournalArticle _article;
216    
217    }