1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.PropsKeys;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Layout;
32  import com.liferay.portal.security.auth.PrincipalException;
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.PropsUtil;
39  import com.liferay.portal.util.WebKeys;
40  import com.liferay.portlet.wiki.NoSuchPageException;
41  import com.liferay.portlet.wiki.model.WikiNode;
42  import com.liferay.portlet.wiki.model.WikiPage;
43  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
44  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
45  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
46  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
47  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
48  import com.liferay.portlet.wiki.util.WikiUtil;
49  
50  import java.util.List;
51  
52  import javax.portlet.ActionRequest;
53  import javax.portlet.RenderRequest;
54  
55  import javax.servlet.http.HttpServletRequest;
56  
57  /**
58   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Brian Wing Shun Chan
61   * @author Jorge Ferrer
62   */
63  public class ActionUtil {
64  
65      public static WikiNode getFirstVisibleNode(RenderRequest renderRequest)
66          throws PortalException, SystemException {
67  
68          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
69              WebKeys.THEME_DISPLAY);
70  
71          WikiNode node = null;
72  
73          int nodesCount = WikiNodeLocalServiceUtil.getNodesCount(
74              themeDisplay.getScopeGroupId());
75  
76          if (nodesCount == 0) {
77              String nodeName = PropsUtil.get(PropsKeys.WIKI_INITIAL_NODE_NAME);
78  
79              Layout layout = themeDisplay.getLayout();
80  
81              ServiceContext serviceContext = ServiceContextFactory.getInstance(
82                  WikiNode.class.getName(), renderRequest);
83  
84              serviceContext.setAddCommunityPermissions(true);
85  
86              if (layout.isPublicLayout()) {
87                  serviceContext.setAddGuestPermissions(true);
88              }
89              else {
90                  serviceContext.setAddGuestPermissions(false);
91              }
92  
93              node = WikiNodeLocalServiceUtil.addNode(
94                  themeDisplay.getUserId(), nodeName, StringPool.BLANK,
95                  serviceContext);
96          }
97          else {
98              List<WikiNode> nodes = WikiUtil.getNodes(renderRequest);
99  
100             if (nodes.size() == 0) {
101                 throw new PrincipalException();
102             }
103 
104             node = nodes.get(0);
105         }
106 
107         renderRequest.setAttribute(WebKeys.WIKI_NODE, node);
108 
109         return node;
110     }
111 
112     public static WikiNode getNode(ActionRequest actionRequest)
113         throws Exception {
114 
115         HttpServletRequest request = PortalUtil.getHttpServletRequest(
116             actionRequest);
117 
118         return getNode(request);
119     }
120 
121     public static WikiNode getNode(RenderRequest renderRequest)
122         throws Exception {
123 
124         HttpServletRequest request = PortalUtil.getHttpServletRequest(
125             renderRequest);
126 
127         return getNode(request);
128     }
129 
130     public static WikiNode getNode(HttpServletRequest request)
131         throws Exception {
132 
133         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
134             WebKeys.THEME_DISPLAY);
135 
136         long nodeId = ParamUtil.getLong(request, "nodeId");
137         String nodeName = ParamUtil.getString(request, "nodeName");
138 
139         WikiNode node = null;
140 
141         if (nodeId > 0) {
142             node = WikiNodeServiceUtil.getNode(nodeId);
143         }
144         else if (Validator.isNotNull(nodeName)) {
145             node = WikiNodeServiceUtil.getNode(
146                 themeDisplay.getScopeGroupId(), nodeName);
147         }
148 
149         request.setAttribute(WebKeys.WIKI_NODE, node);
150 
151         return node;
152     }
153 
154     public static void getPage(ActionRequest actionRequest) throws Exception {
155         HttpServletRequest request = PortalUtil.getHttpServletRequest(
156             actionRequest);
157 
158         getPage(request);
159     }
160 
161     public static void getPage(RenderRequest renderRequest) throws Exception {
162         HttpServletRequest request = PortalUtil.getHttpServletRequest(
163             renderRequest);
164 
165         getPage(request);
166     }
167 
168     public static void getPage(HttpServletRequest request) throws Exception {
169         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
170             WebKeys.THEME_DISPLAY);
171 
172         long nodeId = ParamUtil.getLong(request, "nodeId");
173         String title = ParamUtil.getString(request, "title");
174         double version = ParamUtil.getDouble(request, "version");
175 
176         if (nodeId == 0) {
177             WikiNode node = (WikiNode)request.getAttribute(WebKeys.WIKI_NODE);
178 
179             if (node != null) {
180                 nodeId = node.getNodeId();
181             }
182         }
183 
184         if (Validator.isNull(title)) {
185             title = WikiPageImpl.FRONT_PAGE;
186         }
187 
188         WikiPage page = null;
189 
190         try {
191             page = WikiPageServiceUtil.getPage(nodeId, title, version);
192         }
193         catch (NoSuchPageException nspe) {
194             if (title.equals(WikiPageImpl.FRONT_PAGE) && (version == 0)) {
195                 long userId = PortalUtil.getUserId(request);
196 
197                 if (userId == 0) {
198                     long companyId = PortalUtil.getCompanyId(request);
199 
200                     userId = UserLocalServiceUtil.getDefaultUserId(companyId);
201                 }
202 
203                 ServiceContext serviceContext = new ServiceContext();
204 
205                 Layout layout = themeDisplay.getLayout();
206 
207                 serviceContext.setAddCommunityPermissions(true);
208 
209                 if (layout.isPublicLayout()) {
210                     serviceContext.setAddGuestPermissions(true);
211                 }
212                 else {
213                     serviceContext.setAddGuestPermissions(false);
214                 }
215 
216                 page = WikiPageLocalServiceUtil.addPage(
217                     userId, nodeId, title, null, WikiPageImpl.NEW, true,
218                     serviceContext);
219             }
220             else {
221                 throw nspe;
222             }
223         }
224 
225         request.setAttribute(WebKeys.WIKI_PAGE, page);
226     }
227 
228 }