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