1 package org.collectionspace.services.nuxeo.listener;
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.HashMap;
9 import org.collectionspace.services.common.api.Tools;
10 import org.collectionspace.services.config.tenant.EventListenerConfig;
11 import org.collectionspace.services.config.tenant.Param;
12 import org.nuxeo.common.collections.ScopeType;
13 import org.nuxeo.common.collections.ScopedMap;
14 import org.nuxeo.ecm.core.api.DocumentModel;
15 import org.nuxeo.ecm.core.event.Event;
17 public abstract class AbstractCSEventListenerImpl implements CSEventListener {
18 private static Map<String, List<String>> mapOfrepositoryNames = new HashMap<String, List<String>>(); // <className, repositoryName>
19 private static Map<String, Map<String, Map<String, String>>> eventListenerParamsMap = new HashMap<String, Map<String, Map<String, String>>>(); // <repositoryName, Map<EventListenerId, Map<key, value>>>
20 private static Map<String, String> nameMap = new HashMap<String, String>();
22 public static final String DOCMODEL_CONTEXT_PROPERTY_PREFIX = ScopeType.DEFAULT.getScopePrefix();
24 public AbstractCSEventListenerImpl() {
25 // Intentionally left blank
29 * Find out if we (the event listener) are registered (via tenant bindings config) to respond events.
32 public boolean isRegistered(Event event) {
33 boolean result = false;
35 if (event != null && event.getContext() != null) {
36 result = getRepositoryNameList().contains(event.getContext().getRepositoryName());
43 * An event listener can be registered by multiple tenants, so we keep track of that here.
45 * @return - the list of tenants/repositories that an event listener is registered with.
47 protected List<String> getRepositoryNameList() {
48 String key = this.getClass().getName();
49 List<String> result = mapOfrepositoryNames.get(key);
51 if (result == null) synchronized(this) {
52 result = new ArrayList<String>();
53 mapOfrepositoryNames.put(key, result);
60 * The list of parameters (specified in a tenant's bindings) for event listeners
63 protected Map<String, Map<String, Map<String, String>>> getEventListenerParamsMap() {
64 return eventListenerParamsMap;
68 * Returns 'true' if this collection changed as a result of the call.
71 public boolean register(String respositoryName, EventListenerConfig eventListenerConfig) {
72 boolean result = false;
74 // Using the repositoryName as a qualifier, register this event listener's name as specified in the tenant bindings.
75 setName(respositoryName, eventListenerConfig.getId());
77 // Register this event listener with the given repository name
78 if (getRepositoryNameList().add(respositoryName)) {
82 if (eventListenerConfig.getParamList() != null) {
83 // Set this event listeners parameters, if any. Params are qualified with the repositoryName since multiple tenants might be registering the same event listener but with different params.
84 List<Param> paramList = eventListenerConfig.getParamList().getParam(); // values from the tenant bindings that we need to copy into the event listener
85 if (paramList != null) {
87 // Get the list of event listeners for a given repository
88 Map<String, Map<String, String>> eventListenerRepoParams = getEventListenerParamsMap().get(respositoryName); // Get the set of event listers for a given repository
89 if (eventListenerRepoParams == null) {
90 eventListenerRepoParams = new HashMap<String, Map<String, String>>();
91 getEventListenerParamsMap().put(respositoryName, eventListenerRepoParams); // create and put an empty map
95 // Get the list of params for a given event listener for a given repository
96 Map<String, String> eventListenerParams = eventListenerRepoParams.get(eventListenerConfig.getId()); // Get the set of params for a given event listener for a given repository
97 if (eventListenerParams == null) {
98 eventListenerParams = new HashMap<String, String>();
99 eventListenerRepoParams.put(eventListenerConfig.getId(), eventListenerParams); // create and put an empty map
103 // copy all the values from the tenant bindings into the event listener
104 for (Param params : paramList) {
105 String key = params.getKey();
106 String value = params.getValue();
107 if (Tools.notBlank(key)) {
108 eventListenerParams.put(key, value);
118 protected void setName(String repositoryName, String eventListenerName) {
119 nameMap.put(repositoryName, eventListenerName);
123 public Map<String, String> getParams(Event event) {
124 Map<String, String> result = null;
126 String repositoryName = event.getContext().getRepositoryName();
127 result = getEventListenerParamsMap().get(repositoryName).get(getName(repositoryName)); // We need to qualify with the repositoryName since this event listener might be register by multiple tenants using different params
128 } catch (NullPointerException e) {
129 // Do nothing. Just means no params were configured.
135 public String getName(String repositoryName) {
136 return nameMap.get(repositoryName);
140 // Set a property in the document model's transient context.
143 public void setDocModelContextProperty(DocumentModel collectionObjectDocModel, String key, Serializable value) {
144 ScopedMap contextData = collectionObjectDocModel.getContextData();
145 contextData.putIfAbsent(DOCMODEL_CONTEXT_PROPERTY_PREFIX + key, value);
149 // Clear a property from the docModel's context
152 public void clearDocModelContextProperty(DocumentModel docModel, String key) {
153 ScopedMap contextData = docModel.getContextData();
154 contextData.remove(DOCMODEL_CONTEXT_PROPERTY_PREFIX + key);