001    /**
002     * Copyright (c) 2000-present 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.LockedThreadException;
028    import com.liferay.portlet.messageboards.MBSettings;
029    import com.liferay.portlet.messageboards.MessageBodyException;
030    import com.liferay.portlet.messageboards.MessageSubjectException;
031    import com.liferay.portlet.messageboards.NoSuchMessageException;
032    import com.liferay.portlet.messageboards.NoSuchThreadException;
033    import com.liferay.portlet.messageboards.RequiredMessageException;
034    import com.liferay.portlet.messageboards.model.MBMessage;
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.PortletURL;
049    import javax.portlet.RenderRequest;
050    import javax.portlet.RenderResponse;
051    
052    import org.apache.struts.action.ActionForm;
053    import org.apache.struts.action.ActionForward;
054    import org.apache.struts.action.ActionMapping;
055    
056    /**
057     * @author Jorge Ferrer
058     */
059    public class MoveThreadAction extends PortletAction {
060    
061            @Override
062            public void processAction(
063                            ActionMapping actionMapping, ActionForm actionForm,
064                            PortletConfig portletConfig, ActionRequest actionRequest,
065                            ActionResponse actionResponse)
066                    throws Exception {
067    
068                    try {
069                            moveThread(actionRequest, actionResponse);
070                    }
071                    catch (Exception e) {
072                            if (e instanceof LockedThreadException ||
073                                    e instanceof PrincipalException ||
074                                    e instanceof RequiredMessageException) {
075    
076                                    SessionErrors.add(actionRequest, e.getClass());
077    
078                                    setForward(actionRequest, "portlet.message_boards.error");
079                            }
080                            else if (e instanceof MessageBodyException ||
081                                             e instanceof MessageSubjectException ||
082                                             e instanceof NoSuchThreadException) {
083    
084                                    SessionErrors.add(actionRequest, e.getClass());
085                            }
086                            else {
087                                    throw e;
088                            }
089                    }
090            }
091    
092            @Override
093            public ActionForward render(
094                            ActionMapping actionMapping, ActionForm actionForm,
095                            PortletConfig portletConfig, RenderRequest renderRequest,
096                            RenderResponse renderResponse)
097                    throws Exception {
098    
099                    try {
100                            ActionUtil.getThreadMessage(renderRequest);
101                    }
102                    catch (Exception e) {
103                            if (e instanceof NoSuchMessageException ||
104                                    e instanceof PrincipalException) {
105    
106                                    SessionErrors.add(renderRequest, e.getClass());
107    
108                                    return actionMapping.findForward(
109                                            "portlet.message_boards.error");
110                            }
111                            else {
112                                    throw e;
113                            }
114                    }
115    
116                    return actionMapping.findForward(
117                            getForward(renderRequest, "portlet.message_boards.move_thread"));
118            }
119    
120            protected void moveThread(
121                            ActionRequest actionRequest, ActionResponse actionResponse)
122                    throws Exception {
123    
124                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
125                            WebKeys.THEME_DISPLAY);
126    
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                            MBSettings mbSettings = MBSettings.getInstance(
142                                    themeDisplay.getScopeGroupId());
143    
144                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
145                                    MBMessage.class.getName(), actionRequest);
146    
147                            MBMessageServiceUtil.addMessage(
148                                    thread.getRootMessageId(), subject, body,
149                                    mbSettings.getMessageFormat(),
150                                    Collections.<ObjectValuePair<String, InputStream>>emptyList(),
151                                    false, MBThreadConstants.PRIORITY_NOT_GIVEN, false,
152                                    serviceContext);
153                    }
154    
155                    PortletURL portletURL =
156                            ((ActionResponseImpl)actionResponse).createRenderURL();
157    
158                    portletURL.setParameter(
159                            "struts_action", "/message_boards/view_message");
160                    portletURL.setParameter(
161                            "messageId", String.valueOf(thread.getRootMessageId()));
162    
163                    actionResponse.sendRedirect(portletURL.toString());
164            }
165    
166    }