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    import com.liferay.portlet.messageboards.util.MBUtil;
039    
040    import java.io.InputStream;
041    
042    import java.util.Collections;
043    
044    import javax.portlet.ActionRequest;
045    import javax.portlet.ActionResponse;
046    import javax.portlet.PortletConfig;
047    import javax.portlet.PortletPreferences;
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 PrincipalException ||
073                                    e instanceof RequiredMessageException) {
074    
075                                    SessionErrors.add(actionRequest, e.getClass());
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());
084                            }
085                            else {
086                                    throw e;
087                            }
088                    }
089            }
090    
091            @Override
092            public ActionForward render(
093                            ActionMapping actionMapping, ActionForm actionForm,
094                            PortletConfig portletConfig, RenderRequest renderRequest,
095                            RenderResponse renderResponse)
096                    throws Exception {
097    
098                    try {
099                            ActionUtil.getThreadMessage(renderRequest);
100                    }
101                    catch (Exception e) {
102                            if (e instanceof NoSuchMessageException ||
103                                    e instanceof PrincipalException) {
104    
105                                    SessionErrors.add(renderRequest, e.getClass());
106    
107                                    return actionMapping.findForward(
108                                            "portlet.message_boards.error");
109                            }
110                            else {
111                                    throw e;
112                            }
113                    }
114    
115                    return actionMapping.findForward(
116                            getForward(renderRequest, "portlet.message_boards.move_thread"));
117            }
118    
119            protected void moveThread(
120                            ActionRequest actionRequest, ActionResponse actionResponse)
121                    throws Exception {
122    
123                    PortletPreferences portletPreferences = actionRequest.getPreferences();
124    
125                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
126                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
127    
128                    MBThread thread = MBThreadLocalServiceUtil.getThread(threadId);
129    
130                    MBThreadServiceUtil.moveThread(categoryId, threadId);
131    
132                    boolean addExplanationPost = ParamUtil.getBoolean(
133                            actionRequest, "addExplanationPost");
134    
135                    if (addExplanationPost) {
136                            String subject = ParamUtil.getString(actionRequest, "subject");
137                            String body = ParamUtil.getString(actionRequest, "body");
138    
139                            String format = GetterUtil.getString(
140                                    portletPreferences.getValue("messageFormat", null),
141                                    MBMessageConstants.DEFAULT_FORMAT);
142    
143                            if (!MBUtil.isValidMessageFormat(format)) {
144                                    format = "html";
145                            }
146    
147                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
148                                    MBMessage.class.getName(), actionRequest);
149    
150                            MBMessageServiceUtil.addMessage(
151                                    thread.getRootMessageId(), subject, body, format,
152                                    Collections.<ObjectValuePair<String, InputStream>>emptyList(),
153                                    false, MBThreadConstants.PRIORITY_NOT_GIVEN, false,
154                                    serviceContext);
155                    }
156    
157                    PortletURL portletURL =
158                            ((ActionResponseImpl)actionResponse).createRenderURL();
159    
160                    portletURL.setParameter(
161                            "struts_action", "/message_boards/view_message");
162                    portletURL.setParameter(
163                            "messageId", String.valueOf(thread.getRootMessageId()));
164    
165                    actionResponse.sendRedirect(portletURL.toString());
166            }
167    
168    }