litesoft
@ 939
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 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.bo.change; import org.litesoft.bo.AttributeIllegalArgumentException; import org.litesoft.bo.BoAttribute; import org.litesoft.commonfoundation.typeutils.Objects; import java.util.NoSuchElementException; public abstract class AbstractChangeTrackingObject implements IChangeTrackingObject { public static final int INITIAL_CHANGE_NUMBER = 0; private int mChangeNumber = INITIAL_CHANGE_NUMBER; private boolean mInitialized = false; private boolean mDeleteRequested = false; private transient Boolean mChangeAllowed; // null means that we are reconstituted after Serialization private boolean mNew; private OriginalValueChangeTracker mChangeTracker; /** * For Serialization */ protected AbstractChangeTrackingObject() { mChangeAllowed = null; } protected AbstractChangeTrackingObject( boolean pChangeAllowed, boolean pNew ) { mChangeAllowed = pNew || pChangeAllowed; mNew = pNew; mChangeTracker = OriginalValueChangeTrackerNull.INSTANCE; } public final void forceSetAttributeValue( String pAttributeName, Object pValue ) throws NoSuchElementException, AttributeIllegalArgumentException { boolean zInitialized = mInitialized; mInitialized = false; Boolean zChangeAllowed = mChangeAllowed; mChangeAllowed = true; setAttributeValue( pAttributeName, pValue ); mChangeAllowed = zChangeAllowed; mInitialized = zInitialized; } protected void LLunNew() { mNew = false; } public boolean isInitialized() { return mInitialized; } // IChangeTrackingObject... public void initialize() { mInitialized = true; } public boolean isChangeAllowed() { return Boolean.TRUE.equals( mChangeAllowed ); } public final int getChangeNumber() { return mChangeNumber; } public final boolean isDirty() { return isDeleteRequested() || isNew() || isAnyAttributeChanged() || LLisDirty(); } public final void requestDelete() { if ( !mInitialized ) { throw new IllegalStateException( "During Object initialization, attempted to Delete: " + toString() ); } if ( verifyMutability() ) { mDeleteRequested = LLrequestDelete(); } } public final boolean isDeleteRequested() { return mDeleteRequested; } public final boolean isNew() { return mNew; } public final boolean isAnyAttributeChanged() { return mChangeTracker.hasChanges(); } /** * @return if Attribute exists && it has changed */ public final boolean isAttributeChanged( String pAttributeName ) { return mChangeTracker.isAttributeChanged( pAttributeName ); } public final String[] getChangedAttributeNames() { return mChangeTracker.getChangedAttributeNames(); } public final boolean isOriginalValueAvailableFor( String pAttributeName ) { return mChangeTracker.isOriginalValueAvailableFor( pAttributeName ); } public final Object getOriginalValueFor( String pAttributeName ) { return mChangeTracker.getOriginalValueFor( pAttributeName ); } // support... protected void populateFrom( AbstractChangeTrackingObject pThem ) { boolean zAnyChanges = false; for ( BoAttribute zAttribute : getBoMetaData().getBoAttributes() ) { zAnyChanges |= populateFromRegular( pThem, zAttribute ); } if ( zAnyChanges ) { incChangeNumber(); } } protected boolean populateFromRegular( AbstractChangeTrackingObject pThem, BoAttribute pAttribute ) { String zName = pAttribute.getName(); Object zCurValue = this.getAttributeValue( zName ); Object zNewValue = pThem.getAttributeValue( zName ); if ( !Objects.areEqual(zCurValue, zNewValue) ) { ChangeControlledBoAttribute zCCA = getChangeControlledBoAttribute( pAttribute ); if ( zCCA == null ) { this.setAttributeValue( zName, zNewValue ); // Assume changeable return true; } if ( zCCA.isSetable() && canMutate( zCCA ) ) { this.setAttributeValue( zName, zNewValue ); ChangeEntry zChangeEntry = pThem.mChangeTracker.getOriginalValueChangeEntryFor( zName ); if ( zChangeEntry != null ) { recordChange( zName, zChangeEntry, zCurValue, zNewValue ); } return true; } } return false; } private ChangeControlledBoAttribute getChangeControlledBoAttribute( BoAttribute pAttribute ) { return (pAttribute instanceof ChangeControlledBoAttribute) ? (ChangeControlledBoAttribute) pAttribute : null; } private boolean canMutate( ChangeControlledBoAttribute pAttribute ) { if ( mInitialized ) { switch ( pAttribute.getMutability() ) { case RW: return true; case AddOnly: return isNew(); case RO: return LLallowMutabilityOnRO( pAttribute ); default: throw new UnsupportedOperationException( "Unknown Mutability (" + pAttribute.getMutability() + ") on change of Attribute (" + pAttribute.getName() + ") on: " + toString() ); } } return true; } private void recordChange( ChangeControlledBoAttribute pChangeControlledBoAttribute, Object pOldValue, Object pNewValue ) { ChangeEntry zChangeEntry = pChangeControlledBoAttribute.createChangeEntryFor( pOldValue ); if ( zChangeEntry != null ) { String zAttributeName = pChangeControlledBoAttribute.getName(); recordChange( zAttributeName, zChangeEntry, pOldValue, pNewValue ); } } private void recordChange( String pAttributeName, ChangeEntry pChangeEntry, Object pOldValue, Object pNewValue ) { mChangeTracker = mChangeTracker.recordChange( pAttributeName, pChangeEntry, pOldValue, pNewValue ); } protected final boolean verifyMutability() throws UnsupportedOperationException { if ( !mInitialized ) // We are in "Creation" mode { return false; } if ( mChangeAllowed == null ) { throw new UnsupportedOperationException( "Attempt to change a re-constituted from Serialization immutable: " + toString() ); } if ( Boolean.FALSE.equals( mChangeAllowed ) ) { throw new UnsupportedOperationException( "Attempt to change a read-only: " + toString() ); } LLverifyMutability(); return true; } protected final ChangeControlledBoAttribute verifyMutability( BoAttribute pAttribute ) throws UnsupportedOperationException { return verifyMutability() ? getChangeControlledBoAttribute( pAttribute ) : null; } public final boolean verifyMutabilityOnChange( BoAttribute pBoAttribute, Object pOldValue, Object pNewValue ) throws UnsupportedOperationException { if ( Objects.areEqual( pOldValue, pNewValue ) ) { return false; } if ( mInitialized ) { ChangeControlledBoAttribute zChangeControlledBoAttribute = verifyMutability( pBoAttribute ); if ( zChangeControlledBoAttribute == null ) { incChangeNumber(); } else { if ( !canMutate( zChangeControlledBoAttribute ) ) { throw new UnsupportedOperationException( "Attempt to change read-only attribute '" + pBoAttribute.getName() + "' on: " + toString() ); } incChangeNumber(); recordChange( zChangeControlledBoAttribute, pOldValue, pNewValue ); } } return true; } protected final void incChangeNumber() { mChangeNumber++; } /** * Throw exception if you want to STOP changes! */ protected void LLverifyMutability() { } /** * @return true to allow Object's RO attribute to be changed anyway. */ @SuppressWarnings({"UnusedDeclaration"}) protected boolean LLallowMutabilityOnRO( ChangeControlledBoAttribute pAttribute ) { return false; } /** * @return true to report dirty beyond the tracked attributes, and New, and Delete Requested */ protected boolean LLisDirty() { return false; } /** * @return true if allowed to actually Request Delete */ protected boolean LLrequestDelete() { return true; } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/bo/change/AbstractChangeTrackingObject.java
Revision | Author | Commited | Message |
---|---|---|---|
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
922 Diff | GeorgeS | Tue 18 Feb, 2014 16:18:01 +0000 | Mini App ready for packaging... |
822 Diff | GeorgeS | Sun 19 Aug, 2012 01:03:51 +0000 | |
207 Diff | GeorgeS | Mon 16 May, 2011 05:45:30 +0000 | |
198 Diff | GeorgeS | Fri 06 May, 2011 23:15:51 +0000 | |
197 Diff | GeorgeS | Fri 06 May, 2011 06:38:54 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |