001/* 002 * (C) Copyright 2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 */ 019 020package org.nuxeo.ecm.platform.ec.notification.service; 021 022import java.util.ArrayList; 023import java.util.Arrays; 024import java.util.HashMap; 025import java.util.HashSet; 026import java.util.List; 027import java.util.Map; 028import java.util.Set; 029 030import org.apache.commons.logging.Log; 031import org.apache.commons.logging.LogFactory; 032import org.nuxeo.ecm.platform.ec.notification.NotificationImpl; 033import org.nuxeo.ecm.platform.notification.api.Notification; 034import org.nuxeo.ecm.platform.notification.api.NotificationRegistry; 035 036/** 037 * @author <a href="mailto:npaslaru@nuxeo.com">Narcis Paslaru</a> 038 * @author <a href="mailto:tmartins@nuxeo.com">Thierry Martins</a> 039 */ 040public class NotificationRegistryImpl implements NotificationRegistry { 041 042 private static final long serialVersionUID = 1L; 043 044 private static final Log log = LogFactory.getLog(NotificationRegistryImpl.class); 045 046 // maps EventId to a list of notifications 047 private final Map<String, List<Notification>> notificationRegistry = new HashMap<String, List<Notification>>(); 048 049 private final List<Notification> notificationList = new ArrayList<Notification>(); 050 051 @Override 052 public void clear() { 053 notificationRegistry.clear(); 054 } 055 056 @Override 057 public void registerNotification(Notification notif, List<String> events) { 058 if (notif.getName() == null) { 059 log.error("Notifications contributions must have a name"); 060 } 061 062 if (notif.getEnabled()) { 063 NotificationImpl notification = new NotificationImpl(notif.getName(), notif.getTemplate(), 064 notif.getChannel(), notif.getSubjectTemplate(), notif.getAutoSubscribed(), notif.getSubject(), 065 notif.getAvailableIn(), notif.getLabel()); 066 067 if (notif.getTemplateExpr() != null) { 068 notification.setTemplateExpr(notif.getTemplateExpr()); 069 } 070 071 if (notificationList.contains(notification)) { 072 unregisterNotification(notification, events); 073 } 074 notificationList.add(notification); 075 076 if (events != null && !events.isEmpty()) { 077 for (String event : events) { 078 List<Notification> regNotifs = getNotificationsForEvent(event); 079 if (!regNotifs.contains(notification)) { 080 regNotifs.add(notification); 081 } 082 } 083 } 084 } else { 085 unregisterNotification(notif, events); 086 } 087 } 088 089 @Override 090 @Deprecated 091 /** 092 * Please use unregisterNotification(Notification notif) instead. 093 * Deprecated since 5.7.2 094 */ 095 public void unregisterNotification(Notification notif, List<String> events) { 096 unregisterNotification(notif); 097 } 098 099 @Override 100 public void unregisterNotification(Notification notif) { 101 if (notif == null) { 102 log.warn("Try to unregister a null notification, do nothing"); 103 return; 104 } 105 106 NotificationImpl notification = new NotificationImpl(notif.getName(), notif.getTemplate(), notif.getChannel(), 107 notif.getSubjectTemplate(), notif.getAutoSubscribed(), notif.getSubject(), notif.getAvailableIn(), 108 notif.getLabel()); 109 110 if (notificationList.contains(notification)) { 111 notificationList.remove(notification); 112 } 113 114 for (String event : notificationRegistry.keySet()) { 115 for (int i = notificationRegistry.get(event).size() - 1; i >= 0; i--) { 116 List<Notification> regNotifs = notificationRegistry.get(event); 117 if (regNotifs.contains(notification)) { 118 regNotifs.remove(notification); 119 } 120 } 121 } 122 } 123 124 @Override 125 public Set<String> getNotificationEventNames() { 126 Set<String> ret = new HashSet<String>(); 127 for (String name : notificationRegistry.keySet()) { 128 if (!notificationRegistry.get(name).isEmpty()) { 129 ret.add(name); 130 } 131 } 132 return ret; 133 } 134 135 /** 136 * Gets the list of possible notifications for an event. 137 */ 138 @Override 139 public List<Notification> getNotificationsForEvent(String eventId) { 140 if (notificationRegistry.get(eventId) == null) { 141 notificationRegistry.put(eventId, new ArrayList<Notification>()); 142 } 143 144 return notificationRegistry.get(eventId); 145 } 146 147 @Override 148 public List<Notification> getNotifications() { 149 return notificationList; 150 } 151 152 public Map<String, List<Notification>> getNotificationRegistry() { 153 return notificationRegistry; 154 } 155 156 @Override 157 public List<Notification> getNotificationsForSubscriptions(String parentType) { 158 List<Notification> result = new ArrayList<Notification>(); 159 for (Notification notification : notificationList) { 160 if (notification.getAutoSubscribed()) { 161 continue; 162 } 163 String type = notification.getAvailableIn(); 164 if (type == null || "all".equals(type) || "*".equals(type)) { 165 result.add(notification); 166 } else { 167 String[] types = type.replace(",", " ").split(" "); 168 if (Arrays.asList(types).contains(parentType)) { 169 result.add(notification); 170 } 171 } 172 } 173 return result; 174 } 175 176}