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