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