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.comment.Discussion;
019    import com.liferay.portal.kernel.comment.DiscussionPermission;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
022    import com.liferay.portal.kernel.util.Function;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.registry.Filter;
026    import com.liferay.registry.Registry;
027    import com.liferay.registry.RegistryUtil;
028    import com.liferay.registry.ServiceTracker;
029    
030    /**
031     * @author Andr?? de Oliveira
032     * @author Alexander Chow
033     * @author Raymond Aug??
034     */
035    @OSGiBeanProperties(service = CommentManagerImpl.class)
036    public class CommentManagerImpl implements CommentManager {
037    
038            public CommentManagerImpl() {
039                    this(new DummyCommentManagerImpl());
040            }
041    
042            public CommentManagerImpl(CommentManager defaultCommentManager) {
043                    Registry registry = RegistryUtil.getRegistry();
044    
045                    Class<?> clazz = getClass();
046    
047                    Filter filter = registry.getFilter(
048                            "(&(objectClass=" + CommentManager.class.getName() +
049                                    ")(!(objectClass=" + clazz.getName() + ")))");
050    
051                    _serviceTracker = registry.trackServices(filter);
052    
053                    _serviceTracker.open();
054    
055                    _defaultCommentManager = defaultCommentManager;
056            }
057    
058            @Override
059            public void addComment(
060                            long userId, long groupId, String className, long classPK,
061                            String body,
062                            Function<String, ServiceContext> serviceContextFunction)
063                    throws PortalException {
064    
065                    CommentManager commentManager = getCommentManager();
066    
067                    commentManager.addComment(
068                            userId, groupId, className, classPK, body, serviceContextFunction);
069            }
070    
071            @Override
072            public long addComment(
073                            long userId, long groupId, String className, long classPK,
074                            String userName, String subject, String body,
075                            Function<String, ServiceContext> serviceContextFunction)
076                    throws PortalException {
077    
078                    CommentManager commentManager = getCommentManager();
079    
080                    return commentManager.addComment(
081                            userId, groupId, className, classPK, userName, subject, body,
082                            serviceContextFunction);
083            }
084    
085            @Override
086            public long addComment(
087                            long userId, String className, long classPK, String userName,
088                            long parentCommentId, String subject, String body,
089                            Function<String, ServiceContext> serviceContextFunction)
090                    throws PortalException {
091    
092                    CommentManager commentManager = getCommentManager();
093    
094                    return commentManager.addComment(
095                            userId, className, classPK, userName, parentCommentId, subject,
096                            body, serviceContextFunction);
097            }
098    
099            @Override
100            public void addDiscussion(
101                            long userId, long groupId, String className, long classPK,
102                            String userName)
103                    throws PortalException {
104    
105                    CommentManager commentManager = getCommentManager();
106    
107                    commentManager.addDiscussion(
108                            userId, groupId, className, classPK, userName);
109            }
110    
111            @Override
112            public void deleteComment(long commentId) throws PortalException {
113                    CommentManager commentManager = getCommentManager();
114    
115                    commentManager.deleteComment(commentId);
116            }
117    
118            @Override
119            public void deleteDiscussion(String className, long classPK)
120                    throws PortalException {
121    
122                    CommentManager commentManager = getCommentManager();
123    
124                    commentManager.deleteDiscussion(className, classPK);
125            }
126    
127            @Override
128            public int getCommentsCount(String className, long classPK) {
129                    CommentManager commentManager = getCommentManager();
130    
131                    return commentManager.getCommentsCount(className, classPK);
132            }
133    
134            @Override
135            public Discussion getDiscussion(
136                            long userId, long groupId, String className, long classPK,
137                            Function<String, ServiceContext> serviceContextFunction)
138                    throws PortalException {
139    
140                    CommentManager commentManager = getCommentManager();
141    
142                    return commentManager.getDiscussion(
143                            userId, groupId, className, classPK, serviceContextFunction);
144            }
145    
146            @Override
147            public DiscussionPermission getDiscussionPermission(
148                    PermissionChecker permissionChecker) {
149    
150                    CommentManager commentManager = getCommentManager();
151    
152                    return commentManager.getDiscussionPermission(permissionChecker);
153            }
154    
155            @Override
156            public boolean hasDiscussion(String className, long classPK)
157                    throws PortalException {
158    
159                    CommentManager commentManager = getCommentManager();
160    
161                    return commentManager.hasDiscussion(className, classPK);
162            }
163    
164            @Override
165            public void subscribeDiscussion(
166                            long userId, long groupId, String className, long classPK)
167                    throws PortalException {
168    
169                    CommentManager commentManager = getCommentManager();
170    
171                    commentManager.subscribeDiscussion(userId, groupId, className, classPK);
172            }
173    
174            @Override
175            public void unsubscribeDiscussion(
176                            long userId, String className, long classPK)
177                    throws PortalException {
178    
179                    CommentManager commentManager = getCommentManager();
180    
181                    commentManager.unsubscribeDiscussion(userId, className, classPK);
182            }
183    
184            @Override
185            public long updateComment(
186                            long userId, String className, long classPK, long commentId,
187                            String subject, String body,
188                            Function<String, ServiceContext> serviceContextFunction)
189                    throws PortalException {
190    
191                    CommentManager commentManager = getCommentManager();
192    
193                    return commentManager.updateComment(
194                            userId, className, classPK, commentId, subject, body,
195                            serviceContextFunction);
196            }
197    
198            protected CommentManager getCommentManager() {
199                    if (_serviceTracker.isEmpty()) {
200                            return _defaultCommentManager;
201                    }
202    
203                    return _serviceTracker.getService();
204            }
205    
206            private final CommentManager _defaultCommentManager;
207            private final ServiceTracker<CommentManager, CommentManager>
208                    _serviceTracker;
209    
210    }