001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            @Override
064            public String[] getAvailableLocales() {
065                    return _article.getAvailableLocales();
066            }
067    
068            public long getClassPK() {
069                    if ((_article.isDraft() || _article.isPending()) &&
070                            (_article.getVersion() !=
071                                    JournalArticleConstants.VERSION_DEFAULT)) {
072    
073                            return _article.getPrimaryKey();
074                    }
075                    else {
076                            return _article.getResourcePrimKey();
077                    }
078            }
079    
080            @Override
081            public String getDiscussionPath() {
082                    if (PropsValues.JOURNAL_ARTICLE_COMMENTS_ENABLED) {
083                            return "edit_article_discussion";
084                    }
085                    else {
086                            return null;
087                    }
088            }
089    
090            public long getGroupId() {
091                    return _article.getGroupId();
092            }
093    
094            public String getSummary(Locale locale) {
095                    return _article.getDescription(locale);
096            }
097    
098            public String getTitle(Locale locale) {
099                    return _article.getTitle(locale);
100            }
101    
102            @Override
103            public PortletURL getURLEdit(
104                            LiferayPortletRequest liferayPortletRequest,
105                            LiferayPortletResponse liferayPortletResponse)
106                    throws Exception {
107    
108                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
109                            getControlPanelPlid(liferayPortletRequest), PortletKeys.JOURNAL,
110                            PortletRequest.RENDER_PHASE);
111    
112                    portletURL.setParameter("struts_action", "/journal/edit_article");
113                    portletURL.setParameter(
114                            "groupId", String.valueOf(_article.getGroupId()));
115                    portletURL.setParameter("articleId", _article.getArticleId());
116                    portletURL.setParameter(
117                            "version", String.valueOf(_article.getVersion()));
118    
119                    return portletURL;
120            }
121    
122            @Override
123            public PortletURL getURLExport(
124                    LiferayPortletRequest liferayPortletRequest,
125                    LiferayPortletResponse liferayPortletResponse) {
126    
127                    PortletURL portletURL = liferayPortletResponse.createActionURL();
128    
129                    portletURL.setParameter(
130                            "struts_action", "/asset_publisher/export_journal_article");
131                    portletURL.setParameter(
132                            "groupId", String.valueOf(_article.getGroupId()));
133                    portletURL.setParameter("articleId", _article.getArticleId());
134    
135                    return portletURL;
136            }
137    
138            @Override
139            public String getUrlTitle() {
140                    return _article.getUrlTitle();
141            }
142    
143            @Override
144            public String getURLViewInContext(
145                            LiferayPortletRequest liferayPortletRequest,
146                            LiferayPortletResponse liferayPortletResponse,
147                            String noSuchEntryRedirect)
148                    throws Exception {
149    
150                    ThemeDisplay themeDisplay =
151                            (ThemeDisplay)liferayPortletRequest.getAttribute(
152                                    WebKeys.THEME_DISPLAY);
153    
154                    Group group = themeDisplay.getScopeGroup();
155    
156                    if (Validator.isNotNull(_article.getLayoutUuid())) {
157                            if (group.getGroupId() != _article.getGroupId()) {
158                                    group = GroupLocalServiceUtil.getGroup(_article.getGroupId());
159                            }
160    
161                            String groupFriendlyURL = PortalUtil.getGroupFriendlyURL(
162                                    group, false, themeDisplay);
163    
164                            return groupFriendlyURL.concat(
165                                    JournalArticleConstants.CANONICAL_URL_SEPARATOR).concat(
166                                            HtmlUtil.escape(_article.getUrlTitle()));
167                    }
168    
169                    Layout layout = themeDisplay.getLayout();
170    
171                    List<Long> hitLayoutIds =
172                            JournalContentSearchLocalServiceUtil.getLayoutIds(
173                                    layout.getGroupId(), layout.isPrivateLayout(),
174                                    _article.getArticleId());
175    
176                    if (!hitLayoutIds.isEmpty()) {
177                            Long hitLayoutId = hitLayoutIds.get(0);
178    
179                            Layout hitLayout = LayoutLocalServiceUtil.getLayout(
180                                    layout.getGroupId(), layout.isPrivateLayout(),
181                                    hitLayoutId.longValue());
182    
183                            return PortalUtil.getLayoutURL(hitLayout, themeDisplay);
184                    }
185    
186                    return noSuchEntryRedirect;
187            }
188    
189            public long getUserId() {
190                    return _article.getUserId();
191            }
192    
193            public String getUserName() {
194                    return _article.getUserName();
195            }
196    
197            public String getUuid() {
198                    return _article.getUuid();
199            }
200    
201            @Override
202            public String getViewInContextMessage() {
203                    return "view";
204            }
205    
206            @Override
207            public boolean hasEditPermission(PermissionChecker permissionChecker) {
208                    return JournalArticlePermission.contains(
209                            permissionChecker, _article, ActionKeys.UPDATE);
210            }
211    
212            @Override
213            public boolean hasViewPermission(PermissionChecker permissionChecker) {
214                    return JournalArticlePermission.contains(
215                            permissionChecker, _article, ActionKeys.VIEW);
216            }
217    
218            @Override
219            public boolean isConvertible() {
220                    return true;
221            }
222    
223            @Override
224            public boolean isDisplayable() {
225                    Date now = new Date();
226    
227                    Date displayDate = _article.getDisplayDate();
228    
229                    if ((displayDate != null) && displayDate.after(now)) {
230                            return false;
231                    }
232    
233                    Date expirationDate = _article.getExpirationDate();
234    
235                    if ((expirationDate != null) && expirationDate.before(now)) {
236                            return false;
237                    }
238    
239                    return true;
240            }
241    
242            @Override
243            public boolean isLocalizable() {
244                    return true;
245            }
246    
247            @Override
248            public boolean isPrintable() {
249                    return true;
250            }
251    
252            public String render(
253                            RenderRequest renderRequest, RenderResponse renderResponse,
254                            String template)
255                    throws Exception {
256    
257                    if (template.equals(TEMPLATE_ABSTRACT) ||
258                            template.equals(TEMPLATE_FULL_CONTENT)) {
259    
260                            renderRequest.setAttribute(WebKeys.JOURNAL_ARTICLE, _article);
261    
262                            return "/html/portlet/journal/asset/" + template + ".jsp";
263                    }
264                    else {
265                            return null;
266                    }
267            }
268    
269            @Override
270            protected String getIconPath(ThemeDisplay themeDisplay) {
271                    return themeDisplay.getPathThemeImages() + "/common/history.png";
272            }
273    
274            private JournalArticle _article;
275    
276    }