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.messageboards.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ObjectValuePair;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.service.ServiceContextFactory;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.ActionResponseImpl;
027    import com.liferay.portlet.messageboards.MessageBodyException;
028    import com.liferay.portlet.messageboards.MessageSubjectException;
029    import com.liferay.portlet.messageboards.NoSuchMessageException;
030    import com.liferay.portlet.messageboards.NoSuchThreadException;
031    import com.liferay.portlet.messageboards.RequiredMessageException;
032    import com.liferay.portlet.messageboards.model.MBMessage;
033    import com.liferay.portlet.messageboards.model.MBThread;
034    import com.liferay.portlet.messageboards.model.MBThreadConstants;
035    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
036    import com.liferay.portlet.messageboards.service.MBThreadLocalServiceUtil;
037    import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
038    
039    import java.util.ArrayList;
040    
041    import javax.portlet.ActionRequest;
042    import javax.portlet.ActionResponse;
043    import javax.portlet.PortletConfig;
044    import javax.portlet.PortletURL;
045    import javax.portlet.RenderRequest;
046    import javax.portlet.RenderResponse;
047    
048    import org.apache.struts.action.ActionForm;
049    import org.apache.struts.action.ActionForward;
050    import org.apache.struts.action.ActionMapping;
051    
052    /**
053     * @author Jorge Ferrer
054     */
055    public class MoveThreadAction extends PortletAction {
056    
057            public void processAction(
058                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
059                            ActionRequest actionRequest, ActionResponse actionResponse)
060                    throws Exception {
061    
062                    try {
063                            moveThread(actionRequest, actionResponse);
064                    }
065                    catch (Exception e) {
066                            if (e instanceof PrincipalException ||
067                                    e instanceof RequiredMessageException) {
068    
069                                    SessionErrors.add(actionRequest, e.getClass().getName());
070    
071                                    setForward(actionRequest, "portlet.message_boards.error");
072                            }
073                            else if (e instanceof MessageBodyException ||
074                                             e instanceof MessageSubjectException ||
075                                             e instanceof NoSuchThreadException) {
076    
077                                    SessionErrors.add(actionRequest, e.getClass().getName());
078                            }
079                            else {
080                                    throw e;
081                            }
082                    }
083            }
084    
085            public ActionForward render(
086                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
087                            RenderRequest renderRequest, RenderResponse renderResponse)
088                    throws Exception {
089    
090                    try {
091                            ActionUtil.getThreadMessage(renderRequest);
092                    }
093                    catch (Exception e) {
094                            if (e instanceof NoSuchMessageException ||
095                                    e instanceof PrincipalException) {
096    
097                                    SessionErrors.add(renderRequest, e.getClass().getName());
098    
099                                    return mapping.findForward("portlet.message_boards.error");
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105    
106                    return mapping.findForward(
107                            getForward(renderRequest, "portlet.message_boards.move_thread"));
108            }
109    
110            protected void moveThread(
111                            ActionRequest actionRequest, ActionResponse actionResponse)
112                    throws Exception {
113    
114                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
115                            WebKeys.THEME_DISPLAY);
116    
117                    long groupId = themeDisplay.getScopeGroupId();
118                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
119                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
120    
121                    MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
122    
123                    MBThreadServiceUtil.moveThread(categoryId, threadId);
124    
125                    boolean addExplanationPost = ParamUtil.getBoolean(
126                            actionRequest, "addExplanationPost");
127    
128                    if (addExplanationPost) {
129                            String subject = ParamUtil.getString(actionRequest, "subject");
130                            String body = ParamUtil.getString(actionRequest, "body");
131    
132                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
133                                    MBMessage.class.getName(), actionRequest);
134    
135                            MBMessageServiceUtil.addMessage(
136                                    groupId, categoryId, threadId, thread.getRootMessageId(),
137                                    subject, body, new ArrayList<ObjectValuePair<String, byte[]>>(),
138                                    false, MBThreadConstants.PRIORITY_NOT_GIVEN, false,
139                                    serviceContext);
140                    }
141    
142                    PortletURL portletURL =
143                            ((ActionResponseImpl)actionResponse).createRenderURL();
144    
145                    portletURL.setParameter(
146                            "struts_action", "/message_boards/view_message");
147                    portletURL.setParameter(
148                            "messageId", String.valueOf(thread.getRootMessageId()));
149    
150                    actionResponse.sendRedirect(portletURL.toString());
151            }
152    
153    }