1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.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  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         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
182             WebKeys.THEME_DISPLAY);
183 
184         PortletPreferences prefs = actionRequest.getPreferences();
185 
186         long messageId = ParamUtil.getLong(actionRequest, "messageId");
187 
188         long categoryId = ParamUtil.getLong(actionRequest, "categoryId");
189         long threadId = ParamUtil.getLong(actionRequest, "threadId");
190         long parentMessageId = ParamUtil.getLong(
191             actionRequest, "parentMessageId");
192         String subject = ParamUtil.getString(actionRequest, "subject");
193         String body = ParamUtil.getString(actionRequest, "body");
194         boolean attachments = ParamUtil.getBoolean(
195             actionRequest, "attachments");
196 
197         List<ObjectValuePair<String, byte[]>> files =
198             new ArrayList<ObjectValuePair<String, byte[]>>();
199 
200         if (attachments) {
201             UploadPortletRequest uploadRequest =
202                 PortalUtil.getUploadPortletRequest(actionRequest);
203 
204             for (int i = 1; i <= 5; i++) {
205                 File file = uploadRequest.getFile("msgFile" + i);
206                 String fileName = uploadRequest.getFileName("msgFile" + i);
207                 byte[] bytes = FileUtil.getBytes(file);
208 
209                 if ((bytes != null) && (bytes.length > 0)) {
210                     ObjectValuePair<String, byte[]> ovp =
211                         new ObjectValuePair<String, byte[]>(fileName, bytes);
212 
213                     files.add(ovp);
214                 }
215             }
216         }
217 
218         boolean anonymous = ParamUtil.getBoolean(actionRequest, "anonymous");
219         double priority = ParamUtil.getDouble(actionRequest, "priority");
220 
221         String[] tagsEntries = StringUtil.split(
222             ParamUtil.getString(actionRequest, "tagsEntries"));
223 
224         String[] communityPermissions = PortalUtil.getCommunityPermissions(
225             actionRequest);
226         String[] guestPermissions = PortalUtil.getGuestPermissions(
227             actionRequest);
228 
229         MBMessage message = null;
230 
231         if (messageId <= 0) {
232             if (PropsValues.CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_MESSAGE) {
233                 CaptchaUtil.check(actionRequest);
234             }
235 
236             if (threadId <= 0) {
237 
238                 // Post new thread
239 
240                 message = MBMessageServiceUtil.addMessage(
241                     categoryId, subject, body, files, anonymous, priority,
242                     tagsEntries, prefs, communityPermissions, guestPermissions,
243                     themeDisplay);
244             }
245             else {
246 
247                 // Post reply
248 
249                 message = MBMessageServiceUtil.addMessage(
250                     categoryId, threadId, parentMessageId, subject, body, files,
251                     anonymous, priority, tagsEntries, prefs,
252                     communityPermissions, guestPermissions, themeDisplay);
253             }
254         }
255         else {
256             List<String> existingFiles = new ArrayList<String>();
257 
258             for (int i = 1; i <= 5; i++) {
259                 String path = ParamUtil.getString(
260                     actionRequest, "existingPath" + i);
261 
262                 if (Validator.isNotNull(path)) {
263                     existingFiles.add(path);
264                 }
265             }
266 
267             // Update message
268 
269             message = MBMessageServiceUtil.updateMessage(
270                 messageId, subject, body, files, existingFiles, priority,
271                 tagsEntries, prefs, themeDisplay);
272         }
273 
274         PortletURL portletURL =
275             ((ActionResponseImpl)actionResponse).createRenderURL();
276 
277         portletURL.setParameter(
278             "struts_action", "/message_boards/view_message");
279         portletURL.setParameter(
280             "messageId", String.valueOf(message.getMessageId()));
281 
282         actionResponse.sendRedirect(portletURL.toString());
283     }
284 
285 }