001    /**
002     * Copyright (c) 2000-2013 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.portal.NoSuchUserException;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.security.auth.PrincipalThreadLocal;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextFactory;
029    import com.liferay.portal.service.SubscriptionLocalServiceUtil;
030    import com.liferay.portal.service.UserLocalServiceUtil;
031    import com.liferay.portal.struts.PortletAction;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portal.util.WebKeys;
035    import com.liferay.portlet.messageboards.MessageBodyException;
036    import com.liferay.portlet.messageboards.NoSuchMessageException;
037    import com.liferay.portlet.messageboards.RequiredMessageException;
038    import com.liferay.portlet.messageboards.model.MBMessage;
039    import com.liferay.portlet.messageboards.service.MBMessageServiceUtil;
040    
041    import javax.portlet.ActionRequest;
042    import javax.portlet.ActionResponse;
043    import javax.portlet.PortletConfig;
044    import javax.portlet.RenderRequest;
045    import javax.portlet.RenderResponse;
046    
047    import org.apache.struts.action.ActionForm;
048    import org.apache.struts.action.ActionForward;
049    import org.apache.struts.action.ActionMapping;
050    
051    /**
052     * @author Brian Wing Shun Chan
053     * @author Sergio Gonz??lez
054     */
055    public class EditDiscussionAction extends PortletAction {
056    
057            @Override
058            public void processAction(
059                            ActionMapping actionMapping, ActionForm actionForm,
060                            PortletConfig portletConfig, ActionRequest actionRequest,
061                            ActionResponse actionResponse)
062                    throws Exception {
063    
064                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
065    
066                    try {
067                            String redirect = PortalUtil.escapeRedirect(
068                                    ParamUtil.getString(actionRequest, "redirect"));
069    
070                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
071                                    MBMessage message = updateMessage(actionRequest);
072    
073                                    String randomNamespace = ParamUtil.getString(
074                                            actionRequest, "randomNamespace");
075    
076                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
077    
078                                    jsonObject.put("messageId", message.getMessageId());
079                                    jsonObject.put("randomNamespace", randomNamespace);
080    
081                                    writeJSON(actionRequest, actionResponse, jsonObject);
082    
083                                    return;
084                            }
085                            else if (cmd.equals(Constants.DELETE)) {
086                                    deleteMessage(actionRequest);
087                            }
088                            else if (cmd.equals(Constants.SUBSCRIBE_TO_COMMENTS)) {
089                                    subscribeToComments(actionRequest, true);
090                            }
091                            else if (cmd.equals(Constants.UNSUBSCRIBE_FROM_COMMENTS)) {
092                                    subscribeToComments(actionRequest, false);
093                            }
094    
095                            sendRedirect(actionRequest, actionResponse, redirect);
096                    }
097                    catch (Exception e) {
098                            if (e instanceof MessageBodyException ||
099                                    e instanceof NoSuchMessageException ||
100                                    e instanceof PrincipalException ||
101                                    e instanceof RequiredMessageException) {
102    
103                                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
104    
105                                    jsonObject.putException(e);
106    
107                                    writeJSON(actionRequest, actionResponse, jsonObject);
108                            }
109                            else {
110                                    throw e;
111                            }
112                    }
113            }
114    
115            @Override
116            public ActionForward render(
117                            ActionMapping actionMapping, ActionForm actionForm,
118                            PortletConfig portletConfig, RenderRequest renderRequest,
119                            RenderResponse renderResponse)
120                    throws Exception {
121    
122                    try {
123                            ActionUtil.getMessage(renderRequest);
124                    }
125                    catch (Exception e) {
126                            if (e instanceof NoSuchMessageException ||
127                                    e instanceof PrincipalException) {
128    
129                                    SessionErrors.add(renderRequest, e.getClass());
130    
131                                    return actionMapping.findForward(
132                                            "portlet.message_boards.error");
133                            }
134                            else {
135                                    throw e;
136                            }
137                    }
138    
139                    return actionMapping.findForward(
140                            getForward(
141                                    renderRequest, "portlet.message_boards.edit_discussion"));
142            }
143    
144            protected void deleteMessage(ActionRequest actionRequest) throws Exception {
145                    long groupId = PortalUtil.getScopeGroupId(actionRequest);
146    
147                    String className = ParamUtil.getString(actionRequest, "className");
148                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
149                    String permissionClassName = ParamUtil.getString(
150                            actionRequest, "permissionClassName");
151                    long permissionClassPK = ParamUtil.getLong(
152                            actionRequest, "permissionClassPK");
153                    long permissionOwnerId = ParamUtil.getLong(
154                            actionRequest, "permissionOwnerId");
155    
156                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
157    
158                    MBMessageServiceUtil.deleteDiscussionMessage(
159                            groupId, className, classPK, permissionClassName, permissionClassPK,
160                            permissionOwnerId, messageId);
161            }
162    
163            @Override
164            protected boolean isCheckMethodOnProcessAction() {
165                    return _CHECK_METHOD_ON_PROCESS_ACTION;
166            }
167    
168            protected void subscribeToComments(
169                            ActionRequest actionRequest, boolean subscribe)
170                    throws Exception {
171    
172                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
173                            WebKeys.THEME_DISPLAY);
174    
175                    String className = ParamUtil.getString(actionRequest, "className");
176                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
177    
178                    if (subscribe) {
179                            SubscriptionLocalServiceUtil.addSubscription(
180                                    themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
181                                    className, classPK);
182                    }
183                    else {
184                            SubscriptionLocalServiceUtil.deleteSubscription(
185                                    themeDisplay.getUserId(), className, classPK);
186                    }
187            }
188    
189            protected MBMessage updateMessage(ActionRequest actionRequest)
190                    throws Exception {
191    
192                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
193                            WebKeys.THEME_DISPLAY);
194    
195                    String className = ParamUtil.getString(actionRequest, "className");
196                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
197                    String permissionClassName = ParamUtil.getString(
198                            actionRequest, "permissionClassName");
199                    long permissionClassPK = ParamUtil.getLong(
200                            actionRequest, "permissionClassPK");
201                    long permissionOwnerId = ParamUtil.getLong(
202                            actionRequest, "permissionOwnerId");
203    
204                    long messageId = ParamUtil.getLong(actionRequest, "messageId");
205    
206                    long threadId = ParamUtil.getLong(actionRequest, "threadId");
207                    long parentMessageId = ParamUtil.getLong(
208                            actionRequest, "parentMessageId");
209                    String subject = ParamUtil.getString(actionRequest, "subject");
210                    String body = ParamUtil.getString(actionRequest, "body");
211    
212                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
213                            MBMessage.class.getName(), actionRequest);
214    
215                    MBMessage message = null;
216    
217                    if (messageId <= 0) {
218    
219                            // Add message
220    
221                            User user = null;
222    
223                            if (themeDisplay.isSignedIn()) {
224                                    user = themeDisplay.getUser();
225                            }
226                            else {
227                                    String emailAddress = ParamUtil.getString(
228                                            actionRequest, "emailAddress");
229    
230                                    try {
231                                            user = UserLocalServiceUtil.getUserByEmailAddress(
232                                                    themeDisplay.getCompanyId(), emailAddress);
233                                    }
234                                    catch (NoSuchUserException nsue) {
235                                            return null;
236                                    }
237    
238                                    if (user.getStatus() != WorkflowConstants.STATUS_INCOMPLETE) {
239                                            return null;
240                                    }
241                            }
242    
243                            String name = PrincipalThreadLocal.getName();
244    
245                            PrincipalThreadLocal.setName(user.getUserId());
246    
247                            try {
248                                    message = MBMessageServiceUtil.addDiscussionMessage(
249                                            serviceContext.getScopeGroupId(), className, classPK,
250                                            permissionClassName, permissionClassPK, permissionOwnerId,
251                                            threadId, parentMessageId, subject, body, serviceContext);
252                            }
253                            finally {
254                                    PrincipalThreadLocal.setName(name);
255                            }
256                    }
257                    else {
258    
259                            // Update message
260    
261                            message = MBMessageServiceUtil.updateDiscussionMessage(
262                                    className, classPK, permissionClassName, permissionClassPK,
263                                    permissionOwnerId, messageId, subject, body, serviceContext);
264                    }
265    
266                    // Subscription
267    
268                    boolean subscribe = ParamUtil.getBoolean(actionRequest, "subscribe");
269    
270                    if (subscribe) {
271                            SubscriptionLocalServiceUtil.addSubscription(
272                                    themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
273                                    className, classPK);
274                    }
275    
276                    return message;
277            }
278    
279            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
280    
281    }