1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wikidisplay.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.util.WebKeys;
24  import com.liferay.portlet.wiki.NoSuchNodeException;
25  import com.liferay.portlet.wiki.NoSuchPageException;
26  import com.liferay.portlet.wiki.model.WikiNode;
27  import com.liferay.portlet.wiki.model.WikiPage;
28  import com.liferay.portlet.wiki.model.WikiPageConstants;
29  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
30  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
31  
32  import javax.portlet.PortletConfig;
33  import javax.portlet.PortletPreferences;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionForward;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class ViewAction extends PortletAction {
47  
48      public ActionForward render(
49              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
50              RenderRequest renderRequest, RenderResponse renderResponse)
51          throws Exception {
52  
53          try {
54              PortletPreferences preferences = renderRequest.getPreferences();
55  
56              long nodeId = GetterUtil.getLong(
57                  preferences.getValue("node-id", StringPool.BLANK));
58              String title = ParamUtil.getString(
59                  renderRequest, "title",
60                  preferences.getValue("title", WikiPageConstants.FRONT_PAGE));
61  
62              WikiNode node = WikiNodeServiceUtil.getNode(nodeId);
63  
64              WikiPage wikiPage = null;
65  
66              try {
67                  wikiPage = WikiPageServiceUtil.getPage(nodeId, title);
68              }
69              catch (NoSuchPageException nspe) {
70                  wikiPage = WikiPageServiceUtil.getPage(
71                      nodeId, WikiPageConstants.FRONT_PAGE);
72              }
73  
74              renderRequest.setAttribute(WebKeys.WIKI_NODE, node);
75              renderRequest.setAttribute(WebKeys.WIKI_PAGE, wikiPage);
76  
77              return mapping.findForward("portlet.wiki_display.view");
78          }
79          catch (NoSuchNodeException nsne) {
80              return mapping.findForward("/portal/portlet_not_setup");
81          }
82          catch (NoSuchPageException nsne) {
83              return mapping.findForward("/portal/portlet_not_setup");
84          }
85          catch (PrincipalException pe) {
86              SessionErrors.add(renderRequest, pe.getClass().getName());
87  
88              return mapping.findForward("portlet.wiki_display.error");
89          }
90      }
91  
92  }