1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.service.ServiceContext;
23  import com.liferay.portal.service.ServiceContextFactory;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.WebKeys;
28  import com.liferay.portlet.ActionResponseImpl;
29  import com.liferay.portlet.messageboards.MessageBodyException;
30  import com.liferay.portlet.messageboards.MessageSubjectException;
31  import com.liferay.portlet.messageboards.NoSuchMessageException;
32  import com.liferay.portlet.messageboards.NoSuchThreadException;
33  import com.liferay.portlet.messageboards.RequiredMessageException;
34  import com.liferay.portlet.messageboards.SplitThreadException;
35  import com.liferay.portlet.messageboards.model.MBMessage;
36  import com.liferay.portlet.messageboards.model.MBThread;
37  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
38  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
39  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
40  import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
41  
42  import java.util.ArrayList;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.PortletConfig;
47  import javax.portlet.PortletURL;
48  import javax.portlet.RenderRequest;
49  import javax.portlet.RenderResponse;
50  
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="SplitThreadAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Jorge Ferrer
59   */
60  public class SplitThreadAction extends PortletAction {
61  
62      public void processAction(
63              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
64              ActionRequest actionRequest, ActionResponse actionResponse)
65          throws Exception {
66  
67          try {
68              splitThread(actionRequest, actionResponse);
69          }
70          catch (Exception e) {
71              if (e instanceof PrincipalException ||
72                  e instanceof RequiredMessageException) {
73  
74                  SessionErrors.add(actionRequest, e.getClass().getName());
75  
76                  setForward(actionRequest, "portlet.message_boards.error");
77              }
78              else if (e instanceof MessageBodyException ||
79                       e instanceof MessageSubjectException ||
80                       e instanceof NoSuchThreadException ||
81                       e instanceof SplitThreadException) {
82  
83                  SessionErrors.add(actionRequest, e.getClass().getName());
84              }
85              else {
86                  throw e;
87              }
88          }
89      }
90  
91      public ActionForward render(
92              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
93              RenderRequest renderRequest, RenderResponse renderResponse)
94          throws Exception {
95  
96          try {
97              ActionUtil.getMessage(renderRequest);
98          }
99          catch (Exception e) {
100             if (e instanceof NoSuchMessageException ||
101                 e instanceof PrincipalException) {
102 
103                 SessionErrors.add(renderRequest, e.getClass().getName());
104 
105                 return mapping.findForward("portlet.message_boards.error");
106             }
107             else {
108                 throw e;
109             }
110         }
111 
112         return mapping.findForward(
113             getForward(renderRequest, "portlet.message_boards.split_thread"));
114     }
115 
116     protected void splitThread(
117             ActionRequest actionRequest, ActionResponse actionResponse)
118         throws Exception {
119 
120         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
121             WebKeys.THEME_DISPLAY);
122 
123         long messageId = ParamUtil.getLong(actionRequest, "messageId");
124 
125         MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
126 
127         long oldThreadId = message.getThreadId();
128         long oldParentMessageId = message.getParentMessageId();
129 
130         ServiceContext serviceContext = ServiceContextFactory.getInstance(
131             MBThread.class.getName(), actionRequest);
132 
133         MBThread newThread = MBThreadServiceUtil.splitThread(
134             messageId, serviceContext);
135 
136         boolean addExplanationPost = ParamUtil.getBoolean(
137             actionRequest, "addExplanationPost");
138 
139         if (addExplanationPost) {
140             String subject = ParamUtil.getString(actionRequest, "subject");
141             String body = ParamUtil.getString(actionRequest, "body");
142 
143             String layoutFullURL = PortalUtil.getLayoutFullURL(themeDisplay);
144 
145             String newThreadURL =
146                 layoutFullURL + "/-/message_boards/message/" +
147                     message.getMessageId();
148 
149             body = StringUtil.replace(
150                 body,
151                 new String[] {
152                     "[$NEW_THREAD_URL$]",
153                 },
154                 new String[] {
155                     newThreadURL
156                 });
157 
158             serviceContext.setAddCommunityPermissions(true);
159             serviceContext.setAddGuestPermissions(true);
160 
161             MBMessageServiceUtil.addMessage(
162                 message.getCategoryId(), oldThreadId, oldParentMessageId,
163                 subject, body, new ArrayList<ObjectValuePair<String, byte[]>>(),
164                 false, MBThreadImpl.PRIORITY_NOT_GIVEN, serviceContext);
165         }
166 
167         PortletURL portletURL =
168             ((ActionResponseImpl)actionResponse).createRenderURL();
169 
170         portletURL.setParameter(
171             "struts_action", "/message_boards/view_message");
172         portletURL.setParameter(
173             "messageId", String.valueOf(newThread.getRootMessageId()));
174 
175         actionResponse.sendRedirect(portletURL.toString());
176     }
177 
178 }