1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wiki.action;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.security.permission.ActionKeys;
29  import com.liferay.portal.security.permission.PermissionChecker;
30  import com.liferay.portal.service.UserLocalServiceUtil;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.PropsKeys;
34  import com.liferay.portal.util.PropsUtil;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.wiki.NoSuchPageException;
37  import com.liferay.portlet.wiki.model.WikiNode;
38  import com.liferay.portlet.wiki.model.WikiPage;
39  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
40  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
41  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
42  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
43  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
44  import com.liferay.portlet.wiki.service.permission.WikiNodePermission;
45  import com.liferay.portlet.wiki.util.WikiUtil;
46  
47  import java.util.List;
48  
49  import javax.portlet.ActionRequest;
50  import javax.portlet.RenderRequest;
51  
52  import javax.servlet.http.HttpServletRequest;
53  
54  /**
55   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   * @author Jorge Ferrer
59   *
60   */
61  public class ActionUtil {
62  
63      public static WikiNode getFirstVisibleNode(RenderRequest renderRequest)
64          throws PortalException, SystemException {
65  
66          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
67              WebKeys.THEME_DISPLAY);
68  
69          WikiNode node = null;
70  
71          List<WikiNode> nodes = WikiUtil.getNodes(renderRequest);
72  
73          if (nodes.size() == 0) {
74              String nodeName = PropsUtil.get(PropsKeys.WIKI_INITIAL_NODE_NAME);
75  
76              node = WikiNodeLocalServiceUtil.addNode(
77                  themeDisplay.getUserId(), themeDisplay.getPlid(), nodeName,
78                  StringPool.BLANK, true, true);
79          }
80          else {
81              PermissionChecker permissionChecker =
82                  themeDisplay.getPermissionChecker();
83  
84              for (WikiNode curNode : nodes) {
85                  if (WikiNodePermission.contains(
86                          permissionChecker, curNode.getNodeId(),
87                          ActionKeys.VIEW)) {
88  
89                      node = curNode;
90  
91                      break;
92                  }
93              }
94  
95              if (node == null) {
96                  throw new PrincipalException();
97              }
98          }
99  
100         renderRequest.setAttribute(WebKeys.WIKI_NODE, node);
101 
102         return node;
103     }
104 
105     public static WikiNode getNode(ActionRequest actionRequest)
106         throws Exception {
107 
108         HttpServletRequest request = PortalUtil.getHttpServletRequest(
109             actionRequest);
110 
111         return getNode(request);
112     }
113 
114     public static WikiNode getNode(RenderRequest renderRequest)
115         throws Exception {
116 
117         HttpServletRequest request = PortalUtil.getHttpServletRequest(
118             renderRequest);
119 
120         return getNode(request);
121     }
122 
123     public static WikiNode getNode(HttpServletRequest request)
124         throws Exception {
125 
126         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
127             WebKeys.THEME_DISPLAY);
128 
129         long nodeId = ParamUtil.getLong(request, "nodeId");
130         String nodeName = ParamUtil.getString(request, "nodeName");
131 
132         WikiNode node = null;
133 
134         if (nodeId > 0) {
135             node = WikiNodeServiceUtil.getNode(nodeId);
136         }
137         else if (Validator.isNotNull(nodeName)) {
138             node = WikiNodeServiceUtil.getNode(
139                 themeDisplay.getScopeGroupId(), nodeName);
140         }
141 
142         request.setAttribute(WebKeys.WIKI_NODE, node);
143 
144         return node;
145     }
146 
147     public static void getPage(ActionRequest actionRequest) throws Exception {
148         HttpServletRequest request = PortalUtil.getHttpServletRequest(
149             actionRequest);
150 
151         getPage(request);
152     }
153 
154     public static void getPage(RenderRequest renderRequest) throws Exception {
155         HttpServletRequest request = PortalUtil.getHttpServletRequest(
156             renderRequest);
157 
158         getPage(request);
159     }
160 
161     public static void getPage(HttpServletRequest request) throws Exception {
162         long nodeId = ParamUtil.getLong(request, "nodeId");
163         String title = ParamUtil.getString(request, "title");
164         double version = ParamUtil.getDouble(request, "version");
165 
166         if (nodeId == 0) {
167             WikiNode node = (WikiNode)request.getAttribute(WebKeys.WIKI_NODE);
168 
169             if (node != null) {
170                 nodeId = node.getNodeId();
171             }
172         }
173 
174         if (Validator.isNull(title)) {
175             title = WikiPageImpl.FRONT_PAGE;
176         }
177 
178         WikiPage page = null;
179 
180         try {
181             page = WikiPageServiceUtil.getPage(nodeId, title, version);
182         }
183         catch (NoSuchPageException nspe) {
184             if (title.equals(WikiPageImpl.FRONT_PAGE) && (version == 0)) {
185                 long userId = PortalUtil.getUserId(request);
186 
187                 if (userId == 0) {
188                     long companyId = PortalUtil.getCompanyId(request);
189 
190                     userId = UserLocalServiceUtil.getDefaultUserId(companyId);
191                 }
192 
193                 page = WikiPageLocalServiceUtil.addPage(
194                     userId, nodeId, title, null, WikiPageImpl.NEW, true, null,
195                     null);
196             }
197             else {
198                 throw nspe;
199             }
200         }
201 
202         request.setAttribute(WebKeys.WIKI_PAGE, page);
203     }
204 
205 }