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