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