1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.wiki.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.kernel.workflow.WorkflowConstants;
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.struts.PortletAction;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.portlet.ActionRequestImpl;
31  import com.liferay.portlet.PortletURLImpl;
32  import com.liferay.portlet.asset.AssetTagException;
33  import com.liferay.portlet.wiki.DuplicatePageException;
34  import com.liferay.portlet.wiki.NoSuchNodeException;
35  import com.liferay.portlet.wiki.NoSuchPageException;
36  import com.liferay.portlet.wiki.PageContentException;
37  import com.liferay.portlet.wiki.PageTitleException;
38  import com.liferay.portlet.wiki.PageVersionException;
39  import com.liferay.portlet.wiki.model.WikiNode;
40  import com.liferay.portlet.wiki.model.WikiPage;
41  import com.liferay.portlet.wiki.model.WikiPageConstants;
42  import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
43  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
44  
45  import javax.portlet.ActionRequest;
46  import javax.portlet.ActionResponse;
47  import javax.portlet.PortletConfig;
48  import javax.portlet.PortletRequest;
49  import javax.portlet.RenderRequest;
50  import javax.portlet.RenderResponse;
51  
52  import org.apache.struts.action.ActionForm;
53  import org.apache.struts.action.ActionForward;
54  import org.apache.struts.action.ActionMapping;
55  
56  /**
57   * <a href="EditPageAction.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   * @author Jorge Ferrer
61   */
62  public class EditPageAction extends PortletAction {
63  
64      public void processAction(
65              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
66              ActionRequest actionRequest, ActionResponse actionResponse)
67          throws Exception {
68  
69          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
70  
71          WikiPage page = null;
72  
73          try {
74              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
75                  page = updatePage(actionRequest);
76              }
77              else if (cmd.equals(Constants.DELETE)) {
78                  deletePage(actionRequest);
79              }
80              else if (cmd.equals(Constants.REVERT)) {
81                  revertPage(actionRequest);
82              }
83              else if (cmd.equals(Constants.SUBSCRIBE)) {
84                  subscribePage(actionRequest);
85              }
86              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
87                  unsubscribePage(actionRequest);
88              }
89  
90              if (Validator.isNotNull(cmd)) {
91                  String redirect = ParamUtil.getString(
92                      actionRequest, "redirect");
93  
94                  int workflowAction = ParamUtil.getInteger(
95                      actionRequest, "workflowAction",
96                      WorkflowConstants.ACTION_PUBLISH);
97  
98                  if (page != null) {
99                      if (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT) {
100                         redirect = getSaveAndContinueRedirect(
101                             portletConfig, actionRequest, page, redirect);
102                     }
103                     else if (redirect.endsWith("title=")) {
104                         redirect += page.getTitle();
105                     }
106                 }
107 
108                 sendRedirect(actionRequest, actionResponse, redirect);
109             }
110         }
111         catch (Exception e) {
112             if (e instanceof NoSuchNodeException ||
113                 e instanceof NoSuchPageException ||
114                 e instanceof PrincipalException) {
115 
116                 SessionErrors.add(actionRequest, e.getClass().getName());
117 
118                 setForward(actionRequest, "portlet.wiki.error");
119             }
120             else if (e instanceof DuplicatePageException ||
121                      e instanceof PageContentException ||
122                      e instanceof PageVersionException ||
123                      e instanceof PageTitleException) {
124 
125                 SessionErrors.add(actionRequest, e.getClass().getName());
126             }
127             else if (e instanceof AssetTagException) {
128                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
129             }
130             else {
131                 throw e;
132             }
133         }
134     }
135 
136     public ActionForward render(
137             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
138             RenderRequest renderRequest, RenderResponse renderResponse)
139         throws Exception {
140 
141         try {
142             ActionUtil.getNode(renderRequest);
143 
144             if (!SessionErrors.contains(
145                     renderRequest, DuplicatePageException.class.getName())) {
146 
147                 getPage(renderRequest);
148             }
149         }
150         catch (Exception e) {
151             if (e instanceof NoSuchNodeException ||
152                 e instanceof PageTitleException ||
153                 e instanceof PrincipalException) {
154 
155                 SessionErrors.add(renderRequest, e.getClass().getName());
156 
157                 return mapping.findForward("portlet.wiki.error");
158             }
159             else if (e instanceof NoSuchPageException) {
160 
161                 // Let edit_page.jsp handle this case
162 
163             }
164             else {
165                 throw e;
166             }
167         }
168 
169         return mapping.findForward(
170             getForward(renderRequest, "portlet.wiki.edit_page"));
171     }
172 
173     protected void deletePage(ActionRequest actionRequest) throws Exception {
174         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
175         String title = ParamUtil.getString(actionRequest, "title");
176 
177         WikiPageServiceUtil.deletePage(nodeId, title);
178     }
179 
180     protected void getPage(RenderRequest renderRequest) throws Exception {
181         long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
182         String title = ParamUtil.getString(renderRequest, "title");
183         double version = ParamUtil.getDouble(renderRequest, "version");
184         boolean removeRedirect = ParamUtil.getBoolean(
185             renderRequest, "removeRedirect");
186 
187         if (nodeId == 0) {
188             WikiNode node = (WikiNode)renderRequest.getAttribute(
189                 WebKeys.WIKI_NODE);
190 
191             if (node != null) {
192                 nodeId = node.getNodeId();
193             }
194         }
195 
196         WikiPage page = null;
197 
198         if (Validator.isNotNull(title)) {
199             try {
200                 if ((version == 0) &&
201                     WikiPageLocalServiceUtil.hasDraftPage(nodeId, title)) {
202 
203                     page = WikiPageServiceUtil.getDraftPage(nodeId, title);
204                 }
205                 else {
206                     page = WikiPageServiceUtil.getPage(nodeId, title, version);
207                 }
208             }
209             catch (NoSuchPageException nspe1) {
210                 try {
211                     page = WikiPageServiceUtil.getPage(
212                         nodeId, title, false);
213                 }
214                 catch (NoSuchPageException nspe2) {
215                     if ((title.equals(WikiPageConstants.FRONT_PAGE)) &&
216                         (version == 0)) {
217 
218                         ServiceContext serviceContext = new ServiceContext();
219 
220                         page = WikiPageServiceUtil.addPage(
221                             nodeId, title, null, WikiPageConstants.NEW, true,
222                             serviceContext);
223                     }
224                     else {
225                         throw nspe2;
226                     }
227                 }
228             }
229 
230             if (removeRedirect) {
231                 page.setContent(StringPool.BLANK);
232                 page.setRedirectTitle(StringPool.BLANK);
233             }
234         }
235 
236         renderRequest.setAttribute(WebKeys.WIKI_PAGE, page);
237     }
238 
239     protected String getSaveAndContinueRedirect(
240             PortletConfig portletConfig, ActionRequest actionRequest,
241             WikiPage page, String redirect)
242         throws Exception {
243 
244         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
245             WebKeys.THEME_DISPLAY);
246 
247         Layout layout = themeDisplay.getLayout();
248 
249         String originalRedirect = ParamUtil.getString(
250             actionRequest, "originalRedirect");
251 
252         PortletURLImpl portletURL = new PortletURLImpl(
253             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
254             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
255 
256         portletURL.setParameter("struts_action", "/wiki/edit_page");
257         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
258         portletURL.setParameter("redirect", redirect, false);
259         portletURL.setParameter("originalRedirect", originalRedirect, false);
260         portletURL.setParameter(
261             "groupId", String.valueOf(layout.getGroupId()), false);
262         portletURL.setParameter(
263             "nodeId", String.valueOf(page.getNodeId()), false);
264         portletURL.setParameter("title", page.getTitle(), false);
265 
266         return portletURL.toString();
267     }
268 
269     protected void revertPage(ActionRequest actionRequest) throws Exception {
270         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
271         String title = ParamUtil.getString(actionRequest, "title");
272         double version = ParamUtil.getDouble(actionRequest, "version");
273 
274         ServiceContext serviceContext = ServiceContextFactory.getInstance(
275             WikiPage.class.getName(), actionRequest);
276 
277         WikiPageServiceUtil.revertPage(nodeId, title, version, serviceContext);
278     }
279 
280     protected void subscribePage(ActionRequest actionRequest) throws Exception {
281         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
282         String title = ParamUtil.getString(actionRequest, "title");
283 
284         WikiPageServiceUtil.subscribePage(nodeId, title);
285     }
286 
287     protected void unsubscribePage(ActionRequest actionRequest)
288         throws Exception {
289 
290         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
291         String title = ParamUtil.getString(actionRequest, "title");
292 
293         WikiPageServiceUtil.unsubscribePage(nodeId, title);
294     }
295 
296     protected WikiPage updatePage(ActionRequest actionRequest)
297         throws Exception {
298 
299         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
300 
301         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
302         String title = ParamUtil.getString(actionRequest, "title");
303         double version = ParamUtil.getDouble(actionRequest, "version");
304 
305         String content = ParamUtil.getString(actionRequest, "content");
306         String summary = ParamUtil.getString(actionRequest, "summary");
307         boolean minorEdit = ParamUtil.getBoolean(actionRequest, "minorEdit");
308         String format = ParamUtil.getString(actionRequest, "format");
309         String parentTitle = ParamUtil.getString(actionRequest, "parentTitle");
310         String redirectTitle = null;
311 
312         ServiceContext serviceContext = ServiceContextFactory.getInstance(
313             WikiPage.class.getName(), actionRequest);
314 
315         WikiPage page = null;
316 
317         if (cmd.equals(Constants.ADD)) {
318             page = WikiPageServiceUtil.addPage(
319                 nodeId, title, content, summary, minorEdit, format, parentTitle,
320                 redirectTitle, serviceContext);
321         }
322         else {
323             page = WikiPageServiceUtil.updatePage(
324                 nodeId, title, version, content, summary, minorEdit, format,
325                 parentTitle, redirectTitle, serviceContext);
326         }
327 
328         return page;
329     }
330 
331     protected boolean isCheckMethodOnProcessAction() {
332         return _CHECK_METHOD_ON_PROCESS_ACTION;
333     }
334 
335     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
336 
337 }