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;
016    
017    import com.liferay.portal.kernel.comment.CommentManager;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.util.Function;
020    import com.liferay.portal.service.ServiceContext;
021    import com.liferay.registry.Filter;
022    import com.liferay.registry.Registry;
023    import com.liferay.registry.RegistryUtil;
024    import com.liferay.registry.ServiceTracker;
025    
026    /**
027     * @author Andr?? de Oliveira
028     * @author Alexander Chow
029     * @author Raymond Aug??
030     */
031    public class CommentManagerImpl implements CommentManager {
032    
033            public CommentManagerImpl() {
034                    Registry registry = RegistryUtil.getRegistry();
035    
036                    Class<?> clazz = getClass();
037    
038                    Filter filter = registry.getFilter(
039                            "(&(objectClass=" + CommentManager.class.getName() +
040                                    ")(!(objectClass=" + clazz.getName() + ")))");
041    
042                    _serviceTracker = registry.trackServices(filter);
043    
044                    _serviceTracker.open();
045            }
046    
047            @Override
048            public void addComment(
049                            long userId, long groupId, String className, long classPK,
050                            String body, ServiceContext serviceContext)
051                    throws PortalException {
052    
053                    CommentManager commentManager = getCommentManager();
054    
055                    commentManager.addComment(
056                            userId, groupId, className, classPK, body, serviceContext);
057            }
058    
059            @Override
060            public long addComment(
061                            long userId, long groupId, String className, long classPK,
062                            String userName, String subject, String body,
063                            Function<String, ServiceContext> serviceContextFunction)
064                    throws PortalException {
065    
066                    CommentManager commentManager = getCommentManager();
067    
068                    return commentManager.addComment(
069                            userId, groupId, className, classPK, userName, subject, body,
070                            serviceContextFunction);
071            }
072    
073            @Override
074            public void addDiscussion(
075                            long userId, long groupId, String className, long classPK,
076                            String userName)
077                    throws PortalException {
078    
079                    CommentManager commentManager = getCommentManager();
080    
081                    commentManager.addDiscussion(
082                            userId, groupId, className, classPK, userName);
083            }
084    
085            @Override
086            public void deleteComment(long commentId) throws PortalException {
087                    CommentManager commentManager = getCommentManager();
088    
089                    commentManager.deleteComment(commentId);
090            }
091    
092            @Override
093            public void deleteDiscussion(String className, long classPK)
094                    throws PortalException {
095    
096                    CommentManager commentManager = getCommentManager();
097    
098                    commentManager.deleteDiscussion(className, classPK);
099            }
100    
101            @Override
102            public int getCommentsCount(String className, long classPK) {
103                    CommentManager commentManager = getCommentManager();
104    
105                    return commentManager.getCommentsCount(className, classPK);
106            }
107    
108            protected CommentManager getCommentManager() {
109                    if (_serviceTracker.isEmpty()) {
110                            return _defaultCommentManager;
111                    }
112    
113                    return _serviceTracker.getService();
114            }
115    
116            protected void setDefaultCommentManager(
117                    CommentManager defaultCommentManager) {
118    
119                    _defaultCommentManager = defaultCommentManager;
120            }
121    
122            private CommentManager _defaultCommentManager =
123                    new DummyCommentManagerImpl();
124            private final ServiceTracker<CommentManager, CommentManager>
125                    _serviceTracker;
126    
127    }