001package org.nuxeo.sample;
002
003import java.util.Arrays;
004import java.util.List;
005
006import org.nuxeo.ecm.core.api.DocumentModel;
007import org.nuxeo.ecm.core.event.Event;
008import org.nuxeo.ecm.core.event.EventContext;
009import org.nuxeo.ecm.core.event.PostCommitFilteringEventListener;
010import org.nuxeo.ecm.core.event.EventBundle;
011import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
012
013public class SampleAsyncListener implements PostCommitFilteringEventListener {
014  
015  protected final List<String> handled = Arrays.asList("documentCreated", "documentModified");
016  
017
018    @Override
019    public void handleEvent(EventBundle events) {
020        for (Event event : events) {
021            if (acceptEvent(event)) {
022                handleEvent(event);
023            }
024        }
025    }
026
027    @Override
028    public boolean acceptEvent(Event event) {
029        return handled.contains(event.getName());
030    }
031
032  
033    public void handleEvent(Event event) {
034        EventContext ctx = event.getContext();
035        if (!(ctx instanceof DocumentEventContext)) {
036          return;
037        }
038
039        DocumentEventContext docCtx = (DocumentEventContext) ctx;
040        DocumentModel doc = docCtx.getSourceDocument();
041
042        // Add some logic starting from here.
043    }
044}