1 19 package org.apache.cayenne.intercept; 20 21 import java.util.ArrayList ; 22 import java.util.Collection ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 26 import org.apache.cayenne.DataChannel; 27 import org.apache.cayenne.LifecycleListener; 28 import org.apache.cayenne.graph.GraphChangeHandler; 29 import org.apache.cayenne.graph.GraphDiff; 30 import org.apache.cayenne.graph.GraphManager; 31 32 class SyncCallbackProcessor implements GraphChangeHandler { 33 34 final DataChannelCallbackInterceptor interceptor; 35 private GraphManager graphManager; 36 Collection updated; 37 Collection persisted; 38 Collection removed; 39 private Set seenIds; 40 41 SyncCallbackProcessor(DataChannelCallbackInterceptor interceptor, 42 GraphManager graphManager, GraphDiff changes) { 43 this.interceptor = interceptor; 44 this.seenIds = new HashSet (); 45 this.graphManager = graphManager; 46 changes.apply(this); 47 } 48 49 void applyPreCommit(int syncType) { 50 switch (syncType) { 51 case DataChannel.FLUSH_CASCADE_SYNC: 52 case DataChannel.FLUSH_NOCASCADE_SYNC: 53 apply(LifecycleListener.PRE_UPDATE, updated); 54 55 if (interceptor.isContextCallbacksEnabled()) { 56 apply(LifecycleListener.PRE_PERSIST, persisted); 57 apply(LifecycleListener.PRE_REMOVE, removed); 58 } 59 } 60 } 61 62 void applyPostCommit(int syncType) { 63 switch (syncType) { 64 case DataChannel.FLUSH_CASCADE_SYNC: 65 case DataChannel.FLUSH_NOCASCADE_SYNC: 66 apply(LifecycleListener.POST_UPDATE, updated); 67 apply(LifecycleListener.POST_REMOVE, removed); 68 apply(LifecycleListener.POST_PERSIST, persisted); 69 break; 70 case DataChannel.ROLLBACK_CASCADE_SYNC: 71 apply(LifecycleListener.POST_LOAD, updated); 72 apply(LifecycleListener.POST_LOAD, removed); 73 } 74 } 75 76 void apply(int callbackType, Collection objects) { 77 if (objects != null) { 78 interceptor.getCallbackRegistry().performCallbacks(callbackType, objects); 79 } 80 } 81 82 public void nodeCreated(Object nodeId) { 83 if (seenIds.add(nodeId)) { 84 85 Object node = graphManager.getNode(nodeId); 86 if (node != null) { 87 88 if (persisted == null) { 89 persisted = new ArrayList (); 90 } 91 92 persisted.add(node); 93 } 94 } 95 } 96 97 public void nodeRemoved(Object nodeId) { 98 if (seenIds.add(nodeId)) { 99 100 Object node = graphManager.getNode(nodeId); 101 if (node != null) { 102 103 if (removed == null) { 104 removed = new ArrayList (); 105 } 106 107 removed.add(node); 108 } 109 } 110 } 111 112 public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) { 113 nodeUpdated(nodeId); 115 } 116 117 public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) { 118 nodeUpdated(nodeId); 120 } 121 122 public void nodeIdChanged(Object nodeId, Object newId) { 123 } 124 125 public void nodePropertyChanged( 126 Object nodeId, 127 String property, 128 Object oldValue, 129 Object newValue) { 130 nodeUpdated(nodeId); 131 } 132 133 private void nodeUpdated(Object nodeId) { 134 if (seenIds.add(nodeId)) { 135 136 Object node = graphManager.getNode(nodeId); 137 if (node != null) { 138 139 if (updated == null) { 140 updated = new ArrayList (); 141 } 142 143 updated.add(node); 144 } 145 } 146 } 147 } | Popular Tags |