1 21 package oracle.toplink.essentials.queryframework; 23 24 import java.util.Vector ; 25 import oracle.toplink.essentials.exceptions.*; 26 import oracle.toplink.essentials.expressions.*; 27 import oracle.toplink.essentials.internal.queryframework.DatabaseQueryMechanism; 28 import oracle.toplink.essentials.internal.queryframework.ExpressionQueryMechanism; 29 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 30 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 31 import oracle.toplink.essentials.internal.sessions.AbstractSession; 32 33 40 public abstract class ModifyAllQuery extends ModifyQuery { 41 42 43 public static final int NO_CACHE = 0; 44 public static final int INVALIDATE_CACHE = 1; 45 46 private int m_cacheUsage = INVALIDATE_CACHE; 47 48 protected Class referenceClass; 49 protected String referenceClassName; 50 51 52 protected transient Integer result; 53 54 55 private boolean shouldDeferExecutionInUOW; 56 57 58 protected ExpressionBuilder defaultBuilder; 59 60 61 protected boolean isPreparedUsingTempStorage; 62 63 66 public ModifyAllQuery() { 67 super(); 68 shouldDeferExecutionInUOW = true; 69 } 70 71 75 public ModifyAllQuery(Class referenceClass) { 76 this(); 77 setReferenceClass(referenceClass); 78 } 79 80 85 public ModifyAllQuery(Class referenceClass, Expression selectionCriteria) { 86 this(); 87 setReferenceClass(referenceClass); 88 setSelectionCriteria(selectionCriteria); 89 } 90 91 95 public boolean isModifyQuery() { 96 return true; 97 } 98 99 102 public void setIsPreparedUsingTempStorage(boolean isPreparedUsingTempStorage) { 103 this.isPreparedUsingTempStorage = isPreparedUsingTempStorage; 104 } 105 106 109 public boolean isPreparedUsingTempStorage() { 110 return isPreparedUsingTempStorage; 111 } 112 113 118 protected void clonedQueryExecutionComplete(DatabaseQuery query, AbstractSession session) { 119 super.clonedQueryExecutionComplete(query, session); 120 121 if (session.isUnitOfWork()) { 122 ((UnitOfWorkImpl)session).storeModifyAllQuery(query); 123 } 124 } 125 126 138 public Object executeInUnitOfWork(UnitOfWorkImpl unitOfWork, AbstractRecord translationRow) throws DatabaseException, OptimisticLockException { 139 if (unitOfWork.isNestedUnitOfWork()) { 140 throw ValidationException.nestedUOWNotSupportedForModifyAllQuery(); 141 } 142 143 if(shouldDeferExecutionInUOW()) { 146 unitOfWork.storeDeferredModifyAllQuery(this, translationRow); 147 result = null; 148 } else { 149 if(!unitOfWork.isInTransaction()) { 150 unitOfWork.beginEarlyTransaction(); 151 } 152 unitOfWork.setWasNonObjectLevelModifyQueryExecuted(true); 153 result = (Integer )super.executeInUnitOfWork(unitOfWork, translationRow); 154 } 155 return result; 156 } 157 158 162 public int getCacheUsage() { 163 return m_cacheUsage; 164 } 165 166 171 public ExpressionBuilder getExpressionBuilder() { 172 if (defaultBuilder == null) { 173 initializeDefaultBuilder(); 174 } 175 176 return defaultBuilder; 177 } 178 179 183 public void setExpressionBuilder(ExpressionBuilder builder) { 184 this.defaultBuilder = builder; 185 } 186 187 192 public String getReferenceClassName() { 193 if ((referenceClassName == null) && (referenceClass != null)) { 194 referenceClassName = referenceClass.getName(); 195 } 196 return referenceClassName; 197 } 198 199 203 public Class getReferenceClass() { 204 return referenceClass; 205 } 206 207 212 protected void invalidateCache() { 213 oracle.toplink.essentials.sessions.IdentityMapAccessor identityMapAccessor = getSession().getIdentityMapAccessor(); 214 if (getSelectionCriteria() == null) { 215 if(getDescriptor().isChildDescriptor()) { 217 Vector collectionToInvalidate = identityMapAccessor.getAllFromIdentityMap(null, getReferenceClass(), getTranslationRow(), null); 218 identityMapAccessor.invalidateObjects(collectionToInvalidate); 219 } else { 220 identityMapAccessor.invalidateClass(getReferenceClass()); 222 } 223 } else { 224 boolean noObjectsModifiedInDb = result != null && result.intValue() == 0; 227 try { 228 InMemoryQueryIndirectionPolicy policy = new InMemoryQueryIndirectionPolicy(); 229 if(noObjectsModifiedInDb) { 230 policy.ignoreIndirectionExceptionReturnNotConformed(); 231 } else { 232 policy.ignoreIndirectionExceptionReturnConformed(); 233 } 234 Vector collectionToInvalidate = identityMapAccessor.getAllFromIdentityMap(getSelectionCriteria(), getReferenceClass(), getTranslationRow(), policy); 235 identityMapAccessor.invalidateObjects(collectionToInvalidate); 236 } catch (QueryException ex) { 237 if(ex.getErrorCode() == QueryException.CANNOT_CONFORM_EXPRESSION) { 238 if(!noObjectsModifiedInDb) { 240 identityMapAccessor.invalidateClass(getReferenceClass()); 242 } 243 } else { 244 throw ex; 245 } 246 } 247 } 248 } 249 250 254 public void mergeChangesIntoSharedCache() { 255 if (shouldInvalidateCache()) { 256 invalidateCache(); 257 } 258 } 259 260 264 public void setCacheUsage(int cacheUsage) { 265 m_cacheUsage = cacheUsage; 266 } 267 268 272 public void setReferenceClass(Class referenceClass) { 273 if (this.referenceClass != referenceClass) { 274 setIsPrepared(false); 275 } 276 this.referenceClass = referenceClass; 277 } 278 279 284 public void setReferenceClassName(String className) { 285 referenceClassName = className; 286 } 287 288 292 public void setShouldDeferExecutionInUOW(boolean shouldDeferExecutionInUOW) { 293 this.shouldDeferExecutionInUOW = shouldDeferExecutionInUOW; 294 } 295 296 300 public boolean shouldDeferExecutionInUOW() { 301 return shouldDeferExecutionInUOW; 302 } 303 304 307 protected boolean shouldInvalidateCache() { 308 return m_cacheUsage == INVALIDATE_CACHE; 309 } 310 311 317 protected void initializeDefaultBuilder() { 318 initializeQuerySpecificDefaultBuilder(); 319 if(defaultBuilder == null) { 320 defaultBuilder = new ExpressionBuilder(); 321 } 322 } 323 324 331 protected void initializeQuerySpecificDefaultBuilder() { 332 DatabaseQueryMechanism mech = getQueryMechanism(); 333 if (mech.isExpressionQueryMechanism() && ((ExpressionQueryMechanism)mech).getExpressionBuilder() != null) { 334 this.defaultBuilder = ((ExpressionQueryMechanism)mech).getExpressionBuilder(); 335 } 336 } 337 } 338 | Popular Tags |