1   /**
2    * Copyright (c) 2000-2010 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   *
12   * 
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.struts.PortletAction;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portal.util.WebKeys;
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         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
118             WebKeys.THEME_DISPLAY);
119 
120         long groupId = PortalUtil.getScopeGroupId(actionRequest);
121         String className = ParamUtil.getString(actionRequest, "className");
122         long classPK = ParamUtil.getLong(actionRequest, "classPK");
123 
124         long messageId = ParamUtil.getLong(actionRequest, "messageId");
125 
126         long threadId = ParamUtil.getLong(actionRequest, "threadId");
127         long parentMessageId = ParamUtil.getLong(
128             actionRequest, "parentMessageId");
129         String subject = ParamUtil.getString(actionRequest, "subject");
130         String body = ParamUtil.getString(actionRequest, "body");
131 
132         MBMessage message = null;
133 
134         if (messageId <= 0) {
135 
136             // Add message
137 
138             message = MBMessageServiceUtil.addDiscussionMessage(
139                 groupId, className, classPK, threadId, parentMessageId, subject,
140                 body, themeDisplay);
141         }
142         else {
143 
144             // Update message
145 
146             message = MBMessageServiceUtil.updateDiscussionMessage(
147                 groupId, className, classPK, messageId, subject, body);
148         }
149 
150         return message;
151     }
152 
153     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
154 
155 }