litesoft
@ HEAD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.orsup.nonpublic; import org.litesoft.bo.*; import org.litesoft.changemanagement.ServerStateChange.*; import org.litesoft.commonfoundation.base.*; import org.litesoft.commonfoundation.typeutils.*; import org.litesoft.core.*; import org.litesoft.core.util.*; import org.litesoft.db.*; import org.litesoft.encryption.symmetric.*; import org.litesoft.orsup.base.*; import org.litesoft.orsup.lazyload.*; import org.litesoft.orsup.otherattributeaccessors.*; import org.litesoft.orsup.selection.*; import org.litesoft.orsup.selection.nonpublic.IllegalArgument; import org.litesoft.orsup.selection.nonpublic.*; import org.litesoft.orsup.transact.*; import org.litesoft.sequences.*; import java.io.*; import java.sql.*; import java.util.*; public abstract class PersistentObjectImpl<T extends PersistentObjectImpl> implements LLPersistentObject<T> { @SuppressWarnings({"UnusedDeclaration"}) protected static final WhereClauseFactory WCF = WhereClauseFactory.INSTANCE; public static final int INITIAL_CHANGE_NUMBER = 0; protected MetaDataForPO mMetaDataForPO; private boolean mNew; private boolean mRehydrating; private boolean mForcedUpdate = false; private boolean mForcedChangeNotification = false; private Transaction mTransaction = null; private NonTransactionalFinder mNonTransactionalFinder = null; private boolean mDeleteRequested = false; private OriginalValueChangeTracker<T> mChangeTracker = OriginalValueChangeTrackerNull.getInstance(); private LazyLoadedsTracker mLazyLoadedsTracker = LazyLoadedsTrackerNull.INSTANCE; private int mChangeNumber = INITIAL_CHANGE_NUMBER; private PersistentObjectImpl( MetaDataForPO pMetaDataForPO, Transaction pTransaction, boolean pNew, ConstructionControl pConstructionControlExpected, ConstructionControl pConstructionControlActual ) { mMetaDataForPO = pMetaDataForPO; mNew = pNew; if ( pNew ) { IllegalArgument.ifNull( "Transaction on New PO", pTransaction ); mTransaction = pTransaction; mNonTransactionalFinder = pTransaction.getNonTransactionalFinder(); mRehydrating = false; incChangeNumber(); } else { IllegalArgument.ifNotNull( "Transaction on Rehydrating PO", pTransaction ); mRehydrating = true; } if ( pConstructionControlExpected != pConstructionControlActual ) { throw new IllegalStateException( "Invalid attempt to Construct with ConstructionControl" ); } } @SuppressWarnings({"unchecked"}) protected void setDefaults() { for ( AttributeAccessorSCD zSCD : getMetaDataForPO().getAccessorSCDs() ) { Object zDefault = zSCD.getDefault(); if ( zDefault != null ) { zSCD.setValueOnPO( this, zDefault ); } } } protected PersistentObjectImpl( MetaDataForPO pMetaDataForPO, Transaction pTransaction ) // Only used for New { this( pMetaDataForPO, pTransaction, true, null, null ); } protected PersistentObjectImpl( MetaDataForPO pMetaDataForPO, ConstructionControl pConstructionControlExpected, ConstructionControl pConstructionControlActual ) { this( pMetaDataForPO, null, false, pConstructionControlExpected, pConstructionControlActual ); } protected void registerWithTransaction() { PersistentObjectImpl<T> po = getTransactionBackdoor().addPO( this ); if ( po != this ) // Identity check / this method called outside of 'new' constructor { throw new IllegalStateException( "Apparently 'new' object already in Transaction?\nthis=" + this ); } } protected long getNextSequenceNumber( String pGroup ) { return getSequenceSource().getNextSequenceNumber( pGroup ); } protected long getNextSequenceNumber( Class pGroup ) { return getSequenceSource().getNextSequenceNumber( pGroup ); } protected SequenceSource getSequenceSource() { return getFinder().getSequenceSource(); } @Override public SymmetricEncryptorManager getEncryptorManager() { return getFinder().getEncryptorManager(); } @SuppressWarnings({"unchecked"}) public void setCommitTimestamps( Timestamp pTransactionTimeStamp ) { ((MetaDataForPOinternalExtension) getMetaDataForPO()).setCommitTimestamps( this, pTransactionTimeStamp ); } /** * @return the Finder associated with this PO, either the Transaction of the NonTransactionalFinder */ @Override public final Finder getFinder() { Transaction transaction = getTransaction(); return (transaction != null) ? transaction : getNonTransactionalFinder(); } @Override public final NonTransactionalFinder<?> getNonTransactionalFinder() { return mNonTransactionalFinder; } @Override public final Transaction getTransaction() { return mTransaction; } @Override public String getObjectName() { return getMetaDataForPO().getIdentifierName(); } @Override public String getRegisteredName() { return getClass().getName(); } @Override public final boolean isImmortal() { MetaDataForPO md = getMetaDataForPO(); switch ( md.getMortality() ) { default: case Mortal: return false; case Immortal: return true; case InstanceImmortal: String name = md.getInstanceImmortalImmortalAttributeName(); Object value = getAttributeValue( name ); return Boolean.TRUE.equals( value ); } } @Override public final boolean isUpdatable() { MetaDataForPO md = getMetaDataForPO(); if ( !md.isUpdatable() ) { return false; } String name = md.getInstanceManagedUpdatabilityLockedAttributeName(); if ( name != null ) { Object value = getAttributeValue( name ); return !Boolean.TRUE.equals( value ); } return true; } @Override public void requestDelete() { verifyMutability(); if ( mTransaction instanceof TransactionBackdoor ) { if ( !((TransactionBackdoor) mTransaction).getApprovalFromDeleteAugmentors( this ) ) { return; } } if ( isImmortal() ) { String name = getMetaDataForPO().getImmortalInactiveAttributeName(); setAttributeValue( name, Boolean.TRUE ); } else { mDeleteRequested = true; incChangeNumber(); List<AbstractAttributeAccessorSCDtoPO> zNonDependents = new ArrayList<AbstractAttributeAccessorSCDtoPO>(); // Do the "ThemDependsOnUs"s 1st for ( AttributeAccessorSCD acd : getMetaDataForPO().getAccessorSCDs() ) { if ( acd instanceof AbstractAttributeAccessorSCDtoPO ) { AbstractAttributeAccessorSCDtoPO zToPO = (AbstractAttributeAccessorSCDtoPO) acd; if ( zToPO.isThemDependantOnUs() ) { clrValueOnPO( zToPO ); } else { zNonDependents.add( zToPO ); } } } for ( AbstractAttributeAccessorSCDtoPO acd : zNonDependents ) { clrValueOnPO( acd ); } } } @SuppressWarnings({"unchecked"}) private void clrValueOnPO( AbstractAttributeAccessorSCDtoPO pAcdToPO ) { pAcdToPO.clrValueOnPO( this ); } @Override public final boolean isDeleteRequested() { return mDeleteRequested; } @Override public final boolean isNew() { return mNew; } @Override public int numberOfAttributeChanged() { return mChangeTracker.numberOfChanges(); } @Override public final boolean isAnyAttributeChanged() { return mChangeTracker.hasChanges(); } @Override public boolean isAttributeChanged( String pAttributeName ) { return isAttributeChanged( getAccessorSCDoptional( pAttributeName ) ); } @Override public boolean isAttributeChanged( AttributeAccessorSCD pAttributeAccessorSCD ) { return (pAttributeAccessorSCD != null) && mChangeTracker.hasChangeFor( pAttributeAccessorSCD.getName() ); } @Override public String[] getChangedAttributeNames() { return mChangeTracker.getChangedAttributeNames(); } @Override public Object getOriginalValueFor( String pAttributeName ) { AttributeAccessorSCD<T> zSCD = getAccessorSCDoptional( pAttributeName ); if ( zSCD == null ) { return null; } pAttributeName = zSCD.getName(); if ( mChangeTracker.hasChangeFor( pAttributeName ) ) { return mChangeTracker.getOriginalValueFor( pAttributeName ); } return getAttributeValue( pAttributeName ); } @Override public final boolean willTossFromTransactionCommit() { return (isNew() && isDeleteRequested()) || !isDirty(); } @Override public final boolean isDirty() { return isDeleteRequested() || isNew() || mForcedUpdate || isAnyAttributeChanged(); } @Override public void forceUpdate() { verifyMutability(); mForcedUpdate = true; } private void changeToManyDueToOne() { if ( getMetaDataForPO().isRecordVersionable() ) { mForcedUpdate = true; incChangeNumber(); } } private void changeToManyDueToOne( PersistentObjectImpl pPO ) { if ( pPO != null ) { pPO.changeToManyDueToOne(); } } private void changeNotifyToManyDueToOne( PersistentObjectImpl pPO ) { if ( pPO != null ) { pPO.forceChangeNotification(); } } @Override public void forceChangeNotification() { verifyMutability(); mForcedChangeNotification = true; } @Override public boolean isForcedChangeNotification() { return mForcedChangeNotification; } @Override public Object getAttributeValue( String pAttributeName ) throws NoSuchElementException { //noinspection unchecked return getAccessorSCDrequired( pAttributeName ).getValueOnPO( (T) this ); } @Override public void setAttributeValue( String pAttributeName, Object pValue ) throws NoSuchElementException, AttributeIllegalArgumentException { try { //noinspection unchecked getAccessorSCDrequired( pAttributeName ).setValueOnPO( (T) this, pValue ); } catch ( IllegalArgumentException e ) { throw new AttributeIllegalArgumentException( pAttributeName, pValue, e ); } } @Override public void addToAttributeSet( String pAttributeName, PersistentObject<?> pPOtoAdd ) throws NoSuchElementException, AttributeIllegalArgumentException { if ( pPOtoAdd != null ) { try { //noinspection unchecked getAccessorSCDtoManyrequired( pAttributeName ).addValueOnPO( this, pPOtoAdd ); } catch ( IllegalArgumentException e ) { throw new AttributeIllegalArgumentException( pAttributeName, pPOtoAdd, e ); } } } @Override public void removeFromAttributeSet( String pAttributeName, PersistentObject<?> pPOtoRemove ) throws NoSuchElementException, AttributeIllegalArgumentException { if ( pPOtoRemove != null ) { try { //noinspection unchecked getAccessorSCDtoManyrequired( pAttributeName ).removeValueOnPO( this, pPOtoRemove ); } catch ( IllegalArgumentException e ) { throw new AttributeIllegalArgumentException( pAttributeName, pPOtoRemove, e ); } } } protected AbstractAttributeAccessorSCDtoMany getAccessorSCDtoManyrequired( String pAttributeName ) throws NoSuchElementException { AttributeAccessorSCD<T> zSCD = getAccessorSCDrequired( pAttributeName ); if ( zSCD instanceof AbstractAttributeAccessorSCDtoMany ) { return (AbstractAttributeAccessorSCDtoMany) zSCD; } throw new NoSuchElementException( "'" + pAttributeName + "' Not a 'Set' on PO type: " + getMetaDataForPO().getIdentifierName() ); } @Override public final ObjectHandle getObjectHandle() { return new ObjectHandle( getPersistentObjectURL(), getDisplayValue() ); } @Override public final PersistentObjectURL getPersistentObjectURL() { return new PersistentObjectURL( getRegisteredName(), getPersistentObjectUniqueKey() ); } private PersistentObjectUniqueKey mKey = null; @Override public final PersistentObjectUniqueKey getPersistentObjectUniqueKey() { if ( mKey == null ) { if ( null == (mKey = createPersistentObjectUniqueKey()) ) { mKey = new PersistentObjectUniqueKeyForInstance( this ); } } return mKey; } protected final PersistentObjectUniqueKey createPersistentObjectUniqueKey() { AttributeAccessorKeySet keySet = getMetaDataForPO().getKeySet(); if ( keySet == null ) { return null; } AttributeAccessorSCD<PersistentObject<?>>[] zAASCDs = keySet.getKeyAttributeAccessorSCDs(); return (zAASCDs.length == 1) ? new UniqueKeySingle( this, zAASCDs[0] ) : new UniqueKeyMulti( this, zAASCDs ); } private void forcePersistentObjectUniqueKeyToMatch( PersistentObjectImpl pCommonPairedThem ) { AttributeAccessorKeySet zThisKeySet = this.getMetaDataForPO().getKeySet(); AttributeAccessorKeySet zThemKeySet = pCommonPairedThem.getMetaDataForPO().getKeySet(); if ( (zThisKeySet != null) && (zThemKeySet != null) ) { AttributeAccessorSCD<PersistentObject<?>>[] zThisAASCDs = zThisKeySet.getKeyAttributeAccessorSCDs(); AttributeAccessorSCD<PersistentObject<?>>[] zThemAASCDs = zThemKeySet.getKeyAttributeAccessorSCDs(); if ( (zThisAASCDs.length == 1) && (zThemAASCDs.length == 1) ) { AttributeAccessorSCD<PersistentObject<?>> zThisKey = zThisAASCDs[0]; AttributeAccessorSCD<PersistentObject<?>> zThemKey = zThemAASCDs[0]; if ( zThisKey.getColumnType().equals( zThemKey.getColumnType() ) ) { PersistentObjectURL zOrigURL = this.getPersistentObjectURL(); zThisKey.db_setValueOnPO( this, zThemKey.db_getValueOnPO( pCommonPairedThem ) ); // Between this line and the Next the Display Value will report as the old Key! mKey = null; // .................................................................... Clear Cached Key PersistentObjectURL zNewURL = this.getPersistentObjectURL(); getTransaction().changedURL( zOrigURL, zNewURL ); return; } } } throw new IllegalStateException( "Incompatible KeySets for '" + this + "' & '" + pCommonPairedThem + "'" ); } /** * @return non-Transactional copy of the latest version of this PO, or Null if the PO is in a Transaction and isNew() OR has been deleted */ @Override public final T refreshNonTransactionally() { return getNonTransactionalFinder().refresh( this ); } /** * Refresh this PO into the provided transaction. * * @param pTransaction !null * * @return Transactional copy of the latest version read via Transaction, or Null if the PO is in another Transaction and isNew() OR has been deleted * * @throws UnsupportedOperationException - if the PO is already in the Transaction */ @Override public final T refreshInto( Transaction pTransaction ) throws UnsupportedOperationException { IllegalArgument.ifNull( "Transaction", pTransaction ); return pTransaction.refreshInto( this ); } /** * Refresh this PO into the provided transaction if needed. * * @param pTransaction !null * * @return Transactional copy of the latest version read via Transaction (if it is not already in the Transaction), * or Null if the PO is in another Transaction and isNew() OR has been deleted */ public T freshIntoIfNotAlreadyIn( Transaction pTransaction ) { IllegalArgument.ifNull( "Transaction", pTransaction ); return pTransaction.freshIntoIfNotInTransaction( this ); } /** * @return this if it is already in the Transaction, or a COPY of this in the specified Transaction */ @Override public final T copyInto( Transaction pTransaction ) throws UnsupportedOperationException { IllegalArgument.ifNull( "Transaction", pTransaction ); PersistentObject existing = (pTransaction == getTransaction()) ? this : pTransaction.getPersistentObjectFromIdentityMap( getPersistentObjectURL() ); if ( existing != null ) { return Cast.it( existing ); } // todo: What if !new and in another Transaction & changed ???? MetaDataForPOinternalExtension helper = (MetaDataForPOinternalExtension) getMetaDataForPO(); PersistentObjectImpl them = Cast.it( helper.createPOfromFinder() ); for ( AttributeAccessorSCD acd : helper.getPersistingAccessorSCDs() ) { //noinspection unchecked acd.db_setValueOnPO( them, acd.db_getValueOnPO( this ) ); } them = them.rehydrationComplete( pTransaction ); them.mNew = this.mNew; return Cast.it( them ); } /** * Called on each PO that is in the Transaction (found/read, copyInto, or LazyLoaded) * * @param pTransactionTimestamp * * @return any value (should probably NOT be the PO) that you want passed along in the DataStoreChange */ public Serializable aboutToCommit( Timestamp pTransactionTimestamp ) { return null; } @Override public String getDisplayValue() { String format = ConstrainTo.notNull( getMetaDataForPO().getDisplayValueFormat() ).trim(); if ( format.length() == 0 ) { return "Display Value (" + getObjectName() + "): " + getPersistentObjectUniqueKey(); } int from = 0; StringBuilder sb = new StringBuilder(); for ( int at; -1 != (at = format.indexOf( "${", from )); from = at + 1 ) { if ( at != from ) { sb.append( format.substring( from, at ) ); } int end = format.indexOf( '}', at + 2 ); if ( end == -1 ) { from = at; break; } String attr = format.substring( at + 2, end ); at = end; try { Object value = getAttributeValue( attr ); if ( value != null ) { sb.append( value ); } } catch ( NoSuchElementException e ) { sb.append( "${" ).append( attr ).append( '}' ); } } return sb.append( format.substring( from ) ).toString(); } @Override public Object toSqlValueForEquals() { PersistentObjectUniqueKey key = getPersistentObjectUniqueKey(); if ( key instanceof SQLvalueable ) { return ((SQLvalueable) key).toSQLvalue(); } if ( key instanceof WhereClauseToSQLable ) { return ((WhereClauseToSQLable) key).toSqlValueForEquals(); } throw new UnsupportedOperationException( "WhereClause support constrained to non-new POs with a single column unique key. Not: " + (isNew() ? " New " : "") + getObjectName() + "s." ); } public final boolean equals( PersistentObject pThem ) { return (this == pThem) || ((pThem != null) && this.getPersistentObjectURL().equals( pThem.getPersistentObjectURL() )); } @Override public final boolean equals( Object o ) { return (this == o) || ((o instanceof PersistentObject) && equals( (PersistentObject) o )); } @Override public final int hashCode() { return getPersistentObjectURL().hashCode(); } @Override public final String toString() { return getDisplayValue(); } public final <Them extends PersistentObjectImpl> List<Them> // processLazyLoadAccess( AbstractLazyLoadToMany<T, Them> pValueHolder ) throws UnsupportedOperationException { addToLazyLoadeds( pValueHolder ); return pValueHolder.getValue(); } public final <Them extends PersistentObjectImpl> void // processLazyLoadMutation( AbstractLazyLoadToMany<T, Them> pValueHolder, // List<Them> pNewValue ) throws UnsupportedOperationException { addToLazyLoadeds( pValueHolder ); verifyMutability( pValueHolder.getSCDtoMany() ); if ( (pNewValue != null) && !pNewValue.isEmpty() && anyNotInOurTransaction( pNewValue ) ) { List<Them> zNewPOs = new ArrayList<Them>( pNewValue.size() ); for ( Them zThem : pNewValue ) { if ( zThem != null ) { zNewPOs.add( genericSuppressedCopyIntoOurTransaction( zThem ) ); } } pNewValue = zNewPOs; } pValueHolder.setValues( pNewValue ); } private <Them extends PersistentObjectImpl> boolean anyNotInOurTransaction( List<Them> pNotEmptyPOs ) { for ( Them zThem : pNotEmptyPOs ) { if ( (zThem != null) && (zThem.getTransaction() != this.getTransaction()) ) { return true; } } return false; } public final <Them extends PersistentObjectImpl> void // processLazyLoadMutationAdd( AbstractLazyLoadToMany<T, Them> pValueHolder, // Them pNewValue ) throws UnsupportedOperationException { if ( pNewValue != null ) { addToLazyLoadeds( pValueHolder ); verifyMutability( pValueHolder.getSCDtoMany() ); pValueHolder.addValue( genericSuppressedCopyIntoOurTransaction( pNewValue ) ); } } public final <Them extends PersistentObjectImpl> void // processLazyLoadMutationRemove( AbstractLazyLoadToMany<T, Them> pValueHolder, // Them pNewValue ) throws UnsupportedOperationException { if ( pNewValue != null ) { addToLazyLoadeds( pValueHolder ); verifyMutability( pValueHolder.getSCDtoMany() ); pValueHolder.removeValue( genericSuppressedCopyIntoOurTransaction( pNewValue ) ); } } public final <Them extends PersistentObjectImpl> Them // processLazyLoadAccess( LazyLoadToOneRegular<T, Them> pValueHolder ) throws UnsupportedOperationException { addToLazyLoadeds( pValueHolder ); return pValueHolder.getValue(); } public final <Them extends PersistentObjectImpl> void // processLazyLoadMutation( LazyLoadToOneRegular<T, Them> pValueHolder, // Them pNewThem ) throws UnsupportedOperationException { verifyMutability(); Them zCurThem = processLazyLoadAccess( pValueHolder ); Object oldKey = pValueHolder.db_getValue(); Object newKey = null; if ( pNewThem != null ) { pNewThem = genericSuppressedCopyIntoOurTransaction( pNewThem ); newKey = pValueHolder.extract_db_value( pNewThem ); } AttributeAccessorSCDtoOneRegular<T, Them> acdToPO = pValueHolder.getSCDtoOne(); if ( verifyMutabilityOnChangeToOne( acdToPO, // zCurThem, oldKey, // pNewThem, newKey ) ) { pValueHolder.setValue( pNewThem ); } } public final <Them extends PersistentObjectImpl> Them // processLazyLoadAccess( LazyLoadToOnePaired<T, Them> pValueHolder ) throws UnsupportedOperationException { addToLazyLoadeds( pValueHolder ); return pValueHolder.getValue(); } protected final PersistentObjectImpl // processLazyLoadAccess( LazyLoadToOneCommonPaired<T> pValueHolder ) throws UnsupportedOperationException { addToLazyLoadeds( pValueHolder ); return pValueHolder.getValue(); } protected final void // processLazyLoadMutation( LazyLoadToOneCommonPaired<T> pValueHolder, // PersistentObjectImpl pNewThem ) throws UnsupportedOperationException { verifyMutability(); PersistentObjectImpl zCurThem = processLazyLoadAccess( pValueHolder ); Object oldKey = pValueHolder.db_getValue(); Object newKey = null; if ( pNewThem != null ) { pNewThem = genericSuppressedCopyIntoOurTransaction( pNewThem ); newKey = pValueHolder.extract_db_value( pNewThem, null ); } AttributeAccessorSCDtoOneCommonPaired<T> acdToPO = pValueHolder.getSCDtoOne(); if ( verifyMutabilityOnChangeToOne( acdToPO, // zCurThem, oldKey, // pNewThem, newKey ) ) { //noinspection ConstantConditions pNewThem.forcePersistentObjectUniqueKeyToMatch( this ); pValueHolder.setValue( pNewThem ); } } protected final PersistentObjectImpl // processLazyLoadAccess( LazyLoadToOneVariable<T> pValueHolder ) throws UnsupportedOperationException { addToLazyLoadeds( pValueHolder ); return pValueHolder.getValue(); } protected final void // processLazyLoadMutation( LazyLoadToOneVariable<T> pValueHolder, // PersistentObjectImpl pNewThem, // String pBackRefAttributeNameOnNewThem ) throws UnsupportedOperationException { verifyMutability(); PersistentObjectImpl zCurThem = processLazyLoadAccess( pValueHolder ); Object oldKey = pValueHolder.db_getValue(); Object newKey = null; if ( pNewThem != null ) { pNewThem = genericSuppressedCopyIntoOurTransaction( pNewThem ); newKey = pValueHolder.extract_db_value( pNewThem, pBackRefAttributeNameOnNewThem ); } AttributeAccessorSCDtoOneVariable<T> acdToPO = pValueHolder.getSCDtoOne(); if ( verifyMutabilityOnChangeToOne( acdToPO, // zCurThem, oldKey, // pNewThem, newKey ) ) { pValueHolder.setValue( pNewThem, pBackRefAttributeNameOnNewThem ); } } public final Blob // processLazyLoadAccess( BlobHandler<T> pValueHolder ) throws UnsupportedOperationException { addToLazyLoadeds( pValueHolder ); return pValueHolder.LL_getValue(); } public final void // processLazyLoadMutation( BlobHandler<T> pValueHolder, // Blob pNewThem ) throws UnsupportedOperationException { verifyMutability(); Blob zCurThem = processLazyLoadAccess( pValueHolder ); Object oldKey = pValueHolder.db_getValue(); Object newKey = null; if ( pNewThem != null ) { pNewThem = genericSuppressedCopyIntoOurTransaction( pNewThem ); newKey = pValueHolder.extract_db_value( pNewThem ); } AttributeAccessorSCDtoBlob<T> acdToPO = pValueHolder.getSCDtoOne(); if ( verifyMutabilityOnChangeToOne( acdToPO, // zCurThem, oldKey, // pNewThem, newKey ) ) { pValueHolder.LL_setValue( pNewThem, null ); } } private boolean verifyMutabilityOnChangeToOne( AbstractAttributeAccessorSCDtoOne pSupplimentedSCDtoOne, PersistentObjectImpl pOldThem, Object pOldKey, PersistentObjectImpl pNewThem, Object pNewKey ) { return verifyMutabilityOnChange( pSupplimentedSCDtoOne, // new ToOneChangeEntry( pOldThem, pOldKey ), // new ToOneChangeEntry( pNewThem, pNewKey ) ); } @Override public final void verifyMutability( SupplimentedSCD pSupplimentedSCD, Object pOldValue, Object pNewValue ) throws UnsupportedOperationException { if ( !mRehydrating ) { verifyMutabilityOnChange( pSupplimentedSCD, pOldValue, pNewValue ); } } protected final void verifyMutability() throws UnsupportedOperationException { if ( mTransaction == null ) { throw new UnsupportedOperationException( "Attempt to change a read-only (Nontransactional) PersistentObject" ); } if ( !mTransaction.supportsPOmodification( this ) ) { throw new UnsupportedOperationException( "Attempt to change a PersistentObject who's Transaction is currently: " + mTransaction.getState() ); } } protected final void verifyMutability( SupplimentedSCD pSupplimentedSCD ) throws UnsupportedOperationException { verifyMutability(); if ( pSupplimentedSCD == null ) { return; } switch ( pSupplimentedSCD.getMutability() ) { case RW: return; case AddOnly: if ( isNew() ) { return; } break; case RO: if ( pSupplimentedSCD instanceof AttributeAccessorSCDsimplePersistedSysSetOnly ) { return; } // Fall Thru default: break; } throw new UnsupportedOperationException( "Attempt to change a read-only Attribute (" + pSupplimentedSCD.getName() + ") on a PersistentObject" ); } private void addToLazyLoadeds( AbstractLazyLoadTo pLazyLoadTo ) { mLazyLoadedsTracker = mLazyLoadedsTracker.record( pLazyLoadTo.getAttributeName(), pLazyLoadTo ); } protected AttributeAccessorSCD<T> getAccessorSCDoptional( String pAttributeName ) throws NoSuchElementException { //noinspection unchecked return getMetaDataForPO().getAccessorSCDoptional( pAttributeName ); } protected AttributeAccessorSCD<T> getAccessorSCDrequired( String pAttributeName ) throws NoSuchElementException { //noinspection unchecked return getMetaDataForPO().getAccessorSCDrequired( pAttributeName ); } private boolean verifyMutabilityOnChange( SupplimentedSCD pSupplimentedSCD, Object pOldValue, Object pNewValue ) { if ( Currently.areEqual( pOldValue, pNewValue ) ) { return false; } verifyMutability( pSupplimentedSCD ); String zAttributeName = pSupplimentedSCD.getName(); if ( SupplimentedSCD.Form.ToMany.equals( pSupplimentedSCD.getForm() ) ) { throw new IllegalStateException( "Attempt to record change on ToMany attribute (" + zAttributeName + ") on PO (" + this + ") from (" + pOldValue + ") to (" + pNewValue + ")!" ); } if ( pSupplimentedSCD.isPersisted() ) { incChangeNumber(); recordChange( zAttributeName, pOldValue, pNewValue ); } return true; } @SuppressWarnings({"unchecked"}) private void recordChange( String pAttributeName, Object pOldValue, Object pNewValue ) { mChangeTracker = mChangeTracker.recordChange( (T) this, pAttributeName, pOldValue, pNewValue ); } private void changeToManyDueToOne( AbstractAttributeAccessorSCDtoOne pToOne ) { ToOneChangeEntry entry = (ToOneChangeEntry) mChangeTracker.getOriginalValueControlObjectFor( pToOne.getName() ); if ( entry != null ) { changeToManyDueToOne( entry.getPO() ); } //noinspection unchecked changeToManyDueToOne( (PersistentObjectImpl) pToOne.getValueOnPO( this ) ); } private void changeNotifyToManyDueToOne( AbstractAttributeAccessorSCDtoOne pToOne ) { ToOneChangeEntry entry = (ToOneChangeEntry) mChangeTracker.getOriginalValueControlObjectFor( pToOne.getName() ); if ( entry != null ) { changeNotifyToManyDueToOne( entry.getPO() ); } //noinspection unchecked changeNotifyToManyDueToOne( (PersistentObjectImpl) pToOne.getValueOnPO( this ) ); } @SuppressWarnings({"unchecked"}) private <Them extends PersistentObjectImpl> Them genericSuppressedCopyIntoOurTransaction( Them pNewValue ) { return (Them) pNewValue.copyInto( getTransaction() ); } protected final void incChangeNumber() { mChangeNumber++; } @Override public final int getChangeNumber() { return mChangeNumber; } private void clearChanges() { mChangeNumber = INITIAL_CHANGE_NUMBER; mRehydrating = false; mChangeTracker = mChangeTracker.clear(); mLazyLoadedsTracker = mLazyLoadedsTracker.clear(); } private void backdoorTransactionCommitSucceededDropTransaction() { mTransaction = null; } private void backdoorTransactionCommitSucceededClearTransactionalChanges() { boolean zWasCommitted = !willTossFromTransactionCommit(); successfulCommitBeforeClear( zWasCommitted ); for ( AttributeAccessorSCD zSCD : getMetaDataForPO().getAccessorSCDs() ) { //noinspection unchecked zSCD.onSuccessfulCommit( this, zWasCommitted ); } mNew = mForcedUpdate = mForcedChangeNotification = false; clearChanges(); if ( zWasCommitted ) { successfulCommit(); } } protected void justDeletedInTransaction( DBconnection pDBconnection ) { } protected void successfulCommit() { } @SuppressWarnings({"UnusedDeclaration"}) protected void successfulCommitBeforeClear( boolean pWasCommitted ) { } protected void transactionDisposed() { } private PersistentObjectImpl<T> rehydrationComplete( Finder pFinder ) { clearChanges(); PersistentObjectImpl<T> zResult = this; if ( pFinder instanceof NonTransactionalFinder ) { mTransaction = null; mNonTransactionalFinder = (NonTransactionalFinder) pFinder; zResult.rehydationComplete(); return zResult; } if ( pFinder instanceof Transaction ) { mTransaction = (Transaction) pFinder; mNonTransactionalFinder = mTransaction.getNonTransactionalFinder(); zResult = getTransactionBackdoor().addPO( this ); if ( zResult == this ) { zResult.rehydationComplete(); } return zResult; } throw new IllegalStateException( "rehydrationComplete, but finder was null" ); } private TransactionBackdoor getTransactionBackdoor() { if ( mTransaction instanceof TransactionBackdoor ) { return (TransactionBackdoor) mTransaction; } throw new IllegalStateException( "Transaction(" + mTransaction + "), does NOT implement: TransactionBackdoor" ); } @Override public MetaDataForPO getMetaDataForPO() { return mMetaDataForPO; } private interface LazyLoadedsTracker { LazyLoadedsTracker record( String pAttributeName, AbstractLazyLoadTo pLazyLoadTo ); LazyLoadedsTracker clear(); } private static class LazyLoadedsTrackerNull implements LazyLoadedsTracker { public static final LazyLoadedsTracker INSTANCE = new LazyLoadedsTrackerNull(); @Override public LazyLoadedsTracker record( String pAttributeName, AbstractLazyLoadTo pLazyLoadTo ) { return new LazyLoadedsTrackerImpl().record( pAttributeName, pLazyLoadTo ); } @Override public LazyLoadedsTracker clear() { return this; } } private static class LazyLoadedsTrackerImpl implements LazyLoadedsTracker { private Map<String, AbstractLazyLoadTo> mLazyLoadeds = new HashMap<String, AbstractLazyLoadTo>(); @Override public LazyLoadedsTracker record( String pAttributeName, AbstractLazyLoadTo pLazyLoadTo ) { mLazyLoadeds.put( pAttributeName, pLazyLoadTo ); return this; } @Override public LazyLoadedsTracker clear() { mLazyLoadeds.clear(); mLazyLoadeds = null; return LazyLoadedsTrackerNull.INSTANCE; } } private interface OriginalValueChangeTracker<T extends PersistentObjectImpl> { OriginalValueChangeTracker<T> clear(); int numberOfChanges(); boolean hasChanges(); boolean hasChangeFor( String pAttributeName ); Object getOriginalValueFor( String pAttributeName ); Object getOriginalValueControlObjectFor( String pAttributeName ); String[] getChangedAttributeNames(); OriginalValueChangeTracker<T> recordChange( T pThePo, String pAttributeName, Object pOldValue, Object pNewValue ); @SuppressWarnings({"UnusedDeclaration"}) PersistentObjectImpl getTheChangedPo(); } private static class OriginalValueChangeTrackerNull<T extends PersistentObjectImpl> implements OriginalValueChangeTracker<T> { private static final OriginalValueChangeTracker INSTANCE = new OriginalValueChangeTrackerNull(); @SuppressWarnings({"unchecked"}) public static <Z extends PersistentObjectImpl> OriginalValueChangeTracker<Z> getInstance() { return INSTANCE; } @Override public OriginalValueChangeTracker<T> clear() { return this; } @Override public int numberOfChanges() { return 0; } @Override public boolean hasChanges() { return false; } @Override public boolean hasChangeFor( String pAttributeName ) { return false; } @Override public Object getOriginalValueFor( String pAttributeName ) { return null; } @Override public Object getOriginalValueControlObjectFor( String pAttributeName ) { return null; } @Override public String[] getChangedAttributeNames() { return Strings.EMPTY_ARRAY; } @SuppressWarnings({"UnusedDeclaration"}) public String getOriginalDisplayValue() { return null; } @Override public PersistentObjectImpl getTheChangedPo() { return null; } @Override public OriginalValueChangeTracker<T> recordChange( T pThePo, String pAttributeName, Object pOldValue, Object pNewValue ) { OriginalValueChangeTrackerImpl<T> tracker = new OriginalValueChangeTrackerImpl<T>( pThePo ); tracker.recordChange( pThePo, pAttributeName, pOldValue, pNewValue ); return tracker; } } private static class OriginalValueChangeTrackerImpl<T extends PersistentObjectImpl> implements OriginalValueChangeTracker<T> { private Map<String, Object> mAttributeNameToOriginalValues = new HashMap<String, Object>(); private T mTheChangedPo; private OriginalValueChangeTrackerImpl( T pTheChangedPo ) { mTheChangedPo = pTheChangedPo; } @Override public OriginalValueChangeTracker<T> clear() { mAttributeNameToOriginalValues.clear(); mAttributeNameToOriginalValues = null; return OriginalValueChangeTrackerNull.getInstance(); } @Override public int numberOfChanges() { return mAttributeNameToOriginalValues.size(); } @Override public boolean hasChanges() { return !mAttributeNameToOriginalValues.isEmpty(); } @Override public boolean hasChangeFor( String pAttributeName ) { return mAttributeNameToOriginalValues.containsKey( pAttributeName ); } @Override public Object getOriginalValueFor( String pAttributeName ) { Object rv = getOriginalValueControlObjectFor( pAttributeName ); return (rv instanceof ToOneChangeEntry) ? ((ToOneChangeEntry) rv).getPO() : rv; } @Override public Object getOriginalValueControlObjectFor( String pAttributeName ) { return mAttributeNameToOriginalValues.get( pAttributeName ); } @Override public String[] getChangedAttributeNames() { return mAttributeNameToOriginalValues.isEmpty() ? // Strings.EMPTY_ARRAY : // mAttributeNameToOriginalValues.keySet().toArray( new String[mAttributeNameToOriginalValues.size()] ); } /** * @param pThePo needed to support the static instance of NullChangeTracker */ @Override public OriginalValueChangeTracker<T> recordChange( T pThePo, String pAttributeName, Object pOldValue, Object pNewValue ) { if ( !mAttributeNameToOriginalValues.containsKey( pAttributeName ) ) // No Original { mAttributeNameToOriginalValues.put( pAttributeName, pOldValue ); // then add Original } else { Object zOriginal = mAttributeNameToOriginalValues.get( pAttributeName ); if ( Currently.areEqual( zOriginal, pNewValue ) ) // Original value == new Value { mAttributeNameToOriginalValues.remove( pAttributeName ); // then pretend we never saw it! } } return this; } @Override public T getTheChangedPo() { return mTheChangedPo; } } private static class ToOneChangeEntry { private PersistentObjectImpl mPO; private Object mDBkey; public ToOneChangeEntry( PersistentObjectImpl pPO, Object pDBkey ) { mPO = pPO; mDBkey = pDBkey; } public PersistentObjectImpl getPO() { return mPO; } @Override public String toString() { return (mPO != null) ? mPO.getDisplayValue() : ""; } @Override public boolean equals( Object o ) { if ( this == o ) { return true; } if ( o == null || getClass() != o.getClass() ) { return false; } ToOneChangeEntry them = (ToOneChangeEntry) o; return Currently.areEqual( this.mDBkey, them.mDBkey ) && // Currently.areEqual( this.mPO, them.mPO ); } @Override public int hashCode() { int result; result = (mPO != null ? mPO.hashCode() : 0); result = 31 * result + (mDBkey != null ? mDBkey.hashCode() : 0); return result; } } protected String fromEncrypted( String pEncryptedText ) { return (pEncryptedText == null) ? null : SymmetricEncryptorManager.Helper.fromEncrypted( getEncryptorManager(), pEncryptedText ); } protected String toEncrypted( String pPlainText ) { return (pPlainText == null) ? null : SymmetricEncryptorManager.Helper.toEncrypted( getEncryptorManager(), pPlainText ); } @SuppressWarnings({"UnusedDeclaration"}) protected String toLowerCase( String pText ) { return (pText == null) ? null : pText.toLowerCase(); } @SuppressWarnings({"UnusedDeclaration"}) protected String toUpperCase( String pText ) { return (pText == null) ? null : pText.toUpperCase(); } public static class BackDoor { @SuppressWarnings({"UnusedDeclaration"}) public static boolean isRehydrating( PersistentObjectImpl pPersistentObject ) { return pPersistentObject.mRehydrating; } public static void justDeletedInTransaction( PersistentObjectImpl pPersistentObject, DBconnection pDBconnection ) { pPersistentObject.justDeletedInTransaction( pDBconnection ); } public static void disposeTransactionOn( PersistentObjectImpl pPersistentObject ) { pPersistentObject.transactionDisposed(); } public static void transactionCommitSucceededDropTransactionOn( PersistentObjectImpl pPersistentObject ) { pPersistentObject.backdoorTransactionCommitSucceededDropTransaction(); } public static void transactionCommitSucceededClearTransactionalChangesOn( PersistentObjectImpl pPersistentObject ) { pPersistentObject.backdoorTransactionCommitSucceededClearTransactionalChanges(); } public static <T extends PersistentObjectImpl> T rehydrationComplete( T pPersistentObject, Finder pFinder ) { PersistentObjectImpl zPOI = pPersistentObject; return Cast.it( zPOI.rehydrationComplete( pFinder ) ); } public static void processLazyLoadMutation( PersistentObjectImpl pPersistentObject, LazyLoadToOneVariable pValueHolder, // PersistentObjectImpl pNewThem, // String pBackRefAttributeNameOnNewThem ) throws UnsupportedOperationException { //noinspection unchecked pPersistentObject.processLazyLoadMutation( pValueHolder, pNewThem, pBackRefAttributeNameOnNewThem ); } public static boolean verifyMutabilityOnChangeToOneOn( PersistentObjectImpl pPersistentObject, AbstractAttributeAccessorSCDtoOne pSupplimentedSCDtoOne, PersistentObjectImpl pOldThem, Object pOldKey, PersistentObjectImpl pNewThem, Object pNewKey ) { return pPersistentObject.verifyMutabilityOnChangeToOne( pSupplimentedSCDtoOne, // pOldThem, pOldKey, // pNewThem, pNewKey ); } public static void changeToManyDueToOne( PersistentObjectImpl pPersistentObject, AbstractAttributeAccessorSCDtoOne pToOne ) { pPersistentObject.changeToManyDueToOne( pToOne ); } public static void changeNotifyToManyDueToOne( PersistentObjectImpl pPersistentObject, AbstractAttributeAccessorSCDtoOne pToOne ) { pPersistentObject.changeNotifyToManyDueToOne( pToOne ); } } @Override public int compareTo( PersistentObject pOther ) { return getPersistentObjectURL().compareTo( pOther.getPersistentObjectURL() ); } @Override public Action makeStateChangeAction() { if ( isDeleteRequested() ) { return Action.DELETED; } if ( isNew() ) { return Action.CREATED; } return Action.UPDATED; } @SuppressWarnings({"UnusedDeclaration"}) protected static String extractFirstNonBlankLineForDisplayValue( String pText ) { if ( pText != null ) { String[] zLines = Strings.stringToLines( pText ); for ( String zLine : zLines ) { if ( (zLine = Strings.normalizeCtrlChars( zLine ).trim()).length() != 0 ) { return zLine; } } } return null; } @Override public void rehydationComplete() { } } |
Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/orsup/nonpublic/PersistentObjectImpl.java
Revision | Author | Commited | Message |
---|---|---|---|
950 Diff | GeorgeS | Thu 19 Jun, 2014 17:57:04 +0000 | New Lines |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
917 Diff | GeorgeS | Sun 08 Dec, 2013 20:49:56 +0000 | 1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model! |
823 Diff | GeorgeS | Sun 19 Aug, 2012 16:10:13 +0000 | |
822 Diff | GeorgeS | Sun 19 Aug, 2012 01:03:51 +0000 | |
804 Diff | GeorgeS | Wed 15 Aug, 2012 12:48:51 +0000 | |
803 Diff | GeorgeS | Wed 15 Aug, 2012 04:08:34 +0000 | |
802 | GeorgeS | Wed 15 Aug, 2012 04:04:47 +0000 |