1
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
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 }