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