1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.action;
16  
17  import com.liferay.documentlibrary.FileNameException;
18  import com.liferay.documentlibrary.FileSizeException;
19  import com.liferay.portal.kernel.captcha.CaptchaTextException;
20  import com.liferay.portal.kernel.captcha.CaptchaUtil;
21  import com.liferay.portal.kernel.servlet.SessionErrors;
22  import com.liferay.portal.kernel.upload.UploadPortletRequest;
23  import com.liferay.portal.kernel.util.Constants;
24  import com.liferay.portal.kernel.util.FileUtil;
25  import com.liferay.portal.kernel.util.ObjectValuePair;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.security.auth.PrincipalException;
29  import com.liferay.portal.service.ServiceContext;
30  import com.liferay.portal.service.ServiceContextFactory;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.PropsValues;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.ActionResponseImpl;
37  import com.liferay.portlet.asset.AssetTagException;
38  import com.liferay.portlet.messageboards.LockedThreadException;
39  import com.liferay.portlet.messageboards.MessageBodyException;
40  import com.liferay.portlet.messageboards.MessageSubjectException;
41  import com.liferay.portlet.messageboards.NoSuchMessageException;
42  import com.liferay.portlet.messageboards.RequiredMessageException;
43  import com.liferay.portlet.messageboards.model.MBMessage;
44  import com.liferay.portlet.messageboards.service.MBMessageFlagLocalServiceUtil;
45  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
46  import com.liferay.portlet.messageboards.service.MBThreadServiceUtil;
47  
48  import java.io.File;
49  
50  import java.util.ArrayList;
51  import java.util.List;
52  
53  import javax.portlet.ActionRequest;
54  import javax.portlet.ActionResponse;
55  import javax.portlet.PortletConfig;
56  import javax.portlet.PortletURL;
57  import javax.portlet.RenderRequest;
58  import javax.portlet.RenderResponse;
59  
60  import org.apache.struts.action.ActionForm;
61  import org.apache.struts.action.ActionForward;
62  import org.apache.struts.action.ActionMapping;
63  
64  /**
65   * <a href="EditMessageAction.java.html"><b><i>View Source</i></b></a>
66   *
67   * @author Brian Wing Shun Chan
68   */
69  public class EditMessageAction extends PortletAction {
70  
71      public void processAction(
72              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
73              ActionRequest actionRequest, ActionResponse actionResponse)
74          throws Exception {
75  
76          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
77  
78          try {
79              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
80                  updateMessage(actionRequest, actionResponse);
81              }
82              else if (cmd.equals(Constants.DELETE)) {
83                  deleteMessage(actionRequest);
84              }
85              else if (cmd.equals(Constants.LOCK)) {
86                  lockThread(actionRequest);
87              }
88              else if (cmd.equals(Constants.SUBSCRIBE)) {
89                  subscribeMessage(actionRequest);
90              }
91              else if (cmd.equals(Constants.UNLOCK)) {
92                  unlockThread(actionRequest);
93              }
94              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
95                  unsubscribeMessage(actionRequest);
96              }
97  
98              if (cmd.equals(Constants.DELETE) ||
99                  cmd.equals(Constants.LOCK) ||
100                 cmd.equals(Constants.SUBSCRIBE) ||
101                 cmd.equals(Constants.UNSUBSCRIBE) ||
102                 cmd.equals(Constants.UNLOCK)) {
103 
104                 sendRedirect(actionRequest, actionResponse);
105             }
106         }
107         catch (Exception e) {
108             if (e instanceof NoSuchMessageException ||
109                 e instanceof PrincipalException ||
110                 e instanceof RequiredMessageException) {
111 
112                 SessionErrors.add(actionRequest, e.getClass().getName());
113 
114                 setForward(actionRequest, "portlet.message_boards.error");
115             }
116             else if (e instanceof CaptchaTextException ||
117                      e instanceof FileNameException ||
118                      e instanceof FileSizeException ||
119                      e instanceof LockedThreadException ||
120                      e instanceof MessageBodyException ||
121                      e instanceof MessageSubjectException) {
122 
123                 SessionErrors.add(actionRequest, e.getClass().getName());
124             }
125             else if (e instanceof AssetTagException) {
126                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
127             }
128             else {
129                 throw e;
130             }
131         }
132     }
133 
134     public ActionForward render(
135             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
136             RenderRequest renderRequest, RenderResponse renderResponse)
137         throws Exception {
138 
139         try {
140             ActionUtil.getMessage(renderRequest);
141         }
142         catch (Exception e) {
143             if (e instanceof NoSuchMessageException ||
144                 e instanceof PrincipalException) {
145 
146                 SessionErrors.add(renderRequest, e.getClass().getName());
147 
148                 return mapping.findForward("portlet.message_boards.error");
149             }
150             else {
151                 throw e;
152             }
153         }
154 
155         return mapping.findForward(
156             getForward(renderRequest, "portlet.message_boards.edit_message"));
157     }
158 
159     protected void deleteMessage(ActionRequest actionRequest) throws Exception {
160         long messageId = ParamUtil.getLong(actionRequest, "messageId");
161 
162         MBMessageServiceUtil.deleteMessage(messageId);
163     }
164 
165     protected void lockThread(ActionRequest actionRequest) throws Exception {
166         long threadId = ParamUtil.getLong(actionRequest, "threadId");
167 
168         MBThreadServiceUtil.lockThread(threadId);
169     }
170 
171     protected void subscribeMessage(ActionRequest actionRequest)
172         throws Exception {
173 
174         long messageId = ParamUtil.getLong(actionRequest, "messageId");
175 
176         MBMessageServiceUtil.subscribeMessage(messageId);
177     }
178 
179     protected void unlockThread(ActionRequest actionRequest) throws Exception {
180         long threadId = ParamUtil.getLong(actionRequest, "threadId");
181 
182         MBThreadServiceUtil.unlockThread(threadId);
183     }
184 
185     protected void unsubscribeMessage(ActionRequest actionRequest)
186         throws Exception {
187 
188         long messageId = ParamUtil.getLong(actionRequest, "messageId");
189 
190         MBMessageServiceUtil.unsubscribeMessage(messageId);
191     }
192 
193     protected void updateMessage(
194             ActionRequest actionRequest, ActionResponse actionResponse)
195         throws Exception {
196 
197         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
198             WebKeys.THEME_DISPLAY);
199 
200         long messageId = ParamUtil.getLong(actionRequest, "messageId");
201 
202         long groupId = themeDisplay.getScopeGroupId();
203         long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
204         long threadId = ParamUtil.getLong(actionRequest, "threadId");
205         long parentMessageId = ParamUtil.getLong(
206             actionRequest, "parentMessageId");
207         String subject = ParamUtil.getString(actionRequest, "subject");
208         String body = ParamUtil.getString(actionRequest, "body");
209         boolean attachments = ParamUtil.getBoolean(
210             actionRequest, "attachments");
211 
212         List<ObjectValuePair<String, byte[]>> files =
213             new ArrayList<ObjectValuePair<String, byte[]>>();
214 
215         if (attachments) {
216             UploadPortletRequest uploadRequest =
217                 PortalUtil.getUploadPortletRequest(actionRequest);
218 
219             for (int i = 1; i <= 5; i++) {
220                 File file = uploadRequest.getFile("msgFile" + i);
221                 String fileName = uploadRequest.getFileName("msgFile" + i);
222                 byte[] bytes = FileUtil.getBytes(file);
223 
224                 if ((bytes != null) && (bytes.length > 0)) {
225                     ObjectValuePair<String, byte[]> ovp =
226                         new ObjectValuePair<String, byte[]>(fileName, bytes);
227 
228                     files.add(ovp);
229                 }
230             }
231         }
232 
233         boolean question = ParamUtil.getBoolean(actionRequest, "question");
234         boolean anonymous = ParamUtil.getBoolean(actionRequest, "anonymous");
235         double priority = ParamUtil.getDouble(actionRequest, "priority");
236         boolean allowPingbacks = ParamUtil.getBoolean(
237             actionRequest, "allowPingbacks");
238 
239         ServiceContext serviceContext = ServiceContextFactory.getInstance(
240             MBMessage.class.getName(), actionRequest);
241 
242         MBMessage message = null;
243 
244         if (messageId <= 0) {
245             if (PropsValues.CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_MESSAGE) {
246                 CaptchaUtil.check(actionRequest);
247             }
248 
249             if (threadId <= 0) {
250 
251                 // Post new thread
252 
253                 message = MBMessageServiceUtil.addMessage(
254                     groupId, categoryId, subject, body, files, anonymous,
255                     priority, allowPingbacks, serviceContext);
256 
257                 if (question) {
258                     MBMessageFlagLocalServiceUtil.addQuestionFlag(
259                         message.getMessageId());
260                 }
261             }
262             else {
263 
264                 // Post reply
265 
266                 message = MBMessageServiceUtil.addMessage(
267                     groupId, categoryId, threadId, parentMessageId, subject,
268                     body, files, anonymous, priority, allowPingbacks,
269                     serviceContext);
270             }
271         }
272         else {
273             List<String> existingFiles = new ArrayList<String>();
274 
275             for (int i = 1; i <= 5; i++) {
276                 String path = ParamUtil.getString(
277                     actionRequest, "existingPath" + i);
278 
279                 if (Validator.isNotNull(path)) {
280                     existingFiles.add(path);
281                 }
282             }
283 
284             // Update message
285 
286             message = MBMessageServiceUtil.updateMessage(
287                 messageId, subject, body, files, existingFiles, priority,
288                 allowPingbacks, serviceContext);
289 
290             if (message.isRoot()) {
291                 if (question) {
292                     MBMessageFlagLocalServiceUtil.addQuestionFlag(messageId);
293                 }
294                 else {
295                     MBMessageFlagLocalServiceUtil.deleteQuestionAndAnswerFlags(
296                         message.getThreadId());
297                 }
298             }
299         }
300 
301         PortletURL portletURL =
302             ((ActionResponseImpl)actionResponse).createRenderURL();
303 
304         portletURL.setParameter(
305             "struts_action", "/message_boards/view_message");
306         portletURL.setParameter(
307             "messageId", String.valueOf(message.getMessageId()));
308 
309         actionResponse.sendRedirect(portletURL.toString());
310     }
311 
312 }