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