1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.Constants;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Layout;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.struts.PortletAction;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.ActionRequestImpl;
37  import com.liferay.portlet.PortletURLImpl;
38  import com.liferay.portlet.tags.TagsEntryException;
39  import com.liferay.portlet.wiki.NoSuchNodeException;
40  import com.liferay.portlet.wiki.NoSuchPageException;
41  import com.liferay.portlet.wiki.PageContentException;
42  import com.liferay.portlet.wiki.PageTitleException;
43  import com.liferay.portlet.wiki.PageVersionException;
44  import com.liferay.portlet.wiki.model.WikiNode;
45  import com.liferay.portlet.wiki.model.WikiPage;
46  import com.liferay.portlet.wiki.model.impl.WikiPageImpl;
47  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
48  
49  import javax.portlet.ActionRequest;
50  import javax.portlet.ActionResponse;
51  import javax.portlet.PortletConfig;
52  import javax.portlet.PortletPreferences;
53  import javax.portlet.PortletRequest;
54  import javax.portlet.RenderRequest;
55  import javax.portlet.RenderResponse;
56  import javax.portlet.WindowState;
57  
58  import org.apache.struts.action.ActionForm;
59  import org.apache.struts.action.ActionForward;
60  import org.apache.struts.action.ActionMapping;
61  
62  /**
63   * <a href="EditPageAction.java.html"><b><i>View Source</i></b></a>
64   *
65   * @author Brian Wing Shun Chan
66   * @author Jorge Ferrer
67   *
68   */
69  public class EditPageAction extends PortletAction {
70  
71      public void processAction(
72              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
73              ActionRequest actionRequest, ActionResponse actionResponse)
74          throws Exception {
75  
76          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
77  
78          WikiPage page = null;
79  
80          try {
81              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
82                  page = updatePage(actionRequest);
83              }
84              else if (cmd.equals(Constants.DELETE)) {
85                  deletePage(actionRequest);
86              }
87              else if (cmd.equals(Constants.REVERT)) {
88                  revertPage(actionRequest);
89              }
90              else if (cmd.equals(Constants.SUBSCRIBE)) {
91                  subscribePage(actionRequest);
92              }
93              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
94                  unsubscribePage(actionRequest);
95              }
96  
97              if (Validator.isNotNull(cmd)) {
98                  String redirect = ParamUtil.getString(
99                      actionRequest, "redirect");
100 
101                 if (page != null) {
102                     boolean saveAndContinue = ParamUtil.getBoolean(
103                         actionRequest, "saveAndContinue");
104 
105                     if (saveAndContinue) {
106                         redirect = getSaveAndContinueRedirect(
107                             portletConfig, actionRequest, page, redirect);
108                     }
109                     else if (redirect.endsWith("title=")) {
110                         redirect += page.getTitle();
111                     }
112                 }
113 
114                 sendRedirect(actionRequest, actionResponse, redirect);
115             }
116         }
117         catch (Exception e) {
118             if (e instanceof NoSuchNodeException ||
119                 e instanceof NoSuchPageException ||
120                 e instanceof PrincipalException) {
121 
122                 SessionErrors.add(actionRequest, e.getClass().getName());
123 
124                 setForward(actionRequest, "portlet.wiki.error");
125             }
126             else if (e instanceof PageContentException ||
127                      e instanceof PageVersionException ||
128                      e instanceof PageTitleException) {
129 
130                 SessionErrors.add(actionRequest, e.getClass().getName());
131             }
132             else if (e instanceof TagsEntryException) {
133                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
134             }
135             else {
136                 throw e;
137             }
138         }
139     }
140 
141     public ActionForward render(
142             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
143             RenderRequest renderRequest, RenderResponse renderResponse)
144         throws Exception {
145 
146         try {
147             ActionUtil.getNode(renderRequest);
148             getPage(renderRequest);
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                 page = WikiPageServiceUtil.getPage(nodeId, title, version);
201             }
202             catch (NoSuchPageException nspe) {
203                 if (title.equals(WikiPageImpl.FRONT_PAGE) && (version == 0)) {
204                     page = WikiPageServiceUtil.addPage(
205                         nodeId, title, null, WikiPageImpl.NEW, true, null,
206                         null);
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         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
256             WebKeys.THEME_DISPLAY);
257 
258         PortletPreferences prefs = actionRequest.getPreferences();
259 
260         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
261         String title = ParamUtil.getString(actionRequest, "title");
262         double version = ParamUtil.getDouble(actionRequest, "version");
263 
264         WikiPageServiceUtil.revertPage(
265             nodeId, title, version, prefs, themeDisplay);
266     }
267 
268     protected void subscribePage(ActionRequest actionRequest) throws Exception {
269         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
270         String title = ParamUtil.getString(actionRequest, "title");
271 
272         WikiPageServiceUtil.subscribePage(nodeId, title);
273     }
274 
275     protected void unsubscribePage(ActionRequest actionRequest)
276         throws Exception {
277 
278         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
279         String title = ParamUtil.getString(actionRequest, "title");
280 
281         WikiPageServiceUtil.unsubscribePage(nodeId, title);
282     }
283 
284     protected WikiPage updatePage(ActionRequest actionRequest)
285         throws Exception {
286 
287         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
288             WebKeys.THEME_DISPLAY);
289 
290         PortletPreferences prefs = actionRequest.getPreferences();
291 
292         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
293         String title = ParamUtil.getString(actionRequest, "title");
294         double version = ParamUtil.getDouble(actionRequest, "version");
295 
296         String content = ParamUtil.getString(actionRequest, "content");
297         String summary = ParamUtil.getString(actionRequest, "summary");
298         boolean minorEdit = ParamUtil.getBoolean(actionRequest, "minorEdit");
299         String format = ParamUtil.getString(actionRequest, "format");
300         String parentTitle = ParamUtil.getString(actionRequest, "parentTitle");
301         String redirectTitle = null;
302 
303         String[] tagsEntries = StringUtil.split(
304             ParamUtil.getString(actionRequest, "tagsEntries"));
305 
306         return WikiPageServiceUtil.updatePage(
307             nodeId, title, version, content, summary, minorEdit, format,
308             parentTitle, redirectTitle, tagsEntries, prefs, themeDisplay);
309     }
310 
311     protected boolean isCheckMethodOnProcessAction() {
312         return _CHECK_METHOD_ON_PROCESS_ACTION;
313     }
314 
315     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
316 
317 }