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.Validator;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.service.ServiceContext;
23  import com.liferay.portal.service.ServiceContextFactory;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portlet.wiki.DuplicatePageException;
26  import com.liferay.portlet.wiki.NoSuchNodeException;
27  import com.liferay.portlet.wiki.NoSuchPageException;
28  import com.liferay.portlet.wiki.PageContentException;
29  import com.liferay.portlet.wiki.PageTitleException;
30  import com.liferay.portlet.wiki.model.WikiPage;
31  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.ActionResponse;
35  import javax.portlet.PortletConfig;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="MovePageAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Jorge Ferrer
47   */
48  public class MovePageAction extends PortletAction {
49  
50      public void processAction(
51              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
52              ActionRequest actionRequest, ActionResponse actionResponse)
53          throws Exception {
54  
55          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
56  
57          try {
58              if (cmd.equals("changeParent")) {
59                  changeParentPage(actionRequest);
60              }
61              else if (cmd.equals("rename")) {
62                  renamePage(actionRequest);
63              }
64  
65              if (Validator.isNotNull(cmd)) {
66                  sendRedirect(actionRequest, actionResponse);
67              }
68          }
69          catch (Exception e) {
70              if (e instanceof NoSuchNodeException ||
71                  e instanceof NoSuchPageException ||
72                  e instanceof PrincipalException) {
73  
74                  SessionErrors.add(actionRequest, e.getClass().getName());
75  
76                  setForward(actionRequest, "portlet.wiki.error");
77              }
78              else if (e instanceof DuplicatePageException ||
79                       e instanceof PageContentException ||
80                       e instanceof PageTitleException) {
81  
82                  SessionErrors.add(actionRequest, e.getClass().getName());
83              }
84              else {
85                  throw e;
86              }
87          }
88      }
89  
90      public ActionForward render(
91              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
92              RenderRequest renderRequest, RenderResponse renderResponse)
93          throws Exception {
94  
95          try {
96              ActionUtil.getNode(renderRequest);
97              ActionUtil.getPage(renderRequest);
98          }
99          catch (Exception e) {
100             if (e instanceof NoSuchNodeException ||
101                 e instanceof NoSuchPageException ||
102                 e instanceof PageTitleException ||
103                 e instanceof PrincipalException) {
104 
105                 SessionErrors.add(renderRequest, e.getClass().getName());
106 
107                 return mapping.findForward("portlet.wiki.error");
108             }
109             else {
110                 throw e;
111             }
112         }
113 
114         return mapping.findForward(
115             getForward(renderRequest, "portlet.wiki.move_page"));
116     }
117 
118     protected void changeParentPage(ActionRequest actionRequest)
119         throws Exception {
120 
121         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
122         String title = ParamUtil.getString(actionRequest, "title");
123         String newParentTitle = ParamUtil.getString(
124             actionRequest, "newParentTitle");
125 
126         ServiceContext serviceContext = ServiceContextFactory.getInstance(
127             WikiPage.class.getName(), actionRequest);
128 
129         WikiPageServiceUtil.changeParent(
130             nodeId, title, newParentTitle, serviceContext);
131     }
132 
133     protected void renamePage(ActionRequest actionRequest) throws Exception {
134         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
135         String title = ParamUtil.getString(actionRequest, "title");
136         String newTitle = ParamUtil.getString(actionRequest, "newTitle");
137 
138         ServiceContext serviceContext = ServiceContextFactory.getInstance(
139             WikiPage.class.getName(), actionRequest);
140 
141         WikiPageServiceUtil.movePage(nodeId, title, newTitle, serviceContext);
142     }
143 
144     protected boolean isCheckMethodOnProcessAction() {
145         return _CHECK_METHOD_ON_PROCESS_ACTION;
146     }
147 
148     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
149 
150 }