001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.action;
016    
017    import com.liferay.documentlibrary.FileNameException;
018    import com.liferay.documentlibrary.FileSizeException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.ServiceContextFactory;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.ActionResponseImpl;
028    import com.liferay.portlet.messageboards.MessageBodyException;
029    import com.liferay.portlet.messageboards.MessageSubjectException;
030    import com.liferay.portlet.messageboards.NoSuchMessageException;
031    import com.liferay.portlet.messageboards.RequiredMessageException;
032    import com.liferay.portlet.messageboards.model.MBMessage;
033    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
034    
035    import javax.portlet.ActionRequest;
036    import javax.portlet.ActionResponse;
037    import javax.portlet.PortletConfig;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Brian Wing Shun Chan
047     */
048    public class EditDiscussionAction extends PortletAction {
049    
050            public void processAction(
051                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    ActionResponseImpl actionResponseImpl =
056                            (ActionResponseImpl)actionResponse;
057    
058                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
059    
060                    try {
061                            String redirect = ParamUtil.getString(actionRequest, "redirect");
062    
063                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
064                                    MBMessage message = updateMessage(actionRequest);
065    
066                                    redirect +=
067                                            "#" + actionResponseImpl.getNamespace() + "messageScroll" +
068                                                    message.getMessageId();
069                            }
070                            else if (cmd.equals(Constants.DELETE)) {
071                                    deleteMessage(actionRequest);
072                            }
073    
074                            sendRedirect(actionRequest, actionResponse, redirect);
075                    }
076                    catch (Exception e) {
077                            if (e instanceof NoSuchMessageException ||
078                                    e instanceof PrincipalException ||
079                                    e instanceof RequiredMessageException) {
080    
081                                    SessionErrors.add(actionRequest, e.getClass().getName());
082    
083                                    setForward(actionRequest, "portlet.message_boards.error");
084                            }
085                            else if (e instanceof FileNameException ||
086                                             e instanceof FileSizeException ||
087                                             e instanceof MessageBodyException ||
088                                             e instanceof MessageSubjectException) {
089    
090                                    SessionErrors.add(actionRequest, e.getClass().getName());
091    
092                                    sendRedirect(actionRequest, actionResponse);
093                            }
094                            else {
095                                    throw e;
096                            }
097                    }
098            }
099    
100            public ActionForward render(
101                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
102                            RenderRequest renderRequest, RenderResponse renderResponse)
103                    throws Exception {
104    
105                    try {
106                            ActionUtil.getMessage(renderRequest);
107                    }
108                    catch (Exception e) {
109                            if (e instanceof NoSuchMessageException ||
110                                    e instanceof PrincipalException) {
111    
112                                    SessionErrors.add(renderRequest, e.getClass().getName());
113    
114                                    return mapping.findForward("portlet.message_boards.error");
115                            }
116                            else {
117                                    throw e;
118                            }
119                    }
120    
121                    return mapping.findForward(getForward(
122                            renderRequest, "portlet.message_boards.edit_discussion"));
123            }
124    
125            protected void deleteMessage(ActionRequest actionRequest) throws Exception {
126                    long groupId = PortalUtil.getScopeGroupId(actionRequest);
127    
128                    String className = ParamUtil.getString(actionRequest, "className");
129                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
130                    String permissionClassName = ParamUtil.getString(
131                            actionRequest, "permissionClassName");
132                    long permissionClassPK = ParamUtil.getLong(
133                            actionRequest, "permissionClassPK");
134    
135                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
136    
137                    MBMessageServiceUtil.deleteDiscussionMessage(
138                            groupId, className, classPK, permissionClassName, permissionClassPK,
139                            messageId);
140            }
141    
142            protected boolean isCheckMethodOnProcessAction() {
143                    return _CHECK_METHOD_ON_PROCESS_ACTION;
144            }
145    
146            protected MBMessage updateMessage(ActionRequest actionRequest)
147                    throws Exception {
148    
149                    String className = ParamUtil.getString(actionRequest, "className");
150                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
151                    String permissionClassName = ParamUtil.getString(
152                            actionRequest, "permissionClassName");
153                    long permissionClassPK = ParamUtil.getLong(
154                            actionRequest, "permissionClassPK");
155    
156                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
157    
158                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
159                    long parentMessageId = ParamUtil.getLong(
160                            actionRequest, "parentMessageId");
161                    String subject = ParamUtil.getString(actionRequest, "subject");
162                    String body = ParamUtil.getString(actionRequest, "body");
163    
164                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
165                            MBMessage.class.getName(), actionRequest);
166    
167                    MBMessage message = null;
168    
169                    if (messageId <= 0) {
170    
171                            // Add message
172    
173                            message = MBMessageServiceUtil.addDiscussionMessage(
174                                    serviceContext.getScopeGroupId(), className, classPK,
175                                    permissionClassName, permissionClassPK, threadId,
176                                    parentMessageId, subject, body, serviceContext);
177                    }
178                    else {
179    
180                            // Update message
181    
182                            message = MBMessageServiceUtil.updateDiscussionMessage(
183                                    className, classPK, permissionClassName, permissionClassPK,
184                                    messageId, subject, body, serviceContext);
185                    }
186    
187                    return message;
188            }
189    
190            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
191    
192    }