1 56 57 package org.objectstyle.cayenne.client; 58 59 import java.util.ArrayList ; 60 import java.util.Collection ; 61 import java.util.Collections ; 62 import java.util.HashSet ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 import java.util.Set ; 66 67 import org.objectstyle.cayenne.PersistenceState; 68 import org.objectstyle.cayenne.Persistent; 69 import org.objectstyle.cayenne.graph.GraphChangeHandler; 70 import org.objectstyle.cayenne.graph.GraphMap; 71 72 78 class ClientStateRecorder implements GraphChangeHandler { 79 80 Set dirtyIds; 81 82 ClientStateRecorder() { 83 this.dirtyIds = new HashSet (); 84 } 85 86 void clear() { 87 dirtyIds = new HashSet (); 88 } 89 90 93 void processCommit(GraphMap graphMap) { 94 Iterator it = dirtyIds.iterator(); 95 while (it.hasNext()) { 96 Object node = graphMap.getNode(it.next()); 97 if (node instanceof Persistent) { 98 Persistent persistentNode = (Persistent) node; 99 switch (persistentNode.getPersistenceState()) { 100 case PersistenceState.MODIFIED: 101 case PersistenceState.NEW: 102 persistentNode.setPersistenceState(PersistenceState.COMMITTED); 103 break; 104 case PersistenceState.DELETED: 105 persistentNode.setPersistenceState(PersistenceState.TRANSIENT); 106 break; 107 } 108 } 109 } 110 111 clear(); 112 } 113 114 boolean hasChanges() { 115 return !dirtyIds.isEmpty(); 116 } 117 118 Collection dirtyNodes(GraphMap graphMap) { 119 if (dirtyIds.isEmpty()) { 120 return Collections.EMPTY_SET; 121 } 122 123 List objects = new ArrayList (dirtyIds.size()); 124 Iterator it = dirtyIds.iterator(); 125 while (it.hasNext()) { 126 objects.add(graphMap.getNode(it.next())); 127 } 128 129 return objects; 130 } 131 132 Collection dirtyNodes(GraphMap graphMap, int state) { 133 if (dirtyIds.isEmpty()) { 134 return Collections.EMPTY_SET; 135 } 136 137 int size = dirtyIds.size(); 138 List objects = new ArrayList (size > 50 ? size / 2 : size); 139 Iterator it = dirtyIds.iterator(); 140 while (it.hasNext()) { 141 Persistent o = (Persistent) graphMap.getNode(it.next()); 142 143 if (o.getPersistenceState() == state) { 144 objects.add(o); 145 } 146 } 147 148 return objects; 149 } 150 151 153 public void nodeIdChanged(Object nodeId, Object newId) { 154 if (dirtyIds.remove(nodeId)) { 155 dirtyIds.add(newId); 156 } 157 } 158 159 public void nodeCreated(Object nodeId) { 160 dirtyIds.add(nodeId); 161 } 162 163 public void nodeRemoved(Object nodeId) { 164 dirtyIds.add(nodeId); 165 } 166 167 public void nodePropertyChanged( 168 Object nodeId, 169 String property, 170 Object oldValue, 171 Object newValue) { 172 dirtyIds.add(nodeId); 173 } 174 175 public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) { 176 dirtyIds.add(nodeId); 177 } 178 179 public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) { 180 dirtyIds.add(nodeId); 181 } 182 } 183 | Popular Tags |