]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
36beffc0a16f593f89879d701be88c876ddf934f
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.listener.botgarden;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9
10 import org.apache.commons.lang.StringUtils;
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.collectionspace.services.batch.nuxeo.UpdateAccessCodeBatchJob;
14 import org.collectionspace.services.client.workflow.WorkflowClient;
15 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectBotGardenConstants;
16 import org.collectionspace.services.collectionobject.nuxeo.CollectionObjectConstants;
17 import org.collectionspace.services.common.ResourceMap;
18 import org.collectionspace.services.common.invocable.InvocationResults;
19 import org.collectionspace.services.common.relation.nuxeo.RelationConstants;
20 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
21 import org.collectionspace.services.taxonomy.nuxeo.TaxonBotGardenConstants;
22 import org.collectionspace.services.taxonomy.nuxeo.TaxonConstants;
23 import org.jboss.resteasy.spi.ResteasyProviderFactory;
24 import org.nuxeo.ecm.core.api.DocumentModel;
25 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
26 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
27 import org.nuxeo.ecm.core.event.Event;
28 import org.nuxeo.ecm.core.event.EventContext;
29 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
30
31 /**
32  * A listener that updates the access code on taxon records when collectionobjects
33  * or taxon records are created or modified.
34  * 
35  * @see org.collectionspace.services.batch.nuxeo.UpdateAccessCodeBatchJob
36  * @author ray
37  *
38  */
39 public class UpdateAccessCodeListener extends AbstractCSEventListenerImpl {
40         final Log logger = LogFactory.getLog(UpdateAccessCodeListener.class);
41
42         public static final String PREVIOUS_DEAD_FLAG_PROPERTY_NAME = "UpdateAccessCodeListener.previousDeadFlag";
43         public static final String PREVIOUS_TAXON_NAMES_PROPERTY_NAME = "UpdateAccessCodeListener.previousTaxonNames";
44         public static final String PREVIOUS_ACCESS_CODE_PROPERTY_NAME = "UpdateAccessCodeListener.previousAccessCode";
45         public static final String DELETED_RELATION_PARENT_CSID_PROPERTY_NAME = "UpdateAccessCodeListener.deletedRelationParentCsid";
46         
47         private static final String[] TAXON_PATH_ELEMENTS = CollectionObjectBotGardenConstants.TAXON_FIELD_NAME.split("/");
48         private static final String TAXONOMIC_IDENT_GROUP_LIST_FIELD_NAME = TAXON_PATH_ELEMENTS[0];
49         private static final String TAXON_FIELD_NAME = TAXON_PATH_ELEMENTS[2];
50
51         public void handleEvent(Event event) {
52                 EventContext ec = event.getContext();
53                 
54                 if (ec instanceof DocumentEventContext) {
55                         DocumentEventContext context = (DocumentEventContext) ec;
56                         DocumentModel doc = context.getSourceDocument();
57
58                         logger.debug("docType=" + doc.getType());
59
60                         if (doc.getType().startsWith(CollectionObjectConstants.NUXEO_DOCTYPE) &&
61                                         !doc.isVersion() && 
62                                         !doc.isProxy() && 
63                                         !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
64                                 
65                                 if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
66                                         // Stash the previous dead flag and taxonomic ident values, so they can be retrieved in the documentModified handler.
67                                         
68                                         DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
69
70                                         String previousDeadFlag = (String) previousDoc.getProperty(CollectionObjectBotGardenConstants.DEAD_FLAG_SCHEMA_NAME, 
71                                                         CollectionObjectBotGardenConstants.DEAD_FLAG_FIELD_NAME);
72                                         context.setProperty(PREVIOUS_DEAD_FLAG_PROPERTY_NAME, previousDeadFlag);
73                                         
74                                         List<String> previousTaxonNames = getTaxonNames(previousDoc);
75                                         context.setProperty(PREVIOUS_TAXON_NAMES_PROPERTY_NAME, previousTaxonNames.toArray(new String[previousTaxonNames.size()]));
76                                 }
77                                 else {
78                                         boolean deadFlagChanged = false;
79                                         Set<String> deletedTaxonNames = null;
80                                         Set<String> addedTaxonNames = null;
81                                         
82                                         if (event.getName().equals(DocumentEventTypes.DOCUMENT_UPDATED)) {                                              
83                                                 // As an optimization, check if the dead flag of the collectionobject has
84                                                 // changed, or if the taxonomic identification has changed. If so, we need to
85                                                 // update the access codes of referenced taxon records.
86
87                                                 String previousDeadFlag = (String) context.getProperty(PREVIOUS_DEAD_FLAG_PROPERTY_NAME);
88                                                 String currentDeadFlag = (String) doc.getProperty(CollectionObjectBotGardenConstants.DEAD_FLAG_SCHEMA_NAME, 
89                                                                 CollectionObjectBotGardenConstants.DEAD_FLAG_FIELD_NAME);
90
91                                                 if (previousDeadFlag == null) {
92                                                         previousDeadFlag = "";
93                                                 }
94                                                 
95                                                 if (currentDeadFlag == null) {
96                                                         currentDeadFlag = "";
97                                                 }
98                                                                                                 
99                                                 if (previousDeadFlag.equals(currentDeadFlag)) {
100                                                         logger.debug("dead flag not changed: previousDeadFlag=" + previousDeadFlag + " currentDeadFlag=" + currentDeadFlag);
101                                                 }
102                                                 else {
103                                                         logger.debug("dead flag changed: previousDeadFlag=" + previousDeadFlag + " currentDeadFlag=" + currentDeadFlag);
104                                                         deadFlagChanged = true;
105                                                 }
106                                                 
107                                                 List<String> previousTaxonNames = Arrays.asList((String[]) context.getProperty(PREVIOUS_TAXON_NAMES_PROPERTY_NAME));
108                                                 List<String> currentTaxonNames = getTaxonNames(doc);
109                                                 
110                                                 deletedTaxonNames = findDeletedTaxonNames(previousTaxonNames, currentTaxonNames);
111                                                 logger.debug("found deleted taxon names: " + StringUtils.join(deletedTaxonNames, ", "));
112
113                                                 addedTaxonNames = findAddedTaxonNames(previousTaxonNames, currentTaxonNames);
114                                                 logger.debug("found added taxon names: " + StringUtils.join(addedTaxonNames, ", "));                                    
115                                         }
116                                         else if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
117                                                 deadFlagChanged = true;
118                                         }
119                                         
120                                         UpdateAccessCodeBatchJob updater = createUpdater();
121                                         
122                                         if (deadFlagChanged) {
123                                                 String collectionObjectCsid = doc.getName();
124                                                 
125                                                 try {
126                                                         // Pass false for the second parameter to updateReferencedAccessCodes, so that it doesn't
127                                                         // propagate changes up the taxon hierarchy. Propagation is taken care of by this
128                                                         // event handler: As taxon records are modified, this handler executes, and updates the
129                                                         // parent.
130                                                 
131                                                         InvocationResults results = updater.updateReferencedAccessCodes(collectionObjectCsid, false);
132                         
133                                                         logger.debug("updateReferencedAccessCodes complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
134                                                 }
135                                                 catch (Exception e) {
136                                                         logger.error(e.getMessage(), e);
137                                                 }
138                                         }
139                                         else {
140                                                 // If the dead flag didn't change, we still need to recalculate the access codes of
141                                                 // any taxonomic idents that were added.
142                                                 
143                                                 if (addedTaxonNames != null) {
144                                                         for (String addedTaxonName : addedTaxonNames) {
145                                                                 logger.debug("updating added taxon: " + addedTaxonName);
146
147                                                                 try {                                                                   
148                                                                         InvocationResults results = updater.updateAccessCode(addedTaxonName, false);
149                                                 
150                                                                         logger.debug("updateAccessCode complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
151                                                                 }
152                                                                 catch (Exception e) {
153                                                                         logger.error(e.getMessage(), e);
154                                                                 }                                               
155                                                         }                                                       
156                                                 }
157                                         }
158                                         
159                                         if (deletedTaxonNames != null) {
160                                                 // If any taxonomic idents were removed from the collectionobject, they need to have their
161                                                 // access codes recalculated.
162
163                                                 for (String deletedTaxonName : deletedTaxonNames) {
164                                                         logger.debug("updating deleted taxon: " + deletedTaxonName);
165
166                                                         try {                                                                   
167                                                                 InvocationResults results = updater.updateAccessCode(deletedTaxonName, false);
168                                         
169                                                                 logger.debug("updateAccessCode complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
170                                                         }
171                                                         catch (Exception e) {
172                                                                 logger.error(e.getMessage(), e);
173                                                         }                                               
174                                                 }
175                                         }
176                                 }
177                         }
178                         else if (doc.getType().startsWith(TaxonConstants.NUXEO_DOCTYPE) &&
179                                         !doc.isVersion() && 
180                                         !doc.isProxy() && 
181                                         !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
182                                 
183                                 if (event.getName().equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {                                     
184                                         // Stash the previous access code value, so it can be retrieved in the documentModified handler.
185
186                                         DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
187                                         String previousAccessCode = (String) previousDoc.getProperty(TaxonBotGardenConstants.ACCESS_CODE_SCHEMA_NAME, TaxonBotGardenConstants.ACCESS_CODE_FIELD_NAME);
188
189                                         context.setProperty(PREVIOUS_ACCESS_CODE_PROPERTY_NAME, previousAccessCode);
190                                 }
191                                 else {
192                                         boolean updateRequired = false;
193
194                                         if (event.getName().equals(DocumentEventTypes.DOCUMENT_UPDATED)) {                              
195                                                 // As an optimization, check if the access code of the taxon has
196                                                 // changed. We only need to update the access code of the parent taxon
197                                                 // record if it has.
198
199                                                 String previousAccessCode = (String) context.getProperty(PREVIOUS_ACCESS_CODE_PROPERTY_NAME);
200                                                 String currentAccessCode = (String) doc.getProperty(TaxonBotGardenConstants.ACCESS_CODE_SCHEMA_NAME, TaxonBotGardenConstants.ACCESS_CODE_FIELD_NAME);
201
202                                                 if (previousAccessCode == null) {
203                                                         previousAccessCode = "";
204                                                 }
205                                                 
206                                                 if (currentAccessCode == null) {
207                                                         currentAccessCode = "";
208                                                 }
209                                                 
210                                                 if (previousAccessCode.equals(currentAccessCode)) {
211                                                         logger.debug("update not required: previousAccessCode=" + previousAccessCode + " currentAccessCode=" + currentAccessCode);
212                                                 }
213                                                 else {
214                                                         logger.debug("update required: previousAccessCode=" + previousAccessCode + " currentAccessCode=" + currentAccessCode);
215                                                         updateRequired = true;
216                                                 }
217                                         }
218                                         else if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
219                                                 updateRequired = true;  
220                                         }
221                                 
222                                         if (updateRequired) {
223                                                 String taxonCsid = doc.getName();
224                                                 
225                                                 try {
226                                                         // Pass false for the second parameter to updateReferencedAccessCodes, so that it doesn't
227                                                         // propagate changes up the taxon hierarchy. Propagation is taken care of by this
228                                                         // event handler: As taxon records are modified, this handler executes, and updates the
229                                                         // parent.
230                                                         
231                                                         InvocationResults results = createUpdater().updateParentAccessCode(taxonCsid, false);
232                         
233                                                         logger.debug("updateParentAccessCode complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
234                                                 }
235                                                 catch (Exception e) {
236                                                         logger.error(e.getMessage(), e);
237                                                 }
238                                         }
239                                 }
240                         }
241                         else if (doc.getType().equals(RelationConstants.NUXEO_DOCTYPE) &&
242                                         !doc.isVersion() && 
243                                         !doc.isProxy()) {
244                                 
245                                 if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
246                                         String subjectDocType = (String) doc.getProperty(RelationConstants.SUBJECT_DOCTYPE_SCHEMA_NAME, RelationConstants.SUBJECT_DOCTYPE_FIELD_NAME);
247                                         String objectDocType = (String) doc.getProperty(RelationConstants.OBJECT_DOCTYPE_SCHEMA_NAME, RelationConstants.OBJECT_DOCTYPE_FIELD_NAME);;
248                                         String relationType = (String) doc.getProperty(RelationConstants.TYPE_SCHEMA_NAME, RelationConstants.TYPE_FIELD_NAME);
249         
250                                         logger.debug("subjectDocType=" + subjectDocType + " objectDocType=" + objectDocType + " relationType=" + relationType);
251         
252                                         if (subjectDocType.equals(TaxonConstants.NUXEO_DOCTYPE) && objectDocType.equals(TaxonConstants.NUXEO_DOCTYPE) && relationType.equals(RelationConstants.BROADER_TYPE)) {
253                                                 String parentTaxonCsid = (String) doc.getProperty(RelationConstants.OBJECT_CSID_SCHEMA_NAME, RelationConstants.OBJECT_CSID_FIELD_NAME);
254                                                 logger.debug("child added, updating parent taxon: parentTaxonCsid=" + parentTaxonCsid);
255                                                 
256                                                 try {                                                   
257                                                         InvocationResults results = createUpdater().updateAccessCode(parentTaxonCsid, false);
258                                                         
259                                                         logger.debug("updateAccessCode complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
260                                                 }
261                                                 catch (Exception e) {
262                                                         logger.error(e.getMessage(), e);
263                                                 }
264                                         }
265                                 }
266                                 else if (event.getName().equals(DocumentEventTypes.ABOUT_TO_REMOVE)) {
267                                         String subjectDocType = (String) doc.getProperty(RelationConstants.SUBJECT_DOCTYPE_SCHEMA_NAME, RelationConstants.SUBJECT_DOCTYPE_FIELD_NAME);
268                                         String objectDocType = (String) doc.getProperty(RelationConstants.OBJECT_DOCTYPE_SCHEMA_NAME, RelationConstants.OBJECT_DOCTYPE_FIELD_NAME);;
269                                         String relationType = (String) doc.getProperty(RelationConstants.TYPE_SCHEMA_NAME, RelationConstants.TYPE_FIELD_NAME);
270         
271                                         logger.debug("subjectDocType=" + subjectDocType + " objectDocType=" + objectDocType + " relationType=" + relationType);
272
273                                         if (subjectDocType.equals(TaxonConstants.NUXEO_DOCTYPE) && objectDocType.equals(TaxonConstants.NUXEO_DOCTYPE) && relationType.equals(RelationConstants.BROADER_TYPE)) {
274                                                 String parentTaxonCsid = (String) doc.getProperty(RelationConstants.OBJECT_CSID_SCHEMA_NAME, RelationConstants.OBJECT_CSID_FIELD_NAME);
275                                                 
276                                                 // Stash the parent taxon csid, so it can be retrieved in the documentRemoved handler.
277                                                 logger.debug("about to delete taxon hierarchy relation: parentTaxonCsid=" + parentTaxonCsid);
278                                                 context.setProperty(DELETED_RELATION_PARENT_CSID_PROPERTY_NAME, parentTaxonCsid);
279                                         }
280                                 }
281                                 else if (event.getName().equals(DocumentEventTypes.DOCUMENT_REMOVED)) {
282                                         String parentTaxonCsid = (String) context.getProperty(DELETED_RELATION_PARENT_CSID_PROPERTY_NAME);
283                                         
284                                         if (StringUtils.isNotEmpty(parentTaxonCsid)) {
285                                                 logger.debug("child removed, updating parent taxon: parentTaxonCsid=" + parentTaxonCsid);
286
287                                                 try {                                                   
288                                                         InvocationResults results = createUpdater().updateAccessCode(parentTaxonCsid, false);
289                                                         
290                                                         logger.debug("updateAccessCode complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
291                                                 }
292                                                 catch (Exception e) {
293                                                         logger.error(e.getMessage(), e);
294                                                 }                                               
295                                         }
296                                 }
297                         }
298                 }
299         }
300         
301         private List<String> getTaxonNames(DocumentModel doc) {
302                 List<Map<String, Object>> taxonomicIdentGroupList = (List<Map<String, Object>>) doc.getProperty(CollectionObjectBotGardenConstants.TAXON_SCHEMA_NAME, 
303                                 TAXONOMIC_IDENT_GROUP_LIST_FIELD_NAME);
304                 List<String> taxonNames = new ArrayList<String>();
305
306                 for (Map<String, Object> taxonomicIdentGroup : taxonomicIdentGroupList) {
307                         String taxonName = (String) taxonomicIdentGroup.get(TAXON_FIELD_NAME);
308
309                         if (StringUtils.isNotEmpty(taxonName)) {
310                                 taxonNames.add(taxonName);
311                         }
312                 }
313
314                 return taxonNames;
315         }
316
317         private Set<String> findDeletedTaxonNames(List<String> previousTaxonNames, List<String> currentTaxonNames) {
318                 Set<String> currentTaxonNameSet = new HashSet<String>(currentTaxonNames);
319                 Set<String> deletedTaxonNameSet = new HashSet<String>();
320                 
321                 for (String previousTaxonName : previousTaxonNames) {
322                         if (!currentTaxonNameSet.contains(previousTaxonName)) {
323                                 deletedTaxonNameSet.add(previousTaxonName);
324                         }
325                 }
326                 
327                 return deletedTaxonNameSet;
328         }
329         
330         private Set<String> findAddedTaxonNames(List<String> previousTaxonNames, List<String> currentTaxonNames) {
331                 Set<String> previousTaxonNameSet = new HashSet<String>(previousTaxonNames);
332                 Set<String> addedTaxonNameSet = new HashSet<String>();
333                 
334                 for (String currentTaxonName : currentTaxonNames) {
335                         if (!previousTaxonNameSet.contains(currentTaxonName)) {
336                                 addedTaxonNameSet.add(currentTaxonName);
337                         }
338                 }
339                 
340                 return addedTaxonNameSet;
341         }
342         
343         private UpdateAccessCodeBatchJob createUpdater() {
344                 ResourceMap resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
345
346                 UpdateAccessCodeBatchJob updater = new UpdateAccessCodeBatchJob();
347                 updater.setResourceMap(resourceMap);
348
349                 return updater;
350         }
351 }