001    /**
002     * Copyright (c) 2000-2010 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 mapping, 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().getName());
055    
056                                    return mapping.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 mapping.findForward("portlet.wiki.view_categorized_pages");
067                    }
068                    else {
069                            return mapping.findForward(defaultForward);
070                    }
071            }
072    
073            public ActionForward render(
074                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
075                            RenderRequest renderRequest, RenderResponse renderResponse)
076                    throws Exception {
077    
078                    return viewNode(
079                            mapping, renderRequest,
080                            getForward(renderRequest, "portlet.wiki.view_node"));
081            }
082    
083    }