001
014
015 package com.liferay.portlet.messageboards.action;
016
017 import com.liferay.documentlibrary.FileNameException;
018 import com.liferay.documentlibrary.FileSizeException;
019 import com.liferay.portal.kernel.captcha.CaptchaTextException;
020 import com.liferay.portal.kernel.captcha.CaptchaUtil;
021 import com.liferay.portal.kernel.servlet.SessionErrors;
022 import com.liferay.portal.kernel.upload.UploadPortletRequest;
023 import com.liferay.portal.kernel.util.Constants;
024 import com.liferay.portal.kernel.util.FileUtil;
025 import com.liferay.portal.kernel.util.ObjectValuePair;
026 import com.liferay.portal.kernel.util.ParamUtil;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.kernel.workflow.WorkflowConstants;
029 import com.liferay.portal.security.auth.PrincipalException;
030 import com.liferay.portal.service.ServiceContext;
031 import com.liferay.portal.service.ServiceContextFactory;
032 import com.liferay.portal.struts.PortletAction;
033 import com.liferay.portal.theme.ThemeDisplay;
034 import com.liferay.portal.util.PortalUtil;
035 import com.liferay.portal.util.PropsValues;
036 import com.liferay.portal.util.WebKeys;
037 import com.liferay.portlet.ActionResponseImpl;
038 import com.liferay.portlet.asset.AssetTagException;
039 import com.liferay.portlet.messageboards.LockedThreadException;
040 import com.liferay.portlet.messageboards.MessageBodyException;
041 import com.liferay.portlet.messageboards.MessageSubjectException;
042 import com.liferay.portlet.messageboards.NoSuchMessageException;
043 import com.liferay.portlet.messageboards.RequiredMessageException;
044 import com.liferay.portlet.messageboards.model.MBMessage;
045 import com.liferay.portlet.messageboards.service.MBMessageFlagLocalServiceUtil;
046 import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
047 import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
048
049 import java.io.File;
050
051 import java.util.ArrayList;
052 import java.util.List;
053
054 import javax.portlet.ActionRequest;
055 import javax.portlet.ActionResponse;
056 import javax.portlet.PortletConfig;
057 import javax.portlet.PortletURL;
058 import javax.portlet.RenderRequest;
059 import javax.portlet.RenderResponse;
060
061 import org.apache.struts.action.ActionForm;
062 import org.apache.struts.action.ActionForward;
063 import org.apache.struts.action.ActionMapping;
064
065
068 public class EditMessageAction extends PortletAction {
069
070 public void processAction(
071 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
072 ActionRequest actionRequest, ActionResponse actionResponse)
073 throws Exception {
074
075 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
076
077 try {
078 MBMessage message = null;
079
080 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
081 message = updateMessage(actionRequest, actionResponse);
082 }
083 else if (cmd.equals(Constants.DELETE)) {
084 deleteMessage(actionRequest);
085 }
086 else if (cmd.equals(Constants.LOCK)) {
087 lockThread(actionRequest);
088 }
089 else if (cmd.equals(Constants.SUBSCRIBE)) {
090 subscribeMessage(actionRequest);
091 }
092 else if (cmd.equals(Constants.UNLOCK)) {
093 unlockThread(actionRequest);
094 }
095 else if (cmd.equals(Constants.UNSUBSCRIBE)) {
096 unsubscribeMessage(actionRequest);
097 }
098
099 if (Validator.isNotNull(cmd)) {
100 String redirect = ParamUtil.getString(
101 actionRequest, "redirect");
102
103 int workflowAction = ParamUtil.getInteger(
104 actionRequest, "workflowAction",
105 WorkflowConstants.ACTION_PUBLISH);
106
107 if ((message != null) &&
108 (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT)) {
109
110 redirect = getSaveAndContinueRedirect(
111 actionRequest, actionResponse, message);
112 }
113
114 sendRedirect(actionRequest, actionResponse, redirect);
115 }
116 }
117 catch (Exception e) {
118 if (e instanceof NoSuchMessageException ||
119 e instanceof PrincipalException ||
120 e instanceof RequiredMessageException) {
121
122 SessionErrors.add(actionRequest, e.getClass().getName());
123
124 setForward(actionRequest, "portlet.message_boards.error");
125 }
126 else if (e instanceof CaptchaTextException ||
127 e instanceof FileNameException ||
128 e instanceof FileSizeException ||
129 e instanceof LockedThreadException ||
130 e instanceof MessageBodyException ||
131 e instanceof MessageSubjectException) {
132
133 SessionErrors.add(actionRequest, e.getClass().getName());
134 }
135 else if (e instanceof AssetTagException) {
136 SessionErrors.add(actionRequest, e.getClass().getName(), e);
137 }
138 else {
139 throw e;
140 }
141 }
142 }
143
144 public ActionForward render(
145 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
146 RenderRequest renderRequest, RenderResponse renderResponse)
147 throws Exception {
148
149 try {
150 ActionUtil.getMessage(renderRequest);
151 }
152 catch (Exception e) {
153 if (e instanceof NoSuchMessageException ||
154 e instanceof PrincipalException) {
155
156 SessionErrors.add(renderRequest, e.getClass().getName());
157
158 return mapping.findForward("portlet.message_boards.error");
159 }
160 else {
161 throw e;
162 }
163 }
164
165 return mapping.findForward(
166 getForward(renderRequest, "portlet.message_boards.edit_message"));
167 }
168
169 protected void deleteMessage(ActionRequest actionRequest) throws Exception {
170 long messageId = ParamUtil.getLong(actionRequest, "messageId");
171
172 MBMessageServiceUtil.deleteMessage(messageId);
173 }
174
175 protected String getSaveAndContinueRedirect(
176 ActionRequest actionRequest, ActionResponse actionResponse,
177 MBMessage message) {
178
179 boolean preview = ParamUtil.getBoolean(actionRequest, "preview");
180
181 PortletURL portletURL =
182 ((ActionResponseImpl)actionResponse).createRenderURL();
183
184 portletURL.setParameter(
185 "struts_action", "/message_boards/edit_message");
186 portletURL.setParameter(
187 "messageId", String.valueOf(message.getMessageId()));
188 portletURL.setParameter("preview", String.valueOf(preview));
189
190 return portletURL.toString();
191 }
192
193 protected void lockThread(ActionRequest actionRequest) throws Exception {
194 long threadId = ParamUtil.getLong(actionRequest, "threadId");
195
196 MBThreadServiceUtil.lockThread(threadId);
197 }
198
199 protected void subscribeMessage(ActionRequest actionRequest)
200 throws Exception {
201
202 long messageId = ParamUtil.getLong(actionRequest, "messageId");
203
204 MBMessageServiceUtil.subscribeMessage(messageId);
205 }
206
207 protected void unlockThread(ActionRequest actionRequest) throws Exception {
208 long threadId = ParamUtil.getLong(actionRequest, "threadId");
209
210 MBThreadServiceUtil.unlockThread(threadId);
211 }
212
213 protected void unsubscribeMessage(ActionRequest actionRequest)
214 throws Exception {
215
216 long messageId = ParamUtil.getLong(actionRequest, "messageId");
217
218 MBMessageServiceUtil.unsubscribeMessage(messageId);
219 }
220
221 protected MBMessage updateMessage(
222 ActionRequest actionRequest, ActionResponse actionResponse)
223 throws Exception {
224
225 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
226 WebKeys.THEME_DISPLAY);
227
228 long messageId = ParamUtil.getLong(actionRequest, "messageId");
229
230 long groupId = themeDisplay.getScopeGroupId();
231 long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
232 long threadId = ParamUtil.getLong(actionRequest, "threadId");
233 long parentMessageId = ParamUtil.getLong(
234 actionRequest, "parentMessageId");
235 String subject = ParamUtil.getString(actionRequest, "subject");
236 String body = ParamUtil.getString(actionRequest, "body");
237 boolean attachments = ParamUtil.getBoolean(
238 actionRequest, "attachments");
239
240 List<ObjectValuePair<String, byte[]>> files =
241 new ArrayList<ObjectValuePair<String, byte[]>>();
242
243 if (attachments) {
244 UploadPortletRequest uploadRequest =
245 PortalUtil.getUploadPortletRequest(actionRequest);
246
247 for (int i = 1; i <= 5; i++) {
248 File file = uploadRequest.getFile("msgFile" + i);
249 String fileName = uploadRequest.getFileName("msgFile" + i);
250 byte[] bytes = FileUtil.getBytes(file);
251
252 if ((bytes != null) && (bytes.length > 0)) {
253 ObjectValuePair<String, byte[]> ovp =
254 new ObjectValuePair<String, byte[]>(fileName, bytes);
255
256 files.add(ovp);
257 }
258 }
259 }
260
261 boolean question = ParamUtil.getBoolean(actionRequest, "question");
262 boolean anonymous = ParamUtil.getBoolean(actionRequest, "anonymous");
263 double priority = ParamUtil.getDouble(actionRequest, "priority");
264 boolean allowPingbacks = ParamUtil.getBoolean(
265 actionRequest, "allowPingbacks");
266
267 ServiceContext serviceContext = ServiceContextFactory.getInstance(
268 MBMessage.class.getName(), actionRequest);
269
270 MBMessage message = null;
271
272 if (messageId <= 0) {
273 if (PropsValues.CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_MESSAGE) {
274 CaptchaUtil.check(actionRequest);
275 }
276
277 if (threadId <= 0) {
278
279
280
281 message = MBMessageServiceUtil.addMessage(
282 groupId, categoryId, subject, body, files, anonymous,
283 priority, allowPingbacks, serviceContext);
284
285 if (question) {
286 MBMessageFlagLocalServiceUtil.addQuestionFlag(
287 message.getMessageId());
288 }
289 }
290 else {
291
292
293
294 message = MBMessageServiceUtil.addMessage(
295 groupId, categoryId, threadId, parentMessageId, subject,
296 body, files, anonymous, priority, allowPingbacks,
297 serviceContext);
298 }
299 }
300 else {
301 List<String> existingFiles = new ArrayList<String>();
302
303 for (int i = 1; i <= 5; i++) {
304 String path = ParamUtil.getString(
305 actionRequest, "existingPath" + i);
306
307 if (Validator.isNotNull(path)) {
308 existingFiles.add(path);
309 }
310 }
311
312
313
314 message = MBMessageServiceUtil.updateMessage(
315 messageId, subject, body, files, existingFiles, priority,
316 allowPingbacks, serviceContext);
317
318 if (message.isRoot()) {
319 if (question) {
320 MBMessageFlagLocalServiceUtil.addQuestionFlag(messageId);
321 }
322 else {
323 MBMessageFlagLocalServiceUtil.deleteQuestionAndAnswerFlags(
324 message.getThreadId());
325 }
326 }
327 }
328
329 return message;
330 }
331
332 }