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 mapping, ActionForm form, PortletConfig portletConfig,
063                            ActionRequest actionRequest, ActionResponse actionResponse)
064                    throws Exception {
065    
066                    try {
067                            moveThread(actionRequest, actionResponse);
068                    }
069                    catch (Exception e) {
070                            if (e instanceof PrincipalException ||
071                                    e instanceof RequiredMessageException) {
072    
073                                    SessionErrors.add(actionRequest, e.getClass());
074    
075                                    setForward(actionRequest, "portlet.message_boards.error");
076                            }
077                            else if (e instanceof MessageBodyException ||
078                                             e instanceof MessageSubjectException ||
079                                             e instanceof NoSuchThreadException) {
080    
081                                    SessionErrors.add(actionRequest, e.getClass());
082                            }
083                            else {
084                                    throw e;
085                            }
086                    }
087            }
088    
089            @Override
090            public ActionForward render(
091                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
092                            RenderRequest renderRequest, RenderResponse renderResponse)
093                    throws Exception {
094    
095                    try {
096                            ActionUtil.getThreadMessage(renderRequest);
097                    }
098                    catch (Exception e) {
099                            if (e instanceof NoSuchMessageException ||
100                                    e instanceof PrincipalException) {
101    
102                                    SessionErrors.add(renderRequest, e.getClass());
103    
104                                    return mapping.findForward("portlet.message_boards.error");
105                            }
106                            else {
107                                    throw e;
108                            }
109                    }
110    
111                    return mapping.findForward(
112                            getForward(renderRequest, "portlet.message_boards.move_thread"));
113            }
114    
115            protected void moveThread(
116                            ActionRequest actionRequest, ActionResponse actionResponse)
117                    throws Exception {
118    
119                    PortletPreferences preferences = actionRequest.getPreferences();
120    
121                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
122                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
123    
124                    MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
125    
126                    MBThreadServiceUtil.moveThread(categoryId, threadId);
127    
128                    boolean addExplanationPost = ParamUtil.getBoolean(
129                            actionRequest, "addExplanationPost");
130    
131                    if (addExplanationPost) {
132                            String subject = ParamUtil.getString(actionRequest, "subject");
133                            String body = ParamUtil.getString(actionRequest, "body");
134    
135                            String format = GetterUtil.getString(
136                                    preferences.getValue("messageFormat", null),
137                                    MBMessageConstants.DEFAULT_FORMAT);
138    
139                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
140                                    MBMessage.class.getName(), actionRequest);
141    
142                            MBMessageServiceUtil.addMessage(
143                                    thread.getRootMessageId(), subject, body, format,
144                                    Collections.<ObjectValuePair<String, InputStream>>emptyList(),
145                                    false, MBThreadConstants.PRIORITY_NOT_GIVEN, false,
146                                    serviceContext);
147                    }
148    
149                    PortletURL portletURL =
150                            ((ActionResponseImpl)actionResponse).createRenderURL();
151    
152                    portletURL.setParameter(
153                            "struts_action", "/message_boards/view_message");
154                    portletURL.setParameter(
155                            "messageId", String.valueOf(thread.getRootMessageId()));
156    
157                    actionResponse.sendRedirect(portletURL.toString());
158            }
159    
160    }