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.servlet.SessionErrors;
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.struts.PortletAction;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.ActionResponseImpl;
33  import com.liferay.portlet.messageboards.MessageBodyException;
34  import com.liferay.portlet.messageboards.MessageSubjectException;
35  import com.liferay.portlet.messageboards.NoSuchMessageException;
36  import com.liferay.portlet.messageboards.RequiredMessageException;
37  import com.liferay.portlet.messageboards.model.MBMessage;
38  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
39  
40  import javax.portlet.ActionRequest;
41  import javax.portlet.ActionResponse;
42  import javax.portlet.PortletConfig;
43  
44  import org.apache.struts.action.ActionForm;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditDiscussionAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class EditDiscussionAction extends PortletAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          ActionResponseImpl actionResponseImpl =
61              (ActionResponseImpl)actionResponse;
62  
63          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
64  
65          try {
66              String redirect = ParamUtil.getString(actionRequest, "redirect");
67  
68              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
69                  MBMessage message = updateMessage(actionRequest);
70  
71                  redirect +=
72                      "#" + actionResponseImpl.getNamespace() + "messageScroll" +
73                          message.getMessageId();
74              }
75              else if (cmd.equals(Constants.DELETE)) {
76                  deleteMessage(actionRequest);
77              }
78  
79              sendRedirect(actionRequest, actionResponse, redirect);
80          }
81          catch (Exception e) {
82              if (e instanceof NoSuchMessageException ||
83                  e instanceof PrincipalException ||
84                  e instanceof RequiredMessageException) {
85  
86                  SessionErrors.add(actionRequest, e.getClass().getName());
87  
88                  setForward(actionRequest, "portlet.message_boards.error");
89              }
90              else if (e instanceof FileNameException ||
91                       e instanceof FileSizeException ||
92                       e instanceof MessageBodyException ||
93                       e instanceof MessageSubjectException) {
94  
95                  SessionErrors.add(actionRequest, e.getClass().getName());
96  
97                  sendRedirect(actionRequest, actionResponse);
98              }
99              else {
100                 throw e;
101             }
102         }
103     }
104 
105     protected void deleteMessage(ActionRequest actionRequest) throws Exception {
106         long groupId = PortalUtil.getScopeGroupId(actionRequest);
107         String className = ParamUtil.getString(actionRequest, "className");
108         long classPK = ParamUtil.getLong(actionRequest, "classPK");
109 
110         long messageId = ParamUtil.getLong(actionRequest, "messageId");
111 
112         MBMessageServiceUtil.deleteDiscussionMessage(
113             groupId, className, classPK, messageId);
114     }
115 
116     protected boolean isCheckMethodOnProcessAction() {
117         return _CHECK_METHOD_ON_PROCESS_ACTION;
118     }
119 
120     protected MBMessage updateMessage(ActionRequest actionRequest)
121         throws Exception {
122 
123         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
124             WebKeys.THEME_DISPLAY);
125 
126         long groupId = PortalUtil.getScopeGroupId(actionRequest);
127         String className = ParamUtil.getString(actionRequest, "className");
128         long classPK = ParamUtil.getLong(actionRequest, "classPK");
129 
130         long messageId = ParamUtil.getLong(actionRequest, "messageId");
131 
132         long threadId = ParamUtil.getLong(actionRequest, "threadId");
133         long parentMessageId = ParamUtil.getLong(
134             actionRequest, "parentMessageId");
135         String subject = ParamUtil.getString(actionRequest, "subject");
136         String body = ParamUtil.getString(actionRequest, "body");
137 
138         MBMessage message = null;
139 
140         if (messageId <= 0) {
141 
142             // Add message
143 
144             message = MBMessageServiceUtil.addDiscussionMessage(
145                 groupId, className, classPK, threadId, parentMessageId, subject,
146                 body, themeDisplay);
147         }
148         else {
149 
150             // Update message
151 
152             message = MBMessageServiceUtil.updateDiscussionMessage(
153                 groupId, className, classPK, messageId, subject, body);
154         }
155 
156         return message;
157     }
158 
159     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
160 
161 }