001    /**
002     * Copyright (c) 2000-2010 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.PageContentException;
029    import com.liferay.portlet.wiki.PageTitleException;
030    import com.liferay.portlet.wiki.model.WikiPage;
031    import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionForward;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Jorge Ferrer
045     */
046    public class MovePageAction extends PortletAction {
047    
048            public void processAction(
049                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
050                            ActionRequest actionRequest, ActionResponse actionResponse)
051                    throws Exception {
052    
053                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
054    
055                    try {
056                            if (cmd.equals("changeParent")) {
057                                    changeParentPage(actionRequest);
058                            }
059                            else if (cmd.equals("rename")) {
060                                    renamePage(actionRequest);
061                            }
062    
063                            if (Validator.isNotNull(cmd)) {
064                                    sendRedirect(actionRequest, actionResponse);
065                            }
066                    }
067                    catch (Exception e) {
068                            if (e instanceof NoSuchNodeException ||
069                                    e instanceof NoSuchPageException ||
070                                    e instanceof PrincipalException) {
071    
072                                    SessionErrors.add(actionRequest, e.getClass().getName());
073    
074                                    setForward(actionRequest, "portlet.wiki.error");
075                            }
076                            else if (e instanceof DuplicatePageException ||
077                                             e instanceof PageContentException ||
078                                             e instanceof PageTitleException) {
079    
080                                    SessionErrors.add(actionRequest, e.getClass().getName());
081                            }
082                            else {
083                                    throw e;
084                            }
085                    }
086            }
087    
088            public ActionForward render(
089                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
090                            RenderRequest renderRequest, RenderResponse renderResponse)
091                    throws Exception {
092    
093                    try {
094                            ActionUtil.getNode(renderRequest);
095                            ActionUtil.getPage(renderRequest);
096                    }
097                    catch (Exception e) {
098                            if (e instanceof NoSuchNodeException ||
099                                    e instanceof NoSuchPageException ||
100                                    e instanceof PageTitleException ||
101                                    e instanceof PrincipalException) {
102    
103                                    SessionErrors.add(renderRequest, e.getClass().getName());
104    
105                                    return mapping.findForward("portlet.wiki.error");
106                            }
107                            else {
108                                    throw e;
109                            }
110                    }
111    
112                    return mapping.findForward(
113                            getForward(renderRequest, "portlet.wiki.move_page"));
114            }
115    
116            protected void changeParentPage(ActionRequest actionRequest)
117                    throws Exception {
118    
119                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
120                    String title = ParamUtil.getString(actionRequest, "title");
121                    String newParentTitle = ParamUtil.getString(
122                            actionRequest, "newParentTitle");
123    
124                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
125                            WikiPage.class.getName(), actionRequest);
126    
127                    WikiPageServiceUtil.changeParent(
128                            nodeId, title, newParentTitle, serviceContext);
129            }
130    
131            protected void renamePage(ActionRequest actionRequest) throws Exception {
132                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
133                    String title = ParamUtil.getString(actionRequest, "title");
134                    String newTitle = ParamUtil.getString(actionRequest, "newTitle");
135    
136                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
137                            WikiPage.class.getName(), actionRequest);
138    
139                    WikiPageServiceUtil.movePage(nodeId, title, newTitle, serviceContext);
140            }
141    
142            protected boolean isCheckMethodOnProcessAction() {
143                    return _CHECK_METHOD_ON_PROCESS_ACTION;
144            }
145    
146            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
147    
148    }