Search in sources :

Example 1 with EventSubject

use of org.apache.cayenne.event.EventSubject in project cayenne by apache.

the class ClientChannel method onSync.

public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
    DataChannelSyncCallbackAction callbackAction = DataChannelSyncCallbackAction.getCallbackAction(getEntityResolver().getCallbackRegistry(), originatingContext.getGraphManager(), changes, syncType);
    callbackAction.applyPreCommit();
    changes = diffCompressor.compress(changes);
    GraphDiff replyDiff = send(new SyncMessage(originatingContext, syncType, changes), GraphDiff.class);
    if (channelEventsEnabled) {
        EventSubject subject;
        switch(syncType) {
            case DataChannel.ROLLBACK_CASCADE_SYNC:
                subject = DataChannel.GRAPH_ROLLEDBACK_SUBJECT;
                break;
            case DataChannel.FLUSH_NOCASCADE_SYNC:
                subject = DataChannel.GRAPH_CHANGED_SUBJECT;
                break;
            case DataChannel.FLUSH_CASCADE_SYNC:
                subject = DataChannel.GRAPH_FLUSHED_SUBJECT;
                break;
            default:
                subject = null;
        }
        if (subject != null) {
            // combine message sender changes and message receiver changes into a
            // single event
            boolean sentNoop = changes == null || changes.isNoop();
            boolean receivedNoop = replyDiff == null || replyDiff.isNoop();
            if (!sentNoop || !receivedNoop) {
                CompoundDiff notification = new CompoundDiff();
                if (!sentNoop) {
                    notification.add(changes);
                }
                if (!receivedNoop) {
                    notification.add(replyDiff);
                }
                GraphEvent e = new GraphEvent(this, originatingContext, notification);
                eventManager.postEvent(e, subject);
            }
        }
    }
    callbackAction.applyPostCommit();
    return replyDiff;
}
Also used : GraphEvent(org.apache.cayenne.graph.GraphEvent) GraphDiff(org.apache.cayenne.graph.GraphDiff) DataChannelSyncCallbackAction(org.apache.cayenne.DataChannelSyncCallbackAction) EventSubject(org.apache.cayenne.event.EventSubject) CompoundDiff(org.apache.cayenne.graph.CompoundDiff)

Example 2 with EventSubject

use of org.apache.cayenne.event.EventSubject in project cayenne by apache.

the class EventUtil method listenForSubjects.

/**
 * Registers GraphEventListener for multiple subjects at once.
 */
static void listenForSubjects(EventManager manager, DataChannelListener listener, Object sender, EventSubject[] subjects) {
    for (EventSubject subject : subjects) {
        // assume that subject name and listener method name match
        String fqSubject = subject.getSubjectName();
        String method = fqSubject.substring(fqSubject.lastIndexOf('/') + 1);
        if (manager.isSingleThreaded()) {
            manager.addListener(listener, method, GraphEvent.class, subject, sender);
        } else {
            manager.addNonBlockingListener(listener, method, GraphEvent.class, subject, sender);
        }
    }
}
Also used : EventSubject(org.apache.cayenne.event.EventSubject)

Aggregations

EventSubject (org.apache.cayenne.event.EventSubject)2 DataChannelSyncCallbackAction (org.apache.cayenne.DataChannelSyncCallbackAction)1 CompoundDiff (org.apache.cayenne.graph.CompoundDiff)1 GraphDiff (org.apache.cayenne.graph.GraphDiff)1 GraphEvent (org.apache.cayenne.graph.GraphEvent)1