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