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 portletPreferences =
056                                    renderRequest.getPreferences();
057    
058                            ThemeDisplay themeDisplay =
059                                    (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
060    
061                            long nodeId = GetterUtil.getLong(
062                                    portletPreferences.getValue("nodeId", StringPool.BLANK));
063                            String title = ParamUtil.getString(
064                                    renderRequest, "title",
065                                    portletPreferences.getValue(
066                                            "title", WikiPageConstants.FRONT_PAGE));
067                            double version = ParamUtil.getDouble(renderRequest, "version");
068    
069                            WikiNode node = WikiNodeServiceUtil.getNode(nodeId);
070    
071                            if (node.getGroupId() != themeDisplay.getScopeGroupId()) {
072                                    throw new NoSuchNodeException("{nodeId=" + nodeId + "}");
073                            }
074    
075                            WikiPage page = null;
076    
077                            try {
078                                    page = WikiPageServiceUtil.getPage(nodeId, title, version);
079                            }
080                            catch (NoSuchPageException nspe) {
081                                    page = WikiPageServiceUtil.getPage(
082                                            nodeId, WikiPageConstants.FRONT_PAGE);
083                            }
084    
085                            renderRequest.setAttribute(WebKeys.WIKI_NODE, node);
086                            renderRequest.setAttribute(WebKeys.WIKI_PAGE, page);
087    
088                            return actionMapping.findForward("portlet.wiki_display.view");
089                    }
090                    catch (NoSuchNodeException nsne) {
091                            return actionMapping.findForward("/portal/portlet_not_setup");
092                    }
093                    catch (NoSuchPageException nspe) {
094                            return actionMapping.findForward("/portal/portlet_not_setup");
095                    }
096                    catch (PrincipalException pe) {
097                            SessionErrors.add(renderRequest, pe.getClass());
098    
099                            return actionMapping.findForward("portlet.wiki_display.error");
100                    }
101            }
102    
103    }