001    /**
002     * Copyright (c) 2000-present 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.portal.comment.action;
016    
017    import com.liferay.message.boards.kernel.exception.DiscussionMaxCommentsException;
018    import com.liferay.message.boards.kernel.exception.MessageBodyException;
019    import com.liferay.message.boards.kernel.exception.NoSuchMessageException;
020    import com.liferay.message.boards.kernel.exception.RequiredMessageException;
021    import com.liferay.portal.kernel.comment.Comment;
022    import com.liferay.portal.kernel.comment.CommentManagerUtil;
023    import com.liferay.portal.kernel.comment.DiscussionPermission;
024    import com.liferay.portal.kernel.json.JSONFactoryUtil;
025    import com.liferay.portal.kernel.json.JSONObject;
026    import com.liferay.portal.kernel.model.User;
027    import com.liferay.portal.kernel.security.auth.PrincipalException;
028    import com.liferay.portal.kernel.security.auth.PrincipalThreadLocal;
029    import com.liferay.portal.kernel.service.ServiceContext;
030    import com.liferay.portal.kernel.service.ServiceContextFunction;
031    import com.liferay.portal.kernel.service.UserLocalServiceUtil;
032    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
033    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
034    import com.liferay.portal.kernel.struts.BaseStrutsAction;
035    import com.liferay.portal.kernel.struts.StrutsAction;
036    import com.liferay.portal.kernel.theme.ThemeDisplay;
037    import com.liferay.portal.kernel.util.Constants;
038    import com.liferay.portal.kernel.util.ContentTypes;
039    import com.liferay.portal.kernel.util.Function;
040    import com.liferay.portal.kernel.util.ParamUtil;
041    import com.liferay.portal.kernel.util.PortalUtil;
042    import com.liferay.portal.kernel.util.StringPool;
043    import com.liferay.portal.kernel.util.Validator;
044    import com.liferay.portal.kernel.util.WebKeys;
045    import com.liferay.portal.kernel.workflow.WorkflowConstants;
046    import com.liferay.portal.servlet.NamespaceServletRequest;
047    
048    import java.io.IOException;
049    
050    import javax.servlet.http.HttpServletRequest;
051    import javax.servlet.http.HttpServletResponse;
052    
053    /**
054     * @author Adolfo P??rez
055     */
056    @OSGiBeanProperties(
057            property = "path=/portal/comment/edit_discussion",
058            service = StrutsAction.class
059    )
060    public class EditDiscussionStrutsAction extends BaseStrutsAction {
061    
062            @Override
063            public String execute(
064                            HttpServletRequest request, HttpServletResponse response)
065                    throws Exception {
066    
067                    String namespace = ParamUtil.getString(request, "namespace");
068    
069                    HttpServletRequest namespacedRequest = new NamespaceServletRequest(
070                            request, StringPool.BLANK, namespace);
071    
072                    String cmd = ParamUtil.getString(namespacedRequest, Constants.CMD);
073    
074                    try {
075                            String redirect = PortalUtil.escapeRedirect(
076                                    ParamUtil.getString(request, "redirect"));
077    
078                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
079                                    long commentId = updateComment(namespacedRequest);
080    
081                                    boolean ajax = ParamUtil.getBoolean(request, "ajax", true);
082    
083                                    if (ajax) {
084                                            String randomNamespace = ParamUtil.getString(
085                                                    namespacedRequest, "randomNamespace");
086    
087                                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
088    
089                                            jsonObject.put("commentId", commentId);
090                                            jsonObject.put("randomNamespace", randomNamespace);
091    
092                                            writeJSON(namespacedRequest, response, jsonObject);
093    
094                                            return null;
095                                    }
096                            }
097                            else if (cmd.equals(Constants.DELETE)) {
098                                    deleteComment(namespacedRequest);
099                            }
100                            else if (cmd.equals(Constants.SUBSCRIBE_TO_COMMENTS)) {
101                                    subscribeToComments(namespacedRequest, true);
102                            }
103                            else if (cmd.equals(Constants.UNSUBSCRIBE_FROM_COMMENTS)) {
104                                    subscribeToComments(namespacedRequest, false);
105                            }
106    
107                            if (Validator.isNotNull(redirect)) {
108                                    response.sendRedirect(redirect);
109                            }
110                    }
111                    catch (DiscussionMaxCommentsException | MessageBodyException |
112                               NoSuchMessageException | PrincipalException |
113                               RequiredMessageException e) {
114    
115                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
116    
117                            jsonObject.putException(e);
118    
119                            writeJSON(namespacedRequest, response, jsonObject);
120                    }
121    
122                    return null;
123            }
124    
125            protected void deleteComment(HttpServletRequest request) throws Exception {
126                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
127                            WebKeys.THEME_DISPLAY);
128    
129                    long commentId = ParamUtil.getLong(request, "commentId");
130    
131                    DiscussionPermission discussionPermission = _getDiscussionPermission(
132                            themeDisplay);
133    
134                    discussionPermission.checkDeletePermission(commentId);
135    
136                    CommentManagerUtil.deleteComment(commentId);
137            }
138    
139            protected void subscribeToComments(
140                            HttpServletRequest request, boolean subscribe)
141                    throws Exception {
142    
143                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
144                            WebKeys.THEME_DISPLAY);
145    
146                    String className = ParamUtil.getString(request, "className");
147                    long classPK = ParamUtil.getLong(request, "classPK");
148    
149                    if (subscribe) {
150                            CommentManagerUtil.subscribeDiscussion(
151                                    themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
152                                    className, classPK);
153                    }
154                    else {
155                            CommentManagerUtil.unsubscribeDiscussion(
156                                    themeDisplay.getUserId(), className, classPK);
157                    }
158            }
159    
160            protected long updateComment(HttpServletRequest request) throws Exception {
161                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
162                            WebKeys.THEME_DISPLAY);
163    
164                    long commentId = ParamUtil.getLong(request, "commentId");
165    
166                    String className = ParamUtil.getString(request, "className");
167                    long classPK = ParamUtil.getLong(request, "classPK");
168                    long parentCommentId = ParamUtil.getLong(request, "parentCommentId");
169                    String subject = ParamUtil.getString(request, "subject");
170                    String body = ParamUtil.getString(request, "body");
171    
172                    Function<String, ServiceContext> serviceContextFunction =
173                            new ServiceContextFunction(request);
174    
175                    DiscussionPermission discussionPermission = _getDiscussionPermission(
176                            themeDisplay);
177    
178                    if (commentId <= 0) {
179    
180                            // Add message
181    
182                            User user = null;
183    
184                            if (themeDisplay.isSignedIn()) {
185                                    user = themeDisplay.getUser();
186                            }
187                            else {
188                                    String emailAddress = ParamUtil.getString(
189                                            request, "emailAddress");
190    
191                                    user = UserLocalServiceUtil.fetchUserByEmailAddress(
192                                            themeDisplay.getCompanyId(), emailAddress);
193    
194                                    if ((user == null) ||
195                                            (user.getStatus() != WorkflowConstants.STATUS_INCOMPLETE)) {
196    
197                                            return 0;
198                                    }
199                            }
200    
201                            String name = PrincipalThreadLocal.getName();
202    
203                            PrincipalThreadLocal.setName(user.getUserId());
204    
205                            try {
206                                    discussionPermission.checkAddPermission(
207                                            themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId(),
208                                            className, classPK);
209    
210                                    commentId = CommentManagerUtil.addComment(
211                                            user.getUserId(), className, classPK, user.getFullName(),
212                                            parentCommentId, subject, body, serviceContextFunction);
213                            }
214                            finally {
215                                    PrincipalThreadLocal.setName(name);
216                            }
217                    }
218                    else {
219    
220                            // Update message
221    
222                            if (Validator.isNull(className) || (classPK == 0)) {
223                                    Comment comment = CommentManagerUtil.fetchComment(commentId);
224    
225                                    if (comment != null) {
226                                            className = comment.getClassName();
227                                            classPK = comment.getClassPK();
228                                    }
229                            }
230    
231                            discussionPermission.checkUpdatePermission(commentId);
232    
233                            commentId = CommentManagerUtil.updateComment(
234                                    themeDisplay.getUserId(), className, classPK, commentId,
235                                    subject, body, serviceContextFunction);
236                    }
237    
238                    // Subscription
239    
240                    boolean subscribe = ParamUtil.getBoolean(request, "subscribe");
241    
242                    if (subscribe) {
243                            CommentManagerUtil.subscribeDiscussion(
244                                    themeDisplay.getUserId(), themeDisplay.getScopeGroupId(),
245                                    className, classPK);
246                    }
247    
248                    return commentId;
249            }
250    
251            protected void writeJSON(
252                            HttpServletRequest request, HttpServletResponse response,
253                            Object json)
254                    throws IOException {
255    
256                    response.setContentType(ContentTypes.APPLICATION_JSON);
257    
258                    ServletResponseUtil.write(response, json.toString());
259    
260                    response.flushBuffer();
261            }
262    
263            private DiscussionPermission _getDiscussionPermission(
264                            ThemeDisplay themeDisplay)
265                    throws PrincipalException {
266    
267                    DiscussionPermission discussionPermission =
268                            CommentManagerUtil.getDiscussionPermission(
269                                    themeDisplay.getPermissionChecker());
270    
271                    if (discussionPermission == null) {
272                            throw new PrincipalException("Discussion permission is null");
273                    }
274    
275                    return discussionPermission;
276            }
277    
278    }