1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wikidisplay.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.wiki.NoSuchNodeException;
30  import com.liferay.portlet.wiki.NoSuchPageException;
31  import com.liferay.portlet.wiki.model.WikiNode;
32  import com.liferay.portlet.wiki.model.WikiPage;
33  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
34  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
35  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
36  
37  import javax.portlet.PortletConfig;
38  import javax.portlet.PortletPreferences;
39  import javax.portlet.RenderRequest;
40  import javax.portlet.RenderResponse;
41  
42  import org.apache.struts.action.ActionForm;
43  import org.apache.struts.action.ActionForward;
44  import org.apache.struts.action.ActionMapping;
45  
46  /**
47   * <a href="ViewAction.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class ViewAction extends PortletAction {
53  
54      public ActionForward render(
55              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56              RenderRequest renderRequest, RenderResponse renderResponse)
57          throws Exception {
58  
59          try {
60              PortletPreferences prefs = renderRequest.getPreferences();
61  
62              long nodeId = GetterUtil.getLong(
63                  prefs.getValue("node-id", StringPool.BLANK));
64              String title = ParamUtil.getString(
65                  renderRequest, "title",
66                  prefs.getValue("title", WikiPageImpl.FRONT_PAGE));
67  
68              WikiNode node = WikiNodeServiceUtil.getNode(nodeId);
69  
70              WikiPage wikiPage = null;
71  
72              try {
73                  wikiPage = WikiPageServiceUtil.getPage(nodeId, title);
74              }
75              catch (NoSuchPageException nspe) {
76                  wikiPage = WikiPageServiceUtil.getPage(
77                      nodeId, WikiPageImpl.FRONT_PAGE);
78              }
79  
80              renderRequest.setAttribute(WebKeys.WIKI_NODE, node);
81              renderRequest.setAttribute(WebKeys.WIKI_PAGE, wikiPage);
82  
83              return mapping.findForward("portlet.wiki_display.view");
84          }
85          catch (NoSuchNodeException nsne) {
86              return mapping.findForward("/portal/portlet_not_setup");
87          }
88          catch (NoSuchPageException nsne) {
89              return mapping.findForward("/portal/portlet_not_setup");
90          }
91          catch (PrincipalException pe) {
92              SessionErrors.add(renderRequest, pe.getClass().getName());
93  
94              return mapping.findForward("portlet.wiki_display.error");
95          }
96      }
97  
98  }