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.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.security.permission.ActionKeys;
32  import com.liferay.portal.security.permission.PermissionChecker;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.PropsKeys;
36  import com.liferay.portal.util.PropsUtil;
37  import com.liferay.portal.util.WebKeys;
38  import com.liferay.portlet.wiki.NoSuchPageException;
39  import com.liferay.portlet.wiki.model.WikiNode;
40  import com.liferay.portlet.wiki.model.WikiPage;
41  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
42  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
43  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
44  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
45  import com.liferay.portlet.wiki.service.permission.WikiNodePermission;
46  import com.liferay.portlet.wiki.util.WikiUtil;
47  
48  import java.util.List;
49  
50  import javax.portlet.ActionRequest;
51  import javax.portlet.RenderRequest;
52  
53  import javax.servlet.http.HttpServletRequest;
54  
55  /**
56   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   * @author Jorge Ferrer
60   *
61   */
62  public class ActionUtil {
63  
64      public static WikiNode getFirstVisibleNode(RenderRequest renderRequest)
65          throws PortalException, SystemException {
66  
67          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
68              WebKeys.THEME_DISPLAY);
69  
70          WikiNode node = null;
71  
72          List<WikiNode> nodes = WikiUtil.getNodes(renderRequest);
73  
74          if (nodes.size() == 0) {
75              String nodeName = PropsUtil.get(PropsKeys.WIKI_INITIAL_NODE_NAME);
76  
77              node = WikiNodeLocalServiceUtil.addNode(
78                  themeDisplay.getUserId(), themeDisplay.getPlid(), nodeName,
79                  StringPool.BLANK, true, true);
80          }
81          else {
82              PermissionChecker permissionChecker =
83                  themeDisplay.getPermissionChecker();
84  
85              for (WikiNode curNode : nodes) {
86                  if (WikiNodePermission.contains(
87                          permissionChecker, curNode.getNodeId(),
88                          ActionKeys.VIEW)) {
89  
90                      node = curNode;
91  
92                      break;
93                  }
94              }
95  
96              if (node == null) {
97                  throw new PrincipalException();
98              }
99          }
100 
101         renderRequest.setAttribute(WebKeys.WIKI_NODE, node);
102 
103         return node;
104     }
105 
106     public static WikiNode getNode(ActionRequest actionRequest)
107         throws Exception {
108 
109         HttpServletRequest request = PortalUtil.getHttpServletRequest(
110             actionRequest);
111 
112         return getNode(request);
113     }
114 
115     public static WikiNode getNode(RenderRequest renderRequest)
116         throws Exception {
117 
118         HttpServletRequest request = PortalUtil.getHttpServletRequest(
119             renderRequest);
120 
121         return getNode(request);
122     }
123 
124     public static WikiNode getNode(HttpServletRequest request)
125         throws Exception {
126 
127         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
128             WebKeys.THEME_DISPLAY);
129 
130         long nodeId = ParamUtil.getLong(request, "nodeId");
131         String nodeName = ParamUtil.getString(request, "nodeName");
132 
133         WikiNode node = null;
134 
135         if (nodeId > 0) {
136             node = WikiNodeServiceUtil.getNode(nodeId);
137         }
138         else if (Validator.isNotNull(nodeName)) {
139             node = WikiNodeServiceUtil.getNode(
140                 themeDisplay.getPortletGroupId(), nodeName);
141         }
142 
143         request.setAttribute(WebKeys.WIKI_NODE, node);
144 
145         return node;
146     }
147 
148     public static void getPage(ActionRequest actionRequest) throws Exception {
149         HttpServletRequest request = PortalUtil.getHttpServletRequest(
150             actionRequest);
151 
152         getPage(request);
153     }
154 
155     public static void getPage(RenderRequest renderRequest) throws Exception {
156         HttpServletRequest request = PortalUtil.getHttpServletRequest(
157             renderRequest);
158 
159         getPage(request);
160     }
161 
162     public static void getPage(HttpServletRequest request) throws Exception {
163         long nodeId = ParamUtil.getLong(request, "nodeId");
164         String title = ParamUtil.getString(request, "title");
165         double version = ParamUtil.getDouble(request, "version");
166 
167         if (nodeId == 0) {
168             WikiNode node = (WikiNode)request.getAttribute(WebKeys.WIKI_NODE);
169 
170             if (node != null) {
171                 nodeId = node.getNodeId();
172             }
173         }
174 
175         if (Validator.isNull(title)) {
176             title = WikiPageImpl.FRONT_PAGE;
177         }
178 
179         WikiPage page = null;
180 
181         try {
182             page = WikiPageServiceUtil.getPage(nodeId, title, version);
183         }
184         catch (NoSuchPageException nspe) {
185             if (title.equals(WikiPageImpl.FRONT_PAGE) && (version == 0)) {
186                 page = WikiPageServiceUtil.addPage(
187                     nodeId, title, null, WikiPageImpl.NEW, true, null, null);
188             }
189             else {
190                 throw nspe;
191             }
192         }
193 
194         request.setAttribute(WebKeys.WIKI_PAGE, page);
195     }
196 
197 }