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