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