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.util.PortalUtil;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.wiki.NoSuchPageException;
30  import com.liferay.portlet.wiki.model.WikiNode;
31  import com.liferay.portlet.wiki.model.WikiPage;
32  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
33  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
34  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.RenderRequest;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  /**
42   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class ActionUtil {
48  
49      public static void getNode(ActionRequest req) throws Exception {
50          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
51  
52          getNode(httpReq);
53      }
54  
55      public static void getNode(RenderRequest req) throws Exception {
56          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
57  
58          getNode(httpReq);
59      }
60  
61      public static void getNode(HttpServletRequest req) throws Exception {
62          long nodeId = ParamUtil.getLong(req, "nodeId");
63  
64          WikiNode node = null;
65  
66          if (nodeId > 0) {
67              node = WikiNodeServiceUtil.getNode(nodeId);
68          }
69  
70          req.setAttribute(WebKeys.WIKI_NODE, node);
71      }
72  
73      public static void getPage(ActionRequest req) throws Exception {
74          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
75  
76          getPage(httpReq);
77      }
78  
79      public static void getPage(RenderRequest req) throws Exception {
80          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
81  
82          getPage(httpReq);
83      }
84  
85      public static void getPage(HttpServletRequest req) throws Exception {
86          long nodeId = ParamUtil.getLong(req, "nodeId");
87          String title = ParamUtil.getString(req, "title");
88          double version = ParamUtil.getDouble(req, "version");
89  
90          if (nodeId == 0) {
91              WikiNode node = (WikiNode)req.getAttribute(WebKeys.WIKI_NODE);
92  
93              if (node != null) {
94                  nodeId = node.getNodeId();
95              }
96          }
97  
98          if (Validator.isNull(title)) {
99              title = WikiPageImpl.FRONT_PAGE;
100         }
101 
102         WikiPage page = null;
103 
104         try {
105             page = WikiPageServiceUtil.getPage(nodeId, title, version);
106         }
107         catch (NoSuchPageException nspe) {
108             if (version == 0) {
109                 page = WikiPageServiceUtil.addPage(nodeId, title);
110             }
111         }
112 
113         req.setAttribute(WebKeys.WIKI_PAGE, page);
114     }
115 
116 }