]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
9805c531155cb01be0e472f51bdd9e777f548c3f
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.nuxeo.listener;
2
3 import java.io.Serializable;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
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;
16
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>();
21         
22         static final String DOCMODEL_CONTEXT_PROPERTY_PREFIX = ScopeType.DEFAULT.getScopePrefix();
23         
24         public AbstractCSEventListenerImpl() {
25                 // Intentionally left blank
26         }
27         
28         /**
29          * Find out if we (the event listener) are registered (via tenant bindings config) to respond events.
30          */
31         @Override
32         public boolean isRegistered(Event event) {
33                 boolean result = false;
34                 
35                 if (event != null && event.getContext() != null) {
36                         result = getRepositoryNameList().contains(event.getContext().getRepositoryName());
37                 }
38                 
39                 return result;
40         }
41         
42         /**
43          * An event listener can be registered by multiple tenants, so we keep track of that here.
44          * 
45          * @return - the list of tenants/repositories that an event listener is registered with.
46          */
47         protected List<String> getRepositoryNameList() {
48                 String key = this.getClass().getName();
49                 List<String> result = mapOfrepositoryNames.get(key);
50                 
51                 if (result == null) synchronized(this) {
52                         result = new ArrayList<String>();
53                         mapOfrepositoryNames.put(key, result);
54                 }
55                 
56                 return result;
57         }
58         
59         /**
60          * The list of parameters (specified in a tenant's bindings) for event listeners
61          * @return
62          */
63         protected Map<String, Map<String, Map<String, String>>> getEventListenerParamsMap() {
64                 return eventListenerParamsMap;
65         }
66
67         /**
68          * Returns 'true' if this collection changed as a result of the call. 
69          */
70         @Override
71         public boolean register(String respositoryName, EventListenerConfig eventListenerConfig) {
72                 boolean result = false;
73                 
74                 // Using the repositoryName as a qualifier, register this event listener's name as specified in the tenant bindings.
75                 setName(respositoryName, eventListenerConfig.getId());
76                 
77                 // Register this event listener with the given repository name
78                 if (getRepositoryNameList().add(respositoryName)) {
79                         result = true;
80                 }
81                 
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) {
86                                 //
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
92                                         result = true;
93                                 }
94                                 //
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
100                                         result = true;
101                                 }
102                                 //
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);
109                                                 result = true;
110                                         }
111                                 }
112                         }
113                 }
114                 
115                 return result;
116         }
117         
118         protected void setName(String repositoryName, String eventListenerName) {
119                 nameMap.put(repositoryName, eventListenerName);
120         }
121
122         @Override
123         public Map<String, String> getParams(Event event) {
124                 Map<String, String> result = null;
125                 try {
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.
130                 }
131                 return result;
132         }
133         
134         @Override
135         public String getName(String repositoryName) {
136                 return nameMap.get(repositoryName);
137         }
138         
139         //
140         // Set a property in the document model's transient context.
141         //
142         @Override
143     public void setDocModelContextProperty(DocumentModel collectionObjectDocModel, String key, Serializable value) {
144         ScopedMap contextData = collectionObjectDocModel.getContextData();
145         contextData.putIfAbsent(DOCMODEL_CONTEXT_PROPERTY_PREFIX + key, value);        
146     }
147         
148     //
149     // Clear a property from the docModel's context
150         //
151         @Override
152         public void clearDocModelContextProperty(DocumentModel docModel, String key) {
153         ScopedMap contextData = docModel.getContextData();
154         contextData.remove(DOCMODEL_CONTEXT_PROPERTY_PREFIX + key);     
155         }
156
157 }