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 req) throws Exception {
52          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
53  
54          getNode(httpReq);
55      }
56  
57      public static void getNode(RenderRequest req) throws Exception {
58          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
59  
60          getNode(httpReq);
61      }
62  
63      public static void getNode(HttpServletRequest req) throws Exception {
64          ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
65              WebKeys.THEME_DISPLAY);
66  
67          long nodeId = ParamUtil.getLong(req, "nodeId");
68          String nodeName = ParamUtil.getString(req, "nodeName");
69  
70          WikiNode node = null;
71  
72          if (nodeId > 0) {
73              node = WikiNodeServiceUtil.getNode(nodeId);
74          }
75          else if (Validator.isNotNull(nodeName)) {
76              node = WikiNodeServiceUtil.getNode(
77                  themeDisplay.getPortletGroupId(), nodeName);
78          }
79  
80          req.setAttribute(WebKeys.WIKI_NODE, node);
81      }
82  
83      public static void getPage(ActionRequest req) throws Exception {
84          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
85  
86          getPage(httpReq);
87      }
88  
89      public static void getPage(RenderRequest req) throws Exception {
90          HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(req);
91  
92          getPage(httpReq);
93      }
94  
95      public static void getPage(HttpServletRequest req) throws Exception {
96          long nodeId = ParamUtil.getLong(req, "nodeId");
97          String title = ParamUtil.getString(req, "title");
98          double version = ParamUtil.getDouble(req, "version");
99  
100         if (nodeId == 0) {
101             WikiNode node = (WikiNode)req.getAttribute(WebKeys.WIKI_NODE);
102 
103             if (node != null) {
104                 nodeId = node.getNodeId();
105             }
106         }
107 
108         if (Validator.isNull(title)) {
109             title = WikiPageImpl.FRONT_PAGE;
110         }
111 
112         WikiPage page = null;
113 
114         try {
115             page = WikiPageServiceUtil.getPage(nodeId, title, version);
116         }
117         catch (NoSuchPageException nspe) {
118             if (title.equals(WikiPageImpl.FRONT_PAGE) && (version == 0)) {
119                 page = WikiPageServiceUtil.addPage(nodeId, title, null, null);
120             }
121             else {
122                 throw nspe;
123             }
124         }
125 
126         req.setAttribute(WebKeys.WIKI_PAGE, page);
127     }
128 
129 }