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.portlet.messageboards.comment;
016    
017    import com.liferay.portal.kernel.comment.CommentConstants;
018    import com.liferay.portal.kernel.comment.CommentManager;
019    import com.liferay.portal.kernel.comment.Discussion;
020    import com.liferay.portal.kernel.comment.DiscussionComment;
021    import com.liferay.portal.kernel.comment.DiscussionPermission;
022    import com.liferay.portal.kernel.comment.DuplicateCommentException;
023    import com.liferay.portal.kernel.exception.PortalException;
024    import com.liferay.portal.kernel.util.Function;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.workflow.WorkflowConstants;
027    import com.liferay.portal.security.permission.PermissionChecker;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.messageboards.model.MBDiscussion;
032    import com.liferay.portlet.messageboards.model.MBMessage;
033    import com.liferay.portlet.messageboards.model.MBMessageDisplay;
034    import com.liferay.portlet.messageboards.model.MBThread;
035    import com.liferay.portlet.messageboards.model.MBTreeWalker;
036    import com.liferay.portlet.messageboards.service.MBDiscussionLocalService;
037    import com.liferay.portlet.messageboards.service.MBMessageLocalService;
038    import com.liferay.portlet.messageboards.util.comparator.MessageThreadComparator;
039    import com.liferay.portlet.ratings.model.RatingsEntry;
040    import com.liferay.portlet.ratings.model.RatingsStats;
041    import com.liferay.portlet.ratings.service.RatingsEntryLocalService;
042    import com.liferay.portlet.ratings.service.RatingsStatsLocalService;
043    
044    import java.util.ArrayList;
045    import java.util.Collections;
046    import java.util.List;
047    
048    /**
049     * @author Andr?? de Oliveira
050     * @author Alexander Chow
051     * @author Raymond Aug??
052     */
053    public class MBCommentManagerImpl implements CommentManager {
054    
055            @Override
056            public void addComment(
057                            long userId, long groupId, String className, long classPK,
058                            String body,
059                            Function<String, ServiceContext> serviceContextFunction)
060                    throws PortalException {
061    
062                    MBMessageDisplay messageDisplay =
063                            _mbMessageLocalService.getDiscussionMessageDisplay(
064                                    userId, groupId, className, classPK,
065                                    WorkflowConstants.STATUS_APPROVED);
066    
067                    MBThread thread = messageDisplay.getThread();
068    
069                    List<MBMessage> messages = _mbMessageLocalService.getThreadMessages(
070                            thread.getThreadId(), WorkflowConstants.STATUS_APPROVED);
071    
072                    for (MBMessage message : messages) {
073                            String messageBody = message.getBody();
074    
075                            if (messageBody.equals(body)) {
076                                    throw new DuplicateCommentException();
077                            }
078                    }
079    
080                    ServiceContext serviceContext = serviceContextFunction.apply(
081                            MBMessage.class.getName());
082    
083                    _mbMessageLocalService.addDiscussionMessage(
084                            userId, StringPool.BLANK, groupId, className, classPK,
085                            thread.getThreadId(), thread.getRootMessageId(), StringPool.BLANK,
086                            body, serviceContext);
087            }
088    
089            @Override
090            public long addComment(
091                            long userId, long groupId, String className, long classPK,
092                            String userName, String subject, String body,
093                            Function<String, ServiceContext> serviceContextFunction)
094                    throws PortalException {
095    
096                    MBMessageDisplay mbMessageDisplay =
097                            _mbMessageLocalService.getDiscussionMessageDisplay(
098                                    userId, groupId, className, classPK,
099                                    WorkflowConstants.STATUS_APPROVED);
100    
101                    MBThread mbThread = mbMessageDisplay.getThread();
102    
103                    ServiceContext serviceContext = serviceContextFunction.apply(
104                            MBMessage.class.getName());
105    
106                    MBMessage mbMessage = _mbMessageLocalService.addDiscussionMessage(
107                            userId, userName, groupId, className, classPK,
108                            mbThread.getThreadId(), mbThread.getRootMessageId(), subject, body,
109                            serviceContext);
110    
111                    return mbMessage.getMessageId();
112            }
113    
114            @Override
115            public long addComment(
116                            long userId, String className, long classPK, String userName,
117                            long parentCommentId, String subject, String body,
118                            Function<String, ServiceContext> serviceContextFunction)
119                    throws PortalException {
120    
121                    MBMessage parentMessage = _mbMessageLocalService.getMessage(
122                            parentCommentId);
123    
124                    ServiceContext serviceContext = serviceContextFunction.apply(
125                            MBMessage.class.getName());
126    
127                    MBMessage mbMessage = _mbMessageLocalService.addDiscussionMessage(
128                            userId, userName, parentMessage.getGroupId(), className, classPK,
129                            parentMessage.getThreadId(), parentCommentId, subject, body,
130                            serviceContext);
131    
132                    return mbMessage.getMessageId();
133            }
134    
135            @Override
136            public void addDiscussion(
137                            long userId, long groupId, String className, long classPK,
138                            String userName)
139                    throws PortalException {
140    
141                    _mbMessageLocalService.addDiscussionMessage(
142                            userId, userName, groupId, className, classPK,
143                            WorkflowConstants.ACTION_PUBLISH);
144            }
145    
146            @Override
147            public void deleteComment(long commentId) throws PortalException {
148                    _mbMessageLocalService.deleteDiscussionMessage(commentId);
149            }
150    
151            @Override
152            public void deleteDiscussion(String className, long classPK)
153                    throws PortalException {
154    
155                    _mbMessageLocalService.deleteDiscussionMessages(className, classPK);
156            }
157    
158            @Override
159            public int getCommentsCount(String className, long classPK) {
160                    long classNameId = PortalUtil.getClassNameId(className);
161    
162                    return _mbMessageLocalService.getDiscussionMessagesCount(
163                            classNameId, classPK, WorkflowConstants.STATUS_APPROVED);
164            }
165    
166            @Override
167            public Discussion getDiscussion(
168                            long userId, long groupId, String className, long classPK,
169                            Function<String, ServiceContext> serviceContextFunction)
170                    throws PortalException {
171    
172                    MBMessageDisplay messageDisplay =
173                            _mbMessageLocalService.getDiscussionMessageDisplay(
174                                    userId, groupId, className, classPK,
175                                    WorkflowConstants.STATUS_ANY, new MessageThreadComparator());
176    
177                    MBTreeWalker treeWalker = messageDisplay.getTreeWalker();
178    
179                    List<MBMessage> messages = treeWalker.getMessages();
180    
181                    List<RatingsEntry> ratingsEntries = Collections.emptyList();
182                    List<RatingsStats> ratingsStats = Collections.emptyList();
183    
184                    if (messages.size() > 1) {
185                            List<Long> classPKs = new ArrayList<>();
186    
187                            for (MBMessage curMessage : messages) {
188                                    if (!curMessage.isRoot()) {
189                                            classPKs.add(curMessage.getMessageId());
190                                    }
191                            }
192    
193                            ratingsEntries = _ratingsEntryLocalService.getEntries(
194                                    userId, CommentConstants.getDiscussionClassName(), classPKs);
195                            ratingsStats = _ratingsStatsLocalService.getStats(
196                                    CommentConstants.getDiscussionClassName(), classPKs);
197                    }
198    
199                    ServiceContext serviceContext = serviceContextFunction.apply(
200                            MBMessage.class.getName());
201    
202                    ThemeDisplay themeDisplay = serviceContext.getThemeDisplay();
203    
204                    DiscussionComment rootDiscussionComment = new MBDiscussionCommentImpl(
205                            treeWalker.getRoot(), treeWalker, ratingsEntries, ratingsStats,
206                            themeDisplay.getPathThemeImages());
207    
208                    return new MBDiscussionImpl(
209                            rootDiscussionComment, messageDisplay.isDiscussionMaxComments());
210            }
211    
212            @Override
213            public DiscussionPermission getDiscussionPermission(
214                    PermissionChecker permissionChecker) {
215    
216                    return new MBDiscussionPermissionImpl(permissionChecker);
217            }
218    
219            @Override
220            public boolean hasDiscussion(String className, long classPK) {
221                    MBDiscussion discussion = _mbDiscussionLocalService.fetchDiscussion(
222                            className, classPK);
223    
224                    if (discussion == null) {
225                            return false;
226                    }
227    
228                    return true;
229            }
230    
231            public void setMBDiscussionLocalService(
232                    MBDiscussionLocalService mbDiscussionLocalService) {
233    
234                    _mbDiscussionLocalService = mbDiscussionLocalService;
235            }
236    
237            public void setMBMessageLocalService(
238                    MBMessageLocalService mbMessageLocalService) {
239    
240                    _mbMessageLocalService = mbMessageLocalService;
241            }
242    
243            public void setRatingsEntryLocalService(
244                    RatingsEntryLocalService ratingsEntryLocalService) {
245    
246                    _ratingsEntryLocalService = ratingsEntryLocalService;
247            }
248    
249            public void setRatingsStatsLocalService(
250                    RatingsStatsLocalService ratingsStatsLocalService) {
251    
252                    _ratingsStatsLocalService = ratingsStatsLocalService;
253            }
254    
255            @Override
256            public void subscribeDiscussion(
257                            long userId, long groupId, String className, long classPK)
258                    throws PortalException {
259    
260                    _mbDiscussionLocalService.subscribeDiscussion(
261                            userId, groupId, className, classPK);
262            }
263    
264            @Override
265            public void unsubscribeDiscussion(
266                            long userId, String className, long classPK)
267                    throws PortalException {
268    
269                    _mbDiscussionLocalService.unsubscribeDiscussion(
270                            userId, className, classPK);
271            }
272    
273            @Override
274            public long updateComment(
275                            long userId, String className, long classPK, long commentId,
276                            String subject, String body,
277                            Function<String, ServiceContext> serviceContextFunction)
278                    throws PortalException {
279    
280                    ServiceContext serviceContext = serviceContextFunction.apply(
281                            MBMessage.class.getName());
282    
283                    MBMessage message = _mbMessageLocalService.updateDiscussionMessage(
284                            userId, commentId, className, classPK, subject, body,
285                            serviceContext);
286    
287                    return message.getMessageId();
288            }
289    
290            private MBDiscussionLocalService _mbDiscussionLocalService;
291            private MBMessageLocalService _mbMessageLocalService;
292            private RatingsEntryLocalService _ratingsEntryLocalService;
293            private RatingsStatsLocalService _ratingsStatsLocalService;
294    
295    }