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