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.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.model.Layout;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.service.ServiceContext;
25  import com.liferay.portal.service.ServiceContextFactory;
26  import com.liferay.portal.struts.PortletAction;
27  import com.liferay.portal.theme.ThemeDisplay;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.ActionRequestImpl;
30  import com.liferay.portlet.PortletURLImpl;
31  import com.liferay.portlet.tags.TagsEntryException;
32  import com.liferay.portlet.wiki.DuplicatePageException;
33  import com.liferay.portlet.wiki.NoSuchNodeException;
34  import com.liferay.portlet.wiki.NoSuchPageException;
35  import com.liferay.portlet.wiki.PageContentException;
36  import com.liferay.portlet.wiki.PageTitleException;
37  import com.liferay.portlet.wiki.PageVersionException;
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.WikiPageServiceUtil;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  import javax.portlet.PortletRequest;
47  import javax.portlet.RenderRequest;
48  import javax.portlet.RenderResponse;
49  import javax.portlet.WindowState;
50  
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="EditPageAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   * @author Jorge Ferrer
60   */
61  public class EditPageAction extends PortletAction {
62  
63      public void processAction(
64              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
65              ActionRequest actionRequest, ActionResponse actionResponse)
66          throws Exception {
67  
68          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
69  
70          WikiPage page = null;
71  
72          try {
73              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
74                  page = updatePage(actionRequest);
75              }
76              else if (cmd.equals(Constants.DELETE)) {
77                  deletePage(actionRequest);
78              }
79              else if (cmd.equals(Constants.REVERT)) {
80                  revertPage(actionRequest);
81              }
82              else if (cmd.equals(Constants.SUBSCRIBE)) {
83                  subscribePage(actionRequest);
84              }
85              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
86                  unsubscribePage(actionRequest);
87              }
88  
89              if (Validator.isNotNull(cmd)) {
90                  String redirect = ParamUtil.getString(
91                      actionRequest, "redirect");
92  
93                  if (page != null) {
94                      boolean saveAndContinue = ParamUtil.getBoolean(
95                          actionRequest, "saveAndContinue");
96  
97                      if (saveAndContinue) {
98                          redirect = getSaveAndContinueRedirect(
99                              portletConfig, actionRequest, page, redirect);
100                     }
101                     else if (redirect.endsWith("title=")) {
102                         redirect += page.getTitle();
103                     }
104                 }
105 
106                 sendRedirect(actionRequest, actionResponse, redirect);
107             }
108         }
109         catch (Exception e) {
110             if (e instanceof NoSuchNodeException ||
111                 e instanceof NoSuchPageException ||
112                 e instanceof PrincipalException) {
113 
114                 SessionErrors.add(actionRequest, e.getClass().getName());
115 
116                 setForward(actionRequest, "portlet.wiki.error");
117             }
118             else if (e instanceof DuplicatePageException ||
119                      e instanceof PageContentException ||
120                      e instanceof PageVersionException ||
121                      e instanceof PageTitleException) {
122 
123                 SessionErrors.add(actionRequest, e.getClass().getName());
124             }
125             else if (e instanceof TagsEntryException) {
126                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
127             }
128             else {
129                 throw e;
130             }
131         }
132     }
133 
134     public ActionForward render(
135             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
136             RenderRequest renderRequest, RenderResponse renderResponse)
137         throws Exception {
138 
139         try {
140             ActionUtil.getNode(renderRequest);
141 
142             if (!SessionErrors.contains(
143                     renderRequest, DuplicatePageException.class.getName())) {
144 
145                 getPage(renderRequest);
146             }
147         }
148         catch (Exception e) {
149             if (e instanceof NoSuchNodeException ||
150                 e instanceof PageTitleException ||
151                 e instanceof PrincipalException) {
152 
153                 SessionErrors.add(renderRequest, e.getClass().getName());
154 
155                 return mapping.findForward("portlet.wiki.error");
156             }
157             else if (e instanceof NoSuchPageException) {
158 
159                 // Let edit_page.jsp handle this case
160 
161             }
162             else {
163                 throw e;
164             }
165         }
166 
167         return mapping.findForward(
168             getForward(renderRequest, "portlet.wiki.edit_page"));
169     }
170 
171     protected void deletePage(ActionRequest actionRequest) throws Exception {
172         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
173         String title = ParamUtil.getString(actionRequest, "title");
174 
175         WikiPageServiceUtil.deletePage(nodeId, title);
176     }
177 
178     protected void getPage(RenderRequest renderRequest) throws Exception {
179         long nodeId = ParamUtil.getLong(renderRequest, "nodeId");
180         String title = ParamUtil.getString(renderRequest, "title");
181         double version = ParamUtil.getDouble(renderRequest, "version");
182         boolean removeRedirect = ParamUtil.getBoolean(
183             renderRequest, "removeRedirect");
184 
185         if (nodeId == 0) {
186             WikiNode node = (WikiNode)renderRequest.getAttribute(
187                 WebKeys.WIKI_NODE);
188 
189             if (node != null) {
190                 nodeId = node.getNodeId();
191             }
192         }
193 
194         WikiPage page = null;
195 
196         if (Validator.isNotNull(title)) {
197             try {
198                 page = WikiPageServiceUtil.getPage(nodeId, title, version);
199             }
200             catch (NoSuchPageException nspe) {
201                 if (title.equals(WikiPageImpl.FRONT_PAGE) && (version == 0)) {
202                     ServiceContext serviceContext = new ServiceContext();
203 
204                     page = WikiPageServiceUtil.addPage(
205                         nodeId, title, null, WikiPageImpl.NEW, true,
206                         serviceContext);
207                 }
208                 else {
209                     throw nspe;
210                 }
211             }
212 
213             if (removeRedirect) {
214                 page.setContent(StringPool.BLANK);
215                 page.setRedirectTitle(StringPool.BLANK);
216             }
217         }
218 
219         renderRequest.setAttribute(WebKeys.WIKI_PAGE, page);
220     }
221 
222     protected String getSaveAndContinueRedirect(
223             PortletConfig portletConfig, ActionRequest actionRequest,
224             WikiPage page, String redirect)
225         throws Exception {
226 
227         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
228             WebKeys.THEME_DISPLAY);
229 
230         Layout layout = themeDisplay.getLayout();
231 
232         String originalRedirect = ParamUtil.getString(
233             actionRequest, "originalRedirect");
234 
235         PortletURLImpl portletURL = new PortletURLImpl(
236             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
237             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
238 
239         portletURL.setWindowState(WindowState.MAXIMIZED);
240 
241         portletURL.setParameter("struts_action", "/wiki/edit_page");
242         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
243         portletURL.setParameter("redirect", redirect, false);
244         portletURL.setParameter("originalRedirect", originalRedirect, false);
245         portletURL.setParameter(
246             "groupId", String.valueOf(layout.getGroupId()), false);
247         portletURL.setParameter(
248             "nodeId", String.valueOf(page.getNodeId()), false);
249         portletURL.setParameter("title", page.getTitle(), false);
250 
251         return portletURL.toString();
252     }
253 
254     protected void revertPage(ActionRequest actionRequest) throws Exception {
255         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
256         String title = ParamUtil.getString(actionRequest, "title");
257         double version = ParamUtil.getDouble(actionRequest, "version");
258 
259         ServiceContext serviceContext = ServiceContextFactory.getInstance(
260             WikiPage.class.getName(), actionRequest);
261 
262         WikiPageServiceUtil.revertPage(nodeId, title, version, serviceContext);
263     }
264 
265     protected void subscribePage(ActionRequest actionRequest) throws Exception {
266         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
267         String title = ParamUtil.getString(actionRequest, "title");
268 
269         WikiPageServiceUtil.subscribePage(nodeId, title);
270     }
271 
272     protected void unsubscribePage(ActionRequest actionRequest)
273         throws Exception {
274 
275         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
276         String title = ParamUtil.getString(actionRequest, "title");
277 
278         WikiPageServiceUtil.unsubscribePage(nodeId, title);
279     }
280 
281     protected WikiPage updatePage(ActionRequest actionRequest)
282         throws Exception {
283 
284         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
285 
286         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
287         String title = ParamUtil.getString(actionRequest, "title");
288         double version = ParamUtil.getDouble(actionRequest, "version");
289 
290         String content = ParamUtil.getString(actionRequest, "content");
291         String summary = ParamUtil.getString(actionRequest, "summary");
292         boolean minorEdit = ParamUtil.getBoolean(actionRequest, "minorEdit");
293         String format = ParamUtil.getString(actionRequest, "format");
294         String parentTitle = ParamUtil.getString(actionRequest, "parentTitle");
295         String redirectTitle = null;
296 
297         ServiceContext serviceContext = ServiceContextFactory.getInstance(
298             WikiPage.class.getName(), actionRequest);
299 
300         WikiPage page = null;
301 
302         if (cmd.equals(Constants.ADD)) {
303             page = WikiPageServiceUtil.addPage(
304                 nodeId, title, content, summary, minorEdit, format, parentTitle,
305                 redirectTitle, serviceContext);
306         }
307         else {
308             page = WikiPageServiceUtil.updatePage(
309                 nodeId, title, version, content, summary, minorEdit, format,
310                 parentTitle, redirectTitle, serviceContext);
311         }
312 
313         return page;
314     }
315 
316     protected boolean isCheckMethodOnProcessAction() {
317         return _CHECK_METHOD_ON_PROCESS_ACTION;
318     }
319 
320     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
321 
322 }