001
014
015 package com.liferay.portlet.messageboards.action;
016
017 import com.liferay.portal.kernel.servlet.SessionErrors;
018 import com.liferay.portal.kernel.util.ObjectValuePair;
019 import com.liferay.portal.kernel.util.ParamUtil;
020 import com.liferay.portal.kernel.util.StringUtil;
021 import com.liferay.portal.security.auth.PrincipalException;
022 import com.liferay.portal.service.ServiceContext;
023 import com.liferay.portal.service.ServiceContextFactory;
024 import com.liferay.portal.struts.PortletAction;
025 import com.liferay.portal.theme.ThemeDisplay;
026 import com.liferay.portal.util.PortalUtil;
027 import com.liferay.portal.util.WebKeys;
028 import com.liferay.portlet.ActionResponseImpl;
029 import com.liferay.portlet.messageboards.MBSettings;
030 import com.liferay.portlet.messageboards.MessageBodyException;
031 import com.liferay.portlet.messageboards.MessageSubjectException;
032 import com.liferay.portlet.messageboards.NoSuchMessageException;
033 import com.liferay.portlet.messageboards.NoSuchThreadException;
034 import com.liferay.portlet.messageboards.RequiredMessageException;
035 import com.liferay.portlet.messageboards.SplitThreadException;
036 import com.liferay.portlet.messageboards.model.MBMessage;
037 import com.liferay.portlet.messageboards.model.MBThread;
038 import com.liferay.portlet.messageboards.model.MBThreadConstants;
039 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
040 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
041 import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
042
043 import java.io.InputStream;
044
045 import java.util.Collections;
046
047 import javax.portlet.ActionRequest;
048 import javax.portlet.ActionResponse;
049 import javax.portlet.PortletConfig;
050 import javax.portlet.PortletURL;
051 import javax.portlet.RenderRequest;
052 import javax.portlet.RenderResponse;
053
054 import org.apache.struts.action.ActionForm;
055 import org.apache.struts.action.ActionForward;
056 import org.apache.struts.action.ActionMapping;
057
058
061 public class SplitThreadAction extends PortletAction {
062
063 @Override
064 public void processAction(
065 ActionMapping actionMapping, ActionForm actionForm,
066 PortletConfig portletConfig, ActionRequest actionRequest,
067 ActionResponse actionResponse)
068 throws Exception {
069
070 try {
071 splitThread(actionRequest, actionResponse);
072 }
073 catch (Exception e) {
074 if (e instanceof PrincipalException ||
075 e instanceof RequiredMessageException) {
076
077 SessionErrors.add(actionRequest, e.getClass());
078
079 setForward(actionRequest, "portlet.message_boards.error");
080 }
081 else if (e instanceof MessageBodyException ||
082 e instanceof MessageSubjectException ||
083 e instanceof NoSuchThreadException ||
084 e instanceof SplitThreadException) {
085
086 SessionErrors.add(actionRequest, e.getClass());
087 }
088 else {
089 throw e;
090 }
091 }
092 }
093
094 @Override
095 public ActionForward render(
096 ActionMapping actionMapping, ActionForm actionForm,
097 PortletConfig portletConfig, RenderRequest renderRequest,
098 RenderResponse renderResponse)
099 throws Exception {
100
101 try {
102 ActionUtil.getMessage(renderRequest);
103 }
104 catch (Exception e) {
105 if (e instanceof NoSuchMessageException ||
106 e instanceof PrincipalException) {
107
108 SessionErrors.add(renderRequest, e.getClass());
109
110 return actionMapping.findForward(
111 "portlet.message_boards.error");
112 }
113 else {
114 throw e;
115 }
116 }
117
118 return actionMapping.findForward(
119 getForward(renderRequest, "portlet.message_boards.split_thread"));
120 }
121
122 protected void splitThread(
123 ActionRequest actionRequest, ActionResponse actionResponse)
124 throws Exception {
125
126 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
127 WebKeys.THEME_DISPLAY);
128
129 long messageId = ParamUtil.getLong(actionRequest, "messageId");
130
131 String splitThreadSubject = ParamUtil.getString(
132 actionRequest, "splitThreadSubject");
133
134 ServiceContext serviceContext = ServiceContextFactory.getInstance(
135 MBThread.class.getName(), actionRequest);
136
137 MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
138
139 long oldParentMessageId = message.getParentMessageId();
140
141 MBThread newThread = MBThreadServiceUtil.splitThread(
142 messageId, splitThreadSubject, 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 MBSettings mbSettings = MBSettings.getInstance(
152 themeDisplay.getScopeGroupId());
153
154 String layoutFullURL = PortalUtil.getLayoutFullURL(themeDisplay);
155
156 String newThreadURL =
157 layoutFullURL + "/-/message_boards/view_message/" +
158 message.getMessageId();
159
160 body = StringUtil.replace(
161 body, MBThreadConstants.NEW_THREAD_URL, newThreadURL);
162
163 serviceContext.setAddGroupPermissions(true);
164 serviceContext.setAddGuestPermissions(true);
165
166 MBMessageServiceUtil.addMessage(
167 oldParentMessageId, subject, body,
168 mbSettings.getMessageFormat(),
169 Collections.<ObjectValuePair<String, InputStream>>emptyList(),
170 false, MBThreadConstants.PRIORITY_NOT_GIVEN,
171 message.getAllowPingbacks(), serviceContext);
172 }
173
174 PortletURL portletURL =
175 ((ActionResponseImpl)actionResponse).createRenderURL();
176
177 portletURL.setParameter(
178 "struts_action", "/message_boards/view_message");
179 portletURL.setParameter(
180 "messageId", String.valueOf(newThread.getRootMessageId()));
181
182 actionResponse.sendRedirect(portletURL.toString());
183 }
184
185 }