1 21 package oracle.toplink.essentials.internal.descriptors; 23 24 import java.util.*; 25 import oracle.toplink.essentials.exceptions.*; 26 import oracle.toplink.essentials.indirection.*; 27 import oracle.toplink.essentials.internal.helper.*; 28 import oracle.toplink.essentials.mappings.*; 29 import oracle.toplink.essentials.internal.sessions.AbstractSession; 30 import oracle.toplink.essentials.descriptors.ClassDescriptor; 31 32 45 public abstract class DescriptorIterator { 46 public static final int NoCascading = 1; 47 public static final int CascadePrivateParts = 2; 48 public static final int CascadeAllParts = 3; 49 protected IdentityHashtable visitedObjects; 50 protected Stack visitedStack; 51 protected AbstractSession session; 52 protected DatabaseMapping currentMapping; 53 protected ClassDescriptor currentDescriptor; 54 protected Object result; protected boolean shouldIterateOverIndirectionObjects; 56 protected boolean shouldIterateOverUninstantiatedIndirectionObjects; 57 protected boolean shouldIterateOverWrappedObjects; 58 protected boolean shouldIterateOnIndirectionObjects; 59 protected boolean shouldIterateOnAggregates; 60 protected boolean shouldIterateOnPrimitives; 61 protected boolean shouldBreak; 62 protected int cascadeDepth; 64 74 public DescriptorIterator() { 75 this.visitedObjects = new IdentityHashtable(); 77 this.visitedStack = new Stack(); 78 this.cascadeDepth = CascadeAllParts; 79 this.shouldIterateOverIndirectionObjects = true; this.shouldIterateOverUninstantiatedIndirectionObjects = false; this.shouldIterateOnIndirectionObjects = false; this.shouldIterateOverWrappedObjects = true; this.shouldIterateOnAggregates = false; 84 this.shouldIterateOnPrimitives = false; 85 this.shouldBreak = false; 86 } 87 88 public int getCascadeDepth() { 89 return cascadeDepth; 90 } 91 92 public ClassDescriptor getCurrentDescriptor() { 93 return currentDescriptor; 94 } 95 96 public DatabaseMapping getCurrentMapping() { 97 return currentMapping; 98 } 99 100 103 protected ClassDescriptor getDescriptorFor(Object object) { 104 ClassDescriptor result = getSession().getDescriptor(object); 105 if (result == null) { 106 throw DescriptorException.missingDescriptor(object.getClass().getName()); 107 } 108 return result; 109 } 110 111 public Object getResult() { 112 return result; 113 } 114 115 public AbstractSession getSession() { 116 return session; 117 } 118 119 122 public Object getVisitedGrandparent() { 123 Object parent = getVisitedStack().pop(); 124 Object result = getVisitedStack().peek(); 125 getVisitedStack().push(parent); 126 return result; 127 } 128 129 public IdentityHashtable getVisitedObjects() { 130 return visitedObjects; 131 } 132 133 136 public Object getVisitedParent() { 137 return getVisitedStack().peek(); 138 } 139 140 public Stack getVisitedStack() { 141 return visitedStack; 142 } 143 144 149 protected void internalIterateAggregateObject(Object aggregateObject) { 150 iterate(aggregateObject); 151 } 152 153 157 protected void internalIterateIndirectContainer(IndirectContainer container) { 158 iterate(container); 159 } 160 161 165 protected void internalIteratePrimitive(Object primitiveValue) { 166 iterate(primitiveValue); 167 } 168 169 173 protected void internalIterateReferenceObject(Object referenceObject) { 174 iterate(referenceObject); 175 } 176 177 181 protected void internalIterateValueHolder(ValueHolderInterface valueHolder) { 182 iterate(valueHolder); 183 } 184 185 192 protected abstract void iterate(Object object); 193 194 200 public void iterateForAggregateMapping(Object aggregateObject, DatabaseMapping mapping, ClassDescriptor descriptor) { 201 if (aggregateObject == null) { 202 return; 203 } 204 setCurrentMapping(mapping); 205 setCurrentDescriptor(descriptor); 207 208 if (shouldIterateOnAggregates()) { internalIterateAggregateObject(aggregateObject); 210 if (shouldBreak()) { 211 setShouldBreak(false); 212 return; 213 } 214 } 215 216 iterateReferenceObjects(aggregateObject); 217 } 218 219 222 public void iterateIndirectContainerForMapping(IndirectContainer container, DatabaseMapping mapping) { 223 setCurrentMapping(mapping); 224 setCurrentDescriptor(null); 225 226 if (shouldIterateOnIndirectionObjects()) { internalIterateIndirectContainer(container); 228 } 229 230 if (shouldIterateOverUninstantiatedIndirectionObjects() || (shouldIterateOverIndirectionObjects() && container.isInstantiated())) { 231 mapping.iterateOnRealAttributeValue(this, container); 233 } 234 } 235 236 239 public void iteratePrimitiveForMapping(Object primitiveValue, DatabaseMapping mapping) { 240 if (primitiveValue == null) { 241 return; 242 } 243 setCurrentMapping(mapping); 244 setCurrentDescriptor(null); 245 246 if (shouldIterateOnPrimitives()) { internalIteratePrimitive(primitiveValue); 248 } 249 } 250 251 256 public void iterateReferenceObjectForMapping(Object referenceObject, DatabaseMapping mapping) { 257 if (!(shouldCascadeAllParts() || (shouldCascadePrivateParts() && mapping.isPrivateOwned()))) { 258 return; 259 } 260 261 ClassDescriptor rd = mapping.getReferenceDescriptor(); 265 if ((!shouldIterateOverWrappedObjects()) && (rd != null) && (rd.hasWrapperPolicy())) { 266 return; 267 } 268 if (referenceObject == null) { 269 return; 270 } 271 272 if (getVisitedObjects().containsKey(referenceObject)) { 274 return; 275 } 276 277 getVisitedObjects().put(referenceObject, referenceObject); 278 setCurrentMapping(mapping); 279 setCurrentDescriptor(getDescriptorFor(referenceObject)); 280 281 internalIterateReferenceObject(referenceObject); 282 if (shouldBreak()) { 283 setShouldBreak(false); 284 return; 285 } 286 287 iterateReferenceObjects(referenceObject); 288 } 289 290 294 protected void iterateReferenceObjects(Object sourceObject) { 295 getVisitedStack().push(sourceObject); 296 getCurrentDescriptor().getObjectBuilder().iterate(this); 297 getVisitedStack().pop(); 298 } 299 300 303 public void iterateValueHolderForMapping(ValueHolderInterface valueHolder, DatabaseMapping mapping) { 304 setCurrentMapping(mapping); 305 setCurrentDescriptor(null); 306 307 if (shouldIterateOnIndirectionObjects()) { internalIterateValueHolder(valueHolder); 309 } 310 311 if (shouldIterateOverUninstantiatedIndirectionObjects() || (shouldIterateOverIndirectionObjects() && valueHolder.isInstantiated())) { 312 mapping.iterateOnRealAttributeValue(this, valueHolder.getValue()); 314 } 315 } 316 317 public void setCascadeDepth(int cascadeDepth) { 318 this.cascadeDepth = cascadeDepth; 319 } 320 321 public void setCurrentDescriptor(ClassDescriptor currentDescriptor) { 322 this.currentDescriptor = currentDescriptor; 323 } 324 325 public void setCurrentMapping(DatabaseMapping currentMapping) { 326 this.currentMapping = currentMapping; 327 } 328 329 public void setResult(Object result) { 330 this.result = result; 331 } 332 333 public void setSession(AbstractSession session) { 334 this.session = session; 335 } 336 337 public void setShouldBreak(boolean shouldBreak) { 338 this.shouldBreak = shouldBreak; 339 } 340 341 346 public void setShouldIterateOnAggregates(boolean shouldIterateOnAggregates) { 347 this.shouldIterateOnAggregates = shouldIterateOnAggregates; 348 } 349 350 354 public void setShouldIterateOnIndirectionObjects(boolean shouldIterateOnIndirectionObjects) { 355 this.shouldIterateOnIndirectionObjects = shouldIterateOnIndirectionObjects; 356 } 357 358 362 public void setShouldIterateOnPrimitives(boolean shouldIterateOnPrimitives) { 363 this.shouldIterateOnPrimitives = shouldIterateOnPrimitives; 364 } 365 366 371 public void setShouldIterateOverIndirectionObjects(boolean shouldIterateOverIndirectionObjects) { 372 this.shouldIterateOverIndirectionObjects = shouldIterateOverIndirectionObjects; 373 } 374 375 379 public void setShouldIterateOverUninstantiatedIndirectionObjects(boolean shouldIterateOverUninstantiatedIndirectionObjects) { 380 this.shouldIterateOverUninstantiatedIndirectionObjects = shouldIterateOverUninstantiatedIndirectionObjects; 381 } 382 383 public void setShouldIterateOverWrappedObjects(boolean shouldIterateOverWrappedObjects) { 384 this.shouldIterateOverWrappedObjects = shouldIterateOverWrappedObjects; 385 } 386 387 public void setVisitedObjects(IdentityHashtable visitedObjects) { 388 this.visitedObjects = visitedObjects; 389 } 390 391 protected void setVisitedStack(Stack visitedStack) { 392 this.visitedStack = visitedStack; 393 } 394 395 public boolean shouldBreak() { 396 return shouldBreak; 397 } 398 399 public boolean shouldCascadeAllParts() { 400 return getCascadeDepth() == CascadeAllParts; 401 } 402 403 public boolean shouldCascadeNoParts() { 404 return (getCascadeDepth() == NoCascading); 405 } 406 407 public boolean shouldCascadePrivateParts() { 408 return (getCascadeDepth() == CascadeAllParts) || (getCascadeDepth() == CascadePrivateParts); 409 } 410 411 416 public boolean shouldIterateOnAggregates() { 417 return shouldIterateOnAggregates; 418 } 419 420 424 public boolean shouldIterateOnIndirectionObjects() { 425 return shouldIterateOnIndirectionObjects; 426 } 427 428 432 public boolean shouldIterateOnPrimitives() { 433 return shouldIterateOnPrimitives; 434 } 435 436 441 public boolean shouldIterateOverIndirectionObjects() { 442 return shouldIterateOverIndirectionObjects; 443 } 444 445 449 public boolean shouldIterateOverUninstantiatedIndirectionObjects() { 450 return shouldIterateOverUninstantiatedIndirectionObjects; 451 } 452 453 public boolean shouldIterateOverWrappedObjects() { 454 return shouldIterateOverWrappedObjects; 455 } 456 457 460 public void startIterationOn(Object sourceObject) { 461 getVisitedObjects().put(sourceObject, sourceObject); 462 setCurrentMapping(null); 463 setCurrentDescriptor(getSession().getDescriptor(sourceObject)); 464 465 iterate(sourceObject); 466 467 if ((getCurrentDescriptor() != null) && (!shouldCascadeNoParts()) && !this.shouldBreak()) { 469 iterateReferenceObjects(sourceObject); 470 } 471 } 472 } 473 | Popular Tags |