001    /**
002     * Copyright (c) 2000-2013 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.notifications;
016    
017    import com.liferay.portal.kernel.notifications.Channel;
018    import com.liferay.portal.kernel.notifications.ChannelException;
019    import com.liferay.portal.kernel.notifications.ChannelHub;
020    import com.liferay.portal.kernel.notifications.ChannelHubManager;
021    import com.liferay.portal.kernel.notifications.ChannelListener;
022    import com.liferay.portal.kernel.notifications.DuplicateChannelHubException;
023    import com.liferay.portal.kernel.notifications.NotificationEvent;
024    import com.liferay.portal.kernel.notifications.UnknownChannelHubException;
025    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
026    
027    import java.util.Collection;
028    import java.util.Collections;
029    import java.util.List;
030    import java.util.concurrent.ConcurrentHashMap;
031    import java.util.concurrent.ConcurrentMap;
032    
033    /**
034     * @author Edward Han
035     * @author Brian Wing Shun
036     * @author Shuyang Zhou
037     */
038    @DoPrivileged
039    public class ChannelHubManagerImpl implements ChannelHubManager {
040    
041            public void confirmDelivery(
042                            long companyId, long userId,
043                            Collection<String> notificationEventUuids)
044                    throws ChannelException {
045    
046                    confirmDelivery(companyId, userId, notificationEventUuids, false);
047            }
048    
049            public void confirmDelivery(
050                            long companyId, long userId,
051                            Collection<String> notificationEventUuids, boolean archive)
052                    throws ChannelException {
053    
054                    ChannelHub channelHub = getChannelHub(companyId);
055    
056                    channelHub.confirmDelivery(userId, notificationEventUuids, archive);
057            }
058    
059            public void confirmDelivery(
060                            long companyId, long userId, String notificationEventUuid)
061                    throws ChannelException {
062    
063                    confirmDelivery(companyId, userId, notificationEventUuid, false);
064            }
065    
066            public void confirmDelivery(
067                            long companyId, long userId, String notificationEventUuid,
068                            boolean archive)
069                    throws ChannelException {
070    
071                    ChannelHub channelHub = getChannelHub(companyId);
072    
073                    channelHub.confirmDelivery(userId, notificationEventUuid, archive);
074            }
075    
076            public Channel createChannel(long companyId, long userId)
077                    throws ChannelException {
078    
079                    ChannelHub channelHub = getChannelHub(companyId);
080    
081                    return channelHub.createChannel(userId);
082            }
083    
084            public ChannelHub createChannelHub(long companyId) throws ChannelException {
085                    ChannelHub channelHub = _channelHub.clone(companyId);
086    
087                    if (_channelHubs.putIfAbsent(companyId, channelHub) != null) {
088                            throw new DuplicateChannelHubException(
089                                    "Channel already exists with company id " + companyId);
090                    }
091    
092                    return channelHub;
093            }
094    
095            public void deleteUserNotificiationEvent(
096                            long companyId, long userId, String notificationEventUuid)
097                    throws ChannelException {
098    
099                    ChannelHub channelHub = getChannelHub(companyId);
100    
101                    channelHub.deleteUserNotificiationEvent(userId, notificationEventUuid);
102            }
103    
104            public void deleteUserNotificiationEvents(
105                            long companyId, long userId,
106                            Collection<String> notificationEventUuids)
107                    throws ChannelException {
108    
109                    ChannelHub channelHub = getChannelHub(companyId);
110    
111                    channelHub.deleteUserNotificiationEvents(
112                            userId, notificationEventUuids);
113            }
114    
115            public void destroyChannel(long companyId, long userId)
116                    throws ChannelException {
117    
118                    ChannelHub channelHub = getChannelHub(companyId);
119    
120                    channelHub.destroyChannel(userId);
121            }
122    
123            public void destroyChannelHub(long companyId) throws ChannelException {
124                    ChannelHub channelHub = _channelHubs.remove(companyId);
125    
126                    if (channelHub != null) {
127                            channelHub.destroy();
128                    }
129            }
130    
131            public ChannelHub fetchChannelHub(long companyId) throws ChannelException {
132                    return fetchChannelHub(companyId, false);
133            }
134    
135            public ChannelHub fetchChannelHub(long companyId, boolean createIfAbsent)
136                    throws ChannelException {
137    
138                    ChannelHub channelHub = _channelHubs.get(companyId);
139    
140                    if (channelHub == null) {
141                            synchronized(_channelHubs) {
142                                    channelHub = _channelHubs.get(companyId);
143    
144                                    if (channelHub == null) {
145                                            if (createIfAbsent) {
146                                                    channelHub = createChannelHub(companyId);
147                                            }
148                                    }
149                            }
150                    }
151    
152                    return channelHub;
153            }
154    
155            public List<NotificationEvent> fetchNotificationEvents(
156                            long companyId, long userId, boolean flush)
157                    throws ChannelException {
158    
159                    ChannelHub channelHub = fetchChannelHub(companyId);
160    
161                    if (channelHub == null) {
162                            return Collections.emptyList();
163                    }
164    
165                    return channelHub.fetchNotificationEvents(userId, flush);
166    
167            }
168    
169            public void flush() throws ChannelException {
170                    for (ChannelHub channelHub : _channelHubs.values()) {
171                            channelHub.flush();
172                    }
173            }
174    
175            public void flush(long companyId) throws ChannelException {
176                    ChannelHub channelHub = fetchChannelHub(companyId);
177    
178                    if (channelHub != null) {
179                            channelHub.flush();
180                    }
181            }
182    
183            public void flush(long companyId, long userId, long timestamp)
184                    throws ChannelException {
185    
186                    ChannelHub channelHub = fetchChannelHub(companyId);
187    
188                    if (channelHub != null) {
189                            channelHub.flush(userId, timestamp);
190                    }
191            }
192    
193            public Channel getChannel(long companyId, long userId)
194                    throws ChannelException {
195    
196                    return getChannel(companyId, userId, false);
197            }
198    
199            public Channel getChannel(
200                            long companyId, long userId, boolean createIfAbsent)
201                    throws ChannelException {
202    
203                    ChannelHub channelHub = getChannelHub(companyId, createIfAbsent);
204    
205                    return channelHub.getChannel(userId, createIfAbsent);
206            }
207    
208            public ChannelHub getChannelHub(long companyId) throws ChannelException {
209                    return getChannelHub(companyId, false);
210            }
211    
212            public ChannelHub getChannelHub(long companyId, boolean createIfAbsent)
213                    throws ChannelException {
214    
215                    ChannelHub channelHub = fetchChannelHub(companyId, createIfAbsent);
216    
217                    if (channelHub == null) {
218                            throw new UnknownChannelHubException(
219                                    "No channel exists with company id " + companyId);
220                    }
221    
222                    return channelHub;
223            }
224    
225            public List<NotificationEvent> getNotificationEvents(
226                            long companyId, long userId)
227                    throws ChannelException {
228    
229                    ChannelHub channelHub = getChannelHub(companyId);
230    
231                    return channelHub.getNotificationEvents(userId);
232            }
233    
234            public List<NotificationEvent> getNotificationEvents(
235                            long companyId, long userId, boolean flush)
236                    throws ChannelException {
237    
238                    ChannelHub channelHub = getChannelHub(companyId);
239    
240                    return channelHub.getNotificationEvents(userId, flush);
241            }
242    
243            public Collection<Long> getUserIds(long companyId) throws ChannelException {
244                    ChannelHub channelHub = getChannelHub(companyId);
245    
246                    return channelHub.getUserIds();
247            }
248    
249            public void registerChannelListener(
250                            long companyId, long userId, ChannelListener channelListener)
251                    throws ChannelException {
252    
253                    ChannelHub channelHub = getChannelHub(companyId);
254    
255                    channelHub.registerChannelListener(userId, channelListener);
256            }
257    
258            public void removeTransientNotificationEvents(
259                            long companyId, long userId,
260                            Collection<NotificationEvent> notificationEvents)
261                    throws ChannelException {
262    
263                    ChannelHub channelHub = getChannelHub(companyId);
264    
265                    channelHub.removeTransientNotificationEvents(
266                            userId, notificationEvents);
267            }
268    
269            public void removeTransientNotificationEventsByUuid(
270                            long companyId, long userId,
271                            Collection<String> notificationEventUuids)
272                    throws ChannelException {
273    
274                    ChannelHub channelHub = getChannelHub(companyId);
275    
276                    channelHub.removeTransientNotificationEventsByUuid(
277                            userId, notificationEventUuids);
278            }
279    
280            public void sendNotificationEvent(
281                            long companyId, long userId, NotificationEvent notificationEvent)
282                    throws ChannelException {
283    
284                    ChannelHub channelHub = getChannelHub(companyId);
285    
286                    channelHub.sendNotificationEvent(userId, notificationEvent);
287            }
288    
289            public void sendNotificationEvents(
290                            long companyId, long userId,
291                            Collection<NotificationEvent> notificationEvents)
292                    throws ChannelException {
293    
294                    ChannelHub channelHub = getChannelHub(companyId);
295    
296                    channelHub.sendNotificationEvents(userId, notificationEvents);
297            }
298    
299            public void setChannelHubPrototype(ChannelHub channelHub) {
300                    _channelHub = channelHub;
301            }
302    
303            public void unregisterChannelListener(
304                            long companyId, long userId, ChannelListener channelListener)
305                    throws ChannelException {
306    
307                    ChannelHub channelHub = getChannelHub(companyId);
308    
309                    channelHub.unregisterChannelListener(userId, channelListener);
310            }
311    
312            private ChannelHub _channelHub;
313            private ConcurrentMap<Long, ChannelHub> _channelHubs =
314                    new ConcurrentHashMap<Long, ChannelHub>();
315    
316    }