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.service.ServiceContext;
31 import com.liferay.portal.service.ServiceContextFactory;
32 import com.liferay.portal.struts.PortletAction;
33 import com.liferay.portal.theme.ThemeDisplay;
34 import com.liferay.portal.util.PortalUtil;
35 import com.liferay.portal.util.WebKeys;
36 import com.liferay.portlet.ActionResponseImpl;
37 import com.liferay.portlet.messageboards.MessageBodyException;
38 import com.liferay.portlet.messageboards.MessageSubjectException;
39 import com.liferay.portlet.messageboards.NoSuchMessageException;
40 import com.liferay.portlet.messageboards.NoSuchThreadException;
41 import com.liferay.portlet.messageboards.RequiredMessageException;
42 import com.liferay.portlet.messageboards.model.MBMessage;
43 import com.liferay.portlet.messageboards.model.MBThread;
44 import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
45 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
46 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
47 import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
48
49 import java.util.ArrayList;
50
51 import javax.portlet.ActionRequest;
52 import javax.portlet.ActionResponse;
53 import javax.portlet.PortletConfig;
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
68 public class SplitThreadAction extends PortletAction {
69
70 public void processAction(
71 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
72 ActionRequest actionRequest, ActionResponse actionResponse)
73 throws Exception {
74
75 try {
76 splitThread(actionRequest, actionResponse);
77 }
78 catch (Exception e) {
79 if (e instanceof PrincipalException ||
80 e instanceof RequiredMessageException) {
81
82 SessionErrors.add(actionRequest, e.getClass().getName());
83
84 setForward(actionRequest, "portlet.message_boards.error");
85 }
86 else if (e instanceof MessageBodyException ||
87 e instanceof MessageSubjectException ||
88 e instanceof NoSuchThreadException) {
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 long messageId = ParamUtil.getLong(actionRequest, "messageId");
131
132 MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
133
134 long oldThreadId = message.getThreadId();
135 long oldParentMessageId = message.getParentMessageId();
136
137 ServiceContext serviceContext = ServiceContextFactory.getInstance(
138 MBThread.class.getName(), actionRequest);
139
140 MBThread newThread = MBThreadServiceUtil.splitThread(
141 messageId, serviceContext);
142
143 boolean addExplanationPost = ParamUtil.getBoolean(
144 actionRequest, "addExplanationPost");
145
146 if (addExplanationPost) {
147 String subject = ParamUtil.getString(actionRequest, "subject");
148 String body = ParamUtil.getString(actionRequest, "body");
149
150 String portalURL = PortalUtil.getPortalURL(themeDisplay);
151 String layoutURL = PortalUtil.getLayoutURL(themeDisplay);
152
153 String newThreadURL =
154 portalURL + layoutURL + "/message_boards/message/" +
155 message.getMessageId();
156
157 body = StringUtil.replace(
158 body,
159 new String[] {
160 "[$NEW_THREAD_URL$]",
161 },
162 new String[] {
163 newThreadURL
164 });
165
166 serviceContext.setAddCommunityPermissions(true);
167 serviceContext.setAddGuestPermissions(true);
168
169 MBMessageServiceUtil.addMessage(
170 message.getCategoryId(), oldThreadId, oldParentMessageId,
171 subject, body, new ArrayList<ObjectValuePair<String, byte[]>>(),
172 false, MBThreadImpl.PRIORITY_NOT_GIVEN, serviceContext);
173 }
174
175 PortletURL portletURL =
176 ((ActionResponseImpl)actionResponse).createRenderURL();
177
178 portletURL.setParameter(
179 "struts_action", "/message_boards/view_message");
180 portletURL.setParameter(
181 "messageId", String.valueOf(newThread.getRootMessageId()));
182
183 actionResponse.sendRedirect(portletURL.toString());
184 }
185
186 }