1 19 20 package org.apache.cayenne; 21 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Set ; 29 30 import org.apache.cayenne.graph.GraphChangeHandler; 31 import org.apache.cayenne.graph.GraphManager; 32 33 39 class ObjectContextStateLog implements GraphChangeHandler { 40 41 Set dirtyIds; 42 GraphManager graphManager; 43 44 ObjectContextStateLog(GraphManager graphManager) { 45 this.dirtyIds = new HashSet (); 46 this.graphManager = graphManager; 47 } 48 49 void clear() { 50 dirtyIds = new HashSet (); 51 } 52 53 56 void graphCommitted() { 57 Iterator it = dirtyIds.iterator(); 58 while (it.hasNext()) { 59 Object node = graphManager.getNode(it.next()); 60 if (node instanceof Persistent) { 61 Persistent persistentNode = (Persistent) node; 62 switch (persistentNode.getPersistenceState()) { 63 case PersistenceState.MODIFIED: 64 case PersistenceState.NEW: 65 persistentNode.setPersistenceState(PersistenceState.COMMITTED); 66 break; 67 case PersistenceState.DELETED: 68 persistentNode.setPersistenceState(PersistenceState.TRANSIENT); 69 break; 70 } 71 } 72 } 73 74 clear(); 75 } 76 77 void graphReverted() { 78 Iterator it = dirtyIds.iterator(); 79 while (it.hasNext()) { 80 Object node = graphManager.getNode(it.next()); 81 if (node instanceof Persistent) { 82 Persistent persistentNode = (Persistent) node; 83 switch (persistentNode.getPersistenceState()) { 84 case PersistenceState.MODIFIED: 85 case PersistenceState.DELETED: 86 persistentNode.setPersistenceState(PersistenceState.COMMITTED); 87 break; 88 case PersistenceState.NEW: 89 persistentNode.setPersistenceState(PersistenceState.TRANSIENT); 90 break; 91 } 92 } 93 } 94 95 clear(); 96 } 97 98 boolean hasChanges() { 99 return !dirtyIds.isEmpty(); 100 } 101 102 Collection dirtyNodes() { 103 if (dirtyIds.isEmpty()) { 104 return Collections.EMPTY_SET; 105 } 106 107 List objects = new ArrayList (dirtyIds.size()); 108 Iterator it = dirtyIds.iterator(); 109 while (it.hasNext()) { 110 objects.add(graphManager.getNode(it.next())); 111 } 112 113 return objects; 114 } 115 116 Collection dirtyNodes(int state) { 117 if (dirtyIds.isEmpty()) { 118 return Collections.EMPTY_SET; 119 } 120 121 int size = dirtyIds.size(); 122 List objects = new ArrayList (size > 50 ? size / 2 : size); 123 Iterator it = dirtyIds.iterator(); 124 while (it.hasNext()) { 125 Persistent o = (Persistent) graphManager.getNode(it.next()); 126 127 if (o.getPersistenceState() == state) { 128 objects.add(o); 129 } 130 } 131 132 return objects; 133 } 134 135 void unregisterNode(Object nodeId) { 136 dirtyIds.remove(nodeId); 137 } 138 139 141 public void nodeIdChanged(Object nodeId, Object newId) { 142 if (dirtyIds.remove(nodeId)) { 143 dirtyIds.add(newId); 144 } 145 } 146 147 public void nodeCreated(Object nodeId) { 148 dirtyIds.add(nodeId); 149 } 150 151 public void nodeRemoved(Object nodeId) { 152 dirtyIds.add(nodeId); 153 } 154 155 public void nodePropertyChanged( 156 Object nodeId, 157 String property, 158 Object oldValue, 159 Object newValue) { 160 dirtyIds.add(nodeId); 161 } 162 163 public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) { 164 dirtyIds.add(nodeId); 165 } 166 167 public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) { 168 dirtyIds.add(nodeId); 169 } 170 171 } 172 | Popular Tags |