1 package org.collectionspace.services.listener;
3 import java.io.Serializable;
6 import org.collectionspace.services.nuxeo.listener.AbstractCSEventListenerImpl;
7 import org.nuxeo.ecm.core.api.DocumentModel;
8 import org.nuxeo.ecm.core.api.event.CoreEventConstants;
9 import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
10 import org.nuxeo.ecm.core.event.Event;
11 import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
12 import org.nuxeo.runtime.api.Framework;
15 * Event listener that stores the values of fields of interest before documents are updated or
16 * deleted. This is necessary because the previous/deleted doument model will not be available
17 * to a post-modification/deletion event listener. Storing the previous/deleted values allows
18 * the post-modification/deletion event listener to take action if a field value was changed,
19 * or if a document was deleted that had a certain field value.
21 * This is a separate class from the Reindex listener, because the Reindex listener should be
22 * async and post-commit, so it must implement PostCommitEventListener. This listener must be
23 * synchronous and pre-commit, so it must implement EventListener. Nuxeo does not support
24 * a single class that implements both PostCommitEventListener and EventListener (such a listener
25 * will only run synchronously).
27 public class ReindexSupport extends AbstractCSEventListenerImpl {
30 public void handleEvent(Event event) {
31 // When a media record is about to be updated, store the value of the coverage and
32 // publishToList fields.
34 // When a media record is about to be removed, store the value of the coverage field.
36 // TODO: Make this configurable. This is currently hardcoded to the needs of the material
37 // profile/Material Order application.
39 if (isRegistered(event)) {
40 DocumentEventContext eventContext = (DocumentEventContext) event.getContext();
42 // Set a property if this listener is registered for the current tenant. This allows
43 // the Reindex listener to determine if it should run (since it is async, it cannot
44 // extend AbstractCSEventListenerImpl, so it cannot use the isRegistered method).
46 eventContext.setProperty(Reindex.IS_REGISTERED_KEY, true);
48 if (Framework.isBooleanPropertyTrue("elasticsearch.enabled")) {
49 DocumentModel doc = eventContext.getSourceDocument();
50 String docType = doc.getType();
51 String eventName = event.getName();
53 if (docType.startsWith("Media")) {
54 if (eventName.equals(DocumentEventTypes.BEFORE_DOC_UPDATE)) {
55 DocumentModel previousDoc = (DocumentModel) eventContext.getProperty(CoreEventConstants.PREVIOUS_DOCUMENT_MODEL);
56 String coverage = (String) previousDoc.getProperty("media_common", "coverage");
57 List<String> publishTo = (List<String>) previousDoc.getProperty("media_materials", "publishToList");
59 eventContext.setProperty(Reindex.PREV_COVERAGE_KEY, coverage);
60 eventContext.setProperty(Reindex.PREV_PUBLISH_TO_KEY, (Serializable) publishTo);
62 else if (eventName.equals(DocumentEventTypes.ABOUT_TO_REMOVE)) {
63 String coverage = (String) doc.getProperty("media_common", "coverage");
65 eventContext.setProperty(Reindex.PREV_COVERAGE_KEY, coverage);