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.kernel.notifications;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.model.Portlet;
021    import com.liferay.portal.kernel.model.UserNotificationDeliveryConstants;
022    import com.liferay.portal.kernel.model.UserNotificationEvent;
023    import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
024    import com.liferay.portal.kernel.service.ServiceContext;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.registry.Registry;
027    import com.liferay.registry.RegistryUtil;
028    import com.liferay.registry.ServiceReference;
029    import com.liferay.registry.ServiceRegistration;
030    import com.liferay.registry.ServiceTracker;
031    import com.liferay.registry.ServiceTrackerCustomizer;
032    import com.liferay.registry.collections.ServiceRegistrationMap;
033    import com.liferay.registry.collections.ServiceRegistrationMapImpl;
034    import com.liferay.registry.collections.ServiceTrackerCollections;
035    import com.liferay.registry.collections.ServiceTrackerMap;
036    
037    import java.util.ArrayList;
038    import java.util.Collections;
039    import java.util.HashMap;
040    import java.util.List;
041    import java.util.Map;
042    import java.util.concurrent.ConcurrentHashMap;
043    
044    /**
045     * @author Jonathan Lee
046     * @author Roberto D??az
047     */
048    public class UserNotificationManagerUtil {
049    
050            public static void addUserNotificationDefinition(
051                    String portletId,
052                    UserNotificationDefinition userNotificationDefinition) {
053    
054                    _instance._addUserNotificationDefinition(
055                            portletId, userNotificationDefinition);
056            }
057    
058            public static void addUserNotificationHandler(
059                    UserNotificationHandler userNotificationHandler) {
060    
061                    _instance._addUserNotificationHandler(userNotificationHandler);
062            }
063    
064            public static void deleteUserNotificationDefinitions(String portletId) {
065                    _instance._deleteUserNotificationDefinitions(portletId);
066            }
067    
068            public static void deleteUserNotificationHandler(
069                    UserNotificationHandler userNotificationHandler) {
070    
071                    _instance._deleteUserNotificationHandler(userNotificationHandler);
072            }
073    
074            public static UserNotificationDefinition fetchUserNotificationDefinition(
075                    String portletId, long classNameId, int notificationType) {
076    
077                    return _instance._fetchUserNotificationDefinition(
078                            portletId, classNameId, notificationType);
079            }
080    
081            public static Map<String, List<UserNotificationDefinition>>
082                    getActiveUserNotificationDefinitions() {
083    
084                    return _instance._getUserNotificationDefinitions(true);
085            }
086    
087            public static Map<String, List<UserNotificationDefinition>>
088                    getUserNotificationDefinitions() {
089    
090                    return _instance._getUserNotificationDefinitions(false);
091            }
092    
093            public static Map<String, Map<String, UserNotificationHandler>>
094                    getUserNotificationHandlers() {
095    
096                    return Collections.unmodifiableMap(_instance._userNotificationHandlers);
097            }
098    
099            public static UserNotificationFeedEntry interpret(
100                            String selector, UserNotificationEvent userNotificationEvent,
101                            ServiceContext serviceContext)
102                    throws PortalException {
103    
104                    return _instance._interpret(
105                            selector, userNotificationEvent, serviceContext);
106            }
107    
108            public static boolean isDeliver(
109                            long userId, String portletId, long classNameId,
110                            int notificationType, int deliveryType)
111                    throws PortalException {
112    
113                    return _instance._isDeliver(
114                            userId, StringPool.BLANK, portletId, classNameId, notificationType,
115                            deliveryType, null);
116            }
117    
118            public static boolean isDeliver(
119                            long userId, String selector, String portletId, long classNameId,
120                            int notificationType, int deliveryType,
121                            ServiceContext serviceContext)
122                    throws PortalException {
123    
124                    return _instance._isDeliver(
125                            userId, selector, portletId, classNameId, notificationType,
126                            deliveryType, serviceContext);
127            }
128    
129            private UserNotificationManagerUtil() {
130                    Registry registry = RegistryUtil.getRegistry();
131    
132                    _userNotificationHandlerServiceTracker = registry.trackServices(
133                            UserNotificationHandler.class,
134                            new UserNotificationHandlerServiceTrackerCustomizer());
135    
136                    _userNotificationHandlerServiceTracker.open();
137            }
138    
139            private void _addUserNotificationDefinition(
140                    String portletId,
141                    UserNotificationDefinition userNotificationDefinition) {
142    
143                    Registry registry = RegistryUtil.getRegistry();
144    
145                    Map<String, Object> properties = new HashMap<>();
146    
147                    properties.put("javax.portlet.name", portletId);
148    
149                    ServiceRegistration<UserNotificationDefinition> serviceRegistration =
150                            registry.registerService(
151                                    UserNotificationDefinition.class, userNotificationDefinition,
152                                    properties);
153    
154                    List<ServiceRegistration<UserNotificationDefinition>>
155                            serviceRegistrations = new ArrayList<>();
156    
157                    List<ServiceRegistration<UserNotificationDefinition>>
158                            userNotificationServiceRegistrations =
159                                    _userNotificationDefinitionServiceRegistrations.get(portletId);
160    
161                    if ((userNotificationServiceRegistrations != null) &&
162                            !userNotificationServiceRegistrations.isEmpty()) {
163    
164                            serviceRegistrations.addAll(userNotificationServiceRegistrations);
165                    }
166    
167                    serviceRegistrations.add(serviceRegistration);
168    
169                    _userNotificationDefinitionServiceRegistrations.put(
170                            portletId, serviceRegistrations);
171            }
172    
173            private void _addUserNotificationHandler(
174                    UserNotificationHandler userNotificationHandler) {
175    
176                    Registry registry = RegistryUtil.getRegistry();
177    
178                    ServiceRegistration<UserNotificationHandler> serviceRegistration =
179                            registry.registerService(
180                                    UserNotificationHandler.class, userNotificationHandler);
181    
182                    _userNotificationHandlerServiceRegistrations.put(
183                            userNotificationHandler, serviceRegistration);
184            }
185    
186            private void _deleteUserNotificationDefinitions(String portletId) {
187                    List<ServiceRegistration<UserNotificationDefinition>>
188                            serviceRegistrations =
189                                    _userNotificationDefinitionServiceRegistrations.get(portletId);
190    
191                    for (ServiceRegistration<UserNotificationDefinition>
192                                    serviceRegistration : serviceRegistrations) {
193    
194                            serviceRegistration.unregister();
195                    }
196            }
197    
198            private void _deleteUserNotificationHandler(
199                    UserNotificationHandler userNotificationHandler) {
200    
201                    ServiceRegistration<UserNotificationHandler> serviceRegistration =
202                            _userNotificationHandlerServiceRegistrations.get(
203                                    userNotificationHandler);
204    
205                    if (serviceRegistration != null) {
206                            serviceRegistration.unregister();
207                    }
208            }
209    
210            private UserNotificationDefinition _fetchUserNotificationDefinition(
211                    String portletId, long classNameId, int notificationType) {
212    
213                    List<UserNotificationDefinition> userNotificationDefinitions =
214                            _userNotificationDefinitions.getService(portletId);
215    
216                    if (userNotificationDefinitions == null) {
217                            return null;
218                    }
219    
220                    for (UserNotificationDefinition userNotificationDefinition :
221                                    userNotificationDefinitions) {
222    
223                            if ((userNotificationDefinition.getClassNameId() == classNameId) &&
224                                    (userNotificationDefinition.getNotificationType() ==
225                                            notificationType)) {
226    
227                                    return userNotificationDefinition;
228                            }
229                    }
230    
231                    return null;
232            }
233    
234            private Map<String, List<UserNotificationDefinition>>
235                    _getUserNotificationDefinitions(boolean active) {
236    
237                    Map<String, List<UserNotificationDefinition>>
238                            userNotificationDefinitionsMap = new ConcurrentHashMap<>();
239    
240                    ServiceTrackerMap<String, List<UserNotificationDefinition>>
241                            userNotificationDefinitionsServiceTrackerMap =
242                                    _instance._userNotificationDefinitions;
243    
244                    for (String portletId :
245                                    userNotificationDefinitionsServiceTrackerMap.keySet()) {
246    
247                            if (active) {
248                                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
249                                            portletId);
250    
251                                    if (portlet == null) {
252                                            continue;
253                                    }
254                            }
255    
256                            userNotificationDefinitionsMap.put(
257                                    portletId,
258                                    userNotificationDefinitionsServiceTrackerMap.getService(
259                                            portletId));
260                    }
261    
262                    return Collections.unmodifiableMap(userNotificationDefinitionsMap);
263            }
264    
265            private UserNotificationFeedEntry _interpret(
266                            String selector, UserNotificationEvent userNotificationEvent,
267                            ServiceContext serviceContext)
268                    throws PortalException {
269    
270                    Map<String, UserNotificationHandler> userNotificationHandlers =
271                            _userNotificationHandlers.get(selector);
272    
273                    if (userNotificationHandlers == null) {
274                            return null;
275                    }
276    
277                    UserNotificationHandler userNotificationHandler =
278                            userNotificationHandlers.get(userNotificationEvent.getType());
279    
280                    if (userNotificationHandler == null) {
281                            if (_log.isWarnEnabled()) {
282                                    _log.warn("No interpreter found for " + userNotificationEvent);
283                            }
284    
285                            return null;
286                    }
287    
288                    return userNotificationHandler.interpret(
289                            userNotificationEvent, serviceContext);
290            }
291    
292            private boolean _isDeliver(
293                            long userId, String selector, String portletId, long classNameId,
294                            int notificationType, int deliveryType,
295                            ServiceContext serviceContext)
296                    throws PortalException {
297    
298                    Map<String, UserNotificationHandler> userNotificationHandlers =
299                            _userNotificationHandlers.get(selector);
300    
301                    if (userNotificationHandlers == null) {
302                            return false;
303                    }
304    
305                    UserNotificationHandler userNotificationHandler =
306                            userNotificationHandlers.get(portletId);
307    
308                    if (userNotificationHandler == null) {
309                            if (deliveryType == UserNotificationDeliveryConstants.TYPE_EMAIL) {
310                                    return true;
311                            }
312    
313                            return false;
314                    }
315    
316                    return userNotificationHandler.isDeliver(
317                            userId, classNameId, notificationType, deliveryType,
318                            serviceContext);
319            }
320    
321            private static final Log _log = LogFactoryUtil.getLog(
322                    UserNotificationManagerUtil.class);
323    
324            private static final UserNotificationManagerUtil _instance =
325                    new UserNotificationManagerUtil();
326    
327            private final ServiceTrackerMap<String, List<UserNotificationDefinition>>
328                    _userNotificationDefinitions =
329                            ServiceTrackerCollections.openMultiValueMap(
330                                    UserNotificationDefinition.class, "javax.portlet.name");
331            private final ConcurrentHashMap
332                    <String, List<ServiceRegistration<UserNotificationDefinition>>>
333                            _userNotificationDefinitionServiceRegistrations =
334                                    new ConcurrentHashMap<>();
335            private final Map<String, Map<String, UserNotificationHandler>>
336                    _userNotificationHandlers = new ConcurrentHashMap<>();
337            private final ServiceRegistrationMap<UserNotificationHandler>
338                    _userNotificationHandlerServiceRegistrations =
339                            new ServiceRegistrationMapImpl<>();
340            private final
341                    ServiceTracker<UserNotificationHandler, UserNotificationHandler>
342                            _userNotificationHandlerServiceTracker;
343    
344            private class UserNotificationHandlerServiceTrackerCustomizer
345                    implements ServiceTrackerCustomizer
346                            <UserNotificationHandler, UserNotificationHandler> {
347    
348                    @Override
349                    public UserNotificationHandler addingService(
350                            ServiceReference<UserNotificationHandler> serviceReference) {
351    
352                            Registry registry = RegistryUtil.getRegistry();
353    
354                            UserNotificationHandler userNotificationHandler =
355                                    registry.getService(serviceReference);
356    
357                            String selector = userNotificationHandler.getSelector();
358    
359                            Map<String, UserNotificationHandler> userNotificationHandlers =
360                                    _userNotificationHandlers.get(selector);
361    
362                            if (userNotificationHandlers == null) {
363                                    userNotificationHandlers = new HashMap<>();
364    
365                                    _userNotificationHandlers.put(
366                                            selector, userNotificationHandlers);
367                            }
368    
369                            userNotificationHandlers.put(
370                                    userNotificationHandler.getPortletId(),
371                                    userNotificationHandler);
372    
373                            return userNotificationHandler;
374                    }
375    
376                    @Override
377                    public void modifiedService(
378                            ServiceReference<UserNotificationHandler> serviceReference,
379                            UserNotificationHandler userNotificationHandler) {
380                    }
381    
382                    @Override
383                    public void removedService(
384                            ServiceReference<UserNotificationHandler> serviceReference,
385                            UserNotificationHandler userNotificationHandler) {
386    
387                            Registry registry = RegistryUtil.getRegistry();
388    
389                            registry.ungetService(serviceReference);
390    
391                            Map<String, UserNotificationHandler> userNotificationHandlers =
392                                    _userNotificationHandlers.get(
393                                            userNotificationHandler.getSelector());
394    
395                            if (userNotificationHandlers == null) {
396                                    return;
397                            }
398    
399                            userNotificationHandlers.remove(
400                                    userNotificationHandler.getPortletId());
401                    }
402    
403            }
404    
405    }