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.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 actionMapping, ActionForm actionForm,
068                            PortletConfig portletConfig, ActionRequest actionRequest,
069                            ActionResponse actionResponse)
070                    throws Exception {
071    
072                    try {
073                            splitThread(actionRequest, actionResponse);
074                    }
075                    catch (Exception e) {
076                            if (e instanceof PrincipalException ||
077                                    e instanceof RequiredMessageException) {
078    
079                                    SessionErrors.add(actionRequest, e.getClass());
080    
081                                    setForward(actionRequest, "portlet.message_boards.error");
082                            }
083                            else if (e instanceof MessageBodyException ||
084                                             e instanceof MessageSubjectException ||
085                                             e instanceof NoSuchThreadException ||
086                                             e instanceof SplitThreadException) {
087    
088                                    SessionErrors.add(actionRequest, e.getClass());
089                            }
090                            else {
091                                    throw e;
092                            }
093                    }
094            }
095    
096            @Override
097            public ActionForward render(
098                            ActionMapping actionMapping, ActionForm actionForm,
099                            PortletConfig portletConfig, RenderRequest renderRequest,
100                            RenderResponse renderResponse)
101                    throws Exception {
102    
103                    try {
104                            ActionUtil.getMessage(renderRequest);
105                    }
106                    catch (Exception e) {
107                            if (e instanceof NoSuchMessageException ||
108                                    e instanceof PrincipalException) {
109    
110                                    SessionErrors.add(renderRequest, e.getClass());
111    
112                                    return actionMapping.findForward(
113                                            "portlet.message_boards.error");
114                            }
115                            else {
116                                    throw e;
117                            }
118                    }
119    
120                    return actionMapping.findForward(
121                            getForward(renderRequest, "portlet.message_boards.split_thread"));
122            }
123    
124            protected void splitThread(
125                            ActionRequest actionRequest, ActionResponse actionResponse)
126                    throws Exception {
127    
128                    PortletPreferences preferences = actionRequest.getPreferences();
129    
130                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
131                            WebKeys.THEME_DISPLAY);
132    
133                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
134    
135                    String splitThreadSubject = ParamUtil.getString(
136                            actionRequest, "splitThreadSubject");
137    
138                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
139                            MBThread.class.getName(), actionRequest);
140    
141                    MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
142    
143                    long oldParentMessageId = message.getParentMessageId();
144    
145                    MBThread newThread = MBThreadServiceUtil.splitThread(
146                            messageId, splitThreadSubject, serviceContext);
147    
148                    boolean addExplanationPost = ParamUtil.getBoolean(
149                            actionRequest, "addExplanationPost");
150    
151                    if (addExplanationPost) {
152                            String subject = ParamUtil.getString(actionRequest, "subject");
153                            String body = ParamUtil.getString(actionRequest, "body");
154    
155                            String format = GetterUtil.getString(
156                                    preferences.getValue("messageFormat", null),
157                                    MBMessageConstants.DEFAULT_FORMAT);
158    
159                            String layoutFullURL = PortalUtil.getLayoutFullURL(themeDisplay);
160    
161                            String newThreadURL =
162                                    layoutFullURL + "/-/message_boards/view_message/" +
163                                            message.getMessageId();
164    
165                            body = StringUtil.replace(
166                                    body, MBThreadConstants.NEW_THREAD_URL, newThreadURL);
167    
168                            serviceContext.setAddGroupPermissions(true);
169                            serviceContext.setAddGuestPermissions(true);
170    
171                            MBMessageServiceUtil.addMessage(
172                                    oldParentMessageId, subject, body, format,
173                                    Collections.<ObjectValuePair<String, InputStream>>emptyList(),
174                                    false, MBThreadConstants.PRIORITY_NOT_GIVEN,
175                                    message.getAllowPingbacks(), serviceContext);
176                    }
177    
178                    PortletURL portletURL =
179                            ((ActionResponseImpl)actionResponse).createRenderURL();
180    
181                    portletURL.setParameter(
182                            "struts_action", "/message_boards/view_message");
183                    portletURL.setParameter(
184                            "messageId", String.valueOf(newThread.getRootMessageId()));
185    
186                    actionResponse.sendRedirect(portletURL.toString());
187            }
188    
189    }