1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.ObjectValuePair;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.theme.ThemeDisplay;
24  import com.liferay.portal.util.PortalUtil;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.ActionResponseImpl;
27  import com.liferay.portlet.messageboards.MessageBodyException;
28  import com.liferay.portlet.messageboards.MessageSubjectException;
29  import com.liferay.portlet.messageboards.NoSuchMessageException;
30  import com.liferay.portlet.messageboards.NoSuchThreadException;
31  import com.liferay.portlet.messageboards.RequiredMessageException;
32  import com.liferay.portlet.messageboards.SplitThreadException;
33  import com.liferay.portlet.messageboards.model.MBMessage;
34  import com.liferay.portlet.messageboards.model.MBThread;
35  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
36  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
37  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
38  import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
39  
40  import java.util.ArrayList;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.PortletPreferences;
46  import javax.portlet.PortletURL;
47  import javax.portlet.RenderRequest;
48  import javax.portlet.RenderResponse;
49  
50  import org.apache.struts.action.ActionForm;
51  import org.apache.struts.action.ActionForward;
52  import org.apache.struts.action.ActionMapping;
53  
54  /**
55   * <a href="SplitThreadAction.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Jorge Ferrer
58   */
59  public class SplitThreadAction extends PortletAction {
60  
61      public void processAction(
62              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
63              ActionRequest actionRequest, ActionResponse actionResponse)
64          throws Exception {
65  
66          try {
67              splitThread(actionRequest, actionResponse);
68          }
69          catch (Exception e) {
70              if (e instanceof PrincipalException ||
71                  e instanceof RequiredMessageException) {
72  
73                  SessionErrors.add(actionRequest, e.getClass().getName());
74  
75                  setForward(actionRequest, "portlet.message_boards.error");
76              }
77              else if (e instanceof MessageBodyException ||
78                       e instanceof MessageSubjectException ||
79                       e instanceof NoSuchThreadException ||
80                       e instanceof SplitThreadException) {
81  
82                  SessionErrors.add(actionRequest, e.getClass().getName());
83              }
84              else {
85                  throw e;
86              }
87          }
88      }
89  
90      public ActionForward render(
91              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
92              RenderRequest renderRequest, RenderResponse renderResponse)
93          throws Exception {
94  
95          try {
96              ActionUtil.getMessage(renderRequest);
97          }
98          catch (Exception e) {
99              if (e instanceof NoSuchMessageException ||
100                 e instanceof PrincipalException) {
101 
102                 SessionErrors.add(renderRequest, e.getClass().getName());
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.split_thread"));
113     }
114 
115     protected void splitThread(
116             ActionRequest actionRequest, ActionResponse actionResponse)
117         throws Exception {
118 
119         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
120             WebKeys.THEME_DISPLAY);
121 
122         PortletPreferences prefs = actionRequest.getPreferences();
123 
124         long messageId = ParamUtil.getLong(actionRequest, "messageId");
125 
126         MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
127 
128         long oldThreadId = message.getThreadId();
129         long oldParentMessageId = message.getParentMessageId();
130 
131         MBThread newThread = MBThreadServiceUtil.splitThread(
132             messageId, prefs, themeDisplay);
133 
134         boolean addExplanationPost = ParamUtil.getBoolean(
135             actionRequest, "addExplanationPost");
136 
137         if (addExplanationPost) {
138             String subject = ParamUtil.getString(actionRequest, "subject");
139             String body = ParamUtil.getString(actionRequest, "body");
140 
141             String layoutFullURL = PortalUtil.getLayoutFullURL(themeDisplay);
142 
143             String newThreadURL =
144                 layoutFullURL + "/message_boards/message/" +
145                     message.getMessageId();
146 
147             body = StringUtil.replace(
148                 body,
149                 new String[] {
150                     "[$NEW_THREAD_URL$]",
151                 },
152                 new String[] {
153                     newThreadURL
154                 });
155 
156             MBMessageServiceUtil.addMessage(
157                 message.getCategoryId(), oldThreadId, oldParentMessageId,
158                 subject, body, new ArrayList<ObjectValuePair<String, byte[]>>(),
159                 false, MBThreadImpl.PRIORITY_NOT_GIVEN, null, prefs, true, true,
160                 themeDisplay);
161         }
162 
163         PortletURL portletURL =
164             ((ActionResponseImpl)actionResponse).createRenderURL();
165 
166         portletURL.setParameter(
167             "struts_action", "/message_boards/view_message");
168         portletURL.setParameter(
169             "messageId", String.valueOf(newThread.getRootMessageId()));
170 
171         actionResponse.sendRedirect(portletURL.toString());
172     }
173 
174 }