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.wiki.asset;
16  
17  import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
18  import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
19  import com.liferay.portal.kernel.util.HtmlUtil;
20  import com.liferay.portal.security.permission.ActionKeys;
21  import com.liferay.portal.security.permission.PermissionChecker;
22  import com.liferay.portal.theme.ThemeDisplay;
23  import com.liferay.portal.util.PortletKeys;
24  import com.liferay.portal.util.PropsValues;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.asset.model.BaseAssetRenderer;
27  import com.liferay.portlet.wiki.model.WikiPage;
28  import com.liferay.portlet.wiki.service.permission.WikiPagePermission;
29  
30  import javax.portlet.PortletURL;
31  import javax.portlet.RenderRequest;
32  import javax.portlet.RenderResponse;
33  
34  /**
35   * <a href="WikiPageAssetRenderer.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Julio Camarero
38   */
39  public class WikiPageAssetRenderer extends BaseAssetRenderer {
40  
41      public WikiPageAssetRenderer(WikiPage page) {
42          _page = page;
43      }
44  
45      public long getClassPK() {
46          return _page.getPageId();
47      }
48  
49      public String getDiscussionPath() {
50          if (PropsValues.WIKI_PAGE_COMMENTS_ENABLED) {
51              return "edit_page_discussion";
52          }
53          else {
54              return null;
55          }
56      }
57  
58      public long getGroupId() {
59          return _page.getGroupId();
60      }
61  
62      public String getSummary() {
63          String content = _page.getContent();
64  
65          if (_page.getFormat().equals("html")) {
66              content = HtmlUtil.stripHtml(content);
67          }
68  
69          return content;
70      }
71  
72      public String getTitle() {
73          return _page.getTitle();
74      }
75  
76      public PortletURL getURLEdit(
77          LiferayPortletRequest liferayPortletRequest,
78          LiferayPortletResponse liferayPortletResponse) {
79  
80          PortletURL editPortletURL = liferayPortletResponse.createRenderURL(
81              PortletKeys.WIKI);
82  
83          editPortletURL.setParameter("struts_action", "/wiki/edit_page");
84          editPortletURL.setParameter(
85              "nodeId", String.valueOf(_page.getNodeId()));
86          editPortletURL.setParameter("title", _page.getTitle());
87  
88          return editPortletURL;
89      }
90  
91      public PortletURL getURLExport(
92          LiferayPortletRequest liferayPortletRequest,
93          LiferayPortletResponse liferayPortletResponse) {
94  
95          PortletURL exportPortletURL = liferayPortletResponse.createActionURL();
96  
97          exportPortletURL.setParameter(
98              "struts_action", "/asset_publisher/export_wiki_page");
99          exportPortletURL.setParameter(
100             "nodeId", String.valueOf(_page.getNodeId()));
101         exportPortletURL.setParameter("title", _page.getTitle());
102 
103         return exportPortletURL;
104     }
105 
106     public String getURLViewInContext(
107         LiferayPortletRequest liferayPortletRequest,
108         LiferayPortletResponse liferayPortletResponse,
109         String noSuchEntryRedirect) {
110 
111         ThemeDisplay themeDisplay =
112             (ThemeDisplay)liferayPortletRequest.getAttribute(
113                 WebKeys.THEME_DISPLAY);
114 
115         return themeDisplay.getPathMain() +
116             "/wiki/find_page?pageResourcePrimKey=" + _page.getResourcePrimKey();
117     }
118 
119     public long getUserId() {
120         return _page.getUserId();
121     }
122 
123     public boolean hasEditPermission(PermissionChecker permissionChecker) {
124         return WikiPagePermission.contains(
125             permissionChecker, _page, ActionKeys.UPDATE);
126     }
127 
128     public boolean hasViewPermission(PermissionChecker permissionChecker) {
129         return WikiPagePermission.contains(
130             permissionChecker, _page, ActionKeys.VIEW);
131     }
132 
133     public boolean isConvertible() {
134         return true;
135     }
136 
137     public boolean isPrintable() {
138         return true;
139     }
140 
141     public String render(
142             RenderRequest renderRequest, RenderResponse renderResponse,
143             String template)
144         throws Exception {
145 
146         if (template.equals(TEMPLATE_FULL_CONTENT)) {
147             renderRequest.setAttribute(WebKeys.WIKI_PAGE, _page);
148 
149             return "/html/portlet/wiki/asset/" + template + ".jsp";
150         }
151         else {
152             return null;
153         }
154     }
155 
156     protected String getIconPath(ThemeDisplay themeDisplay) {
157         return themeDisplay.getPathThemeImages() + "/common/pages.png";
158     }
159 
160     private WikiPage _page;
161 
162 }