1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.action;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.PropsKeys;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.Layout;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.service.ServiceContext;
26  import com.liferay.portal.service.ServiceContextFactory;
27  import com.liferay.portal.service.UserLocalServiceUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.PropsUtil;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.wiki.NoSuchNodeException;
33  import com.liferay.portlet.wiki.NoSuchPageException;
34  import com.liferay.portlet.wiki.model.WikiNode;
35  import com.liferay.portlet.wiki.model.WikiPage;
36  import com.liferay.portlet.wiki.model.WikiPageConstants;
37  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
38  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
39  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
40  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
41  import com.liferay.portlet.wiki.util.WikiUtil;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.PortletRequest;
45  import javax.portlet.RenderRequest;
46  
47  import javax.servlet.http.HttpServletRequest;
48  
49  /**
50   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   * @author Jorge Ferrer
54   */
55  public class ActionUtil {
56  
57      public static WikiNode getFirstVisibleNode(PortletRequest portletRequest)
58          throws PortalException, SystemException {
59  
60          ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
61              WebKeys.THEME_DISPLAY);
62  
63          WikiNode node = null;
64  
65          int nodesCount = WikiNodeLocalServiceUtil.getNodesCount(
66              themeDisplay.getScopeGroupId());
67  
68          if (nodesCount == 0) {
69              String nodeName = PropsUtil.get(PropsKeys.WIKI_INITIAL_NODE_NAME);
70  
71              Layout layout = themeDisplay.getLayout();
72  
73              ServiceContext serviceContext = ServiceContextFactory.getInstance(
74                  WikiNode.class.getName(), portletRequest);
75  
76              serviceContext.setAddCommunityPermissions(true);
77  
78              if (layout.isPublicLayout()) {
79                  serviceContext.setAddGuestPermissions(true);
80              }
81              else {
82                  serviceContext.setAddGuestPermissions(false);
83              }
84  
85              node = WikiNodeLocalServiceUtil.addNode(
86                  themeDisplay.getUserId(), nodeName, StringPool.BLANK,
87                  serviceContext);
88          }
89          else {
90              node = WikiUtil.getFirstNode(portletRequest);
91  
92              if (node == null) {
93                  throw new PrincipalException();
94              }
95  
96              return node;
97          }
98  
99          portletRequest.setAttribute(WebKeys.WIKI_NODE, node);
100 
101         return node;
102     }
103 
104     public static WikiNode getNode(PortletRequest portletRequest)
105         throws Exception {
106 
107         HttpServletRequest request = PortalUtil.getHttpServletRequest(
108             portletRequest);
109 
110         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
111             WebKeys.THEME_DISPLAY);
112 
113         long nodeId = ParamUtil.getLong(request, "nodeId");
114         String nodeName = ParamUtil.getString(request, "nodeName");
115 
116         WikiNode node = null;
117 
118         try {
119             if (nodeId > 0) {
120                 node = WikiNodeServiceUtil.getNode(nodeId);
121             }
122             else if (Validator.isNotNull(nodeName)) {
123                 node = WikiNodeServiceUtil.getNode(
124                     themeDisplay.getScopeGroupId(), nodeName);
125             }
126             else {
127                 throw new NoSuchNodeException();
128             }
129         }
130         catch (NoSuchNodeException nsne) {
131             node = ActionUtil.getFirstVisibleNode(portletRequest);
132         }
133 
134         request.setAttribute(WebKeys.WIKI_NODE, node);
135 
136         return node;
137     }
138 
139     public static void getPage(ActionRequest actionRequest) throws Exception {
140         HttpServletRequest request = PortalUtil.getHttpServletRequest(
141             actionRequest);
142 
143         getPage(request);
144     }
145 
146     public static void getPage(RenderRequest renderRequest) throws Exception {
147         HttpServletRequest request = PortalUtil.getHttpServletRequest(
148             renderRequest);
149 
150         getPage(request);
151     }
152 
153     public static void getPage(HttpServletRequest request) throws Exception {
154         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
155             WebKeys.THEME_DISPLAY);
156 
157         long nodeId = ParamUtil.getLong(request, "nodeId");
158         String title = ParamUtil.getString(request, "title");
159         double version = ParamUtil.getDouble(request, "version");
160 
161         WikiNode node = null;
162 
163         try {
164             if (nodeId > 0) {
165                 node = WikiNodeServiceUtil.getNode(nodeId);
166             }
167         }
168         catch (NoSuchNodeException nsne) {
169         }
170 
171         if (node == null) {
172             node = (WikiNode)request.getAttribute(WebKeys.WIKI_NODE);
173 
174             if (node != null) {
175                 nodeId = node.getNodeId();
176             }
177         }
178 
179         if (Validator.isNull(title)) {
180             title = WikiPageConstants.FRONT_PAGE;
181         }
182 
183         WikiPage page = null;
184 
185         try {
186             page = WikiPageServiceUtil.getPage(nodeId, title, version);
187         }
188         catch (NoSuchPageException nspe) {
189             if (title.equals(WikiPageConstants.FRONT_PAGE) && (version == 0)) {
190                 long userId = PortalUtil.getUserId(request);
191 
192                 if (userId == 0) {
193                     long companyId = PortalUtil.getCompanyId(request);
194 
195                     userId = UserLocalServiceUtil.getDefaultUserId(companyId);
196                 }
197 
198                 ServiceContext serviceContext = new ServiceContext();
199 
200                 Layout layout = themeDisplay.getLayout();
201 
202                 serviceContext.setAddCommunityPermissions(true);
203 
204                 if (layout.isPublicLayout()) {
205                     serviceContext.setAddGuestPermissions(true);
206                 }
207                 else {
208                     serviceContext.setAddGuestPermissions(false);
209                 }
210 
211                 page = WikiPageLocalServiceUtil.addPage(
212                     userId, nodeId, title, null, WikiPageConstants.NEW, true,
213                     serviceContext);
214             }
215             else {
216                 throw nspe;
217             }
218         }
219 
220         request.setAttribute(WebKeys.WIKI_PAGE, page);
221     }
222 
223 }