1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wiki.action;
24  
25  import com.liferay.portal.kernel.util.ParamUtil;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portal.util.WebKeys;
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.ActionRequest;
38  import javax.portlet.RenderRequest;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  /**
43   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Jorge Ferrer
47   *
48   */
49  public class ActionUtil {
50  
51      public static void getNode(ActionRequest actionRequest) throws Exception {
52          HttpServletRequest request = PortalUtil.getHttpServletRequest(
53              actionRequest);
54  
55          getNode(request);
56      }
57  
58      public static void getNode(RenderRequest renderRequest) throws Exception {
59          HttpServletRequest request = PortalUtil.getHttpServletRequest(
60              renderRequest);
61  
62          getNode(request);
63      }
64  
65      public static void getNode(HttpServletRequest request) throws Exception {
66          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
67              WebKeys.THEME_DISPLAY);
68  
69          long nodeId = ParamUtil.getLong(request, "nodeId");
70          String nodeName = ParamUtil.getString(request, "nodeName");
71  
72          WikiNode node = null;
73  
74          if (nodeId > 0) {
75              node = WikiNodeServiceUtil.getNode(nodeId);
76          }
77          else if (Validator.isNotNull(nodeName)) {
78              node = WikiNodeServiceUtil.getNode(
79                  themeDisplay.getPortletGroupId(), nodeName);
80          }
81  
82          request.setAttribute(WebKeys.WIKI_NODE, node);
83      }
84  
85      public static void getPage(ActionRequest actionRequest) throws Exception {
86          HttpServletRequest request = PortalUtil.getHttpServletRequest(
87              actionRequest);
88  
89          getPage(request);
90      }
91  
92      public static void getPage(RenderRequest renderRequest) throws Exception {
93          HttpServletRequest request = PortalUtil.getHttpServletRequest(
94              renderRequest);
95  
96          getPage(request);
97      }
98  
99      public static void getPage(HttpServletRequest request) throws Exception {
100         long nodeId = ParamUtil.getLong(request, "nodeId");
101         String title = ParamUtil.getString(request, "title");
102         double version = ParamUtil.getDouble(request, "version");
103 
104         if (nodeId == 0) {
105             WikiNode node = (WikiNode)request.getAttribute(WebKeys.WIKI_NODE);
106 
107             if (node != null) {
108                 nodeId = node.getNodeId();
109             }
110         }
111 
112         if (Validator.isNull(title)) {
113             title = WikiPageImpl.FRONT_PAGE;
114         }
115 
116         WikiPage page = null;
117 
118         try {
119             page = WikiPageServiceUtil.getPage(nodeId, title, version);
120         }
121         catch (NoSuchPageException nspe) {
122             if (title.equals(WikiPageImpl.FRONT_PAGE) && (version == 0)) {
123                 page = WikiPageServiceUtil.addPage(
124                     nodeId, title, null, WikiPageImpl.NEW, true, null, null);
125             }
126             else {
127                 throw nspe;
128             }
129         }
130 
131         request.setAttribute(WebKeys.WIKI_PAGE, page);
132     }
133 
134 }