com.liferay.portlet.messageboards.action.MoveThreadAction.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.portlet.messageboards.action.MoveThreadAction.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.messageboards.action;

import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ObjectValuePair;
import com.liferay.portal.kernel.util.ParamUtil;
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.portal.theme.ThemeDisplay;
import com.liferay.portal.util.WebKeys;
import com.liferay.portlet.ActionResponseImpl;
import com.liferay.portlet.messageboards.MessageBodyException;
import com.liferay.portlet.messageboards.MessageSubjectException;
import com.liferay.portlet.messageboards.NoSuchMessageException;
import com.liferay.portlet.messageboards.NoSuchThreadException;
import com.liferay.portlet.messageboards.RequiredMessageException;
import com.liferay.portlet.messageboards.model.MBMessage;
import com.liferay.portlet.messageboards.model.MBMessageConstants;
import com.liferay.portlet.messageboards.model.MBThread;
import com.liferay.portlet.messageboards.model.MBThreadConstants;
import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;

import java.io.InputStream;

import java.util.Collections;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletURL;
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 MoveThreadAction extends PortletAction {

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

        try {
            moveThread(actionRequest, actionResponse);
        } catch (Exception e) {
            if (e instanceof PrincipalException || e instanceof RequiredMessageException) {

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

                setForward(actionRequest, "portlet.message_boards.error");
            } else if (e instanceof MessageBodyException || e instanceof MessageSubjectException
                    || e instanceof NoSuchThreadException) {

                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.getThreadMessage(renderRequest);
        } catch (Exception e) {
            if (e instanceof NoSuchMessageException || e instanceof PrincipalException) {

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

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

        return mapping.findForward(getForward(renderRequest, "portlet.message_boards.move_thread"));
    }

    protected void moveThread(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

        PortletPreferences preferences = actionRequest.getPreferences();

        ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

        long groupId = themeDisplay.getScopeGroupId();
        long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
        long threadId = ParamUtil.getLong(actionRequest, "threadId");

        MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);

        MBThreadServiceUtil.moveThread(categoryId, threadId);

        boolean addExplanationPost = ParamUtil.getBoolean(actionRequest, "addExplanationPost");

        if (addExplanationPost) {
            String subject = ParamUtil.getString(actionRequest, "subject");
            String body = ParamUtil.getString(actionRequest, "body");

            String format = GetterUtil.getString(preferences.getValue("messageFormat", null),
                    MBMessageConstants.DEFAULT_FORMAT);

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

            MBMessageServiceUtil.addMessage(groupId, categoryId, threadId, thread.getRootMessageId(), subject, body,
                    format, Collections.<ObjectValuePair<String, InputStream>>emptyList(), false,
                    MBThreadConstants.PRIORITY_NOT_GIVEN, false, serviceContext);
        }

        PortletURL portletURL = ((ActionResponseImpl) actionResponse).createRenderURL();

        portletURL.setParameter("struts_action", "/message_boards/view_message");
        portletURL.setParameter("messageId", String.valueOf(thread.getRootMessageId()));

        actionResponse.sendRedirect(portletURL.toString());
    }

}