1 21 package oracle.toplink.essentials.internal.sessions; 23 24 import java.util.*; 25 import oracle.toplink.essentials.mappings.Association; 26 27 public class DirectMapChangeRecord extends ChangeRecord { 28 protected HashMap addObjectsList; 29 protected HashMap removeObjectsList; 30 31 34 protected transient Object originalCollection; 35 36 39 protected transient Object latestCollection; 40 41 public DirectMapChangeRecord() { 42 super(); 43 } 44 45 public DirectMapChangeRecord(ObjectChangeSet owner) { 46 this.owner = owner; 47 } 48 49 53 public Vector getAddAssociations() { 54 Vector addAssociations = new Vector(); 55 56 for (Iterator i = getAddObjects().keySet().iterator(); i.hasNext(); ) { 57 Association association = new Association(); 58 Object key = i.next(); 59 Object value = getAddObjects().get(key); 60 61 association.setKey(key); 62 association.setValue(value); 63 addAssociations.add(association); 64 } 65 if (addAssociations.size() == 0) { 66 return null; 67 } 68 return addAssociations; 69 } 70 71 74 public boolean hasChanges() { 75 return (!(getAddObjects().isEmpty() && getRemoveObjects().isEmpty())) || getOwner().isNew(); 76 } 77 78 82 public void mergeRecord(ChangeRecord mergeFromRecord, UnitOfWorkChangeSet mergeToChangeSet, UnitOfWorkChangeSet mergeFromChangeSet) { 83 Iterator addKeys = ((DirectMapChangeRecord)mergeFromRecord).getAddObjects().keySet().iterator(); 84 while (addKeys.hasNext()) { 85 Object key = addKeys.next(); 86 87 if (!this.getAddObjects().containsKey(key)) { 88 if (this.getRemoveObjects().containsKey(key)) { 89 this.getRemoveObjects().remove(key); 90 } else { 91 this.getAddObjects().put(key, ((DirectMapChangeRecord)mergeFromRecord).getAddObjects().get(key)); 92 } 93 } 94 } 95 96 Iterator removeKeys = ((DirectMapChangeRecord)mergeFromRecord).getRemoveObjects().keySet().iterator(); 97 while (removeKeys.hasNext()) { 98 Object key = removeKeys.next(); 99 100 if (!this.getRemoveObjects().containsKey(key)) { 101 if (this.getAddObjects().containsKey(key)) { 102 this.getAddObjects().remove(key); 103 } else { 104 this.getRemoveObjects().put(key, ((DirectMapChangeRecord)mergeFromRecord).getRemoveObjects().get(key)); 105 } 106 } 107 } 108 } 109 110 114 public void setAddAssociations(Vector addAssociations) { 115 HashMap addMap = new HashMap(); 116 117 for (Enumeration enumtr = addAssociations.elements(); enumtr.hasMoreElements();) { 118 Association association = (Association)enumtr.nextElement(); 119 addMap.put(association.getKey(), association.getValue()); 120 } 121 if (addMap.isEmpty()) { 122 addObjectsList = null; 123 } 124 addObjectsList = addMap; 125 } 126 127 131 public Object getLatestCollection() { 132 return latestCollection; 133 } 134 135 139 public Object getOriginalCollection() { 140 return originalCollection; 141 } 142 143 147 public Vector getRemoveAssociations() { 148 Vector removeAssociations = new Vector(); 149 150 for (Iterator i = getRemoveObjects().keySet().iterator(); i.hasNext(); ) { 151 Association association = new Association(); 152 153 Object key = i.next(); 154 Object value = getAddObjects().get(key); 155 156 association.setKey(key); 157 association.setValue(value); 158 removeAssociations.add(association); 159 } 160 if (removeAssociations.size() == 0) { 161 return null; 162 } 163 return removeAssociations; 164 } 165 166 170 public void setRemoveAssociations(Vector removeAssociations) { 171 HashMap removeMap = new HashMap(); 172 173 for (Enumeration enumtr = removeAssociations.elements(); enumtr.hasMoreElements();) { 174 Association association = (Association)enumtr.nextElement(); 175 removeMap.put(association.getKey(), association.getValue()); 176 } 177 if (removeMap.isEmpty()) { 178 removeObjectsList = null; 179 } 180 removeObjectsList = removeMap; 181 } 182 183 187 public void addAdditionChange(HashMap additions) { 188 if (getAddObjects().size() == 0) { 189 addObjectsList = additions; 190 return; 191 } 192 193 for (Iterator i = additions.keySet().iterator(); i.hasNext(); ) { 194 Object key = i.next(); 195 if (getAddObjects().containsKey(key)) { 196 getAddObjects().put(key, additions.get(key)); 197 } else if (additions.get(key).equals(getAddObjects().get(key))) { 198 getAddObjects().put(key, additions.get(key)); 199 } 200 } 201 } 202 203 207 public void addRemoveChange(HashMap subtractions) { 208 if (getRemoveObjects().size() == 0) { 209 this.removeObjectsList = subtractions; 210 return; 211 } 212 213 for (Iterator i = subtractions.keySet().iterator(); i.hasNext(); ) { 214 Object key = i.next(); 215 if (!getRemoveObjects().containsKey(key)) { 216 getRemoveObjects().put(key, subtractions.get(key)); 217 } else if (subtractions.get(key).equals(getRemoveObjects().get(key))) { 218 getRemoveObjects().put(key, subtractions.get(key)); 219 } 220 } 221 } 222 223 227 public void addAdditionChange(Object key, Object value) { 228 if ( getRemoveObjects().containsKey(key) ) { 229 if ( value.equals(getRemoveObjects().get(key)) ) { 230 getRemoveObjects().remove(key); 231 }else { 232 getAddObjects().put(key, value); 233 } 234 } else { 235 getAddObjects().put(key, value); 236 } 237 } 238 239 243 public void addRemoveChange(Object key, Object value) { 244 if ( getAddObjects().containsKey(key) ) { 247 getAddObjects().remove(key); 248 }else if ( ! getRemoveObjects().containsKey(key) ) { 249 getRemoveObjects().put(key, value); 250 } 251 } 252 253 257 public void setAddObjects(HashMap addObjects) { 258 this.addObjectsList = addObjects; 259 } 260 261 265 public void setLatestCollection(Object latestCollection) { 266 this.latestCollection = latestCollection; 267 } 268 269 273 public void setOriginalCollection(Object originalCollection) { 274 this.originalCollection = originalCollection; 275 } 276 277 281 public HashMap getAddObjects() { 282 if (addObjectsList == null) { 283 addObjectsList = new HashMap(); 285 } 286 return addObjectsList; 287 } 288 289 293 public void setRemoveObjects(HashMap removeObjects) { 294 this.removeObjectsList = removeObjects; 295 } 296 297 301 public HashMap getRemoveObjects() { 302 if (removeObjectsList == null) { 303 removeObjectsList = new HashMap(); 304 } 305 306 return removeObjectsList; 307 } 308 309 313 public void updateReferences(UnitOfWorkChangeSet mergeToChangeSet, UnitOfWorkChangeSet mergeFromChangeSet) { 314 } 316 } 317 | Popular Tags |