com.liferay.portlet.wiki.action.MovePageAction.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.portlet.wiki.action.MovePageAction.java

Source

/**
 * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.portlet.wiki.action;

import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.security.auth.PrincipalException;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portlet.wiki.DuplicatePageException;
import com.liferay.portlet.wiki.NoSuchNodeException;
import com.liferay.portlet.wiki.NoSuchPageException;
import com.liferay.portlet.wiki.PageContentException;
import com.liferay.portlet.wiki.PageTitleException;
import com.liferay.portlet.wiki.model.WikiPage;
import com.liferay.portlet.wiki.service.WikiPageServiceUtil;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * @author Jorge Ferrer
 */
public class MovePageAction extends PortletAction {

    @Override
    public void processAction(ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
            ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

        try {
            if (cmd.equals("changeParent")) {
                changeParentPage(actionRequest);
            } else if (cmd.equals("rename")) {
                renamePage(actionRequest);
            }

            if (Validator.isNotNull(cmd)) {
                sendRedirect(actionRequest, actionResponse);
            }
        } catch (Exception e) {
            if (e instanceof NoSuchNodeException || e instanceof NoSuchPageException
                    || e instanceof PrincipalException) {

                SessionErrors.add(actionRequest, e.getClass().getName());

                setForward(actionRequest, "portlet.wiki.error");
            } else if (e instanceof DuplicatePageException || e instanceof PageContentException
                    || e instanceof PageTitleException) {

                SessionErrors.add(actionRequest, e.getClass().getName());
            } else {
                throw e;
            }
        }
    }

    @Override
    public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
            RenderRequest renderRequest, RenderResponse renderResponse) throws Exception {

        try {
            ActionUtil.getNode(renderRequest);
            ActionUtil.getPage(renderRequest);
        } catch (Exception e) {
            if (e instanceof NoSuchNodeException || e instanceof NoSuchPageException
                    || e instanceof PageTitleException || e instanceof PrincipalException) {

                SessionErrors.add(renderRequest, e.getClass().getName());

                return mapping.findForward("portlet.wiki.error");
            } else {
                throw e;
            }
        }

        return mapping.findForward(getForward(renderRequest, "portlet.wiki.move_page"));
    }

    protected void changeParentPage(ActionRequest actionRequest) throws Exception {

        long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
        String title = ParamUtil.getString(actionRequest, "title");
        String newParentTitle = ParamUtil.getString(actionRequest, "newParentTitle");

        ServiceContext serviceContext = ServiceContextFactory.getInstance(WikiPage.class.getName(), actionRequest);

        WikiPageServiceUtil.changeParent(nodeId, title, newParentTitle, serviceContext);
    }

    protected void renamePage(ActionRequest actionRequest) throws Exception {
        long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
        String title = ParamUtil.getString(actionRequest, "title");
        String newTitle = ParamUtil.getString(actionRequest, "newTitle");

        ServiceContext serviceContext = ServiceContextFactory.getInstance(WikiPage.class.getName(), actionRequest);

        WikiPageServiceUtil.movePage(nodeId, title, newTitle, serviceContext);
    }

    @Override
    protected boolean isCheckMethodOnProcessAction() {
        return _CHECK_METHOD_ON_PROCESS_ACTION;
    }

    private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;

}