Search in sources :

Example 1 with DataChannelSyncCallbackAction

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

use of org.apache.cayenne.DataChannelSyncCallbackAction in project cayenne by apache.

the class TransactionFilter method onSync.

@Override
public GraphDiff onSync(final ObjectContext originatingContext, final GraphDiff changes, final int syncType, final DataChannelFilterChain filterChain) {
    DataChannelSyncCallbackAction callbackAction = DataChannelSyncCallbackAction.getCallbackAction(originatingContext.getChannel().getEntityResolver().getCallbackRegistry(), originatingContext.getGraphManager(), changes, syncType);
    callbackAction.applyPreCommit();
    GraphDiff result;
    switch(syncType) {
        case DataChannel.ROLLBACK_CASCADE_SYNC:
            result = filterChain.onSync(originatingContext, changes, syncType);
            ;
            break;
        // including transaction handling logic
        case DataChannel.FLUSH_NOCASCADE_SYNC:
        case DataChannel.FLUSH_CASCADE_SYNC:
            result = transactionManager.performInTransaction(new TransactionalOperation<GraphDiff>() {

                @Override
                public GraphDiff perform() {
                    return filterChain.onSync(originatingContext, changes, syncType);
                }
            });
            break;
        default:
            throw new CayenneRuntimeException("Invalid synchronization type: %d", syncType);
    }
    callbackAction.applyPostCommit();
    return result;
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) DataChannelSyncCallbackAction(org.apache.cayenne.DataChannelSyncCallbackAction) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Aggregations

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