1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.action;
24  
25  import com.liferay.portal.kernel.util.ObjectValuePair;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.WebKeys;
33  import com.liferay.portlet.ActionResponseImpl;
34  import com.liferay.portlet.messageboards.MessageBodyException;
35  import com.liferay.portlet.messageboards.MessageSubjectException;
36  import com.liferay.portlet.messageboards.NoSuchMessageException;
37  import com.liferay.portlet.messageboards.NoSuchThreadException;
38  import com.liferay.portlet.messageboards.RequiredMessageException;
39  import com.liferay.portlet.messageboards.model.MBMessage;
40  import com.liferay.portlet.messageboards.model.MBThread;
41  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
42  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
43  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
44  import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
45  import com.liferay.util.servlet.SessionErrors;
46  
47  import java.util.ArrayList;
48  
49  import javax.portlet.ActionRequest;
50  import javax.portlet.ActionResponse;
51  import javax.portlet.PortletConfig;
52  import javax.portlet.PortletPreferences;
53  import javax.portlet.PortletURL;
54  import javax.portlet.RenderRequest;
55  import javax.portlet.RenderResponse;
56  
57  import org.apache.struts.action.ActionForm;
58  import org.apache.struts.action.ActionForward;
59  import org.apache.struts.action.ActionMapping;
60  
61  /**
62   * <a href="SplitThreadAction.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Jorge Ferrer
65   *
66   */
67  public class SplitThreadAction extends PortletAction {
68  
69      public void processAction(
70              ActionMapping mapping, ActionForm form, PortletConfig config,
71              ActionRequest req, ActionResponse res)
72          throws Exception {
73  
74          try {
75              splitThread(req, res);
76          }
77          catch (Exception e) {
78              if (e instanceof PrincipalException ||
79                  e instanceof RequiredMessageException) {
80  
81                  SessionErrors.add(req, e.getClass().getName());
82  
83                  setForward(req, "portlet.message_boards.error");
84              }
85              else if (e instanceof MessageBodyException ||
86                       e instanceof MessageSubjectException ||
87                       e instanceof NoSuchThreadException) {
88  
89                  SessionErrors.add(req, e.getClass().getName());
90              }
91              else {
92                  throw e;
93              }
94          }
95      }
96  
97      public ActionForward render(
98              ActionMapping mapping, ActionForm form, PortletConfig config,
99              RenderRequest req, RenderResponse res)
100         throws Exception {
101 
102         try {
103             ActionUtil.getMessage(req);
104         }
105         catch (Exception e) {
106             if (e instanceof NoSuchMessageException ||
107                 e instanceof PrincipalException) {
108 
109                 SessionErrors.add(req, e.getClass().getName());
110 
111                 return mapping.findForward("portlet.message_boards.error");
112             }
113             else {
114                 throw e;
115             }
116         }
117 
118         return mapping.findForward(
119             getForward(req, "portlet.message_boards.split_thread"));
120     }
121 
122     protected void splitThread(ActionRequest req, ActionResponse res)
123         throws Exception {
124 
125         ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
126             WebKeys.THEME_DISPLAY);
127 
128         PortletPreferences prefs = req.getPreferences();
129 
130         long messageId = ParamUtil.getLong(req, "messageId");
131 
132         MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
133 
134         long oldThreadId = message.getThreadId();
135         long oldParentMessageId = message.getParentMessageId();
136 
137         MBThread newThread = MBThreadServiceUtil.splitThread(
138             messageId, prefs, themeDisplay);
139 
140         boolean addExplanationPost = ParamUtil.getBoolean(
141             req, "addExplanationPost");
142 
143         if (addExplanationPost) {
144             String subject = ParamUtil.getString(req, "subject");
145             String body = ParamUtil.getString(req, "body");
146 
147             String portalURL = PortalUtil.getPortalURL(themeDisplay);
148             String layoutURL = PortalUtil.getLayoutURL(themeDisplay);
149 
150             String newThreadURL =
151                 portalURL + layoutURL + "/message_boards/message/" +
152                     message.getMessageId();
153 
154             body = StringUtil.replace(
155                 body,
156                 new String[] {
157                     "[$NEW_THREAD_URL$]",
158                 },
159                 new String[] {
160                     newThreadURL
161                 });
162 
163             MBMessageServiceUtil.addMessage(
164                 message.getCategoryId(), oldThreadId, oldParentMessageId,
165                 subject, body, new ArrayList<ObjectValuePair<String, byte[]>>(),
166                 false, MBThreadImpl.PRIORITY_NOT_GIVEN, null, prefs, true, true,
167                 themeDisplay);
168         }
169 
170         PortletURL portletURL = ((ActionResponseImpl)res).createRenderURL();
171 
172         portletURL.setParameter(
173             "struts_action", "/message_boards/view_message");
174         portletURL.setParameter(
175             "messageId", String.valueOf(newThread.getRootMessageId()));
176 
177         res.sendRedirect(portletURL.toString());
178     }
179 
180 }