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.CommentManager;
018    import com.liferay.portal.kernel.comment.DuplicateCommentException;
019    import com.liferay.portal.kernel.exception.PortalException;
020    import com.liferay.portal.kernel.util.Function;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.workflow.WorkflowConstants;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portlet.messageboards.model.MBMessage;
026    import com.liferay.portlet.messageboards.model.MBMessageDisplay;
027    import com.liferay.portlet.messageboards.model.MBThread;
028    import com.liferay.portlet.messageboards.service.MBMessageLocalService;
029    
030    import java.util.List;
031    
032    /**
033     * @author Andr?? de Oliveira
034     * @author Alexander Chow
035     * @author Raymond Aug??
036     */
037    public class MBCommentManagerImpl implements CommentManager {
038    
039            @Override
040            public void addComment(
041                            long userId, long groupId, String className, long classPK,
042                            String body, ServiceContext serviceContext)
043                    throws PortalException {
044    
045                    MBMessageDisplay messageDisplay =
046                            _mbMessageLocalService.getDiscussionMessageDisplay(
047                                    userId, groupId, className, classPK,
048                                    WorkflowConstants.STATUS_APPROVED);
049    
050                    MBThread thread = messageDisplay.getThread();
051    
052                    List<MBMessage> messages = _mbMessageLocalService.getThreadMessages(
053                            thread.getThreadId(), WorkflowConstants.STATUS_APPROVED);
054    
055                    for (MBMessage message : messages) {
056                            String messageBody = message.getBody();
057    
058                            if (messageBody.equals(body)) {
059                                    throw new DuplicateCommentException();
060                            }
061                    }
062    
063                    _mbMessageLocalService.addDiscussionMessage(
064                            userId, StringPool.BLANK, groupId, className, classPK,
065                            thread.getThreadId(), thread.getRootMessageId(), StringPool.BLANK,
066                            body, serviceContext);
067            }
068    
069            @Override
070            public long addComment(
071                            long userId, long groupId, String className, long classPK,
072                            String userName, String subject, String body,
073                            Function<String, ServiceContext> serviceContextFunction)
074                    throws PortalException {
075    
076                    MBMessageDisplay mbMessageDisplay =
077                            _mbMessageLocalService.getDiscussionMessageDisplay(
078                                    userId, groupId, className, classPK,
079                                    WorkflowConstants.STATUS_APPROVED);
080    
081                    MBThread mbThread = mbMessageDisplay.getThread();
082    
083                    ServiceContext serviceContext = serviceContextFunction.apply(
084                            MBMessage.class.getName());
085    
086                    MBMessage mbMessage = _mbMessageLocalService.addDiscussionMessage(
087                            userId, userName, groupId, className, classPK,
088                            mbThread.getThreadId(), mbThread.getRootMessageId(), subject, body,
089                            serviceContext);
090    
091                    return mbMessage.getMessageId();
092            }
093    
094            @Override
095            public void addDiscussion(
096                            long userId, long groupId, String className, long classPK,
097                            String userName)
098                    throws PortalException {
099    
100                    _mbMessageLocalService.addDiscussionMessage(
101                            userId, userName, groupId, className, classPK,
102                            WorkflowConstants.ACTION_PUBLISH);
103            }
104    
105            @Override
106            public void deleteComment(long commentId) throws PortalException {
107                    _mbMessageLocalService.deleteDiscussionMessage(commentId);
108            }
109    
110            @Override
111            public void deleteDiscussion(String className, long classPK)
112                    throws PortalException {
113    
114                    _mbMessageLocalService.deleteDiscussionMessages(className, classPK);
115            }
116    
117            @Override
118            public int getCommentsCount(String className, long classPK) {
119                    long classNameId = PortalUtil.getClassNameId(className);
120    
121                    return _mbMessageLocalService.getDiscussionMessagesCount(
122                            classNameId, classPK, WorkflowConstants.STATUS_APPROVED);
123            }
124    
125            public void setMBMessageLocalService(
126                    MBMessageLocalService mbMessageLocalService) {
127    
128                    _mbMessageLocalService = mbMessageLocalService;
129            }
130    
131            private MBMessageLocalService _mbMessageLocalService;
132    
133    }