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