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.Validator;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.service.ServiceContext;
33  import com.liferay.portal.service.ServiceContextFactory;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.ActionRequestImpl;
38  import com.liferay.portlet.PortletURLImpl;
39  import com.liferay.portlet.tags.TagsEntryException;
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.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                     ServiceContext serviceContext = new ServiceContext();
205 
206                     page = WikiPageServiceUtil.addPage(
207                         nodeId, title, null, WikiPageImpl.NEW, true,
208                         serviceContext);
209                 }
210                 else {
211                     throw nspe;
212                 }
213             }
214 
215             if (removeRedirect) {
216                 page.setContent(StringPool.BLANK);
217                 page.setRedirectTitle(StringPool.BLANK);
218             }
219         }
220 
221         renderRequest.setAttribute(WebKeys.WIKI_PAGE, page);
222     }
223 
224     protected String getSaveAndContinueRedirect(
225             PortletConfig portletConfig, ActionRequest actionRequest,
226             WikiPage page, String redirect)
227         throws Exception {
228 
229         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
230             WebKeys.THEME_DISPLAY);
231 
232         Layout layout = themeDisplay.getLayout();
233 
234         String originalRedirect = ParamUtil.getString(
235             actionRequest, "originalRedirect");
236 
237         PortletURLImpl portletURL = new PortletURLImpl(
238             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
239             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
240 
241         portletURL.setWindowState(WindowState.MAXIMIZED);
242 
243         portletURL.setParameter("struts_action", "/wiki/edit_page");
244         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
245         portletURL.setParameter("redirect", redirect, false);
246         portletURL.setParameter("originalRedirect", originalRedirect, false);
247         portletURL.setParameter(
248             "groupId", String.valueOf(layout.getGroupId()), false);
249         portletURL.setParameter(
250             "nodeId", String.valueOf(page.getNodeId()), false);
251         portletURL.setParameter("title", page.getTitle(), false);
252 
253         return portletURL.toString();
254     }
255 
256     protected void revertPage(ActionRequest actionRequest) throws Exception {
257         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
258         String title = ParamUtil.getString(actionRequest, "title");
259         double version = ParamUtil.getDouble(actionRequest, "version");
260 
261         ServiceContext serviceContext = ServiceContextFactory.getInstance(
262             WikiPage.class.getName(), actionRequest);
263 
264         WikiPageServiceUtil.revertPage(nodeId, title, version, serviceContext);
265     }
266 
267     protected void subscribePage(ActionRequest actionRequest) throws Exception {
268         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
269         String title = ParamUtil.getString(actionRequest, "title");
270 
271         WikiPageServiceUtil.subscribePage(nodeId, title);
272     }
273 
274     protected void unsubscribePage(ActionRequest actionRequest)
275         throws Exception {
276 
277         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
278         String title = ParamUtil.getString(actionRequest, "title");
279 
280         WikiPageServiceUtil.unsubscribePage(nodeId, title);
281     }
282 
283     protected WikiPage updatePage(ActionRequest actionRequest)
284         throws Exception {
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         return WikiPageServiceUtil.updatePage(
301             nodeId, title, version, content, summary, minorEdit, format,
302             parentTitle, redirectTitle, serviceContext);
303     }
304 
305     protected boolean isCheckMethodOnProcessAction() {
306         return _CHECK_METHOD_ON_PROCESS_ACTION;
307     }
308 
309     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
310 
311 }