1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.wiki.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.Constants;
24  import com.liferay.portal.kernel.util.ParamUtil;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.WebKeys;
30  import com.liferay.portlet.wiki.DuplicatePageException;
31  import com.liferay.portlet.wiki.NoSuchNodeException;
32  import com.liferay.portlet.wiki.NoSuchPageException;
33  import com.liferay.portlet.wiki.PageContentException;
34  import com.liferay.portlet.wiki.PageTitleException;
35  import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.PortletPreferences;
41  import javax.portlet.RenderRequest;
42  import javax.portlet.RenderResponse;
43  
44  import org.apache.struts.action.ActionForm;
45  import org.apache.struts.action.ActionForward;
46  import org.apache.struts.action.ActionMapping;
47  
48  /**
49   * <a href="MovePageAction.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Jorge Ferrer
52   *
53   */
54  public class MovePageAction extends PortletAction {
55  
56      public void processAction(
57              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
58              ActionRequest actionRequest, ActionResponse actionResponse)
59          throws Exception {
60  
61          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
62  
63          try {
64              if (cmd.equals("changeParent")) {
65                  changeParentPage(actionRequest);
66              }
67              else if (cmd.equals("rename")) {
68                  renamePage(actionRequest);
69              }
70  
71              if (Validator.isNotNull(cmd)) {
72                  sendRedirect(actionRequest, actionResponse);
73              }
74          }
75          catch (Exception e) {
76              if (e instanceof NoSuchNodeException ||
77                  e instanceof NoSuchPageException ||
78                  e instanceof PrincipalException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81  
82                  setForward(actionRequest, "portlet.wiki.error");
83              }
84              else if (e instanceof DuplicatePageException ||
85                       e instanceof PageContentException ||
86                       e instanceof PageTitleException) {
87  
88                  SessionErrors.add(actionRequest, e.getClass().getName());
89              }
90              else {
91                  throw e;
92              }
93          }
94      }
95  
96      public ActionForward render(
97              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
98              RenderRequest renderRequest, RenderResponse renderResponse)
99          throws Exception {
100 
101         try {
102             ActionUtil.getNode(renderRequest);
103             ActionUtil.getPage(renderRequest);
104         }
105         catch (Exception e) {
106             if (e instanceof NoSuchNodeException ||
107                 e instanceof NoSuchPageException ||
108                 e instanceof PageTitleException ||
109                 e instanceof PrincipalException) {
110 
111                 SessionErrors.add(renderRequest, e.getClass().getName());
112 
113                 return mapping.findForward("portlet.wiki.error");
114             }
115             else {
116                 throw e;
117             }
118         }
119 
120         return mapping.findForward(
121             getForward(renderRequest, "portlet.wiki.move_page"));
122     }
123 
124     protected void changeParentPage(ActionRequest actionRequest)
125         throws Exception {
126 
127         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
128             WebKeys.THEME_DISPLAY);
129 
130         PortletPreferences prefs = actionRequest.getPreferences();
131 
132         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
133         String title = ParamUtil.getString(actionRequest, "title");
134         String newParentTitle = ParamUtil.getString(
135             actionRequest, "newParentTitle");
136 
137         WikiPageServiceUtil.changeParent(
138             nodeId, title, newParentTitle, prefs, themeDisplay);
139     }
140 
141     protected void renamePage(ActionRequest actionRequest) throws Exception {
142         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
143             WebKeys.THEME_DISPLAY);
144 
145         PortletPreferences prefs = actionRequest.getPreferences();
146 
147         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
148         String title = ParamUtil.getString(actionRequest, "title");
149         String newTitle = ParamUtil.getString(actionRequest, "newTitle");
150 
151         WikiPageServiceUtil.movePage(
152             nodeId, title, newTitle, prefs, themeDisplay);
153     }
154 
155     protected boolean isCheckMethodOnProcessAction() {
156         return _CHECK_METHOD_ON_PROCESS_ACTION;
157     }
158 
159     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
160 
161 }