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.kernel.util.Validator;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.struts.PortletAction;
022    import com.liferay.portal.util.WebKeys;
023    import com.liferay.portlet.wiki.NoSuchNodeException;
024    import com.liferay.portlet.wiki.NoSuchPageException;
025    import com.liferay.portlet.wiki.model.WikiNode;
026    
027    import javax.portlet.PortletConfig;
028    import javax.portlet.RenderRequest;
029    import javax.portlet.RenderResponse;
030    
031    import org.apache.struts.action.ActionForm;
032    import org.apache.struts.action.ActionForward;
033    import org.apache.struts.action.ActionMapping;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Jorge Ferrer
038     */
039    public class ViewPageAction extends PortletAction {
040    
041            public ActionForward render(
042                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
043                            RenderRequest renderRequest, RenderResponse renderResponse)
044                    throws Exception {
045    
046                    long categoryId = ParamUtil.getLong(renderRequest, "categoryId");
047                    String tag = ParamUtil.getString(renderRequest, "tag");
048    
049                    if (categoryId > 0) {
050                            return ViewNodeAction.viewNode(
051                                    mapping, renderRequest, "portlet.wiki.view_categorized_pages");
052                    }
053    
054                    if (Validator.isNotNull(tag)) {
055                            return ViewNodeAction.viewNode(
056                                    mapping, renderRequest, "portlet.wiki.view_tagged_pages");
057                    }
058    
059                    try {
060                            ActionUtil.getNode(renderRequest);
061                            ActionUtil.getPage(renderRequest);
062                    }
063                    catch (Exception e) {
064                            if (e instanceof NoSuchNodeException ||
065                                    e instanceof NoSuchPageException ||
066                                    e instanceof PrincipalException) {
067    
068                                    SessionErrors.add(renderRequest, e.getClass().getName());
069    
070                                    return mapping.findForward("portlet.wiki.error");
071                            }
072                            else {
073                                    throw e;
074                            }
075                    }
076    
077                    return mapping.findForward(
078                            getForward(renderRequest, "portlet.wiki.view_page"));
079            }
080    
081            protected void getNode(RenderRequest renderRequest) throws Exception {
082                    ActionUtil.getNode(renderRequest);
083    
084                    WikiNode node = (WikiNode)renderRequest.getAttribute(WebKeys.WIKI_NODE);
085    
086                    if (node != null) {
087                            return;
088                    }
089    
090                    ActionUtil.getFirstVisibleNode(renderRequest);
091            }
092    
093            protected boolean isCheckMethodOnProcessAction() {
094                    return false;
095            }
096    
097    }