1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.asset;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
19  import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
20  import com.liferay.portal.kernel.util.HtmlUtil;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.security.permission.ActionKeys;
23  import com.liferay.portal.security.permission.PermissionChecker;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.PortletKeys;
26  import com.liferay.portal.util.PropsValues;
27  import com.liferay.portal.util.WebKeys;
28  import com.liferay.portlet.asset.model.BaseAssetRenderer;
29  import com.liferay.portlet.journal.model.JournalArticle;
30  import com.liferay.portlet.journal.model.JournalArticleDisplay;
31  import com.liferay.portlet.journal.service.permission.JournalArticlePermission;
32  import com.liferay.portlet.journalcontent.util.JournalContentUtil;
33  
34  import javax.portlet.PortletURL;
35  import javax.portlet.RenderRequest;
36  import javax.portlet.RenderResponse;
37  
38  /**
39   * <a href="JournalArticleAssetRenderer.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Julio Camarero
42   */
43  public class JournalArticleAssetRenderer extends BaseAssetRenderer {
44  
45      public JournalArticleAssetRenderer(JournalArticle article) {
46          _article = article;
47      }
48  
49      public String[] getAvailableLocales() {
50          return _article.getAvailableLocales();
51      }
52  
53      public long getClassPK() {
54          return _article.getResourcePrimKey();
55      }
56  
57      public String getDiscussionPath() {
58          if (PropsValues.JOURNAL_ARTICLE_COMMENTS_ENABLED) {
59              return "edit_article_discussion";
60          }
61          else {
62              return null;
63          }
64      }
65  
66      public long getGroupId() {
67          return _article.getGroupId();
68      }
69  
70      public String getSummary() {
71          return HtmlUtil.stripHtml(_article.getContent());
72      }
73  
74      public String getTitle() {
75              return _article.getTitle();
76      }
77  
78      public PortletURL getURLEdit(
79          LiferayPortletRequest liferayPortletRequest,
80          LiferayPortletResponse liferayPortletResponse) {
81  
82          PortletURL editPortletURL = liferayPortletResponse.createRenderURL(
83              PortletKeys.JOURNAL);
84  
85          editPortletURL.setParameter(
86              "struts_action", "/journal/edit_article");
87          editPortletURL.setParameter(
88              "groupId", String.valueOf(_article.getGroupId()));
89          editPortletURL.setParameter(
90              "articleId", _article.getArticleId());
91          editPortletURL.setParameter(
92              "version", String.valueOf(_article.getVersion()));
93  
94          return editPortletURL;
95      }
96  
97      public PortletURL getURLExport(
98          LiferayPortletRequest liferayPortletRequest,
99          LiferayPortletResponse liferayPortletResponse) {
100 
101         PortletURL exportPortletURL = liferayPortletResponse.createActionURL();
102 
103         exportPortletURL.setParameter(
104             "struts_action", "/asset_publisher/export_journal_article");
105         exportPortletURL.setParameter(
106             "groupId", String.valueOf(_article.getGroupId()));
107         exportPortletURL.setParameter("articleId", _article.getArticleId());
108 
109         return exportPortletURL;
110     }
111 
112     public String getUrlTitle() {
113         return _article.getUrlTitle();
114     }
115 
116     public String getURLViewInContext(
117             LiferayPortletRequest liferayPortletRequest,
118             LiferayPortletResponse liferayPortletResponse,
119             String noSuchEntryRedirect)
120         throws Exception {
121 
122         ThemeDisplay themeDisplay =
123             (ThemeDisplay)liferayPortletRequest.getAttribute(
124                 WebKeys.THEME_DISPLAY);
125 
126         String languageId = LanguageUtil.getLanguageId(liferayPortletRequest);
127 
128         JournalArticleDisplay articleDisplay =
129             JournalContentUtil.getDisplay(
130                 _article.getGroupId(), _article.getArticleId(),
131                 null, null, languageId, themeDisplay);
132 
133         String viewURL = StringPool.BLANK;
134 
135         if (articleDisplay != null) {
136 
137             PortletURL viewPortletURL =
138                 liferayPortletResponse.createRenderURL();
139 
140             viewPortletURL.setParameter(
141                 "struts_action", "/asset_publisher/view_content");
142             viewPortletURL.setParameter("urlTitle", _article.getUrlTitle());
143             viewPortletURL.setParameter(
144                 "type", JournalArticleAssetRendererFactory.TYPE);
145 
146             viewURL = viewPortletURL.toString();
147         }
148 
149         return viewURL;
150     }
151 
152     public long getUserId() {
153         return _article.getUserId();
154     }
155 
156     public String getViewInContextMessage() {
157         return "view";
158     }
159 
160     public boolean hasEditPermission(PermissionChecker permissionChecker) {
161         return JournalArticlePermission.contains(
162             permissionChecker,_article, ActionKeys.UPDATE);
163     }
164 
165     public boolean hasViewPermission(PermissionChecker permissionChecker) {
166         return JournalArticlePermission.contains(
167             permissionChecker,_article, ActionKeys.VIEW);
168     }
169 
170     public boolean isConvertible() {
171         return true;
172     }
173 
174     public boolean isLocalizable() {
175         return true;
176     }
177 
178     public boolean isPrintable() {
179         return true;
180     }
181 
182     public String render(
183             RenderRequest renderRequest, RenderResponse renderResponse,
184             String template)
185         throws Exception {
186 
187         if (template.equals(TEMPLATE_ABSTRACT) ||
188             template.equals(TEMPLATE_FULL_CONTENT)) {
189 
190             renderRequest.setAttribute(WebKeys.JOURNAL_ARTICLE, _article);
191 
192             return "/html/portlet/journal/asset/" + template + ".jsp";
193         }
194         else {
195             return null;
196         }
197     }
198 
199     private JournalArticle _article;
200 
201 }