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