001/* 002 * (C) Copyright 2007-2018 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 */ 019package org.nuxeo.ecm.platform.ec.notification.service; 020 021import java.util.ArrayList; 022import java.util.Arrays; 023import java.util.HashMap; 024import java.util.List; 025import java.util.Map; 026import java.util.Map.Entry; 027import java.util.Set; 028import java.util.stream.Collectors; 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:[email protected]">Narcis Paslaru</a> 038 * @author <a href="mailto:[email protected]">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<>(); 048 049 private final List<Notification> notificationList = new ArrayList<>(); 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); 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); 086 } 087 } 088 089 @Override 090 public void unregisterNotification(Notification notif) { 091 if (notif == null) { 092 log.warn("Try to unregister a null notification, do nothing"); 093 return; 094 } 095 096 NotificationImpl notification = new NotificationImpl(notif.getName(), notif.getTemplate(), notif.getChannel(), 097 notif.getSubjectTemplate(), notif.getAutoSubscribed(), notif.getSubject(), notif.getAvailableIn(), 098 notif.getLabel()); 099 100 notificationList.remove(notification); 101 notificationRegistry.values().forEach(notifications -> notifications.remove(notification)); 102 } 103 104 @Override 105 public Set<String> getNotificationEventNames() { 106 return notificationRegistry.entrySet() 107 .stream() 108 .filter(entry -> !entry.getValue().isEmpty()) 109 .map(Entry::getKey) 110 .collect(Collectors.toSet()); 111 } 112 113 /** 114 * Gets the list of possible notifications for an event. 115 */ 116 @Override 117 public List<Notification> getNotificationsForEvent(String eventId) { 118 return notificationRegistry.computeIfAbsent(eventId, k -> new ArrayList<>()); 119 } 120 121 @Override 122 public List<Notification> getNotifications() { 123 return notificationList; 124 } 125 126 /** 127 * @deprecated since 10.2, seems unused 128 */ 129 @Deprecated 130 public Map<String, List<Notification>> getNotificationRegistry() { 131 return notificationRegistry; 132 } 133 134 @Override 135 public List<Notification> getNotificationsForSubscriptions(String parentType) { 136 List<Notification> result = new ArrayList<>(); 137 for (Notification notification : notificationList) { 138 if (notification.getAutoSubscribed()) { 139 continue; 140 } 141 String type = notification.getAvailableIn(); 142 if (type == null || "all".equals(type) || "*".equals(type)) { 143 result.add(notification); 144 } else { 145 String[] types = type.replace(",", " ").split(" "); 146 if (Arrays.asList(types).contains(parentType)) { 147 result.add(notification); 148 } 149 } 150 } 151 return result; 152 } 153 154}