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.service.ServiceContext;
24  import com.liferay.portal.service.ServiceContextFactory;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portal.util.PortalUtil;
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         long ownerId = ParamUtil.getLong(actionRequest, "ownerId");
104 
105         long messageId = ParamUtil.getLong(actionRequest, "messageId");
106 
107         MBMessageServiceUtil.deleteDiscussionMessage(
108             groupId, className, classPK, ownerId, messageId);
109     }
110 
111     protected boolean isCheckMethodOnProcessAction() {
112         return _CHECK_METHOD_ON_PROCESS_ACTION;
113     }
114 
115     protected MBMessage updateMessage(ActionRequest actionRequest)
116         throws Exception {
117 
118         String className = ParamUtil.getString(actionRequest, "className");
119         long classPK = ParamUtil.getLong(actionRequest, "classPK");
120         long ownerId = ParamUtil.getLong(actionRequest, "ownerId");
121 
122         long messageId = ParamUtil.getLong(actionRequest, "messageId");
123 
124         long threadId = ParamUtil.getLong(actionRequest, "threadId");
125         long parentMessageId = ParamUtil.getLong(
126             actionRequest, "parentMessageId");
127         String subject = ParamUtil.getString(actionRequest, "subject");
128         String body = ParamUtil.getString(actionRequest, "body");
129 
130         ServiceContext serviceContext = ServiceContextFactory.getInstance(
131             MBMessage.class.getName(), actionRequest);
132 
133         MBMessage message = null;
134 
135         if (messageId <= 0) {
136 
137             // Add message
138 
139             message = MBMessageServiceUtil.addDiscussionMessage(
140                 className, classPK, ownerId, threadId, parentMessageId, subject,
141                 body, serviceContext);
142         }
143         else {
144 
145             // Update message
146 
147             message = MBMessageServiceUtil.updateDiscussionMessage(
148                 className, classPK, ownerId, messageId, subject, body,
149                 serviceContext);
150         }
151 
152         return message;
153     }
154 
155     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
156 
157 }