1   /**
2    * Copyright (c) 2000-2008 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.captcha.CaptchaTextException;
28  import com.liferay.portal.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.StringUtil;
36  import com.liferay.portal.kernel.util.Validator;
37  import com.liferay.portal.security.auth.PrincipalException;
38  import com.liferay.portal.struts.PortletAction;
39  import com.liferay.portal.theme.ThemeDisplay;
40  import com.liferay.portal.util.PortalUtil;
41  import com.liferay.portal.util.PropsValues;
42  import com.liferay.portal.util.WebKeys;
43  import com.liferay.portlet.ActionResponseImpl;
44  import com.liferay.portlet.messageboards.MessageBodyException;
45  import com.liferay.portlet.messageboards.MessageSubjectException;
46  import com.liferay.portlet.messageboards.NoSuchMessageException;
47  import com.liferay.portlet.messageboards.RequiredMessageException;
48  import com.liferay.portlet.messageboards.model.MBMessage;
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.PortletPreferences;
61  import javax.portlet.PortletURL;
62  import javax.portlet.RenderRequest;
63  import javax.portlet.RenderResponse;
64  
65  import org.apache.struts.action.ActionForm;
66  import org.apache.struts.action.ActionForward;
67  import org.apache.struts.action.ActionMapping;
68  
69  /**
70   * <a href="EditMessageAction.java.html"><b><i>View Source</i></b></a>
71   *
72   * @author Brian Wing Shun Chan
73   *
74   */
75  public class EditMessageAction extends PortletAction {
76  
77      public void processAction(
78              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
79              ActionRequest actionRequest, ActionResponse actionResponse)
80          throws Exception {
81  
82          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
83  
84          try {
85              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
86                  updateMessage(actionRequest, actionResponse);
87              }
88              else if (cmd.equals(Constants.DELETE)) {
89                  deleteMessage(actionRequest);
90              }
91              else if (cmd.equals(Constants.SUBSCRIBE)) {
92                  subscribeMessage(actionRequest);
93              }
94              else if (cmd.equals(Constants.UNSUBSCRIBE)) {
95                  unsubscribeMessage(actionRequest);
96              }
97  
98              if (cmd.equals(Constants.DELETE) ||
99                  cmd.equals(Constants.SUBSCRIBE) ||
100                 cmd.equals(Constants.UNSUBSCRIBE)) {
101 
102                 sendRedirect(actionRequest, actionResponse);
103             }
104         }
105         catch (Exception e) {
106             if (e instanceof NoSuchMessageException ||
107                 e instanceof PrincipalException ||
108                 e instanceof RequiredMessageException) {
109 
110                 SessionErrors.add(actionRequest, e.getClass().getName());
111 
112                 setForward(actionRequest, "portlet.message_boards.error");
113             }
114             else if (e instanceof CaptchaTextException ||
115                      e instanceof FileNameException ||
116                      e instanceof FileSizeException ||
117                      e instanceof MessageBodyException ||
118                      e instanceof MessageSubjectException) {
119 
120                 SessionErrors.add(actionRequest, e.getClass().getName());
121             }
122             else if (e instanceof TagsEntryException) {
123                 SessionErrors.add(actionRequest, e.getClass().getName(), e);
124             }
125             else {
126                 throw e;
127             }
128         }
129     }
130 
131     public ActionForward render(
132             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
133             RenderRequest renderRequest, RenderResponse renderResponse)
134         throws Exception {
135 
136         try {
137             ActionUtil.getMessage(renderRequest);
138         }
139         catch (Exception e) {
140             if (e instanceof NoSuchMessageException ||
141                 e instanceof PrincipalException) {
142 
143                 SessionErrors.add(renderRequest, e.getClass().getName());
144 
145                 return mapping.findForward("portlet.message_boards.error");
146             }
147             else {
148                 throw e;
149             }
150         }
151 
152         return mapping.findForward(
153             getForward(renderRequest, "portlet.message_boards.edit_message"));
154     }
155 
156     protected void deleteMessage(ActionRequest actionRequest) throws Exception {
157         long messageId = ParamUtil.getLong(actionRequest, "messageId");
158 
159         MBMessageServiceUtil.deleteMessage(messageId);
160     }
161 
162     protected void subscribeMessage(ActionRequest actionRequest)
163         throws Exception {
164 
165         long messageId = ParamUtil.getLong(actionRequest, "messageId");
166 
167         MBMessageServiceUtil.subscribeMessage(messageId);
168     }
169 
170     protected void unsubscribeMessage(ActionRequest actionRequest)
171         throws Exception {
172 
173         long messageId = ParamUtil.getLong(actionRequest, "messageId");
174 
175         MBMessageServiceUtil.unsubscribeMessage(messageId);
176     }
177 
178     protected void updateMessage(
179             ActionRequest actionRequest, ActionResponse actionResponse)
180         throws Exception {
181 
182         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
183             WebKeys.THEME_DISPLAY);
184 
185         PortletPreferences prefs = actionRequest.getPreferences();
186 
187         long messageId = ParamUtil.getLong(actionRequest, "messageId");
188 
189         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
190         long threadId = ParamUtil.getLong(actionRequest, "threadId");
191         long parentMessageId = ParamUtil.getLong(
192             actionRequest, "parentMessageId");
193         String subject = ParamUtil.getString(actionRequest, "subject");
194         String body = ParamUtil.getString(actionRequest, "body");
195         boolean attachments = ParamUtil.getBoolean(
196             actionRequest, "attachments");
197 
198         List<ObjectValuePair<String, byte[]>> files =
199             new ArrayList<ObjectValuePair<String, byte[]>>();
200 
201         if (attachments) {
202             UploadPortletRequest uploadRequest =
203                 PortalUtil.getUploadPortletRequest(actionRequest);
204 
205             for (int i = 1; i <= 5; i++) {
206                 File file = uploadRequest.getFile("msgFile" + i);
207                 String fileName = uploadRequest.getFileName("msgFile" + i);
208                 byte[] bytes = FileUtil.getBytes(file);
209 
210                 if ((bytes != null) && (bytes.length > 0)) {
211                     ObjectValuePair<String, byte[]> ovp =
212                         new ObjectValuePair<String, byte[]>(fileName, bytes);
213 
214                     files.add(ovp);
215                 }
216             }
217         }
218 
219         boolean anonymous = ParamUtil.getBoolean(actionRequest, "anonymous");
220         double priority = ParamUtil.getDouble(actionRequest, "priority");
221 
222         String[] tagsEntries = StringUtil.split(
223             ParamUtil.getString(actionRequest, "tagsEntries"));
224 
225         String[] communityPermissions = actionRequest.getParameterValues(
226             "communityPermissions");
227         String[] guestPermissions = actionRequest.getParameterValues(
228             "guestPermissions");
229 
230         MBMessage message = null;
231 
232         if (messageId <= 0) {
233             if (PropsValues.CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_MESSAGE) {
234                 CaptchaUtil.check(actionRequest);
235             }
236 
237             if (threadId <= 0) {
238 
239                 // Post new thread
240 
241                 message = MBMessageServiceUtil.addMessage(
242                     categoryId, subject, body, files, anonymous, priority,
243                     tagsEntries, prefs, communityPermissions, guestPermissions,
244                     themeDisplay);
245             }
246             else {
247 
248                 // Post reply
249 
250                 message = MBMessageServiceUtil.addMessage(
251                     categoryId, threadId, parentMessageId, subject, body, files,
252                     anonymous, priority, tagsEntries, prefs,
253                     communityPermissions, guestPermissions, themeDisplay);
254             }
255         }
256         else {
257             List<String> existingFiles = new ArrayList<String>();
258 
259             for (int i = 1; i <= 5; i++) {
260                 String path = ParamUtil.getString(
261                     actionRequest, "existingPath" + i);
262 
263                 if (Validator.isNotNull(path)) {
264                     existingFiles.add(path);
265                 }
266             }
267 
268             // Update message
269 
270             message = MBMessageServiceUtil.updateMessage(
271                 messageId, subject, body, files, existingFiles, priority,
272                 tagsEntries, prefs, themeDisplay);
273         }
274 
275         PortletURL portletURL =
276             ((ActionResponseImpl)actionResponse).createRenderURL();
277 
278         portletURL.setParameter(
279             "struts_action", "/message_boards/view_message");
280         portletURL.setParameter(
281             "messageId", String.valueOf(message.getMessageId()));
282 
283         actionResponse.sendRedirect(portletURL.toString());
284     }
285 
286 }