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.GetterUtil;
019 import com.liferay.portal.kernel.util.ObjectValuePair;
020 import com.liferay.portal.kernel.util.ParamUtil;
021 import com.liferay.portal.kernel.util.StringUtil;
022 import com.liferay.portal.security.auth.PrincipalException;
023 import com.liferay.portal.service.ServiceContext;
024 import com.liferay.portal.service.ServiceContextFactory;
025 import com.liferay.portal.struts.PortletAction;
026 import com.liferay.portal.theme.ThemeDisplay;
027 import com.liferay.portal.util.PortalUtil;
028 import com.liferay.portal.util.WebKeys;
029 import com.liferay.portlet.ActionResponseImpl;
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.MBMessageConstants;
038 import com.liferay.portlet.messageboards.model.MBThread;
039 import com.liferay.portlet.messageboards.model.MBThreadConstants;
040 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
041 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
042 import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
043 import com.liferay.portlet.messageboards.util.MBUtil;
044
045 import java.io.InputStream;
046
047 import java.util.Collections;
048
049 import javax.portlet.ActionRequest;
050 import javax.portlet.ActionResponse;
051 import javax.portlet.PortletConfig;
052 import javax.portlet.PortletPreferences;
053 import javax.portlet.PortletURL;
054 import javax.portlet.RenderRequest;
055 import javax.portlet.RenderResponse;
056
057 import org.apache.struts.action.ActionForm;
058 import org.apache.struts.action.ActionForward;
059 import org.apache.struts.action.ActionMapping;
060
061
064 public class SplitThreadAction extends PortletAction {
065
066 @Override
067 public void processAction(
068 ActionMapping actionMapping, ActionForm actionForm,
069 PortletConfig portletConfig, ActionRequest actionRequest,
070 ActionResponse actionResponse)
071 throws Exception {
072
073 try {
074 splitThread(actionRequest, actionResponse);
075 }
076 catch (Exception e) {
077 if (e instanceof PrincipalException ||
078 e instanceof RequiredMessageException) {
079
080 SessionErrors.add(actionRequest, e.getClass());
081
082 setForward(actionRequest, "portlet.message_boards.error");
083 }
084 else if (e instanceof MessageBodyException ||
085 e instanceof MessageSubjectException ||
086 e instanceof NoSuchThreadException ||
087 e instanceof SplitThreadException) {
088
089 SessionErrors.add(actionRequest, e.getClass());
090 }
091 else {
092 throw e;
093 }
094 }
095 }
096
097 @Override
098 public ActionForward render(
099 ActionMapping actionMapping, ActionForm actionForm,
100 PortletConfig portletConfig, RenderRequest renderRequest,
101 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());
112
113 return actionMapping.findForward(
114 "portlet.message_boards.error");
115 }
116 else {
117 throw e;
118 }
119 }
120
121 return actionMapping.findForward(
122 getForward(renderRequest, "portlet.message_boards.split_thread"));
123 }
124
125 protected void splitThread(
126 ActionRequest actionRequest, ActionResponse actionResponse)
127 throws Exception {
128
129 PortletPreferences portletPreferences = actionRequest.getPreferences();
130
131 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
132 WebKeys.THEME_DISPLAY);
133
134 long messageId = ParamUtil.getLong(actionRequest, "messageId");
135
136 String splitThreadSubject = ParamUtil.getString(
137 actionRequest, "splitThreadSubject");
138
139 ServiceContext serviceContext = ServiceContextFactory.getInstance(
140 MBThread.class.getName(), actionRequest);
141
142 MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
143
144 long oldParentMessageId = message.getParentMessageId();
145
146 MBThread newThread = MBThreadServiceUtil.splitThread(
147 messageId, splitThreadSubject, serviceContext);
148
149 boolean addExplanationPost = ParamUtil.getBoolean(
150 actionRequest, "addExplanationPost");
151
152 if (addExplanationPost) {
153 String subject = ParamUtil.getString(actionRequest, "subject");
154 String body = ParamUtil.getString(actionRequest, "body");
155
156 String format = GetterUtil.getString(
157 portletPreferences.getValue("messageFormat", null),
158 MBMessageConstants.DEFAULT_FORMAT);
159
160 if (!MBUtil.isValidMessageFormat(format)) {
161 format = "html";
162 }
163
164 String layoutFullURL = PortalUtil.getLayoutFullURL(themeDisplay);
165
166 String newThreadURL =
167 layoutFullURL + "/-/message_boards/view_message/" +
168 message.getMessageId();
169
170 body = StringUtil.replace(
171 body, MBThreadConstants.NEW_THREAD_URL, newThreadURL);
172
173 serviceContext.setAddGroupPermissions(true);
174 serviceContext.setAddGuestPermissions(true);
175
176 MBMessageServiceUtil.addMessage(
177 oldParentMessageId, subject, body, format,
178 Collections.<ObjectValuePair<String, InputStream>>emptyList(),
179 false, MBThreadConstants.PRIORITY_NOT_GIVEN,
180 message.getAllowPingbacks(), serviceContext);
181 }
182
183 PortletURL portletURL =
184 ((ActionResponseImpl)actionResponse).createRenderURL();
185
186 portletURL.setParameter(
187 "struts_action", "/message_boards/view_message");
188 portletURL.setParameter(
189 "messageId", String.valueOf(newThread.getRootMessageId()));
190
191 actionResponse.sendRedirect(portletURL.toString());
192 }
193
194 }