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.MessageBodyException;
030 import com.liferay.portlet.messageboards.MessageSubjectException;
031 import com.liferay.portlet.messageboards.NoSuchMessageException;
032 import com.liferay.portlet.messageboards.NoSuchThreadException;
033 import com.liferay.portlet.messageboards.RequiredMessageException;
034 import com.liferay.portlet.messageboards.SplitThreadException;
035 import com.liferay.portlet.messageboards.model.MBMessage;
036 import com.liferay.portlet.messageboards.model.MBThread;
037 import com.liferay.portlet.messageboards.model.MBThreadConstants;
038 import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
039 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
040 import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
041
042 import java.util.ArrayList;
043
044 import javax.portlet.ActionRequest;
045 import javax.portlet.ActionResponse;
046 import javax.portlet.PortletConfig;
047 import javax.portlet.PortletURL;
048 import javax.portlet.RenderRequest;
049 import javax.portlet.RenderResponse;
050
051 import org.apache.struts.action.ActionForm;
052 import org.apache.struts.action.ActionForward;
053 import org.apache.struts.action.ActionMapping;
054
055
058 public class SplitThreadAction extends PortletAction {
059
060 public void processAction(
061 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
062 ActionRequest actionRequest, ActionResponse actionResponse)
063 throws Exception {
064
065 try {
066 splitThread(actionRequest, actionResponse);
067 }
068 catch (Exception e) {
069 if (e instanceof PrincipalException ||
070 e instanceof RequiredMessageException) {
071
072 SessionErrors.add(actionRequest, e.getClass().getName());
073
074 setForward(actionRequest, "portlet.message_boards.error");
075 }
076 else if (e instanceof MessageBodyException ||
077 e instanceof MessageSubjectException ||
078 e instanceof NoSuchThreadException ||
079 e instanceof SplitThreadException) {
080
081 SessionErrors.add(actionRequest, e.getClass().getName());
082 }
083 else {
084 throw e;
085 }
086 }
087 }
088
089 public ActionForward render(
090 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
091 RenderRequest renderRequest, RenderResponse renderResponse)
092 throws Exception {
093
094 try {
095 ActionUtil.getMessage(renderRequest);
096 }
097 catch (Exception e) {
098 if (e instanceof NoSuchMessageException ||
099 e instanceof PrincipalException) {
100
101 SessionErrors.add(renderRequest, e.getClass().getName());
102
103 return mapping.findForward("portlet.message_boards.error");
104 }
105 else {
106 throw e;
107 }
108 }
109
110 return mapping.findForward(
111 getForward(renderRequest, "portlet.message_boards.split_thread"));
112 }
113
114 protected void splitThread(
115 ActionRequest actionRequest, ActionResponse actionResponse)
116 throws Exception {
117
118 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
119 WebKeys.THEME_DISPLAY);
120
121 long messageId = ParamUtil.getLong(actionRequest, "messageId");
122
123 MBMessage message = MBMessageLocalServiceUtil.getMessage(messageId);
124
125 long oldThreadId = message.getThreadId();
126 long oldParentMessageId = message.getParentMessageId();
127
128 ServiceContext serviceContext = ServiceContextFactory.getInstance(
129 MBThread.class.getName(), actionRequest);
130
131 MBThread newThread = MBThreadServiceUtil.splitThread(
132 messageId, serviceContext);
133
134 boolean addExplanationPost = ParamUtil.getBoolean(
135 actionRequest, "addExplanationPost");
136
137 if (addExplanationPost) {
138 String subject = ParamUtil.getString(actionRequest, "subject");
139 String body = ParamUtil.getString(actionRequest, "body");
140
141 String layoutFullURL = PortalUtil.getLayoutFullURL(themeDisplay);
142
143 String newThreadURL =
144 layoutFullURL + "/-/message_boards/view_message/" +
145 message.getMessageId();
146
147 body = StringUtil.replace(
148 body,
149 new String[] {
150 "[$NEW_THREAD_URL$]",
151 },
152 new String[] {
153 newThreadURL
154 });
155
156 serviceContext.setAddCommunityPermissions(true);
157 serviceContext.setAddGuestPermissions(true);
158
159 MBMessageServiceUtil.addMessage(
160 message.getGroupId(), message.getCategoryId(), oldThreadId,
161 oldParentMessageId, subject, body,
162 new ArrayList<ObjectValuePair<String, byte[]>>(), false,
163 MBThreadConstants.PRIORITY_NOT_GIVEN,
164 message.getAllowPingbacks(), serviceContext);
165 }
166
167 PortletURL portletURL =
168 ((ActionResponseImpl)actionResponse).createRenderURL();
169
170 portletURL.setParameter(
171 "struts_action", "/message_boards/view_message");
172 portletURL.setParameter(
173 "messageId", String.valueOf(newThread.getRootMessageId()));
174
175 actionResponse.sendRedirect(portletURL.toString());
176 }
177
178 }