Search in sources :

Example 1 with GraphEvent

use of org.apache.cayenne.graph.GraphEvent 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 GraphEvent

use of org.apache.cayenne.graph.GraphEvent in project cayenne by apache.

the class CayenneContextMergeHandlerIT method testShouldProcessEvent.

@Test
public void testShouldProcessEvent() {
    CayenneContextMergeHandler handler = new CayenneContextMergeHandler(context);
    // 1. Our context initiated the sync:
    // src == channel && postedBy == context
    GraphEvent e1 = new GraphEvent(context.getChannel(), context, null);
    assertFalse(handler.shouldProcessEvent(e1));
    // 2. Another context initiated the sync:
    // postedBy != context && source == channel
    GraphEvent e2 = new GraphEvent(context.getChannel(), mock(ObjectContext.class), null);
    assertTrue(handler.shouldProcessEvent(e2));
    // 2.1 Another object initiated the sync:
    // postedBy != context && source == channel
    GraphEvent e21 = new GraphEvent(context.getChannel(), new Object(), null);
    assertTrue(handler.shouldProcessEvent(e21));
    // 3. Another channel initiated the sync:
    // postedBy == ? && source != channel
    GraphEvent e3 = new GraphEvent(new MockDataChannel(), new Object(), null);
    assertFalse(handler.shouldProcessEvent(e3));
    // 4. inactive
    GraphEvent e4 = new GraphEvent(context.getChannel(), mock(ObjectContext.class), null);
    handler.active = false;
    assertFalse(handler.shouldProcessEvent(e4));
}
Also used : GraphEvent(org.apache.cayenne.graph.GraphEvent) Test(org.junit.Test)

Example 3 with GraphEvent

use of org.apache.cayenne.graph.GraphEvent in project cayenne by apache.

the class CayenneContextGraphManager method send.

/**
 * Wraps GraphDiff in a GraphEvent and sends it via EventManager with specified
 * subject.
 */
void send(GraphDiff diff, EventSubject subject, Object eventSource) {
    EventManager manager = (context.getChannel() != null) ? context.getChannel().getEventManager() : null;
    if (manager != null) {
        GraphEvent e = new GraphEvent(context, eventSource, diff);
        manager.postEvent(e, subject);
    }
}
Also used : GraphEvent(org.apache.cayenne.graph.GraphEvent) EventManager(org.apache.cayenne.event.EventManager)

Example 4 with GraphEvent

use of org.apache.cayenne.graph.GraphEvent in project cayenne by apache.

the class BaseContext method fireDataChannelRolledback.

/**
 * @since 1.2
 */
protected void fireDataChannelRolledback(Object postedBy, GraphDiff changes) {
    EventManager manager = getEventManager();
    if (manager != null) {
        GraphEvent e = new GraphEvent(this, postedBy, changes);
        manager.postEvent(e, DataChannel.GRAPH_ROLLEDBACK_SUBJECT);
    }
}
Also used : GraphEvent(org.apache.cayenne.graph.GraphEvent) EventManager(org.apache.cayenne.event.EventManager)

Example 5 with GraphEvent

use of org.apache.cayenne.graph.GraphEvent in project cayenne by apache.

the class BaseContext method fireDataChannelCommitted.

/**
 * @since 1.2
 */
protected void fireDataChannelCommitted(Object postedBy, GraphDiff changes) {
    EventManager manager = getEventManager();
    if (manager != null) {
        GraphEvent e = new GraphEvent(this, postedBy, changes);
        manager.postEvent(e, DataChannel.GRAPH_FLUSHED_SUBJECT);
    }
}
Also used : GraphEvent(org.apache.cayenne.graph.GraphEvent) EventManager(org.apache.cayenne.event.EventManager)

Aggregations

GraphEvent (org.apache.cayenne.graph.GraphEvent)6 EventManager (org.apache.cayenne.event.EventManager)4 DataChannelSyncCallbackAction (org.apache.cayenne.DataChannelSyncCallbackAction)1 EventSubject (org.apache.cayenne.event.EventSubject)1 CompoundDiff (org.apache.cayenne.graph.CompoundDiff)1 GraphDiff (org.apache.cayenne.graph.GraphDiff)1 Test (org.junit.Test)1