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.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                            if (node == null) {
047                                    ActionUtil.getFirstVisibleNode(renderRequest);
048                            }
049                    }
050                    catch (Exception e) {
051                            if (e instanceof NoSuchNodeException ||
052                                    e instanceof PrincipalException) {
053    
054                                    SessionErrors.add(renderRequest, e.getClass());
055    
056                                    return actionMapping.findForward("portlet.wiki.error");
057                            }
058                            else {
059                                    throw e;
060                            }
061                    }
062    
063                    long categoryId = ParamUtil.getLong(renderRequest, "categoryId");
064    
065                    if (categoryId > 0) {
066                            return actionMapping.findForward(
067                                    "portlet.wiki.view_categorized_pages");
068                    }
069                    else {
070                            return actionMapping.findForward(defaultForward);
071                    }
072            }
073    
074            @Override
075            public ActionForward render(
076                            ActionMapping actionMapping, ActionForm actionForm,
077                            PortletConfig portletConfig, RenderRequest renderRequest,
078                            RenderResponse renderResponse)
079                    throws Exception {
080    
081                    return viewNode(
082                            actionMapping, renderRequest,
083                            getForward(renderRequest, "portlet.wiki.view_node"));
084            }
085    
086    }