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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.notifications.UserNotificationDefinition;
022    import com.liferay.portal.kernel.notifications.UserNotificationFeedEntry;
023    import com.liferay.portal.kernel.notifications.UserNotificationHandler;
024    import com.liferay.portal.kernel.notifications.UserNotificationManager;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.model.UserNotificationEvent;
027    import com.liferay.portal.service.ServiceContext;
028    
029    import java.util.ArrayList;
030    import java.util.Collections;
031    import java.util.HashMap;
032    import java.util.List;
033    import java.util.Map;
034    
035    /**
036     * @author Jonathan Lee
037     * @author Brian Wing Shun Chan
038     */
039    public class UserNotificationManagerImpl implements UserNotificationManager {
040    
041            @Override
042            public void addUserNotificationDefinition(
043                    String portletId,
044                    UserNotificationDefinition userNotificationDefinition) {
045    
046                    List<UserNotificationDefinition> userNotificationDefinitions =
047                            _userNotificationDefinitions.get(portletId);
048    
049                    if (userNotificationDefinitions == null) {
050                            userNotificationDefinitions =
051                                    new ArrayList<UserNotificationDefinition>();
052    
053                            _userNotificationDefinitions.put(
054                                    portletId, userNotificationDefinitions);
055                    }
056    
057                    userNotificationDefinitions.add(userNotificationDefinition);
058            }
059    
060            /**
061             * Adds the use notification handler to the list of available handlers.
062             *
063             * @param userNotificationHandler the user notification handler
064             */
065            @Override
066            public void addUserNotificationHandler(
067                    UserNotificationHandler userNotificationHandler) {
068    
069                    String selector = userNotificationHandler.getSelector();
070    
071                    Map<String, UserNotificationHandler> userNotificationHandlers =
072                            _userNotificationHandlers.get(selector);
073    
074                    if (userNotificationHandlers == null) {
075                            userNotificationHandlers =
076                                    new HashMap<String, UserNotificationHandler>();
077    
078                            _userNotificationHandlers.put(selector, userNotificationHandlers);
079                    }
080    
081                    userNotificationHandlers.put(
082                            userNotificationHandler.getPortletId(), userNotificationHandler);
083            }
084    
085            @Override
086            public void deleteUserNotificationDefinitions(String portletId) {
087                    _userNotificationDefinitions.remove(portletId);
088            }
089    
090            @Override
091            public void deleteUserNotificationHandler(
092                    UserNotificationHandler userNotificationHandler) {
093    
094                    Map<String, UserNotificationHandler> userNotificationHandlers =
095                            _userNotificationHandlers.get(
096                                    userNotificationHandler.getSelector());
097    
098                    if (userNotificationHandlers == null) {
099                            return;
100                    }
101    
102                    userNotificationHandlers.remove(userNotificationHandler.getPortletId());
103            }
104    
105            @Override
106            public UserNotificationDefinition fetchUserNotificationDefinition(
107                    String portletId, long classNameId, int notificationType) {
108    
109                    List<UserNotificationDefinition> userNotificationDefinitions =
110                            _userNotificationDefinitions.get(portletId);
111    
112                    if (userNotificationDefinitions == null) {
113                            return null;
114                    }
115    
116                    for (UserNotificationDefinition userNotificationDefinition :
117                                    userNotificationDefinitions) {
118    
119                            if ((userNotificationDefinition.getClassNameId() == classNameId) &&
120                                    (userNotificationDefinition.getNotificationType() ==
121                                            notificationType)) {
122    
123                                    return userNotificationDefinition;
124                            }
125                    }
126    
127                    return null;
128            }
129    
130            @Override
131            public Map<String, List<UserNotificationDefinition>>
132                    getUserNotificationDefinitions() {
133    
134                    return Collections.unmodifiableMap(_userNotificationDefinitions);
135            }
136    
137            @Override
138            public Map<String, Map<String, UserNotificationHandler>>
139                    getUserNotificationHandlers() {
140    
141                    return Collections.unmodifiableMap(_userNotificationHandlers);
142            }
143    
144            /**
145             * Creates a human readable user notification feed entry for the user
146             * notification using an available compatible user notification handler.
147             *
148             * <p>
149             * This method finds the appropriate handler for the user notification by
150             * going through the available handler and asking them if they can handle
151             * the user notidication based on the portlet.
152             * </p>
153             *
154             * @param  userNotificationEvent the user notification event to be
155             *         translated to human readable form
156             * @return the user notification feed that is a human readable form of the
157             *         user notification record or <code>null</code> if a compatible
158             *         handler is not found
159             */
160            @Override
161            public UserNotificationFeedEntry interpret(
162                            String selector, UserNotificationEvent userNotificationEvent,
163                            ServiceContext serviceContext)
164                    throws PortalException {
165    
166                    Map<String, UserNotificationHandler> userNotificationHandlers =
167                            _userNotificationHandlers.get(selector);
168    
169                    if (userNotificationHandlers == null) {
170                            return null;
171                    }
172    
173                    UserNotificationHandler userNotificationHandler =
174                            userNotificationHandlers.get(userNotificationEvent.getType());
175    
176                    if (userNotificationHandler == null) {
177                            if (_log.isWarnEnabled()) {
178                                    _log.warn("No interpreter found for " + userNotificationEvent);
179                            }
180    
181                            return null;
182                    }
183    
184                    return userNotificationHandler.interpret(
185                            userNotificationEvent, serviceContext);
186            }
187    
188            @Override
189            public boolean isDeliver(
190                            long userId, String portletId, long classNameId,
191                            int notificationType, int deliveryType)
192                    throws PortalException, SystemException {
193    
194                    return isDeliver(
195                            userId, StringPool.BLANK, portletId, classNameId, notificationType,
196                            deliveryType, null);
197            }
198    
199            @Override
200            public boolean isDeliver(
201                            long userId, String selector, String portletId, long classNameId,
202                            int notificationType, int deliveryType,
203                            ServiceContext serviceContext)
204                    throws PortalException, SystemException {
205    
206                    Map<String, UserNotificationHandler> userNotificationHandlers =
207                            _userNotificationHandlers.get(selector);
208    
209                    if (userNotificationHandlers == null) {
210                            return false;
211                    }
212    
213                    UserNotificationHandler userNotificationHandler =
214                            userNotificationHandlers.get(portletId);
215    
216                    if (userNotificationHandler == null) {
217                            return false;
218                    }
219    
220                    return userNotificationHandler.isDeliver(
221                            userId, classNameId, notificationType, deliveryType,
222                            serviceContext);
223            }
224    
225            private static Log _log = LogFactoryUtil.getLog(
226                    UserNotificationManagerImpl.class);
227    
228            private Map<String, List<UserNotificationDefinition>>
229                    _userNotificationDefinitions =
230                            new HashMap<String, List<UserNotificationDefinition>>();
231            private Map<String, Map<String, UserNotificationHandler>>
232                    _userNotificationHandlers =
233                            new HashMap<String, Map<String, UserNotificationHandler>>();
234    
235    }