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