001    /**
002     * Copyright (c) 2000-present 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.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.struts.PortletAction;
021    import com.liferay.portlet.wiki.NoSuchNodeException;
022    import com.liferay.portlet.wiki.model.WikiNode;
023    
024    import javax.portlet.PortletConfig;
025    import javax.portlet.RenderRequest;
026    import javax.portlet.RenderResponse;
027    
028    import org.apache.struts.action.ActionForm;
029    import org.apache.struts.action.ActionForward;
030    import org.apache.struts.action.ActionMapping;
031    
032    /**
033     * @author Jorge Ferrer
034     * @author Jack Li
035     */
036    public class ViewNodeAction extends PortletAction {
037    
038            public static ActionForward viewNode(
039                            ActionMapping actionMapping, RenderRequest renderRequest,
040                            String defaultForward)
041                    throws Exception {
042    
043                    try {
044                            WikiNode node = ActionUtil.getNode(renderRequest);
045    
046                            ActionUtil.getFirstVisiblePage(node.getNodeId(), renderRequest);
047                    }
048                    catch (Exception e) {
049                            if (e instanceof NoSuchNodeException ||
050                                    e instanceof PrincipalException) {
051    
052                                    SessionErrors.add(renderRequest, e.getClass());
053    
054                                    return actionMapping.findForward("portlet.wiki.error");
055                            }
056                            else {
057                                    throw e;
058                            }
059                    }
060    
061                    long categoryId = ParamUtil.getLong(renderRequest, "categoryId");
062    
063                    if (categoryId > 0) {
064                            return actionMapping.findForward(
065                                    "portlet.wiki.view_categorized_pages");
066                    }
067                    else {
068                            return actionMapping.findForward(defaultForward);
069                    }
070            }
071    
072            @Override
073            public ActionForward render(
074                            ActionMapping actionMapping, ActionForm actionForm,
075                            PortletConfig portletConfig, RenderRequest renderRequest,
076                            RenderResponse renderResponse)
077                    throws Exception {
078    
079                    return viewNode(
080                            actionMapping, renderRequest,
081                            getForward(renderRequest, "portlet.wiki.view_node"));
082            }
083    
084    }