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, Map<String, UserNotificationHandler>>
132                    getUserNotificationHandlers() {
133    
134                    return Collections.unmodifiableMap(_userNotificationHandlers);
135            }
136    
137            /**
138             * Creates a human readable user notification feed entry for the user
139             * notification using an available compatible user notification handler.
140             *
141             * <p>
142             * This method finds the appropriate handler for the user notification by
143             * going through the available handler and asking them if they can handle
144             * the user notidication based on the portlet.
145             * </p>
146             *
147             * @param  userNotificationEvent the user notification event to be
148             *         translated to human readable form
149             * @return the user notification feed that is a human readable form of the
150             *         user notification record or <code>null</code> if a compatible
151             *         handler is not found
152             */
153            @Override
154            public UserNotificationFeedEntry interpret(
155                            String selector, UserNotificationEvent userNotificationEvent,
156                            ServiceContext serviceContext)
157                    throws PortalException {
158    
159                    Map<String, UserNotificationHandler> userNotificationHandlers =
160                            _userNotificationHandlers.get(selector);
161    
162                    if (userNotificationHandlers == null) {
163                            return null;
164                    }
165    
166                    UserNotificationHandler userNotificationHandler =
167                            userNotificationHandlers.get(userNotificationEvent.getType());
168    
169                    if (userNotificationHandler == null) {
170                            if (_log.isWarnEnabled()) {
171                                    _log.warn("No interpreter found for " + userNotificationEvent);
172                            }
173    
174                            return null;
175                    }
176    
177                    return userNotificationHandler.interpret(
178                            userNotificationEvent, serviceContext);
179            }
180    
181            @Override
182            public boolean isDeliver(
183                            long userId, String portletId, long classNameId,
184                            int notificationType, int deliveryType)
185                    throws PortalException, SystemException {
186    
187                    return isDeliver(
188                            userId, StringPool.BLANK, portletId, classNameId, notificationType,
189                            deliveryType, null);
190            }
191    
192            @Override
193            public boolean isDeliver(
194                            long userId, String selector, String portletId, long classNameId,
195                            int notificationType, int deliveryType,
196                            ServiceContext serviceContext)
197                    throws PortalException, SystemException {
198    
199                    Map<String, UserNotificationHandler> userNotificationHandlers =
200                            _userNotificationHandlers.get(selector);
201    
202                    if (userNotificationHandlers == null) {
203                            return false;
204                    }
205    
206                    UserNotificationHandler userNotificationHandler =
207                            userNotificationHandlers.get(portletId);
208    
209                    if (userNotificationHandler == null) {
210                            return false;
211                    }
212    
213                    return userNotificationHandler.isDeliver(
214                            userId, classNameId, notificationType, deliveryType,
215                            serviceContext);
216            }
217    
218            private static Log _log = LogFactoryUtil.getLog(
219                    UserNotificationManagerImpl.class);
220    
221            private Map<String, List<UserNotificationDefinition>>
222                    _userNotificationDefinitions =
223                            new HashMap<String, List<UserNotificationDefinition>>();
224            private Map<String, Map<String, UserNotificationHandler>>
225                    _userNotificationHandlers =
226                            new HashMap<String, Map<String, UserNotificationHandler>>();
227    
228    }