001    /**
002     * Copyright (c) 2000-2012 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.portlet.LiferayPortletRequest;
018    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.Group;
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.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.PortletKeys;
028    import com.liferay.portal.util.PropsValues;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.asset.model.BaseAssetRenderer;
031    import com.liferay.portlet.journal.model.JournalArticle;
032    import com.liferay.portlet.journal.model.JournalArticleConstants;
033    import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
034    
035    import java.util.Date;
036    import java.util.Locale;
037    
038    import javax.portlet.PortletRequest;
039    import javax.portlet.PortletURL;
040    import javax.portlet.RenderRequest;
041    import javax.portlet.RenderResponse;
042    
043    /**
044     * @author Julio Camarero
045     * @author Juan Fernández
046     * @author Sergio González
047     */
048    public class JournalArticleAssetRenderer extends BaseAssetRenderer {
049    
050            public JournalArticleAssetRenderer(JournalArticle article) {
051                    _article = article;
052            }
053    
054            public JournalArticle getArticle() {
055                    return _article;
056            }
057    
058            @Override
059            public String[] getAvailableLocales() {
060                    return _article.getAvailableLocales();
061            }
062    
063            public long getClassPK() {
064                    if ((_article.isDraft() || _article.isPending()) &&
065                            (_article.getVersion() !=
066                                    JournalArticleConstants.VERSION_DEFAULT)) {
067    
068                            return _article.getPrimaryKey();
069                    }
070                    else {
071                            return _article.getResourcePrimKey();
072                    }
073            }
074    
075            @Override
076            public String getDiscussionPath() {
077                    if (PropsValues.JOURNAL_ARTICLE_COMMENTS_ENABLED) {
078                            return "edit_article_discussion";
079                    }
080                    else {
081                            return null;
082                    }
083            }
084    
085            public long getGroupId() {
086                    return _article.getGroupId();
087            }
088    
089            public String getSummary(Locale locale) {
090                    return _article.getDescription(locale);
091            }
092    
093            public String getTitle(Locale locale) {
094                    return _article.getTitle(locale);
095            }
096    
097            @Override
098            public PortletURL getURLEdit(
099                            LiferayPortletRequest liferayPortletRequest,
100                            LiferayPortletResponse liferayPortletResponse)
101                    throws Exception {
102    
103                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
104                            getControlPanelPlid(liferayPortletRequest), PortletKeys.JOURNAL,
105                            PortletRequest.RENDER_PHASE);
106    
107                    portletURL.setParameter("struts_action", "/journal/edit_article");
108                    portletURL.setParameter(
109                            "groupId", String.valueOf(_article.getGroupId()));
110                    portletURL.setParameter("articleId", _article.getArticleId());
111                    portletURL.setParameter(
112                            "version", String.valueOf(_article.getVersion()));
113    
114                    return portletURL;
115            }
116    
117            @Override
118            public PortletURL getURLExport(
119                    LiferayPortletRequest liferayPortletRequest,
120                    LiferayPortletResponse liferayPortletResponse) {
121    
122                    PortletURL portletURL = liferayPortletResponse.createActionURL();
123    
124                    portletURL.setParameter(
125                            "struts_action", "/asset_publisher/export_journal_article");
126                    portletURL.setParameter(
127                            "groupId", String.valueOf(_article.getGroupId()));
128                    portletURL.setParameter("articleId", _article.getArticleId());
129    
130                    return portletURL;
131            }
132    
133            @Override
134            public String getUrlTitle() {
135                    return _article.getUrlTitle();
136            }
137    
138            @Override
139            public String getURLViewInContext(
140                            LiferayPortletRequest liferayPortletRequest,
141                            LiferayPortletResponse liferayPortletResponse,
142                            String noSuchEntryRedirect)
143                    throws Exception {
144    
145                    if (Validator.isNull(_article.getLayoutUuid())) {
146                            return null;
147                    }
148    
149                    ThemeDisplay themeDisplay =
150                            (ThemeDisplay)liferayPortletRequest.getAttribute(
151                                    WebKeys.THEME_DISPLAY);
152    
153                    Group group = themeDisplay.getScopeGroup();
154    
155                    if (group.getGroupId() != _article.getGroupId()) {
156                            group = GroupLocalServiceUtil.getGroup(_article.getGroupId());
157                    }
158    
159                    String groupFriendlyURL = PortalUtil.getGroupFriendlyURL(
160                            group, false, themeDisplay);
161    
162                    return groupFriendlyURL.concat(
163                            JournalArticleConstants.CANONICAL_URL_SEPARATOR).concat(
164                                    HtmlUtil.escape(_article.getUrlTitle()));
165            }
166    
167            public long getUserId() {
168                    return _article.getUserId();
169            }
170    
171            public String getUuid() {
172                    return _article.getUuid();
173            }
174    
175            @Override
176            public String getViewInContextMessage() {
177                    return "view";
178            }
179    
180            @Override
181            public boolean hasEditPermission(PermissionChecker permissionChecker) {
182                    return JournalArticlePermission.contains(
183                            permissionChecker, _article, ActionKeys.UPDATE);
184            }
185    
186            @Override
187            public boolean hasViewPermission(PermissionChecker permissionChecker) {
188                    return JournalArticlePermission.contains(
189                            permissionChecker, _article, ActionKeys.VIEW);
190            }
191    
192            @Override
193            public boolean isConvertible() {
194                    return true;
195            }
196    
197            @Override
198            public boolean isDisplayable() {
199                    Date now = new Date();
200    
201                    Date displayDate = _article.getDisplayDate();
202    
203                    if ((displayDate != null) && displayDate.after(now)) {
204                            return false;
205                    }
206    
207                    Date expirationDate = _article.getExpirationDate();
208    
209                    if ((expirationDate != null) && expirationDate.before(now)) {
210                            return false;
211                    }
212    
213                    return true;
214            }
215    
216            @Override
217            public boolean isLocalizable() {
218                    return true;
219            }
220    
221            @Override
222            public boolean isPrintable() {
223                    return true;
224            }
225    
226            public String render(
227                            RenderRequest renderRequest, RenderResponse renderResponse,
228                            String template)
229                    throws Exception {
230    
231                    if (template.equals(TEMPLATE_ABSTRACT) ||
232                            template.equals(TEMPLATE_FULL_CONTENT)) {
233    
234                            renderRequest.setAttribute(WebKeys.JOURNAL_ARTICLE, _article);
235    
236                            return "/html/portlet/journal/asset/" + template + ".jsp";
237                    }
238                    else {
239                            return null;
240                    }
241            }
242    
243            @Override
244            protected String getIconPath(ThemeDisplay themeDisplay) {
245                    return themeDisplay.getPathThemeImages() + "/common/history.png";
246            }
247    
248            private JournalArticle _article;
249    
250    }