1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.action;
16  
17  import com.liferay.documentlibrary.FileNameException;
18  import com.liferay.documentlibrary.FileSizeException;
19  import com.liferay.portal.kernel.servlet.SessionErrors;
20  import com.liferay.portal.kernel.util.Constants;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.security.auth.PrincipalException;
23  import com.liferay.portal.service.ServiceContext;
24  import com.liferay.portal.service.ServiceContextFactory;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portlet.ActionResponseImpl;
28  import com.liferay.portlet.messageboards.MessageBodyException;
29  import com.liferay.portlet.messageboards.MessageSubjectException;
30  import com.liferay.portlet.messageboards.NoSuchMessageException;
31  import com.liferay.portlet.messageboards.RequiredMessageException;
32  import com.liferay.portlet.messageboards.model.MBMessage;
33  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
34  
35  import javax.portlet.ActionRequest;
36  import javax.portlet.ActionResponse;
37  import javax.portlet.PortletConfig;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="EditDiscussionAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   */
47  public class EditDiscussionAction extends PortletAction {
48  
49      public void processAction(
50              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
51              ActionRequest actionRequest, ActionResponse actionResponse)
52          throws Exception {
53  
54          ActionResponseImpl actionResponseImpl =
55              (ActionResponseImpl)actionResponse;
56  
57          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
58  
59          try {
60              String redirect = ParamUtil.getString(actionRequest, "redirect");
61  
62              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
63                  MBMessage message = updateMessage(actionRequest);
64  
65                  redirect +=
66                      "#" + actionResponseImpl.getNamespace() + "messageScroll" +
67                          message.getMessageId();
68              }
69              else if (cmd.equals(Constants.DELETE)) {
70                  deleteMessage(actionRequest);
71              }
72  
73              sendRedirect(actionRequest, actionResponse, redirect);
74          }
75          catch (Exception e) {
76              if (e instanceof NoSuchMessageException ||
77                  e instanceof PrincipalException ||
78                  e instanceof RequiredMessageException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81  
82                  setForward(actionRequest, "portlet.message_boards.error");
83              }
84              else if (e instanceof FileNameException ||
85                       e instanceof FileSizeException ||
86                       e instanceof MessageBodyException ||
87                       e instanceof MessageSubjectException) {
88  
89                  SessionErrors.add(actionRequest, e.getClass().getName());
90  
91                  sendRedirect(actionRequest, actionResponse);
92              }
93              else {
94                  throw e;
95              }
96          }
97      }
98  
99      protected void deleteMessage(ActionRequest actionRequest) throws Exception {
100         long groupId = PortalUtil.getScopeGroupId(actionRequest);
101         String className = ParamUtil.getString(actionRequest, "className");
102         long classPK = ParamUtil.getLong(actionRequest, "classPK");
103 
104         long messageId = ParamUtil.getLong(actionRequest, "messageId");
105 
106         MBMessageServiceUtil.deleteDiscussionMessage(
107             groupId, className, classPK, messageId);
108     }
109 
110     protected boolean isCheckMethodOnProcessAction() {
111         return _CHECK_METHOD_ON_PROCESS_ACTION;
112     }
113 
114     protected MBMessage updateMessage(ActionRequest actionRequest)
115         throws Exception {
116 
117         String className = ParamUtil.getString(actionRequest, "className");
118         long classPK = ParamUtil.getLong(actionRequest, "classPK");
119 
120         long messageId = ParamUtil.getLong(actionRequest, "messageId");
121 
122         long threadId = ParamUtil.getLong(actionRequest, "threadId");
123         long parentMessageId = ParamUtil.getLong(
124             actionRequest, "parentMessageId");
125         String subject = ParamUtil.getString(actionRequest, "subject");
126         String body = ParamUtil.getString(actionRequest, "body");
127 
128         ServiceContext serviceContext = ServiceContextFactory.getInstance(
129             MBMessage.class.getName(), actionRequest);
130 
131         MBMessage message = null;
132 
133         if (messageId <= 0) {
134 
135             // Add message
136 
137             message = MBMessageServiceUtil.addDiscussionMessage(
138                 className, classPK, threadId, parentMessageId, subject, body,
139                 serviceContext);
140         }
141         else {
142 
143             // Update message
144 
145             message = MBMessageServiceUtil.updateDiscussionMessage(
146                 className, classPK, messageId, subject, body, serviceContext);
147         }
148 
149         return message;
150     }
151 
152     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
153 
154 }