001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portal.service.ServiceContextFactory;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portlet.wiki.DuplicatePageException;
026    import com.liferay.portlet.wiki.NoSuchNodeException;
027    import com.liferay.portlet.wiki.NoSuchPageException;
028    import com.liferay.portlet.wiki.NodeChangeException;
029    import com.liferay.portlet.wiki.PageContentException;
030    import com.liferay.portlet.wiki.PageTitleException;
031    import com.liferay.portlet.wiki.model.WikiPage;
032    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
033    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.ActionResponse;
036    import javax.portlet.PortletConfig;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Jorge Ferrer
046     */
047    public class MovePageAction extends PortletAction {
048    
049            @Override
050            public void processAction(
051                            ActionMapping actionMapping, ActionForm actionForm,
052                            PortletConfig portletConfig, ActionRequest actionRequest,
053                            ActionResponse actionResponse)
054                    throws Exception {
055    
056                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
057    
058                    try {
059                            if (cmd.equals(Constants.CHANGE_PARENT)) {
060                                    changeParentPage(actionRequest);
061                            }
062                            else if (cmd.equals(Constants.MOVE)) {
063                                    changeNode(actionRequest);
064                            }
065                            else if (cmd.equals(Constants.RENAME)) {
066                                    renamePage(actionRequest);
067                            }
068    
069                            if (Validator.isNotNull(cmd)) {
070                                    sendRedirect(actionRequest, actionResponse);
071                            }
072                    }
073                    catch (Exception e) {
074                            if (e instanceof NoSuchNodeException ||
075                                    e instanceof NoSuchPageException ||
076                                    e instanceof PrincipalException) {
077    
078                                    SessionErrors.add(actionRequest, e.getClass());
079    
080                                    setForward(actionRequest, "portlet.wiki.error");
081                            }
082                            else if (e instanceof DuplicatePageException ||
083                                             e instanceof PageContentException ||
084                                             e instanceof PageTitleException) {
085    
086                                    SessionErrors.add(actionRequest, e.getClass());
087                            }
088                            else if (e instanceof NodeChangeException) {
089                                    SessionErrors.add(actionRequest, e.getClass(), e);
090                            }
091                            else {
092                                    throw e;
093                            }
094                    }
095            }
096    
097            @Override
098            public ActionForward render(
099                            ActionMapping actionMapping, ActionForm actionForm,
100                            PortletConfig portletConfig, RenderRequest renderRequest,
101                            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());
115    
116                                    return actionMapping.findForward("portlet.wiki.error");
117                            }
118                            else {
119                                    throw e;
120                            }
121                    }
122    
123                    return actionMapping.findForward(
124                            getForward(renderRequest, "portlet.wiki.move_page"));
125            }
126    
127            protected void changeNode(ActionRequest actionRequest) throws Exception {
128                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
129                    String title = ParamUtil.getString(actionRequest, "title");
130                    long newNodeId = ParamUtil.getLong(actionRequest, "newNodeId");
131    
132                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
133                            WikiPage.class.getName(), actionRequest);
134    
135                    WikiPageServiceUtil.changeNode(
136                            nodeId, title, newNodeId, serviceContext);
137            }
138    
139            protected void changeParentPage(ActionRequest actionRequest)
140                    throws Exception {
141    
142                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
143                    String title = ParamUtil.getString(actionRequest, "title");
144                    String newParentTitle = ParamUtil.getString(
145                            actionRequest, "newParentTitle");
146    
147                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
148                            WikiPage.class.getName(), actionRequest);
149    
150                    WikiPageServiceUtil.changeParent(
151                            nodeId, title, newParentTitle, serviceContext);
152            }
153    
154            @Override
155            protected boolean isCheckMethodOnProcessAction() {
156                    return _CHECK_METHOD_ON_PROCESS_ACTION;
157            }
158    
159            protected void renamePage(ActionRequest actionRequest) throws Exception {
160                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
161                    String title = ParamUtil.getString(actionRequest, "title");
162                    String newTitle = ParamUtil.getString(actionRequest, "newTitle");
163    
164                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
165                            WikiPage.class.getName(), actionRequest);
166    
167                    WikiPageServiceUtil.renamePage(nodeId, title, newTitle, serviceContext);
168            }
169    
170            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
171    
172    }