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