1 21 package oracle.toplink.essentials.internal.sessions; 23 24 import java.util.*; 25 import oracle.toplink.essentials.internal.queryframework.ContainerPolicy; 26 import oracle.toplink.essentials.internal.sessions.AbstractSession; 27 28 37 public class DirectCollectionChangeRecord extends ChangeRecord implements oracle.toplink.essentials.changesets.DirectCollectionChangeRecord { 38 protected java.util.HashMap addObjectMap; 39 protected java.util.HashMap removeObjectMap; 40 protected java.util.HashMap commitAddMap; 44 45 48 protected transient Object originalCollection; 49 50 53 protected transient Object latestCollection; 54 55 public static NULL Null = new NULL(); 56 57 60 public DirectCollectionChangeRecord() { 61 super(); 62 } 63 64 68 public DirectCollectionChangeRecord(ObjectChangeSet owner) { 69 this.owner = owner; 70 } 71 72 77 public void addAdditionChange(HashMap additions, HashMap databaseCount) { 78 Iterator enumtr = additions.keySet().iterator(); 79 while (enumtr.hasNext()) { 80 Object object = enumtr.next(); 81 if (databaseCount.containsKey(object)){ 82 getCommitAddMap().put(object, databaseCount.get(object)); 83 } 84 addAdditionChange(object, (Integer )additions.get(object)); 85 } 86 } 87 88 91 public void addAdditionChange(Object key, Integer count){ 92 if (getRemoveObjectMap().containsKey(key)){ 93 int removeValue = ((Integer )getRemoveObjectMap().get(key)).intValue(); 94 int addition = count.intValue(); 95 int result = removeValue - addition; 96 if (result > 0 ) { getRemoveObjectMap().put(key, new Integer (result)); 98 }else if (result < 0) { getRemoveObjectMap().remove(key); 100 getAddObjectMap().put(key, new Integer (Math.abs(result))); 101 }else{ getRemoveObjectMap().remove(key); 103 } 104 }else{ 105 if (this.getAddObjectMap().containsKey(key)){ 106 int addValue = ((Integer )this.getAddObjectMap().get(key)).intValue(); 107 addValue += count.intValue(); 108 this.getAddObjectMap().put(key, new Integer (addValue)); 109 }else{ 110 this.getAddObjectMap().put(key, count); 111 } 112 } 113 int addValue = count.intValue(); 115 int commitValue = 0; 116 if (getCommitAddMap().containsKey(key)){ 117 commitValue = ((Integer )getCommitAddMap().get(key)).intValue(); 118 } 119 getCommitAddMap().put(key, new Integer (addValue+commitValue)); 120 } 121 127 public void addRemoveChange(HashMap additions, HashMap databaseCount) { 128 Iterator enumtr = additions.keySet().iterator(); 129 while (enumtr.hasNext()) { 130 Object object = enumtr.next(); 131 if (databaseCount.containsKey(object)){ 132 getCommitAddMap().put(object, databaseCount.get(object)); 133 } 134 addRemoveChange(object, (Integer )additions.get(object)); 135 } 136 } 137 138 141 public void addRemoveChange(Object key, Integer count){ 142 if (getAddObjectMap().containsKey(key)){ 143 int removeValue = ((Integer )getAddObjectMap().get(key)).intValue(); 144 int addition = count.intValue(); 145 int result = removeValue - addition; 146 if (result > 0 ) { getAddObjectMap().put(key, new Integer (result)); 148 }else if (result < 0) { getAddObjectMap().remove(key); 150 getRemoveObjectMap().put(key, new Integer (Math.abs(result))); 151 }else{ getAddObjectMap().remove(key); 153 } 154 }else{ 155 if (this.getRemoveObjectMap().containsKey(key)){ 156 int addValue = ((Integer )this.getRemoveObjectMap().get(key)).intValue(); 157 addValue += count.intValue(); 158 this.getRemoveObjectMap().put(key, new Integer (addValue)); 159 }else{ 160 this.getRemoveObjectMap().put(key, count); 161 } 162 } 163 int removeValue = count.intValue(); 164 int commitValue = 0; 165 if (getCommitAddMap().containsKey(key)){ 166 commitValue = ((Integer )getCommitAddMap().get(key)).intValue(); 167 } 168 getCommitAddMap().put(key, new Integer (commitValue - removeValue)); 169 170 } 171 172 178 public void setCommitAddition(Hashtable additions){ 179 Enumeration enumtr = additions.keys(); 180 while (enumtr.hasMoreElements()) { 181 Object object = enumtr.nextElement(); 182 getCommitAddMap().put(object, additions.get(object)); 183 } 184 } 185 186 190 public void setLatestCollection(Object latestCollection) { 191 this.latestCollection = latestCollection; 192 } 193 194 198 public void setOriginalCollection(Object originalCollection) { 199 this.originalCollection = originalCollection; 200 } 201 202 206 public void storeDatabaseCounts(Object collection, ContainerPolicy containerPolicy, AbstractSession session){ 207 Object iterator = containerPolicy.iteratorFor(collection); 208 while (containerPolicy.hasNext(iterator)){ 209 Object object = containerPolicy.next(iterator, session); 210 if (getCommitAddMap().containsKey(object)){ 211 int count = ((Integer )getCommitAddMap().get(object)).intValue(); 212 getCommitAddMap().put(object, new Integer (++count)); 213 }else{ 214 getCommitAddMap().put(object, new Integer (1)); 215 } 216 } 217 } 218 219 223 public Vector getAddObjectList(){ 224 Vector vector = new Vector(); 225 for (Iterator iterator = getAddObjectMap().keySet().iterator(); iterator.hasNext();){ 226 Object object = iterator.next(); 227 int count = ((Integer )getAddObjectMap().get(object)).intValue(); 228 while (count > 0){ 229 vector.add(object); 230 --count; 231 } 232 } 233 return vector; 234 } 235 236 240 public void setAddObjectList(Vector list){ 241 for (Iterator iterator = list.iterator(); iterator.hasNext();){ 242 Object object = iterator.next(); 243 this.addAdditionChange(object, new Integer (1)); 244 } 245 } 246 247 252 public java.util.HashMap getAddObjectMap() { 253 if (this.addObjectMap == null) { 254 this.addObjectMap = new HashMap(1); 255 } 256 return addObjectMap; 257 } 258 259 264 public java.util.HashMap getCommitAddMap() { 265 if (this.commitAddMap == null) { 266 this.commitAddMap = new HashMap(1); 267 } 268 return commitAddMap; 269 } 270 271 275 public Object getLatestCollection() { 276 return latestCollection; 277 } 278 279 283 public Object getOriginalCollection() { 284 return originalCollection; 285 } 286 287 291 public Vector getRemoveObjectList(){ 292 Vector vector = new Vector(); 293 for (Iterator iterator = getRemoveObjectMap().keySet().iterator(); iterator.hasNext();){ 294 Object object = iterator.next(); 295 int count = ((Integer )getRemoveObjectMap().get(object)).intValue(); 296 while (count > 0){ 297 vector.add(object); 298 --count; 299 } 300 } 301 return vector; 302 } 303 304 308 public void setRemoveObjectList(Vector list){ 309 for (Iterator iterator = list.iterator(); iterator.hasNext();){ 310 Object object = iterator.next(); 311 this.addRemoveChange(object, new Integer (1)); 312 } 313 } 314 315 320 public java.util.HashMap getRemoveObjectMap() { 321 if (this.removeObjectMap == null) { 322 removeObjectMap = new HashMap(1); 323 } 324 return removeObjectMap; 325 } 326 327 330 public boolean hasChanges() { 331 return (!(getAddObjectMap().isEmpty() && getRemoveObjectMap().isEmpty())) || getOwner().isNew(); 332 } 333 334 338 public void mergeRecord(ChangeRecord mergeFromRecord, UnitOfWorkChangeSet mergeToChangeSet, UnitOfWorkChangeSet mergeFromChangeSet) { 339 HashMap addMapToMerge = ((DirectCollectionChangeRecord)mergeFromRecord).getAddObjectMap(); 340 HashMap removeMapToMerge = ((DirectCollectionChangeRecord)mergeFromRecord).getRemoveObjectMap(); 341 for (Iterator iterator = addMapToMerge.keySet().iterator(); iterator.hasNext();){ 343 Object added = iterator.next(); 344 if (!((DirectCollectionChangeRecord)mergeFromRecord).getCommitAddMap().containsKey(added)){ 345 this.getCommitAddMap().put(added, ((DirectCollectionChangeRecord)mergeFromRecord).getCommitAddMap().get(added)); 347 } 348 this.addAdditionChange(added, (Integer )addMapToMerge.get(added)); 349 } 350 for (Iterator iterator = removeMapToMerge.keySet().iterator(); iterator.hasNext();){ 352 Object removed = iterator.next(); 353 if (!((DirectCollectionChangeRecord)mergeFromRecord).getCommitAddMap().containsKey(removed)){ 354 this.getCommitAddMap().put(removed, ((DirectCollectionChangeRecord)mergeFromRecord).getCommitAddMap().get(removed)); 356 } 357 this.addRemoveChange(removed, (Integer )addMapToMerge.get(removed)); 358 } 359 } 360 361 365 public void updateReferences(UnitOfWorkChangeSet mergeToChangeSet, UnitOfWorkChangeSet mergeFromChangeSet) { 366 } 368 369 public static class NULL { 370 public NULL(){ 372 } 373 374 public boolean equals(Object object){ 375 return object instanceof NULL; 376 } 377 378 } 379 } 380 | Popular Tags |