1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
13   */
14  
15  package com.liferay.portlet.wiki.action;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.PropsKeys;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.service.UserLocalServiceUtil;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.PropsUtil;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.wiki.NoSuchPageException;
30  import com.liferay.portlet.wiki.model.WikiNode;
31  import com.liferay.portlet.wiki.model.WikiPage;
32  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
33  import com.liferay.portlet.wiki.service.WikiNodeLocalServiceUtil;
34  import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
35  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
36  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
37  import com.liferay.portlet.wiki.util.WikiUtil;
38  
39  import java.util.List;
40  
41  import javax.portlet.ActionRequest;
42  import javax.portlet.RenderRequest;
43  
44  import javax.servlet.http.HttpServletRequest;
45  
46  /**
47   * <a href="ActionUtil.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Jorge Ferrer
51   */
52  public class ActionUtil {
53  
54      public static WikiNode getFirstVisibleNode(RenderRequest renderRequest)
55          throws PortalException, SystemException {
56  
57          ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
58              WebKeys.THEME_DISPLAY);
59  
60          WikiNode node = null;
61  
62          int nodesCount = WikiNodeLocalServiceUtil.getNodesCount(
63              themeDisplay.getScopeGroupId());
64  
65          if (nodesCount == 0) {
66              String nodeName = PropsUtil.get(PropsKeys.WIKI_INITIAL_NODE_NAME);
67  
68              node = WikiNodeLocalServiceUtil.addNode(
69                  themeDisplay.getUserId(), themeDisplay.getPlid(), nodeName,
70                  StringPool.BLANK, true, true);
71          }
72          else {
73              List<WikiNode> nodes = WikiUtil.getNodes(renderRequest);
74  
75              if (nodes.size() == 0) {
76                  throw new PrincipalException();
77              }
78  
79              node = nodes.get(0);
80          }
81  
82          renderRequest.setAttribute(WebKeys.WIKI_NODE, node);
83  
84          return node;
85      }
86  
87      public static WikiNode getNode(ActionRequest actionRequest)
88          throws Exception {
89  
90          HttpServletRequest request = PortalUtil.getHttpServletRequest(
91              actionRequest);
92  
93          return getNode(request);
94      }
95  
96      public static WikiNode getNode(RenderRequest renderRequest)
97          throws Exception {
98  
99          HttpServletRequest request = PortalUtil.getHttpServletRequest(
100             renderRequest);
101 
102         return getNode(request);
103     }
104 
105     public static WikiNode getNode(HttpServletRequest request)
106         throws Exception {
107 
108         ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
109             WebKeys.THEME_DISPLAY);
110 
111         long nodeId = ParamUtil.getLong(request, "nodeId");
112         String nodeName = ParamUtil.getString(request, "nodeName");
113 
114         WikiNode node = null;
115 
116         if (nodeId > 0) {
117             node = WikiNodeServiceUtil.getNode(nodeId);
118         }
119         else if (Validator.isNotNull(nodeName)) {
120             node = WikiNodeServiceUtil.getNode(
121                 themeDisplay.getScopeGroupId(), nodeName);
122         }
123 
124         request.setAttribute(WebKeys.WIKI_NODE, node);
125 
126         return node;
127     }
128 
129     public static void getPage(ActionRequest actionRequest) throws Exception {
130         HttpServletRequest request = PortalUtil.getHttpServletRequest(
131             actionRequest);
132 
133         getPage(request);
134     }
135 
136     public static void getPage(RenderRequest renderRequest) throws Exception {
137         HttpServletRequest request = PortalUtil.getHttpServletRequest(
138             renderRequest);
139 
140         getPage(request);
141     }
142 
143     public static void getPage(HttpServletRequest request) throws Exception {
144         long nodeId = ParamUtil.getLong(request, "nodeId");
145         String title = ParamUtil.getString(request, "title");
146         double version = ParamUtil.getDouble(request, "version");
147 
148         if (nodeId == 0) {
149             WikiNode node = (WikiNode)request.getAttribute(WebKeys.WIKI_NODE);
150 
151             if (node != null) {
152                 nodeId = node.getNodeId();
153             }
154         }
155 
156         if (Validator.isNull(title)) {
157             title = WikiPageImpl.FRONT_PAGE;
158         }
159 
160         WikiPage page = null;
161 
162         try {
163             page = WikiPageServiceUtil.getPage(nodeId, title, version);
164         }
165         catch (NoSuchPageException nspe) {
166             if (title.equals(WikiPageImpl.FRONT_PAGE) && (version == 0)) {
167                 long userId = PortalUtil.getUserId(request);
168 
169                 if (userId == 0) {
170                     long companyId = PortalUtil.getCompanyId(request);
171 
172                     userId = UserLocalServiceUtil.getDefaultUserId(companyId);
173                 }
174 
175                 page = WikiPageLocalServiceUtil.addPage(
176                     userId, nodeId, title, null, WikiPageImpl.NEW, true, null,
177                     null);
178             }
179             else {
180                 throw nspe;
181             }
182         }
183 
184         request.setAttribute(WebKeys.WIKI_PAGE, page);
185     }
186 
187 }