1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
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.struts.PortletAction;
23  import com.liferay.portal.theme.ThemeDisplay;
24  import com.liferay.portal.util.WebKeys;
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.service.WikiPageServiceUtil;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  import javax.portlet.PortletPreferences;
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         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
122             WebKeys.THEME_DISPLAY);
123 
124         PortletPreferences prefs = actionRequest.getPreferences();
125 
126         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
127         String title = ParamUtil.getString(actionRequest, "title");
128         String newParentTitle = ParamUtil.getString(
129             actionRequest, "newParentTitle");
130 
131         WikiPageServiceUtil.changeParent(
132             nodeId, title, newParentTitle, prefs, themeDisplay);
133     }
134 
135     protected void renamePage(ActionRequest actionRequest) throws Exception {
136         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
137             WebKeys.THEME_DISPLAY);
138 
139         PortletPreferences prefs = actionRequest.getPreferences();
140 
141         long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
142         String title = ParamUtil.getString(actionRequest, "title");
143         String newTitle = ParamUtil.getString(actionRequest, "newTitle");
144 
145         WikiPageServiceUtil.movePage(
146             nodeId, title, newTitle, prefs, themeDisplay);
147     }
148 
149     protected boolean isCheckMethodOnProcessAction() {
150         return _CHECK_METHOD_ON_PROCESS_ACTION;
151     }
152 
153     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
154 
155 }