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