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.ParamUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.ActionResponseImpl;
33  import com.liferay.portlet.messageboards.MessageBodyException;
34  import com.liferay.portlet.messageboards.MessageSubjectException;
35  import com.liferay.portlet.messageboards.NoSuchMessageException;
36  import com.liferay.portlet.messageboards.NoSuchThreadException;
37  import com.liferay.portlet.messageboards.RequiredMessageException;
38  import com.liferay.portlet.messageboards.model.MBMessage;
39  import com.liferay.portlet.messageboards.model.MBThread;
40  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
41  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
42  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
43  import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
44  import com.liferay.util.servlet.SessionErrors;
45  
46  import java.util.ArrayList;
47  
48  import javax.portlet.ActionRequest;
49  import javax.portlet.ActionResponse;
50  import javax.portlet.PortletConfig;
51  import javax.portlet.PortletPreferences;
52  import javax.portlet.PortletURL;
53  import javax.portlet.RenderRequest;
54  import javax.portlet.RenderResponse;
55  
56  import org.apache.struts.action.ActionForm;
57  import org.apache.struts.action.ActionForward;
58  import org.apache.struts.action.ActionMapping;
59  
60  /**
61   * <a href="SplitThreadAction.java.html"><b><i>View Source</i></b></a>
62   *
63   * @author Jorge Ferrer
64   *
65   */
66  public class SplitThreadAction extends PortletAction {
67  
68      public void processAction(
69              ActionMapping mapping, ActionForm form, PortletConfig config,
70              ActionRequest req, ActionResponse res)
71          throws Exception {
72  
73          try {
74              splitThread(req, res);
75          }
76          catch (Exception e) {
77              if (e instanceof PrincipalException ||
78                  e instanceof RequiredMessageException) {
79  
80                  SessionErrors.add(req, e.getClass().getName());
81  
82                  setForward(req, "portlet.message_boards.error");
83              }
84              else if (e instanceof MessageBodyException ||
85                       e instanceof MessageSubjectException ||
86                       e instanceof NoSuchThreadException) {
87  
88                  SessionErrors.add(req, e.getClass().getName());
89              }
90              else {
91                  throw e;
92              }
93          }
94      }
95  
96      public ActionForward render(
97              ActionMapping mapping, ActionForm form, PortletConfig config,
98              RenderRequest req, RenderResponse res)
99          throws Exception {
100 
101         try {
102             ActionUtil.getMessage(req);
103         }
104         catch (Exception e) {
105             if (e instanceof NoSuchMessageException ||
106                 e instanceof PrincipalException) {
107 
108                 SessionErrors.add(req, e.getClass().getName());
109 
110                 return mapping.findForward("portlet.message_boards.error");
111             }
112             else {
113                 throw e;
114             }
115         }
116 
117         return mapping.findForward(
118             getForward(req, "portlet.message_boards.split_thread"));
119     }
120 
121     protected void splitThread(ActionRequest req, ActionResponse res)
122         throws Exception {
123 
124         ThemeDisplay themeDisplay = (ThemeDisplay)req.getAttribute(
125             WebKeys.THEME_DISPLAY);
126 
127         PortletPreferences prefs = req.getPreferences();
128 
129         long messageId = ParamUtil.getLong(req, "messageId");
130 
131         MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
132 
133         long oldThreadId = message.getThreadId();
134         long oldParentMessageId = message.getParentMessageId();
135 
136         MBThread newThread = MBThreadServiceUtil.splitThread(
137             messageId, prefs, themeDisplay);
138 
139         boolean addExplanationPost = ParamUtil.getBoolean(
140             req, "addExplanationPost");
141 
142         if (addExplanationPost) {
143             String subject = ParamUtil.getString(req, "subject");
144             String body = ParamUtil.getString(req, "body");
145 
146             String portalURL = PortalUtil.getPortalURL(themeDisplay);
147             String layoutURL = PortalUtil.getLayoutURL(themeDisplay);
148 
149             String newThreadURL =
150                 portalURL + layoutURL + "/message_boards/message/" +
151                     message.getMessageId();
152 
153             body = StringUtil.replace(
154                 body,
155                 new String[] {
156                     "[$NEW_THREAD_URL$]",
157                 },
158                 new String[] {
159                     newThreadURL
160                 });
161 
162             MBMessageServiceUtil.addMessage(
163                 message.getCategoryId(), oldThreadId, oldParentMessageId,
164                 subject, body, new ArrayList(), false,
165                 MBThreadImpl.PRIORITY_NOT_GIVEN, null, prefs, true, true,
166                 themeDisplay);
167         }
168 
169         PortletURL portletURL = ((ActionResponseImpl)res).createRenderURL();
170 
171         portletURL.setParameter(
172             "struts_action", "/message_boards/view_message");
173         portletURL.setParameter(
174             "messageId", String.valueOf(newThread.getRootMessageId()));
175 
176         res.sendRedirect(portletURL.toString());
177     }
178 
179 }