1 19 package org.apache.cayenne.intercept; 20 21 import java.util.List ; 22 23 import org.apache.cayenne.DataChannel; 24 import org.apache.cayenne.LifecycleListener; 25 import org.apache.cayenne.ObjectContext; 26 import org.apache.cayenne.QueryResponse; 27 import org.apache.cayenne.graph.GraphDiff; 28 import org.apache.cayenne.graph.GraphManager; 29 import org.apache.cayenne.query.Query; 30 import org.apache.cayenne.reflect.LifecycleCallbackRegistry; 31 32 47 public class DataChannelCallbackInterceptor extends DataChannelDecorator { 48 49 protected LifecycleCallbackRegistry callbackRegistry; 50 protected boolean contextCallbacksEnabled; 51 52 public void setChannel(DataChannel channel) { 53 this.channel = channel; 54 55 callbackRegistry = (channel != null) 56 ? getEntityResolver().getCallbackRegistry() 57 : null; 58 } 59 60 protected boolean isEmpty() { 61 if (!(callbackRegistry.isEmpty(LifecycleListener.PRE_UPDATE) 62 && callbackRegistry.isEmpty(LifecycleListener.POST_PERSIST) 63 && callbackRegistry.isEmpty(LifecycleListener.POST_REMOVE) 64 && callbackRegistry.isEmpty(LifecycleListener.POST_UPDATE) && callbackRegistry 65 .isEmpty(LifecycleListener.POST_LOAD))) { 66 return false; 67 } 68 69 return contextCallbacksEnabled ? callbackRegistry.isEmpty(LifecycleListener.PRE_REMOVE) 70 && callbackRegistry.isEmpty(LifecycleListener.PRE_PERSIST) : true; 71 } 72 73 public QueryResponse onQuery(ObjectContext originatingContext, Query query) { 74 QueryResponse response = channel.onQuery(originatingContext, query); 75 76 79 if (!callbackRegistry.isEmpty(LifecycleListener.POST_LOAD)) { 80 81 List list = response.firstList(); 82 if (list != null 83 && !list.isEmpty() 84 && !(query.getMetaData(channel.getEntityResolver())) 85 .isFetchingDataRows()) { 86 callbackRegistry.performCallbacks(LifecycleListener.POST_LOAD, list); 87 } 88 } 89 90 return response; 91 } 92 93 public GraphDiff onSync( 94 ObjectContext originatingContext, 95 GraphDiff changes, 96 int syncType) { 97 98 if (isEmpty()) { 99 return channel.onSync(originatingContext, changes, syncType); 100 } 101 102 SyncCallbackProcessor processor = createSyncProcessor(originatingContext 103 .getGraphManager(), changes); 104 105 processor.applyPreCommit(syncType); 106 GraphDiff parentDiff = channel.onSync(originatingContext, changes, syncType); 107 processor.applyPostCommit(syncType); 108 109 return parentDiff; 110 } 111 112 SyncCallbackProcessor createSyncProcessor(GraphManager graphManager, GraphDiff changes) { 113 return new SyncCallbackProcessor(this, graphManager, changes); 114 } 115 116 121 public boolean isContextCallbacksEnabled() { 122 return contextCallbacksEnabled; 123 } 124 125 public void setContextCallbacksEnabled(boolean contextCallbacksEnabled) { 126 this.contextCallbacksEnabled = contextCallbacksEnabled; 127 } 128 129 public LifecycleCallbackRegistry getCallbackRegistry() { 130 return callbackRegistry; 131 } 132 } 133 | Popular Tags |