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