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.SplitThreadException;
41 import com.liferay.portlet.messageboards.model.MBMessage;
42 import com.liferay.portlet.messageboards.model.MBThread;
43 import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
44 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
45 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
46 import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
47
48 import java.util.ArrayList;
49
50 import javax.portlet.ActionRequest;
51 import javax.portlet.ActionResponse;
52 import javax.portlet.PortletConfig;
53 import javax.portlet.PortletPreferences;
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
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 e instanceof SplitThreadException) {
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 PortletPreferences prefs = actionRequest.getPreferences();
131
132 long messageId = ParamUtil.getLong(actionRequest, "messageId");
133
134 MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
135
136 long oldThreadId = message.getThreadId();
137 long oldParentMessageId = message.getParentMessageId();
138
139 MBThread newThread = MBThreadServiceUtil.splitThread(
140 messageId, prefs, themeDisplay);
141
142 boolean addExplanationPost = ParamUtil.getBoolean(
143 actionRequest, "addExplanationPost");
144
145 if (addExplanationPost) {
146 String subject = ParamUtil.getString(actionRequest, "subject");
147 String body = ParamUtil.getString(actionRequest, "body");
148
149 String layoutFullURL = PortalUtil.getLayoutFullURL(themeDisplay);
150
151 String newThreadURL =
152 layoutFullURL + "/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 }