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.wikidisplay.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.struts.PortletAction;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.WebKeys;
025    import com.liferay.portlet.wiki.NoSuchNodeException;
026    import com.liferay.portlet.wiki.NoSuchPageException;
027    import com.liferay.portlet.wiki.model.WikiNode;
028    import com.liferay.portlet.wiki.model.WikiPage;
029    import com.liferay.portlet.wiki.model.WikiPageConstants;
030    import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
031    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
032    
033    import javax.portlet.PortletConfig;
034    import javax.portlet.PortletPreferences;
035    import javax.portlet.RenderRequest;
036    import javax.portlet.RenderResponse;
037    
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class ViewAction extends PortletAction {
046    
047            @Override
048            public ActionForward render(
049                            ActionMapping actionMapping, ActionForm actionForm,
050                            PortletConfig portletConfig, RenderRequest renderRequest,
051                            RenderResponse renderResponse)
052                    throws Exception {
053    
054                    try {
055                            PortletPreferences preferences = renderRequest.getPreferences();
056    
057                            ThemeDisplay themeDisplay =
058                                    (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
059    
060                            long nodeId = GetterUtil.getLong(
061                                    preferences.getValue("nodeId", StringPool.BLANK));
062                            String title = ParamUtil.getString(
063                                    renderRequest, "title",
064                                    preferences.getValue("title", WikiPageConstants.FRONT_PAGE));
065                            double version = ParamUtil.getDouble(renderRequest, "version");
066    
067                            WikiNode node = WikiNodeServiceUtil.getNode(nodeId);
068    
069                            if (node.getGroupId() != themeDisplay.getScopeGroupId()) {
070                                    throw new NoSuchNodeException();
071                            }
072    
073                            WikiPage wikiPage = null;
074    
075                            try {
076                                    wikiPage = WikiPageServiceUtil.getPage(nodeId, title, version);
077                            }
078                            catch (NoSuchPageException nspe) {
079                                    wikiPage = WikiPageServiceUtil.getPage(
080                                            nodeId, WikiPageConstants.FRONT_PAGE);
081                            }
082    
083                            renderRequest.setAttribute(WebKeys.WIKI_NODE, node);
084                            renderRequest.setAttribute(WebKeys.WIKI_PAGE, wikiPage);
085    
086                            return actionMapping.findForward("portlet.wiki_display.view");
087                    }
088                    catch (NoSuchNodeException nsne) {
089                            return actionMapping.findForward("/portal/portlet_not_setup");
090                    }
091                    catch (NoSuchPageException nspe) {
092                            return actionMapping.findForward("/portal/portlet_not_setup");
093                    }
094                    catch (PrincipalException pe) {
095                            SessionErrors.add(renderRequest, pe.getClass());
096    
097                            return actionMapping.findForward("portlet.wiki_display.error");
098                    }
099            }
100    
101    }