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 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 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.orsup.lazyload; import org.litesoft.commonfoundation.typeutils.*; import java.util.*; import org.litesoft.orsup.base.*; import org.litesoft.orsup.nonpublic.*; import org.litesoft.orsup.selection.*; import org.litesoft.util.*; public abstract class AbstractLazyLoadToMany<Owner extends PersistentObjectImpl, Them extends PersistentObjectImpl> extends AbstractLazyLoadTo<Owner, Them> { protected MetaDataForPO mThierMetaData; private final OrderBy mOrderBy; private List<Them> mValue = null; private ModificationAccessableArrayList<Them> mMutableValueToGiveOut = null; private boolean mMutated = false; public AbstractLazyLoadToMany( Owner pOwner, AbstractAttributeAccessorSCDtoMany<Owner, Them> pSCDtoMany ) { super( pOwner, pSCDtoMany ); mThierMetaData = getPOMetaDataRequired( pSCDtoMany.getToThemName() ); mOrderBy = createOrderBy( pSCDtoMany ); } public boolean isMutated() { return mMutated; } private void setValue( List<Them> pNewThems ) { mValue = (pNewThems instanceof ArrayList) ? pNewThems : new ArrayList<Them>( pNewThems ); loadedListMutated(); } @Override protected void releaseLoadeds() { mValue.clear(); mValue = null; mMutableValueToGiveOut = null; mMutated = false; } private void loadedListMutated() { mMutableValueToGiveOut = null; mMutated = true; } public List<Them> getValue() { loadValue(); if ( (mMutableValueToGiveOut == null) || mMutableValueToGiveOut.isModified() ) { mMutableValueToGiveOut = new ModificationAccessableArrayList<Them>( mValue ); // copy it } return mMutableValueToGiveOut; } @Override public void revertToNotLoaded() { super.revertToNotLoaded(); clearAdded(); clearRemoved(); } private OrderBy createOrderBy( AbstractAttributeAccessorSCDtoMany<Owner, Them> pSCDtoMany ) { String orderby = Strings.noEmpty( pSCDtoMany.getOrderByAttribute() ); if ( orderby != null ) { return BuilderMetaDataAwareOrderBy.create( mThierMetaData, orderby ); } if ( null != (orderby = Strings.noEmpty( mThierMetaData.getDefaultOrderByAttributeName() )) ) { return BuilderMetaDataAwareOrderBy.create( mThierMetaData, orderby ); } OrderBy zOrderBy = null; AttributeAccessorKeySet keySet = mThierMetaData.getKeySet(); if ( keySet != null ) { AttributeAccessorSCD[] keys = keySet.getKeyAttributeAccessorSCDs(); if ( (keys != null) && (keys.length != 0) ) { int i = keys.length - 1; zOrderBy = new OrderBy( keys[i] ); while ( --i >= 0 ) { zOrderBy = new OrderBy( keys[i], zOrderBy ); } } } return zOrderBy; } abstract protected Object getLoadingWhereClauseIsEqualValue(); public abstract AbstractAttributeAccessorSCDtoMany<Owner, Them> getSCDtoMany(); @Override public final String getAttributeName() { return getSCDtoMany().getName(); } @Override protected final boolean isThemDependantOnUs() { return getSCDtoMany().isThemDependantOnUs(); } protected WhereClause createLoadingWhereClause() { String backReference = getSCDtoMany().getBackReference(); Object value = getLoadingWhereClauseIsEqualValue(); AttributeAccessorSCD scd = mThierMetaData.getAccessorSCDrequired( backReference ); return WhereClauseFactory.INSTANCE.isEqual( scd, value ); } /** * @param pNewThems Nullable and may actually be no changes */ public void setValues( List<Them> pNewThems ) { loadValue(); // Since already added to loaded List if ( pNewThems == null ) { pNewThems = new ArrayList<Them>(); } List<Them> zCurThems = new ArrayList<Them>( mValue ); for ( Them newThem : pNewThems ) { if ( !zCurThems.remove( newThem ) ) // NEW newThem { addValue( newThem ); } } for ( Them curThem : zCurThems ) { removeValue( curThem ); } setValue( pNewThems ); } /** * @param pPO !null, but may already be in the list */ public void addValue( Them pPO ) { loadValue(); // Since already added to loaded List if ( LLaddValue( pPO ) ) { getToOneLazyLoader( pPO ).backdoorSetValue( mOwner, getAttributeName() ); } } /** * @param pPO !null , but might not be in the list */ public void removeValue( Them pPO ) { loadValue(); // Since already added to loaded List if ( LLremoveValue( pPO ) ) { getToOneLazyLoader( pPO ).backdoorSetValue( null, getAttributeName() ); if ( isThemDependantOnUs() ) { pPO.requestDelete(); } } } private AbstractLazyLoadToOne<Them, Owner> getToOneLazyLoader( Them pThem ) { checkTransactions( pThem ); String backReference = getSCDtoMany().getBackReference(); pThem.getAttributeValue( backReference ); // Force them to LazyLoad AbstractAttributeAccessorSCDtoOne acdToOne = (AbstractAttributeAccessorSCDtoOne) mThierMetaData.getAccessorSCDrequired( backReference ); //noinspection unchecked return acdToOne.getValueHolder( pThem ); } /** * Already Lazy Loaded * * @param pPO !null, but may already be in the list * * @return true if added */ private boolean LLaddValue( Them pPO ) { boolean notInList = !mValue.contains( pPO ); if ( notInList ) { loadedListMutated(); mValue.add( pPO ); } return notInList; } /** * Already Lazy Loaded * * @param pPO !null , but might not be in the list * * @return true if removed */ private boolean LLremoveValue( Them pPO ) { boolean inList = mValue.remove( pPO ); if ( inList ) { loadedListMutated(); } return inList; } private void loadValue() { if ( !mLoaded ) { List<Them> zPOs; if ( mOwner.isNew() ) { zPOs = new ArrayList<Them>(); } else { String themName = mThierMetaData.getPOregistrationName(); WhereClause whereClause = createLoadingWhereClause(); List found = mOwner.getFinder().findAll( themName, whereClause, mOrderBy ); //noinspection unchecked zPOs = (List<Them>) found; } applyAddedsAndRemoveds( zPOs ); setValue( zPOs ); mLoaded = true; } } private void clearRemoved() { if ( mRemoved != null ) { mRemoved.clear(); mRemoved = null; } } private void clearAdded() { if ( mAdded != null ) { mAdded.clear(); mAdded = null; } } private Set<Them> initAdded() { return (mAdded != null) ? mAdded : (mAdded = new HashSet<Them>()); } private Set<Them> initRemoved() { return (mRemoved != null) ? mRemoved : (mRemoved = new HashSet<Them>()); } private void applyAddedsAndRemoveds( List<Them> pPOs ) { if ( (mAdded != null) || (mRemoved != null) ) { initAdded(); initRemoved(); if ( !mAdded.isEmpty() || !mRemoved.isEmpty() ) { for ( int i = pPOs.size(); --i >= 0; ) // Backwards for Safe Removal! { Them zThem = pPOs.get( i ); if ( mRemoved.contains( zThem ) ) // Should remove? { mRemoved.remove( zThem ); pPOs.remove( i ); continue; } if ( mAdded.contains( zThem ) ) // Allready in the List? { mAdded.remove( zThem ); } } pPOs.addAll( mAdded ); } clearAdded(); clearRemoved(); } } private Set<Them> mAdded = null; private Set<Them> mRemoved = null; // /** * May or may not be Already Lazy Loaded * * @param pPO !null */ void backdoorAddValue( Them pPO ) { if ( !mLoaded ) { initAdded().add( pPO ); } else if ( !mValue.contains( pPO ) ) { loadedListMutated(); mValue.add( pPO ); } } /** * May or may not be Already Lazy Loaded * * @param pPO !null */ void backdoorRemoveValue( Them pPO ) { if ( !mLoaded ) { if ( (mAdded == null) || !mAdded.remove( pPO ) ) { initRemoved().add( pPO ); } } else if ( mValue.remove( pPO ) ) // true if was removed { loadedListMutated(); } } } |
Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/orsup/lazyload/AbstractLazyLoadToMany.java
Revision | Author | Commited | Message |
---|---|---|---|
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
801 Diff | GeorgeS | Wed 15 Aug, 2012 03:59:02 +0000 | |
222 Diff | GeorgeS | Fri 20 May, 2011 18:04:31 +0000 | |
151 Diff | GeorgeS | Thu 17 Mar, 2011 04:16:22 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
24 Diff | GeorgeS | Wed 24 Feb, 2010 01:51:38 +0000 | |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |