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