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.wiki.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.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.asset.model.BaseAssetRenderer;
027    import com.liferay.portlet.wiki.model.WikiPage;
028    import com.liferay.portlet.wiki.model.WikiPageConstants;
029    import com.liferay.portlet.wiki.service.permission.WikiPagePermission;
030    import com.liferay.portlet.wiki.util.WikiUtil;
031    
032    import java.util.Locale;
033    
034    import javax.portlet.PortletRequest;
035    import javax.portlet.PortletURL;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    import javax.portlet.WindowState;
039    
040    /**
041     * @author Julio Camarero
042     * @author Sergio Gonz??lez
043     */
044    public class WikiPageAssetRenderer extends BaseAssetRenderer {
045    
046            public WikiPageAssetRenderer(WikiPage page) {
047                    _page = page;
048            }
049    
050            @Override
051            public long getClassPK() {
052                    if (!_page.isApproved() && !_page.isDraft() && !_page.isPending() &&
053                            (_page.getVersion() != WikiPageConstants.VERSION_DEFAULT)) {
054    
055                            return _page.getPageId();
056                    }
057                    else {
058                            return _page.getResourcePrimKey();
059                    }
060            }
061    
062            @Override
063            public String getDiscussionPath() {
064                    if (PropsValues.WIKI_PAGE_COMMENTS_ENABLED) {
065                            return "edit_page_discussion";
066                    }
067                    else {
068                            return null;
069                    }
070            }
071    
072            @Override
073            public long getGroupId() {
074                    return _page.getGroupId();
075            }
076    
077            @Override
078            public String getSummary(Locale locale) {
079                    String content = _page.getContent();
080    
081                    try {
082                            content = HtmlUtil.extractText(
083                                    WikiUtil.convert(_page, null, null, null));
084                    }
085                    catch (Exception e) {
086                    }
087    
088                    return content;
089            }
090    
091            @Override
092            public String getTitle(Locale locale) {
093                    return _page.getTitle();
094            }
095    
096            @Override
097            public PortletURL getURLEdit(
098                            LiferayPortletRequest liferayPortletRequest,
099                            LiferayPortletResponse liferayPortletResponse)
100                    throws Exception {
101    
102                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
103                            getControlPanelPlid(liferayPortletRequest), PortletKeys.WIKI,
104                            PortletRequest.RENDER_PHASE);
105    
106                    portletURL.setParameter("struts_action", "/wiki/edit_page");
107                    portletURL.setParameter("nodeId", String.valueOf(_page.getNodeId()));
108                    portletURL.setParameter("title", _page.getTitle());
109    
110                    return portletURL;
111            }
112    
113            @Override
114            public PortletURL getURLExport(
115                    LiferayPortletRequest liferayPortletRequest,
116                    LiferayPortletResponse liferayPortletResponse) {
117    
118                    PortletURL exportPortletURL = liferayPortletResponse.createActionURL();
119    
120                    exportPortletURL.setParameter(
121                            "struts_action", "/asset_publisher/export_wiki_page");
122                    exportPortletURL.setParameter(
123                            "nodeId", String.valueOf(_page.getNodeId()));
124                    exportPortletURL.setParameter("title", _page.getTitle());
125    
126                    return exportPortletURL;
127            }
128    
129            @Override
130            public PortletURL getURLView(
131                            LiferayPortletResponse liferayPortletResponse,
132                            WindowState windowState)
133                    throws Exception {
134    
135                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
136                            PortletKeys.WIKI, PortletRequest.RENDER_PHASE);
137    
138                    portletURL.setParameter("struts_action", "/wiki/view");
139                    portletURL.setParameter("nodeId", String.valueOf(_page.getNodeId()));
140                    portletURL.setParameter("title", _page.getTitle());
141                    portletURL.setWindowState(windowState);
142    
143                    return portletURL;
144            }
145    
146            @Override
147            public String getURLViewInContext(
148                    LiferayPortletRequest liferayPortletRequest,
149                    LiferayPortletResponse liferayPortletResponse,
150                    String noSuchEntryRedirect) {
151    
152                    return getURLViewInContext(
153                            liferayPortletRequest, noSuchEntryRedirect, "/wiki/find_page",
154                            "pageResourcePrimKey", _page.getResourcePrimKey());
155            }
156    
157            @Override
158            public long getUserId() {
159                    return _page.getUserId();
160            }
161    
162            @Override
163            public String getUserName() {
164                    return _page.getUserName();
165            }
166    
167            @Override
168            public String getUuid() {
169                    return _page.getUuid();
170            }
171    
172            @Override
173            public boolean hasEditPermission(PermissionChecker permissionChecker) {
174                    return WikiPagePermission.contains(
175                            permissionChecker, _page, ActionKeys.UPDATE);
176            }
177    
178            @Override
179            public boolean hasViewPermission(PermissionChecker permissionChecker) {
180                    return WikiPagePermission.contains(
181                            permissionChecker, _page, ActionKeys.VIEW);
182            }
183    
184            @Override
185            public boolean isConvertible() {
186                    return true;
187            }
188    
189            @Override
190            public boolean isPrintable() {
191                    return true;
192            }
193    
194            @Override
195            public String render(
196                            RenderRequest renderRequest, RenderResponse renderResponse,
197                            String template)
198                    throws Exception {
199    
200                    if (template.equals(TEMPLATE_ABSTRACT) ||
201                            template.equals(TEMPLATE_FULL_CONTENT)) {
202    
203                            renderRequest.setAttribute(WebKeys.WIKI_PAGE, _page);
204    
205                            return "/html/portlet/wiki/asset/" + template + ".jsp";
206                    }
207                    else {
208                            return null;
209                    }
210            }
211    
212            @Override
213            protected String getIconPath(ThemeDisplay themeDisplay) {
214                    return themeDisplay.getPathThemeImages() + "/common/pages.png";
215            }
216    
217            private WikiPage _page;
218    
219    }