001    /**
002     * Copyright (c) 2000-2012 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.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            @Override
048            public ActionForward render(
049                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
050                            RenderRequest renderRequest, RenderResponse renderResponse)
051                    throws Exception {
052    
053                    try {
054                            PortletPreferences preferences = renderRequest.getPreferences();
055    
056                            ThemeDisplay themeDisplay =
057                                    (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
058    
059                            long nodeId = GetterUtil.getLong(
060                                    preferences.getValue("nodeId", StringPool.BLANK));
061                            String title = ParamUtil.getString(
062                                    renderRequest, "title",
063                                    preferences.getValue("title", WikiPageConstants.FRONT_PAGE));
064    
065                            WikiNode node = WikiNodeServiceUtil.getNode(nodeId);
066    
067                            if (node.getGroupId() != themeDisplay.getScopeGroupId()) {
068                                    throw new NoSuchNodeException();
069                            }
070    
071                            WikiPage wikiPage = null;
072    
073                            try {
074                                    wikiPage = WikiPageServiceUtil.getPage(nodeId, title);
075                            }
076                            catch (NoSuchPageException nspe) {
077                                    wikiPage = WikiPageServiceUtil.getPage(
078                                            nodeId, WikiPageConstants.FRONT_PAGE);
079                            }
080    
081                            renderRequest.setAttribute(WebKeys.WIKI_NODE, node);
082                            renderRequest.setAttribute(WebKeys.WIKI_PAGE, wikiPage);
083    
084                            return mapping.findForward("portlet.wiki_display.view");
085                    }
086                    catch (NoSuchNodeException nsne) {
087                            return mapping.findForward("/portal/portlet_not_setup");
088                    }
089                    catch (NoSuchPageException nsne) {
090                            return mapping.findForward("/portal/portlet_not_setup");
091                    }
092                    catch (PrincipalException pe) {
093                            SessionErrors.add(renderRequest, pe.getClass().getName());
094    
095                            return mapping.findForward("portlet.wiki_display.error");
096                    }
097            }
098    
099    }