001    /**
002     * Copyright (c) 2000-2012 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            @Override
049            public void processAction(
050                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
051                            ActionRequest actionRequest, ActionResponse actionResponse)
052                    throws Exception {
053    
054                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
055    
056                    try {
057                            if (cmd.equals("changeParent")) {
058                                    changeParentPage(actionRequest);
059                            }
060                            else if (cmd.equals("rename")) {
061                                    renamePage(actionRequest);
062                            }
063    
064                            if (Validator.isNotNull(cmd)) {
065                                    sendRedirect(actionRequest, actionResponse);
066                            }
067                    }
068                    catch (Exception e) {
069                            if (e instanceof NoSuchNodeException ||
070                                    e instanceof NoSuchPageException ||
071                                    e instanceof PrincipalException) {
072    
073                                    SessionErrors.add(actionRequest, e.getClass().getName());
074    
075                                    setForward(actionRequest, "portlet.wiki.error");
076                            }
077                            else if (e instanceof DuplicatePageException ||
078                                             e instanceof PageContentException ||
079                                             e instanceof PageTitleException) {
080    
081                                    SessionErrors.add(actionRequest, e.getClass().getName());
082                            }
083                            else {
084                                    throw e;
085                            }
086                    }
087            }
088    
089            @Override
090            public ActionForward render(
091                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
092                            RenderRequest renderRequest, RenderResponse renderResponse)
093                    throws Exception {
094    
095                    try {
096                            ActionUtil.getNode(renderRequest);
097                            ActionUtil.getPage(renderRequest);
098                    }
099                    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                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
122                    String title = ParamUtil.getString(actionRequest, "title");
123                    String newParentTitle = ParamUtil.getString(
124                            actionRequest, "newParentTitle");
125    
126                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
127                            WikiPage.class.getName(), actionRequest);
128    
129                    WikiPageServiceUtil.changeParent(
130                            nodeId, title, newParentTitle, serviceContext);
131            }
132    
133            @Override
134            protected boolean isCheckMethodOnProcessAction() {
135                    return _CHECK_METHOD_ON_PROCESS_ACTION;
136            }
137    
138            protected void renamePage(ActionRequest actionRequest) throws Exception {
139                    long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
140                    String title = ParamUtil.getString(actionRequest, "title");
141                    String newTitle = ParamUtil.getString(actionRequest, "newTitle");
142    
143                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
144                            WikiPage.class.getName(), actionRequest);
145    
146                    WikiPageServiceUtil.movePage(nodeId, title, newTitle, serviceContext);
147            }
148    
149            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
150    
151    }