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.SplitThreadException;
43 import com.liferay.portlet.messageboards.model.MBMessage;
44 import com.liferay.portlet.messageboards.model.MBThread;
45 import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
46 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
47 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
48 import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
49
50 import java.util.ArrayList;
51
52 import javax.portlet.ActionRequest;
53 import javax.portlet.ActionResponse;
54 import javax.portlet.PortletConfig;
55 import javax.portlet.PortletURL;
56 import javax.portlet.RenderRequest;
57 import javax.portlet.RenderResponse;
58
59 import org.apache.struts.action.ActionForm;
60 import org.apache.struts.action.ActionForward;
61 import org.apache.struts.action.ActionMapping;
62
63
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 e instanceof SplitThreadException) {
90
91 SessionErrors.add(actionRequest, e.getClass().getName());
92 }
93 else {
94 throw e;
95 }
96 }
97 }
98
99 public ActionForward render(
100 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
101 RenderRequest renderRequest, RenderResponse renderResponse)
102 throws Exception {
103
104 try {
105 ActionUtil.getMessage(renderRequest);
106 }
107 catch (Exception e) {
108 if (e instanceof NoSuchMessageException ||
109 e instanceof PrincipalException) {
110
111 SessionErrors.add(renderRequest, e.getClass().getName());
112
113 return mapping.findForward("portlet.message_boards.error");
114 }
115 else {
116 throw e;
117 }
118 }
119
120 return mapping.findForward(
121 getForward(renderRequest, "portlet.message_boards.split_thread"));
122 }
123
124 protected void splitThread(
125 ActionRequest actionRequest, ActionResponse actionResponse)
126 throws Exception {
127
128 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
129 WebKeys.THEME_DISPLAY);
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 ServiceContext serviceContext = ServiceContextFactory.getInstance(
139 MBThread.class.getName(), actionRequest);
140
141 MBThread newThread = MBThreadServiceUtil.splitThread(
142 messageId, serviceContext);
143
144 boolean addExplanationPost = ParamUtil.getBoolean(
145 actionRequest, "addExplanationPost");
146
147 if (addExplanationPost) {
148 String subject = ParamUtil.getString(actionRequest, "subject");
149 String body = ParamUtil.getString(actionRequest, "body");
150
151 String layoutFullURL = PortalUtil.getLayoutFullURL(themeDisplay);
152
153 String newThreadURL =
154 layoutFullURL + "/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 }