1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.action;
24  
25  import com.liferay.documentlibrary.FileNameException;
26  import com.liferay.documentlibrary.FileSizeException;
27  import com.liferay.portal.kernel.captcha.CaptchaTextException;
28  import com.liferay.portal.kernel.captcha.CaptchaUtil;
29  import com.liferay.portal.kernel.servlet.SessionErrors;
30  import com.liferay.portal.kernel.upload.UploadPortletRequest;
31  import com.liferay.portal.kernel.util.Constants;
32  import com.liferay.portal.kernel.util.FileUtil;
33  import com.liferay.portal.kernel.util.ObjectValuePair;
34  import com.liferay.portal.kernel.util.ParamUtil;
35  import com.liferay.portal.kernel.util.Validator;
36  import com.liferay.portal.security.auth.PrincipalException;
37  import com.liferay.portal.service.ServiceContext;
38  import com.liferay.portal.service.ServiceContextFactory;
39  import com.liferay.portal.struts.PortletAction;
40  import com.liferay.portal.util.PortalUtil;
41  import com.liferay.portal.util.PropsValues;
42  import com.liferay.portlet.ActionResponseImpl;
43  import com.liferay.portlet.messageboards.MessageBodyException;
44  import com.liferay.portlet.messageboards.MessageSubjectException;
45  import com.liferay.portlet.messageboards.NoSuchMessageException;
46  import com.liferay.portlet.messageboards.RequiredMessageException;
47  import com.liferay.portlet.messageboards.model.MBMessage;
48  import com.liferay.portlet.messageboards.service.MBMessageFlagLocalServiceUtil;
49  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
50  import com.liferay.portlet.tags.TagsEntryException;
51  
52  import java.io.File;
53  
54  import java.util.ArrayList;
55  import java.util.List;
56  
57  import javax.portlet.ActionRequest;
58  import javax.portlet.ActionResponse;
59  import javax.portlet.PortletConfig;
60  import javax.portlet.PortletURL;
61  import javax.portlet.RenderRequest;
62  import javax.portlet.RenderResponse;
63  
64  import org.apache.struts.action.ActionForm;
65  import org.apache.struts.action.ActionForward;
66  import org.apache.struts.action.ActionMapping;
67  
68  /**
69   * <a href="EditMessageAction.java.html"><b><i>View Source</i></b></a>
70   *
71   * @author Brian Wing Shun Chan
72   *
73   */
74  public class EditMessageAction extends PortletAction {
75  
76      public void processAction(
77              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
78              ActionRequest actionRequest, ActionResponse actionResponse)
79          throws Exception {
80  
81          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
82  
83          try {
84              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
85                  updateMessage(actionRequest, actionResponse);
86              }
87              else if (cmd.equals(Constants.DELETE)) {
88                  deleteMessage(actionRequest);
89              }
90              else if (cmd.equals(Constants.SUBSCRIBE)) {
91                  subscribeMessage(actionRequest);
92              }
93              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
94                  unsubscribeMessage(actionRequest);
95              }
96  
97              if (cmd.equals(Constants.DELETE) ||
98                  cmd.equals(Constants.SUBSCRIBE) ||
99                  cmd.equals(Constants.UNSUBSCRIBE)) {
100 
101                 sendRedirect(actionRequest, actionResponse);
102             }
103         }
104         catch (Exception e) {
105             if (e instanceof NoSuchMessageException ||
106                 e instanceof PrincipalException ||
107                 e instanceof RequiredMessageException) {
108 
109                 SessionErrors.add(actionRequest, e.getClass().getName());
110 
111                 setForward(actionRequest, "portlet.message_boards.error");
112             }
113             else if (e instanceof CaptchaTextException ||
114                      e instanceof FileNameException ||
115                      e instanceof FileSizeException ||
116                      e instanceof MessageBodyException ||
117                      e instanceof MessageSubjectException) {
118 
119                 SessionErrors.add(actionRequest, e.getClass().getName());
120             }
121             else if (e instanceof TagsEntryException) {
122                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
123             }
124             else {
125                 throw e;
126             }
127         }
128     }
129 
130     public ActionForward render(
131             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
132             RenderRequest renderRequest, RenderResponse renderResponse)
133         throws Exception {
134 
135         try {
136             ActionUtil.getMessage(renderRequest);
137         }
138         catch (Exception e) {
139             if (e instanceof NoSuchMessageException ||
140                 e instanceof PrincipalException) {
141 
142                 SessionErrors.add(renderRequest, e.getClass().getName());
143 
144                 return mapping.findForward("portlet.message_boards.error");
145             }
146             else {
147                 throw e;
148             }
149         }
150 
151         return mapping.findForward(
152             getForward(renderRequest, "portlet.message_boards.edit_message"));
153     }
154 
155     protected void deleteMessage(ActionRequest actionRequest) throws Exception {
156         long messageId = ParamUtil.getLong(actionRequest, "messageId");
157 
158         MBMessageServiceUtil.deleteMessage(messageId);
159     }
160 
161     protected void subscribeMessage(ActionRequest actionRequest)
162         throws Exception {
163 
164         long messageId = ParamUtil.getLong(actionRequest, "messageId");
165 
166         MBMessageServiceUtil.subscribeMessage(messageId);
167     }
168 
169     protected void unsubscribeMessage(ActionRequest actionRequest)
170         throws Exception {
171 
172         long messageId = ParamUtil.getLong(actionRequest, "messageId");
173 
174         MBMessageServiceUtil.unsubscribeMessage(messageId);
175     }
176 
177     protected void updateMessage(
178             ActionRequest actionRequest, ActionResponse actionResponse)
179         throws Exception {
180 
181         long messageId = ParamUtil.getLong(actionRequest, "messageId");
182 
183         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
184         long threadId = ParamUtil.getLong(actionRequest, "threadId");
185         long parentMessageId = ParamUtil.getLong(
186             actionRequest, "parentMessageId");
187         String subject = ParamUtil.getString(actionRequest, "subject");
188         String body = ParamUtil.getString(actionRequest, "body");
189         boolean attachments = ParamUtil.getBoolean(
190             actionRequest, "attachments");
191 
192         List<ObjectValuePair<String, byte[]>> files =
193             new ArrayList<ObjectValuePair<String, byte[]>>();
194 
195         if (attachments) {
196             UploadPortletRequest uploadRequest =
197                 PortalUtil.getUploadPortletRequest(actionRequest);
198 
199             for (int i = 1; i <= 5; i++) {
200                 File file = uploadRequest.getFile("msgFile" + i);
201                 String fileName = uploadRequest.getFileName("msgFile" + i);
202                 byte[] bytes = FileUtil.getBytes(file);
203 
204                 if ((bytes != null) && (bytes.length > 0)) {
205                     ObjectValuePair<String, byte[]> ovp =
206                         new ObjectValuePair<String, byte[]>(fileName, bytes);
207 
208                     files.add(ovp);
209                 }
210             }
211         }
212 
213         boolean question = ParamUtil.getBoolean(actionRequest, "question");
214         boolean anonymous = ParamUtil.getBoolean(actionRequest, "anonymous");
215         double priority = ParamUtil.getDouble(actionRequest, "priority");
216 
217         ServiceContext serviceContext = ServiceContextFactory.getInstance(
218             MBMessage.class.getName(), actionRequest);
219 
220         MBMessage message = null;
221 
222         if (messageId <= 0) {
223             if (PropsValues.CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_MESSAGE) {
224                 CaptchaUtil.check(actionRequest);
225             }
226 
227             if (threadId <= 0) {
228 
229                 // Post new thread
230 
231                 message = MBMessageServiceUtil.addMessage(
232                     categoryId, subject, body, files, anonymous, priority,
233                     serviceContext);
234 
235                 if (question) {
236                     MBMessageFlagLocalServiceUtil.addQuestionFlag(
237                         message.getMessageId());
238                 }
239             }
240             else {
241 
242                 // Post reply
243 
244                 message = MBMessageServiceUtil.addMessage(
245                     categoryId, threadId, parentMessageId, subject, body, files,
246                     anonymous, priority, serviceContext);
247             }
248         }
249         else {
250             List<String> existingFiles = new ArrayList<String>();
251 
252             for (int i = 1; i <= 5; i++) {
253                 String path = ParamUtil.getString(
254                     actionRequest, "existingPath" + i);
255 
256                 if (Validator.isNotNull(path)) {
257                     existingFiles.add(path);
258                 }
259             }
260 
261             // Update message
262 
263             message = MBMessageServiceUtil.updateMessage(
264                 messageId, subject, body, files, existingFiles, priority,
265                 serviceContext);
266 
267             if (message.isRoot()) {
268                 if (question) {
269                     MBMessageFlagLocalServiceUtil.addQuestionFlag(messageId);
270                 }
271                 else {
272                     MBMessageFlagLocalServiceUtil.deleteQuestionAndAnswerFlags(
273                         message.getThreadId());
274                 }
275             }
276         }
277 
278         PortletURL portletURL =
279             ((ActionResponseImpl)actionResponse).createRenderURL();
280 
281         portletURL.setParameter(
282             "struts_action", "/message_boards/view_message");
283         portletURL.setParameter(
284             "messageId", String.valueOf(message.getMessageId()));
285 
286         actionResponse.sendRedirect(portletURL.toString());
287     }
288 
289 }