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.UserLocalServiceUtil;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.PropsKeys;
35  import com.liferay.portal.util.PropsUtil;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.wiki.NoSuchPageException;
38  import com.liferay.portlet.wiki.model.WikiNode;
39  import com.liferay.portlet.wiki.model.WikiPage;
40  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
41  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
42  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
43  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
44  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
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          int nodesCount = WikiNodeLocalServiceUtil.getNodesCount(
72              themeDisplay.getScopeGroupId());
73  
74          if (nodesCount == 0) {
75              String nodeName = PropsUtil.get(PropsKeys.WIKI_INITIAL_NODE_NAME);
76  
77              node = WikiNodeLocalServiceUtil.addNode(
78                  themeDisplay.getUserId(), themeDisplay.getPlid(), nodeName,
79                  StringPool.BLANK, true, true);
80          }
81          else {
82              List<WikiNode> nodes = WikiUtil.getNodes(renderRequest);
83  
84              if (nodes.size() == 0) {
85                  throw new PrincipalException();
86              }
87  
88              node = nodes.get(0);
89          }
90  
91          renderRequest.setAttribute(WebKeys.WIKI_NODE, node);
92  
93          return node;
94      }
95  
96      public static WikiNode getNode(ActionRequest actionRequest)
97          throws Exception {
98  
99          HttpServletRequest request = PortalUtil.getHttpServletRequest(
100             actionRequest);
101 
102         return getNode(request);
103     }
104 
105     public static WikiNode getNode(RenderRequest renderRequest)
106         throws Exception {
107 
108         HttpServletRequest request = PortalUtil.getHttpServletRequest(
109             renderRequest);
110 
111         return getNode(request);
112     }
113 
114     public static WikiNode getNode(HttpServletRequest request)
115         throws Exception {
116 
117         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
118             WebKeys.THEME_DISPLAY);
119 
120         long nodeId = ParamUtil.getLong(request, "nodeId");
121         String nodeName = ParamUtil.getString(request, "nodeName");
122 
123         WikiNode node = null;
124 
125         if (nodeId > 0) {
126             node = WikiNodeServiceUtil.getNode(nodeId);
127         }
128         else if (Validator.isNotNull(nodeName)) {
129             node = WikiNodeServiceUtil.getNode(
130                 themeDisplay.getScopeGroupId(), nodeName);
131         }
132 
133         request.setAttribute(WebKeys.WIKI_NODE, node);
134 
135         return node;
136     }
137 
138     public static void getPage(ActionRequest actionRequest) throws Exception {
139         HttpServletRequest request = PortalUtil.getHttpServletRequest(
140             actionRequest);
141 
142         getPage(request);
143     }
144 
145     public static void getPage(RenderRequest renderRequest) throws Exception {
146         HttpServletRequest request = PortalUtil.getHttpServletRequest(
147             renderRequest);
148 
149         getPage(request);
150     }
151 
152     public static void getPage(HttpServletRequest request) throws Exception {
153         long nodeId = ParamUtil.getLong(request, "nodeId");
154         String title = ParamUtil.getString(request, "title");
155         double version = ParamUtil.getDouble(request, "version");
156 
157         if (nodeId == 0) {
158             WikiNode node = (WikiNode)request.getAttribute(WebKeys.WIKI_NODE);
159 
160             if (node != null) {
161                 nodeId = node.getNodeId();
162             }
163         }
164 
165         if (Validator.isNull(title)) {
166             title = WikiPageImpl.FRONT_PAGE;
167         }
168 
169         WikiPage page = null;
170 
171         try {
172             page = WikiPageServiceUtil.getPage(nodeId, title, version);
173         }
174         catch (NoSuchPageException nspe) {
175             if (title.equals(WikiPageImpl.FRONT_PAGE) && (version == 0)) {
176                 long userId = PortalUtil.getUserId(request);
177 
178                 if (userId == 0) {
179                     long companyId = PortalUtil.getCompanyId(request);
180 
181                     userId = UserLocalServiceUtil.getDefaultUserId(companyId);
182                 }
183 
184                 page = WikiPageLocalServiceUtil.addPage(
185                     userId, nodeId, title, null, WikiPageImpl.NEW, true, null,
186                     null);
187             }
188             else {
189                 throw nspe;
190             }
191         }
192 
193         request.setAttribute(WebKeys.WIKI_PAGE, page);
194     }
195 
196 }