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