1 package org.collectionspace.services.listener;
3 import java.io.Serializable;
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
9 import org.collectionspace.services.nuxeo.listener.AbstractCSEventSyncListenerImpl;
11 import org.nuxeo.ecm.core.api.DocumentModel;
12 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
13 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
14 import org.nuxeo.ecm.core.event.Event;
15 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
16 import org.nuxeo.runtime.api.Framework;
19 * Event listener that stores the values of fields of interest before documents are updated or
20 * deleted. This is necessary because the previous/deleted document model will not be available
21 * to a post-modification/deletion event listener. Storing the previous/deleted values allows
22 * the post-modification/deletion event listener to take action if a field value was changed,
23 * or if a document was deleted that had a certain field value.
25 * This is a separate class from the Reindex listener, because the Reindex listener should be
26 * async and post-commit, so it must implement PostCommitEventListener. This listener must be
27 * synchronous and pre-commit, so it must implement EventListener. Nuxeo does not support
28 * a single class that implements both PostCommitEventListener and EventListener (such a listener
29 * will only run synchronously).
31 public class ReindexSupport extends AbstractCSEventSyncListenerImpl {
32 final static Log logger = LogFactory.getLog(ReindexSupport.class);
35 public boolean shouldHandleEvent(Event event) {
36 if (Framework.isBooleanPropertyTrue(Reindex.ELASTICSEARCH_ENABLED_PROP) && event instanceof DocumentEventContext) {
37 DocumentEventContext eventContext = (DocumentEventContext) event.getContext();
38 DocumentModel doc = eventContext.getSourceDocument();
39 String docType = doc.getType();
40 if (docType.startsWith("Media")) {
49 @SuppressWarnings("unchecked")
50 public void handleCSEvent(Event event) {
51 // When a media record is about to be updated, store the value of the coverage and
52 // publishToList fields.
54 // When a media record is about to be removed, store the value of the coverage field.
56 // TODO: Make this configurable. This is currently hardcoded to the needs of the material
57 // profile/Material Order application.
58 DocumentEventContext eventContext = (DocumentEventContext) event.getContext();
59 DocumentModel doc = eventContext.getSourceDocument();
60 String eventName = event.getName();
62 if (eventName.equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
63 DocumentModel previousDoc = (DocumentModel) eventContext.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
64 String coverage = (String) previousDoc.getProperty("media_common", "coverage");
65 List<String> publishTo = (List<String>) previousDoc.getProperty("media_materials", "publishToList");
67 eventContext.setProperty(Reindex.PREV_COVERAGE_KEY, coverage);
68 eventContext.setProperty(Reindex.PREV_PUBLISH_TO_KEY, (Serializable) publishTo);
70 else if (eventName.equals(DocumentEventTypes.ABOUT_TO_REMOVE)) {
71 String coverage = (String) doc.getProperty("media_common", "coverage");
72 eventContext.setProperty(Reindex.PREV_COVERAGE_KEY, coverage);
77 public Log getLogger() {