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