1
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.struts.PortletAction;
31 import com.liferay.portal.theme.ThemeDisplay;
32 import com.liferay.portal.util.PortalUtil;
33 import com.liferay.portal.util.WebKeys;
34 import com.liferay.portlet.ActionResponseImpl;
35 import com.liferay.portlet.messageboards.MessageBodyException;
36 import com.liferay.portlet.messageboards.MessageSubjectException;
37 import com.liferay.portlet.messageboards.NoSuchMessageException;
38 import com.liferay.portlet.messageboards.NoSuchThreadException;
39 import com.liferay.portlet.messageboards.RequiredMessageException;
40 import com.liferay.portlet.messageboards.model.MBMessage;
41 import com.liferay.portlet.messageboards.model.MBThread;
42 import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
43 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
44 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
45 import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
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
67 public class SplitThreadAction extends PortletAction {
68
69 public void processAction(
70 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
71 ActionRequest actionRequest, ActionResponse actionResponse)
72 throws Exception {
73
74 try {
75 splitThread(actionRequest, actionResponse);
76 }
77 catch (Exception e) {
78 if (e instanceof PrincipalException ||
79 e instanceof RequiredMessageException) {
80
81 SessionErrors.add(actionRequest, e.getClass().getName());
82
83 setForward(actionRequest, "portlet.message_boards.error");
84 }
85 else if (e instanceof MessageBodyException ||
86 e instanceof MessageSubjectException ||
87 e instanceof NoSuchThreadException) {
88
89 SessionErrors.add(actionRequest, e.getClass().getName());
90 }
91 else {
92 throw e;
93 }
94 }
95 }
96
97 public ActionForward render(
98 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
99 RenderRequest renderRequest, RenderResponse renderResponse)
100 throws Exception {
101
102 try {
103 ActionUtil.getMessage(renderRequest);
104 }
105 catch (Exception e) {
106 if (e instanceof NoSuchMessageException ||
107 e instanceof PrincipalException) {
108
109 SessionErrors.add(renderRequest, 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(renderRequest, "portlet.message_boards.split_thread"));
120 }
121
122 protected void splitThread(
123 ActionRequest actionRequest, ActionResponse actionResponse)
124 throws Exception {
125
126 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
127 WebKeys.THEME_DISPLAY);
128
129 PortletPreferences prefs = actionRequest.getPreferences();
130
131 long messageId = ParamUtil.getLong(actionRequest, "messageId");
132
133 MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
134
135 long oldThreadId = message.getThreadId();
136 long oldParentMessageId = message.getParentMessageId();
137
138 MBThread newThread = MBThreadServiceUtil.splitThread(
139 messageId, prefs, themeDisplay);
140
141 boolean addExplanationPost = ParamUtil.getBoolean(
142 actionRequest, "addExplanationPost");
143
144 if (addExplanationPost) {
145 String subject = ParamUtil.getString(actionRequest, "subject");
146 String body = ParamUtil.getString(actionRequest, "body");
147
148 String portalURL = PortalUtil.getPortalURL(themeDisplay);
149 String layoutURL = PortalUtil.getLayoutURL(themeDisplay);
150
151 String newThreadURL =
152 portalURL + layoutURL + "/message_boards/message/" +
153 message.getMessageId();
154
155 body = StringUtil.replace(
156 body,
157 new String[] {
158 "[$NEW_THREAD_URL$]",
159 },
160 new String[] {
161 newThreadURL
162 });
163
164 MBMessageServiceUtil.addMessage(
165 message.getCategoryId(), oldThreadId, oldParentMessageId,
166 subject, body, new ArrayList<ObjectValuePair<String, byte[]>>(),
167 false, MBThreadImpl.PRIORITY_NOT_GIVEN, null, prefs, true, true,
168 themeDisplay);
169 }
170
171 PortletURL portletURL =
172 ((ActionResponseImpl)actionResponse).createRenderURL();
173
174 portletURL.setParameter(
175 "struts_action", "/message_boards/view_message");
176 portletURL.setParameter(
177 "messageId", String.valueOf(newThread.getRootMessageId()));
178
179 actionResponse.sendRedirect(portletURL.toString());
180 }
181
182 }