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