1 package org.collectionspace.services.listener.botgarden;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.HashSet;
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;
32 * A listener that updates the access code on taxon records when collectionobjects
33 * or taxon records are created or modified.
35 * @see org.collectionspace.services.batch.nuxeo.UpdateAccessCodeBatchJob
39 public class UpdateAccessCodeListener extends AbstractCSEventListenerImpl {
40 final Log logger = LogFactory.getLog(UpdateAccessCodeListener.class);
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";
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];
51 public void handleEvent(Event event) {
52 EventContext ec = event.getContext();
54 if (ec instanceof DocumentEventContext) {
55 DocumentEventContext context = (DocumentEventContext) ec;
56 DocumentModel doc = context.getSourceDocument();
58 logger.debug("docType=" + doc.getType());
60 if (doc.getType().startsWith(CollectionObjectConstants.NUXEO_DOCTYPE) &&
63 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
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.
68 DocumentModel previousDoc = (DocumentModel) context.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
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);
74 List<String> previousTaxonNames = getTaxonNames(previousDoc);
75 context.setProperty(PREVIOUS_TAXON_NAMES_PROPERTY_NAME, previousTaxonNames.toArray(new String[previousTaxonNames.size()]));
78 boolean deadFlagChanged = false;
79 Set<String> deletedTaxonNames = null;
80 Set<String> addedTaxonNames = null;
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.
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);
91 if (previousDeadFlag == null) {
92 previousDeadFlag = "";
95 if (currentDeadFlag == null) {
99 if (previousDeadFlag.equals(currentDeadFlag)) {
100 logger.debug("dead flag not changed: previousDeadFlag=" + previousDeadFlag + " currentDeadFlag=" + currentDeadFlag);
103 logger.debug("dead flag changed: previousDeadFlag=" + previousDeadFlag + " currentDeadFlag=" + currentDeadFlag);
104 deadFlagChanged = true;
107 List<String> previousTaxonNames = Arrays.asList((String[]) context.getProperty(PREVIOUS_TAXON_NAMES_PROPERTY_NAME));
108 List<String> currentTaxonNames = getTaxonNames(doc);
110 deletedTaxonNames = findDeletedTaxonNames(previousTaxonNames, currentTaxonNames);
111 logger.debug("found deleted taxon names: " + StringUtils.join(deletedTaxonNames, ", "));
113 addedTaxonNames = findAddedTaxonNames(previousTaxonNames, currentTaxonNames);
114 logger.debug("found added taxon names: " + StringUtils.join(addedTaxonNames, ", "));
116 else if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
117 deadFlagChanged = true;
120 UpdateAccessCodeBatchJob updater = createUpdater();
122 if (deadFlagChanged) {
123 String collectionObjectCsid = doc.getName();
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
131 InvocationResults results = updater.updateReferencedAccessCodes(collectionObjectCsid, false);
133 logger.debug("updateReferencedAccessCodes complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
135 catch (Exception e) {
136 logger.error(e.getMessage(), e);
140 // If the dead flag didn't change, we still need to recalculate the access codes of
141 // any taxonomic idents that were added.
143 if (addedTaxonNames != null) {
144 for (String addedTaxonName : addedTaxonNames) {
145 logger.debug("updating added taxon: " + addedTaxonName);
148 InvocationResults results = updater.updateAccessCode(addedTaxonName, false);
150 logger.debug("updateAccessCode complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
152 catch (Exception e) {
153 logger.error(e.getMessage(), e);
159 if (deletedTaxonNames != null) {
160 // If any taxonomic idents were removed from the collectionobject, they need to have their
161 // access codes recalculated.
163 for (String deletedTaxonName : deletedTaxonNames) {
164 logger.debug("updating deleted taxon: " + deletedTaxonName);
167 InvocationResults results = updater.updateAccessCode(deletedTaxonName, false);
169 logger.debug("updateAccessCode complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
171 catch (Exception e) {
172 logger.error(e.getMessage(), e);
178 else if (doc.getType().startsWith(TaxonConstants.NUXEO_DOCTYPE) &&
181 !doc.getCurrentLifeCycleState().equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
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.
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);
189 context.setProperty(PREVIOUS_ACCESS_CODE_PROPERTY_NAME, previousAccessCode);
192 boolean updateRequired = false;
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
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);
202 if (previousAccessCode == null) {
203 previousAccessCode = "";
206 if (currentAccessCode == null) {
207 currentAccessCode = "";
210 if (previousAccessCode.equals(currentAccessCode)) {
211 logger.debug("update not required: previousAccessCode=" + previousAccessCode + " currentAccessCode=" + currentAccessCode);
214 logger.debug("update required: previousAccessCode=" + previousAccessCode + " currentAccessCode=" + currentAccessCode);
215 updateRequired = true;
218 else if (event.getName().equals(DocumentEventTypes.DOCUMENT_CREATED)) {
219 updateRequired = true;
222 if (updateRequired) {
223 String taxonCsid = doc.getName();
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
231 InvocationResults results = createUpdater().updateParentAccessCode(taxonCsid, false);
233 logger.debug("updateParentAccessCode complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
235 catch (Exception e) {
236 logger.error(e.getMessage(), e);
241 else if (doc.getType().equals(RelationConstants.NUXEO_DOCTYPE) &&
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);
250 logger.debug("subjectDocType=" + subjectDocType + " objectDocType=" + objectDocType + " relationType=" + relationType);
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);
257 InvocationResults results = createUpdater().updateAccessCode(parentTaxonCsid, false);
259 logger.debug("updateAccessCode complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
261 catch (Exception e) {
262 logger.error(e.getMessage(), e);
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);
271 logger.debug("subjectDocType=" + subjectDocType + " objectDocType=" + objectDocType + " relationType=" + relationType);
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);
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);
281 else if (event.getName().equals(DocumentEventTypes.DOCUMENT_REMOVED)) {
282 String parentTaxonCsid = (String) context.getProperty(DELETED_RELATION_PARENT_CSID_PROPERTY_NAME);
284 if (StringUtils.isNotEmpty(parentTaxonCsid)) {
285 logger.debug("child removed, updating parent taxon: parentTaxonCsid=" + parentTaxonCsid);
288 InvocationResults results = createUpdater().updateAccessCode(parentTaxonCsid, false);
290 logger.debug("updateAccessCode complete: numAffected=" + results.getNumAffected() + " userNote=" + results.getUserNote());
292 catch (Exception e) {
293 logger.error(e.getMessage(), e);
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>();
306 for (Map<String, Object> taxonomicIdentGroup : taxonomicIdentGroupList) {
307 String taxonName = (String) taxonomicIdentGroup.get(TAXON_FIELD_NAME);
309 if (StringUtils.isNotEmpty(taxonName)) {
310 taxonNames.add(taxonName);
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>();
321 for (String previousTaxonName : previousTaxonNames) {
322 if (!currentTaxonNameSet.contains(previousTaxonName)) {
323 deletedTaxonNameSet.add(previousTaxonName);
327 return deletedTaxonNameSet;
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>();
334 for (String currentTaxonName : currentTaxonNames) {
335 if (!previousTaxonNameSet.contains(currentTaxonName)) {
336 addedTaxonNameSet.add(currentTaxonName);
340 return addedTaxonNameSet;
343 private UpdateAccessCodeBatchJob createUpdater() {
344 ResourceMap resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
346 UpdateAccessCodeBatchJob updater = new UpdateAccessCodeBatchJob();
347 updater.setResourceMap(resourceMap);