1 2 12 package com.versant.core.ejb; 13 14 import com.versant.core.metadata.ClassMetaData; 15 import com.versant.core.metadata.ModelMetaData; 16 import com.versant.core.metadata.MDStatics; 17 import com.versant.core.metadata.FieldMetaData; 18 import com.versant.core.jdo.*; 19 import com.versant.core.common.*; 20 21 import javax.jdo.spi.StateManager; 22 import javax.jdo.spi.PersistenceCapable; 23 import javax.jdo.PersistenceManager; 24 25 28 public class StateManagerImp implements VersantStateManager { 29 private EMProxy emProxy; 30 OID oid; 31 public ClassMetaData cmd; 32 private ModelMetaData modelMetaData; 33 State state; 34 private State origState; 35 PersistenceCapable pc; 36 private byte jdoFlags; 37 40 private boolean[] loadedFields; 41 private boolean addedForDelete; 42 45 private boolean removed; 46 private State toStoreState; 47 48 public StateManagerImp(EMProxy em, ModelMetaData modelMetaData) { 49 this.emProxy = em; 50 this.modelMetaData = modelMetaData; 51 } 52 53 58 public void manageNew(PersistenceCapable pc, LocalCache cache, int newOidVal) { 59 this.pc = (PersistenceCapable) pc; 60 pc.jdoReplaceStateManager(this); 61 cmd = modelMetaData.getClassMetaData(pc.getClass()); 62 loadedFields = new boolean[cmd.stateFields.length]; 63 64 state = cmd.createState(); 66 state.setClassMetaData(cmd); 67 68 pc.jdoProvideFields(cmd.allManagedFieldNosArray); 70 oid = createNewOid(newOidVal); 72 73 cache.add(oid, this); 75 if (oid.isNew() && oid.getRealOID() != null) { 77 cache.add(oid.getRealOID(), this); 78 } 79 80 updateJdoFlag(pc, PersistenceCapable.LOAD_REQUIRED); 82 setBooleanArray(loadedFields, true); 84 } 85 86 91 public void manage(OID oidVal, State stateVal, LocalCache cache) { 92 this.oid = oidVal; 93 cmd = stateVal.getClassMetaData(modelMetaData); 94 try { 95 pc = (PersistenceCapable) cmd.cls.newInstance(); 96 } catch (Exception e) { 97 throw new RuntimeException ("The class '" 98 + cmd.cls.getName() 99 + "' does not have a default constructor", e); 100 } 101 loadedFields = new boolean[cmd.stateFields.length]; 102 103 pc.jdoReplaceStateManager(this); 104 105 state = cmd.createState(); 107 state.setClassMetaData(cmd); 108 state.updateFrom(stateVal); 109 110 updateOrigState(stateVal); 111 112 cache.add(oidVal, this); 114 115 updateJdoFlag(pc, PersistenceCapable.LOAD_REQUIRED); 116 117 if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 119 state.copyFields(oid); 120 pc.jdoReplaceFields(cmd.pkFieldNos); 121 } 122 } 123 124 private void updateOrigState(State aState) { 125 if (aState == null) return; 126 if (origState == null) { 127 origState = cmd.createState(); 128 origState.setClassMetaData(cmd); 129 } 130 if (cmd.changedOptimisticLocking) { 131 origState.updateNonFilled(aState); 132 } else if (cmd.optimisticLockingField != null) { 133 origState.copyOptimisticLockingField(aState); 134 } 135 } 136 137 private void updateJdoFlag(PersistenceCapable pc, byte val) { 138 jdoFlags = val; 139 pc.jdoReplaceFlags(); 140 } 141 142 private void setBooleanArray(boolean[] fields, boolean val) { 143 for (int i = fields.length - 1; i >= 0; i--) { 144 fields[i] = val; 145 } 146 } 147 148 151 public void postStore(StatesReturned sr, boolean flush) { 152 if (removed) { 153 } else { 154 EntrySet.Entry e = sr.getEntry(oid); 155 if (e != null) { 156 updateAutoFields((State) e.getValue()); 157 if (flush) { 158 updateOrigState((State) e.getValue()); 159 } 160 } 161 if (oid.isNew()) { 162 oid = ((NewObjectOID)oid).setRealOid(((OID)e.getKey()).getRealOID()); 164 state.copyFields(oid); 165 } 166 pc.jdoReplaceFields(cmd.dfgAbsFieldNos); 167 if (cmd.autoSetManagedFieldNos.length > 0) { 168 pc.jdoReplaceFields(cmd.autoSetManagedFieldNos); 169 } 170 state.makeClean(); 171 } 172 } 173 174 178 public void detachOnCommit(VersantDetachedStateManager dsm) { 179 if (state == null) return; 181 if (removed) { 183 pc.jdoReplaceStateManager(null); 184 } else { 185 if (!state.containsFetchGroup(cmd.fetchGroups[0])) { 187 getEm().fetchState(this, cmd.fetchGroups[0]); 188 } 189 190 pc.jdoReplaceFields(cmd.dfgAbsFieldNos); 192 193 ((VersantDetachable)pc).versantSetOID(getExternalOID()); 194 ((VersantDetachable)pc).versantSetVersion(state.getOptimisticLockingValue()); 195 ((VersantDetachable)pc).jdoReplaceStateManager(dsm); 196 198 for (int i = 0; i < loadedFields.length; i++) { 199 if (loadedFields[i]) ((VersantDetachable)pc).versantSetLoaded(cmd.stateFields[i].managedFieldNo); 200 } 201 } 202 state.clear(); 203 state = null; 204 pc = null; 205 } 206 207 public Object getOptimisticLockingValue() { 208 if (oid.isNew()) return null; 209 return state.getOptimisticLockingValue(); 212 } 213 214 private Object getExternalOID() { 215 if (oid.isNew()) throw new RuntimeException ("Not to be called on new oid"); 216 if (cmd.identityType == MDStatics.IDENTITY_TYPE_DATASTORE) { 217 return new VersantOid(oid, modelMetaData, oid.isResolved()); 218 } else if (cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION) { 219 if (cmd.objectIdClass == null) { 220 return new VersantOid(oid, modelMetaData, oid.isResolved()); 221 } else { 222 throw new RuntimeException ("NOT IMPLEMENTED"); 223 } 228 } else { 229 throw BindingSupportImpl.getInstance().unsupported(); 230 } 231 } 232 233 private EntityManagerImp getEm() { 234 return emProxy.getEm(); 235 } 236 237 238 public void makeDirty(PersistenceCapable persistenceCapable, int managedFieldNo) { 239 } 241 242 public void fillNewAppPKField(int fieldNo) { 243 if (oid.isNew()) { 244 NewObjectOID newObjectOID = (NewObjectOID)oid; 245 if (newObjectOID.realOID == null) { 246 if (cmd.postInsertKeyGenerator) { 247 getEm().flush(); 248 } else { 249 newObjectOID.realOID = getEm().storageMan.createOID(cmd); 250 state.copyFields(newObjectOID.realOID); 251 pc.jdoReplaceFields(cmd.pkFieldNos); 252 } 253 } 254 } 255 } 256 257 260 public void updateAutoFields(State autoS) { 261 if (autoS != null && cmd.hasAutoSetFields) { 262 this.state.updateFrom(autoS); 263 } 264 } 265 266 public OID getOID() { 267 return oid; 268 } 269 270 public PersistenceCapable getPersistenceCapable() { 271 return pc; 272 } 273 274 275 276 public void flush() { 277 } 278 279 283 public void manageReferences() { 284 } 285 286 289 private OID createNewOid(int newOIDVal) { 290 NewObjectOID newOID = cmd.createNewObjectOID(); 291 newOID.idNo = newOIDVal; 292 getRealOIDIfAppId(cmd, newOID, state); 293 return newOID; 294 } 295 296 301 public static OID getRealOIDIfAppId(ClassMetaData cmd, NewObjectOID newOID, 302 State state) { 303 if (newOID.realOID != null) return newOID.realOID; 304 if (!cmd.postInsertKeyGenerator 305 && cmd.identityType == MDStatics.IDENTITY_TYPE_APPLICATION 306 && state.containsValidAppIdFields()) { 307 308 final OID rOid = cmd.createOID(true); 309 state.copyKeyFields(rOid); 310 311 newOID.realOID = rOid; 312 return rOid; 313 } 314 return null; 315 } 316 317 321 public boolean isRemoved() { 322 return removed; 323 } 324 325 328 public void remove() { 329 emProxy.getEm().addToTxList(this); 330 removed = true; 331 } 332 333 FieldMetaData getFmd(int absFieldNo) { 334 return cmd.managedFields[absFieldNo]; 335 } 336 337 342 public void prepareCommitOrFlush(boolean commit, DeletePacket toDelete, 343 StatesToStore toStore) { 344 349 if (isRemoved()) { 350 addForDelete(toDelete); 351 } else if (isDirty()) { 353 354 if (commit) state.clearTransactionNonPersistentFields(); 356 357 State toStoreState = createToStoreState(); 358 oid.resolve(state); 359 360 if (!state.fillToStoreState(toStoreState, getEm(), this) 364 && (!commit || !oid.isNew())) { 365 return; 366 } 367 addToStoreOidContainer(toStoreState, oid.isNew(), toStore); 368 } 369 } 370 371 private void addToStoreOidContainer(State aState, boolean aNew, 372 StatesToStore toStore) { 373 if (aNew) { 374 toStore.add(oid, aState, null, 375 aNew && cmd.postInsertKeyGenerator); 376 } else { 377 toStore.add(oid, aState, origState, 378 aNew && cmd.postInsertKeyGenerator); 379 } 380 } 381 382 386 private void addForDelete(DeletePacket toDelete) { 387 if (!oid.isNew() && !addedForDelete) { 388 toDelete.add(oid, null); 389 addedForDelete = true; 390 } 391 } 392 393 private State createToStoreState() { 394 if (toStoreState == null) { 395 toStoreState = cmd.createState(); 396 toStoreState.setClassMetaData(cmd); 397 } else { 398 toStoreState.clear(); 399 } 400 return toStoreState; 401 } 402 403 407 public boolean isDirty() { 408 return oid.isNew() || state.isDirty(); 409 } 410 411 412 public OID getInternalOID(PersistenceCapable pc) { 413 return null; } 415 416 public Object getObjectById(Object oid, boolean b) { 417 return null; } 419 420 public PCStateMan getInternalSM(PersistenceCapable pc) { 421 return null; } 423 424 public PersistenceManager getPersistenceManager() { 425 return getEm(); 426 } 427 428 429 430 431 432 433 434 435 436 public byte replacingFlags(PersistenceCapable persistenceCapable) { 437 return jdoFlags; 438 } 439 440 public StateManager replacingStateManager(PersistenceCapable persistenceCapable, 441 StateManager stateManager) { 442 return stateManager; 443 } 444 445 public boolean isDirty(PersistenceCapable persistenceCapable) { 446 return false; 447 } 448 449 public boolean isTransactional(PersistenceCapable persistenceCapable) { 450 return false; 451 } 452 453 public boolean isPersistent(PersistenceCapable persistenceCapable) { 454 return false; 455 } 456 457 public boolean isNew(PersistenceCapable persistenceCapable) { 458 return false; 459 } 460 461 public boolean isDeleted(PersistenceCapable persistenceCapable) { 462 return false; 463 } 464 465 public PersistenceManager getPersistenceManager(PersistenceCapable persistenceCapable) { 466 return getEm(); 467 } 468 469 public void makeDirty(PersistenceCapable persistenceCapable, String s) { 470 } 472 473 public Object getObjectId(PersistenceCapable persistenceCapable) { 474 return null; 475 } 476 477 public Object getTransactionalObjectId(PersistenceCapable persistenceCapable) { 478 return null; } 480 481 public boolean isLoaded(PersistenceCapable persistenceCapable, int i) { 482 return loadedFields[getFmd(i).stateFieldNo]; 483 } 484 485 public void preSerialize(PersistenceCapable persistenceCapable) { 486 } 488 489 public boolean getBooleanField(PersistenceCapable persistenceCapable, int i, 490 boolean b) { 491 FieldMetaData fmd = getFmd(i); 492 loadedFields[fmd.stateFieldNo] = true; 493 return state.getBooleanField(getFmd(i).stateFieldNo); 494 } 495 496 public char getCharField(PersistenceCapable persistenceCapable, int i, 497 char c) { 498 FieldMetaData fmd = getFmd(i); 499 loadedFields[fmd.stateFieldNo] = true; 500 return state.getCharField(getFmd(i).stateFieldNo); 501 } 502 503 public byte getByteField(PersistenceCapable persistenceCapable, int i, 504 byte b) { 505 FieldMetaData fmd = getFmd(i); 506 loadedFields[fmd.stateFieldNo] = true; 507 return state.getByteField(getFmd(i).stateFieldNo); 508 } 509 510 public short getShortField(PersistenceCapable persistenceCapable, int i, 511 short i1) { 512 FieldMetaData fmd = getFmd(i); 513 loadedFields[fmd.stateFieldNo] = true; 514 return state.getShortField(getFmd(i).stateFieldNo); 515 } 516 517 public int getIntField(PersistenceCapable persistenceCapable, int i, 518 int i1) { 519 FieldMetaData fmd = getFmd(i); 520 loadedFields[fmd.stateFieldNo] = true; 521 return state.getIntField(getFmd(i).stateFieldNo); 522 } 523 524 public long getLongField(PersistenceCapable persistenceCapable, int i, 525 long l) { 526 FieldMetaData fmd = getFmd(i); 527 loadedFields[fmd.stateFieldNo] = true; 528 return state.getLongField(getFmd(i).stateFieldNo); 529 } 530 531 public float getFloatField(PersistenceCapable persistenceCapable, int i, 532 float v) { 533 FieldMetaData fmd = getFmd(i); 534 loadedFields[fmd.stateFieldNo] = true; 535 return state.getFloatField(getFmd(i).stateFieldNo); 536 } 537 538 public double getDoubleField(PersistenceCapable persistenceCapable, int i, 539 double v) { 540 FieldMetaData fmd = getFmd(i); 541 loadedFields[fmd.stateFieldNo] = true; 542 return state.getDoubleField(getFmd(i).stateFieldNo); 543 } 544 545 public String getStringField(PersistenceCapable persistenceCapable, int i, 546 String s) { 547 FieldMetaData fmd = getFmd(i); 548 loadedFields[fmd.stateFieldNo] = true; 549 return state.getStringField(getFmd(i).stateFieldNo); 550 } 551 552 public Object getObjectField(PersistenceCapable persistenceCapable, int i, Object o) { 553 return state.getObjectField(getFmd(i).stateFieldNo, pc, getEm(), oid); 554 } 555 556 public void setBooleanField(PersistenceCapable persistenceCapable, int i, 557 boolean currentVal, boolean newVal) { 558 emProxy.getEm().addToTxList(this); 559 state.setBooleanField(getFmd(i).stateFieldNo, newVal); 560 pc.jdoReplaceField(i); 561 } 562 563 public void setCharField(PersistenceCapable persistenceCapable, int i, 564 char currentVal, char newVal) { 565 emProxy.getEm().addToTxList(this); 566 state.setCharField(getFmd(i).stateFieldNo, newVal); 567 pc.jdoReplaceField(i); 568 } 569 570 public void setByteField(PersistenceCapable persistenceCapable, int i, 571 byte currentVal, byte newVal) { 572 emProxy.getEm().addToTxList(this); 573 state.setByteField(getFmd(i).stateFieldNo, newVal); 574 pc.jdoReplaceField(i); 575 } 576 577 public void setShortField(PersistenceCapable persistenceCapable, int i, 578 short currentVal, short newVal) { 579 emProxy.getEm().addToTxList(this); 580 state.setShortField(getFmd(i).stateFieldNo, newVal); 581 pc.jdoReplaceField(i); 582 } 583 584 public void setIntField(PersistenceCapable persistenceCapable, int i, 585 int currentVal, int newVal) { 586 emProxy.getEm().addToTxList(this); 587 state.setIntField(getFmd(i).stateFieldNo, newVal); 588 pc.jdoReplaceField(i); 589 } 590 591 public void setLongField(PersistenceCapable persistenceCapable, int i, 592 long currentVal, long newVal) { 593 emProxy.getEm().addToTxList(this); 594 state.setLongField(getFmd(i).stateFieldNo, newVal); 595 pc.jdoReplaceField(i); 596 } 597 598 public void setFloatField(PersistenceCapable persistenceCapable, int i, 599 float currentVal, float newVal) { 600 emProxy.getEm().addToTxList(this); 601 state.setFloatField(getFmd(i).stateFieldNo, newVal); 602 pc.jdoReplaceField(i); 603 } 604 605 public void setDoubleField(PersistenceCapable persistenceCapable, int i, 606 double currentVal, double newVal) { 607 emProxy.getEm().addToTxList(this); 608 state.setDoubleField(getFmd(i).stateFieldNo, newVal); 609 pc.jdoReplaceField(i); 610 } 611 612 public void setStringField(PersistenceCapable persistenceCapable, int i, 613 String currentVal, String newVal) { 614 emProxy.getEm().addToTxList(this); 615 state.setStringField(getFmd(i).stateFieldNo, newVal); 616 pc.jdoReplaceField(i); 617 } 618 619 public void setObjectField(PersistenceCapable persistenceCapable, int i, 620 Object currentVal, Object newVal) { 621 emProxy.getEm().addToTxList(this); 622 state.setObjectField(getFmd(i).stateFieldNo, newVal); 623 pc.jdoReplaceField(i); 624 } 625 626 public void providedBooleanField(PersistenceCapable persistenceCapable, 627 int i, boolean val) { 628 state.setBooleanField(getFmd(i).stateFieldNo, val); 629 } 630 631 public void providedCharField(PersistenceCapable persistenceCapable, int i, char val) { 632 state.setCharField(getFmd(i).stateFieldNo, val); 633 } 634 635 public void providedByteField(PersistenceCapable persistenceCapable, int i, byte val) { 636 state.setByteField(getFmd(i).stateFieldNo, val); 637 } 638 639 public void providedShortField(PersistenceCapable persistenceCapable, int i, short val) { 640 state.setShortField(getFmd(i).stateFieldNo, val); 641 } 642 643 public void providedIntField(PersistenceCapable persistenceCapable, int i, int val) { 644 state.setIntField(getFmd(i).stateFieldNo, val); 645 } 646 647 public void providedLongField(PersistenceCapable persistenceCapable, int i, long val) { 648 state.setLongField(getFmd(i).stateFieldNo, val); 649 } 650 651 public void providedFloatField(PersistenceCapable persistenceCapable, int i, float val) { 652 state.setFloatField(getFmd(i).stateFieldNo, val); 653 } 654 655 public void providedDoubleField(PersistenceCapable persistenceCapable, int i, double val) { 656 state.setDoubleField(getFmd(i).stateFieldNo, val); 657 } 658 659 public void providedStringField(PersistenceCapable persistenceCapable, int i, String val) { 660 state.setStringField(getFmd(i).stateFieldNo, val); 661 } 662 663 public void providedObjectField(PersistenceCapable persistenceCapable, int i, Object val) { 664 state.setObjectField(getFmd(i).stateFieldNo, val); 665 } 666 667 public boolean replacingBooleanField(PersistenceCapable persistenceCapable, int i) { 668 FieldMetaData fmd = getFmd(i); 669 loadedFields[fmd.stateFieldNo] = true; 670 return state.getBooleanField(getFmd(i).stateFieldNo); 671 } 672 673 public char replacingCharField(PersistenceCapable persistenceCapable, int i) { 674 FieldMetaData fmd = getFmd(i); 675 loadedFields[fmd.stateFieldNo] = true; 676 return state.getCharField(getFmd(i).stateFieldNo); 677 } 678 679 public byte replacingByteField(PersistenceCapable persistenceCapable, int i) { 680 FieldMetaData fmd = getFmd(i); 681 loadedFields[fmd.stateFieldNo] = true; 682 return state.getByteField(getFmd(i).stateFieldNo); 683 } 684 685 public short replacingShortField(PersistenceCapable persistenceCapable, int i) { 686 FieldMetaData fmd = getFmd(i); 687 loadedFields[fmd.stateFieldNo] = true; 688 return state.getShortField(getFmd(i).stateFieldNo); 689 } 690 691 public int replacingIntField(PersistenceCapable persistenceCapable, int i) { 692 FieldMetaData fmd = getFmd(i); 693 loadedFields[fmd.stateFieldNo] = true; 694 return state.getIntField(getFmd(i).stateFieldNo); 695 } 696 697 public long replacingLongField(PersistenceCapable persistenceCapable, int i) { 698 FieldMetaData fmd = getFmd(i); 699 loadedFields[fmd.stateFieldNo] = true; 700 return state.getLongField(getFmd(i).stateFieldNo); 701 } 702 703 public float replacingFloatField(PersistenceCapable persistenceCapable, int i) { 704 FieldMetaData fmd = getFmd(i); 705 loadedFields[fmd.stateFieldNo] = true; 706 return state.getFloatField(getFmd(i).stateFieldNo); 707 } 708 709 public double replacingDoubleField(PersistenceCapable persistenceCapable, int i) { 710 FieldMetaData fmd = getFmd(i); 711 loadedFields[fmd.stateFieldNo] = true; 712 return state.getDoubleField(getFmd(i).stateFieldNo); 713 } 714 715 public String replacingStringField(PersistenceCapable persistenceCapable, int i) { 716 FieldMetaData fmd = getFmd(i); 717 loadedFields[fmd.stateFieldNo] = true; 718 return state.getStringField(getFmd(i).stateFieldNo); 719 } 720 721 public Object replacingObjectField(PersistenceCapable persistenceCapable, int i) { 722 FieldMetaData fmd = getFmd(i); 723 loadedFields[fmd.stateFieldNo] = true; 724 return state.getObjectField(getFmd(i).stateFieldNo, pc, getEm(), oid); 725 } 726 727 public void firePostStore(LifecycleListenerManager listeners) { 728 if (pc != null) { 730 listeners.firePostStore(pc); 731 } 732 } 733 734 public void persistReferences(com.versant.core.ejb.common.EntrySet mergeSet) { 735 FieldMetaData[] fmdsToPersist = cmd.managedFields; 736 for (int i = 0; i < fmdsToPersist.length; i++) { 737 FieldMetaData fmd = fmdsToPersist[i]; 738 if ((fmd.cascadeType & MDStatics.CASCADE_PERSIST) == 0) continue; 739 switch (fmd.category) { 740 case MDStatics.CATEGORY_REF: 741 case MDStatics.CATEGORY_POLYREF: 742 Object ref = state.getObjectField(fmd.stateFieldNo, pc, 743 getEm(), oid); 744 if (ref != null) mergeSet.add(ref); 745 746 } 747 } 748 } 749 750 public StateManagerImp mergeReferences(com.versant.core.ejb.common.EntrySet mergeSet) { 751 FieldMetaData[] fmdsToPersist = cmd.managedFields; 752 for (int i = 0; i < fmdsToPersist.length; i++) { 753 FieldMetaData fmd = fmdsToPersist[i]; 754 if ((fmd.cascadeType & MDStatics.CASCADE_MERGE) == 0) continue; 755 switch (fmd.category) { 756 case MDStatics.CATEGORY_REF: 757 case MDStatics.CATEGORY_POLYREF: 758 if (state.isDirty(fmd.stateFieldNo)) { 759 Object ref = state.getObjectField(fmd.stateFieldNo, pc, 760 getEm(), oid); 761 if (ref != null) { 762 mergeSet.add(ref); 763 state.setInternalObjectField(fmd.stateFieldNo, 764 getEm().mergeInternal(ref, mergeSet).pc); 765 loadedFields[fmd.stateFieldNo] = false; 766 } 767 768 } 769 770 } 771 } 772 return this; 773 } 774 775 776 public void updateState(State state) { 777 state.updateNonFilled(state); 778 } 779 } 780 | Popular Tags |