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