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