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