1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.struts.PortletAction;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.PortalUtil;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.ActionResponseImpl;
35  import com.liferay.portlet.messageboards.MessageBodyException;
36  import com.liferay.portlet.messageboards.MessageSubjectException;
37  import com.liferay.portlet.messageboards.NoSuchMessageException;
38  import com.liferay.portlet.messageboards.RequiredMessageException;
39  import com.liferay.portlet.messageboards.model.MBMessage;
40  import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
41  import com.liferay.util.servlet.SessionErrors;
42  
43  import javax.portlet.ActionRequest;
44  import javax.portlet.ActionResponse;
45  import javax.portlet.PortletConfig;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="EditDiscussionAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Brian Wing Shun Chan
54   *
55   */
56  public class EditDiscussionAction extends PortletAction {
57  
58      public void processAction(
59              ActionMapping mapping, ActionForm form, PortletConfig config,
60              ActionRequest req, ActionResponse res)
61          throws Exception {
62  
63          ActionResponseImpl resImpl = (ActionResponseImpl)res;
64  
65          String cmd = ParamUtil.getString(req, Constants.CMD);
66  
67          try {
68              String redirect = ParamUtil.getString(req, "redirect");
69  
70              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
71                  MBMessage message = updateMessage(req);
72  
73                  redirect +=
74                      "#" + resImpl.getNamespace() + "messageScroll" +
75                          message.getMessageId();
76              }
77              else if (cmd.equals(Constants.DELETE)) {
78                  deleteMessage(req);
79              }
80  
81              sendRedirect(req, res, redirect);
82          }
83          catch (Exception e) {
84              if (e instanceof NoSuchMessageException ||
85                  e instanceof PrincipalException ||
86                  e instanceof RequiredMessageException) {
87  
88                  SessionErrors.add(req, e.getClass().getName());
89  
90                  setForward(req, "portlet.message_boards.error");
91              }
92              else if (e instanceof FileNameException ||
93                       e instanceof FileSizeException ||
94                       e instanceof MessageBodyException ||
95                       e instanceof MessageSubjectException) {
96  
97                  SessionErrors.add(req, e.getClass().getName());
98  
99                  sendRedirect(req, res);
100             }
101             else {
102                 throw e;
103             }
104         }
105     }
106 
107     protected void deleteMessage(ActionRequest req) throws Exception {
108         long groupId = PortalUtil.getPortletGroupId(req);
109         String className = ParamUtil.getString(req, "className");
110         long classPK = ParamUtil.getLong(req, "classPK");
111 
112         long messageId = ParamUtil.getLong(req, "messageId");
113 
114         MBMessageServiceUtil.deleteDiscussionMessage(
115             groupId, className, classPK, messageId);
116     }
117 
118     protected MBMessage updateMessage(ActionRequest req) throws Exception {
119         ThemeDisplay themeDisplay =
120             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
121 
122         long groupId = PortalUtil.getPortletGroupId(req);
123         String className = ParamUtil.getString(req, "className");
124         long classPK = ParamUtil.getLong(req, "classPK");
125 
126         long messageId = ParamUtil.getLong(req, "messageId");
127 
128         long threadId = ParamUtil.getLong(req, "threadId");
129         long parentMessageId = ParamUtil.getLong(req, "parentMessageId");
130         String subject = ParamUtil.getString(req, "subject");
131         String body = ParamUtil.getString(req, "body");
132 
133         MBMessage message = null;
134 
135         if (messageId <= 0) {
136 
137             // Add message
138 
139             message = MBMessageServiceUtil.addDiscussionMessage(
140                 groupId, className, classPK, threadId, parentMessageId, subject,
141                 body, themeDisplay);
142         }
143         else {
144 
145             // Update message
146 
147             message = MBMessageServiceUtil.updateDiscussionMessage(
148                 groupId, className, classPK, messageId, subject, body);
149         }
150 
151         return message;
152     }
153 
154 }