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