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.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                            double version = ParamUtil.getDouble(renderRequest, "version");
065    
066                            WikiNode node = WikiNodeServiceUtil.getNode(nodeId);
067    
068                            if (node.getGroupId() != themeDisplay.getScopeGroupId()) {
069                                    throw new NoSuchNodeException();
070                            }
071    
072                            WikiPage page = null;
073    
074                            try {
075                                    page = WikiPageServiceUtil.getPage(nodeId, title, version);
076                            }
077                            catch (NoSuchPageException nspe) {
078                                    page = WikiPageServiceUtil.getPage(
079                                            nodeId, WikiPageConstants.FRONT_PAGE);
080                            }
081    
082                            renderRequest.setAttribute(WebKeys.WIKI_NODE, node);
083                            renderRequest.setAttribute(WebKeys.WIKI_PAGE, page);
084    
085                            return mapping.findForward("portlet.wiki_display.view");
086                    }
087                    catch (NoSuchNodeException nsne) {
088                            return mapping.findForward("/portal/portlet_not_setup");
089                    }
090                    catch (NoSuchPageException nspe) {
091                            return mapping.findForward("/portal/portlet_not_setup");
092                    }
093                    catch (PrincipalException pe) {
094                            SessionErrors.add(renderRequest, pe.getClass());
095    
096                            return mapping.findForward("portlet.wiki_display.error");
097                    }
098            }
099    
100    }