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