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