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