001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public ActionForward render(
048                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
049                            RenderRequest renderRequest, RenderResponse renderResponse)
050                    throws Exception {
051    
052                    try {
053                            PortletPreferences preferences = renderRequest.getPreferences();
054    
055                            ThemeDisplay themeDisplay =
056                                    (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
057    
058                            long nodeId = GetterUtil.getLong(
059                                    preferences.getValue("node-id", StringPool.BLANK));
060                            String title = ParamUtil.getString(
061                                    renderRequest, "title",
062                                    preferences.getValue("title", WikiPageConstants.FRONT_PAGE));
063    
064                            WikiNode node = WikiNodeServiceUtil.getNode(nodeId);
065    
066                            if (node.getGroupId() != themeDisplay.getScopeGroupId()) {
067                                    throw new NoSuchNodeException();
068                            }
069    
070                            WikiPage wikiPage = null;
071    
072                            try {
073                                    wikiPage = WikiPageServiceUtil.getPage(nodeId, title);
074                            }
075                            catch (NoSuchPageException nspe) {
076                                    wikiPage = WikiPageServiceUtil.getPage(
077                                            nodeId, WikiPageConstants.FRONT_PAGE);
078                            }
079    
080                            renderRequest.setAttribute(WebKeys.WIKI_NODE, node);
081                            renderRequest.setAttribute(WebKeys.WIKI_PAGE, wikiPage);
082    
083                            return mapping.findForward("portlet.wiki_display.view");
084                    }
085                    catch (NoSuchNodeException nsne) {
086                            return mapping.findForward("/portal/portlet_not_setup");
087                    }
088                    catch (NoSuchPageException nsne) {
089                            return mapping.findForward("/portal/portlet_not_setup");
090                    }
091                    catch (PrincipalException pe) {
092                            SessionErrors.add(renderRequest, pe.getClass().getName());
093    
094                            return mapping.findForward("portlet.wiki_display.error");
095                    }
096            }
097    
098    }