1 21 package oracle.toplink.essentials.queryframework; 23 24 import java.util.*; 25 import oracle.toplink.essentials.descriptors.ClassDescriptor; 26 import oracle.toplink.essentials.exceptions.*; 27 import oracle.toplink.essentials.internal.helper.*; 28 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 29 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 30 import oracle.toplink.essentials.internal.sessions.AbstractSession; 31 32 45 public class DoesExistQuery extends DatabaseQuery { 46 public static final int AssumeNonExistence = 1; 47 public static final int AssumeExistence = 2; 48 public static final int CheckCache = 3; 49 public static final int CheckDatabase = 4; 50 51 52 protected Vector primaryKey; 53 protected Object object; 54 55 56 protected int existencePolicy; 57 58 62 protected boolean checkDatabaseIfInvalid; 64 public boolean checkCacheFirst; 66 71 public DoesExistQuery() { 72 this.existencePolicy = CheckCache; 73 this.checkDatabaseIfInvalid = true; 74 this.checkCacheFirst = false; 75 } 76 77 81 public DoesExistQuery(Object object) { 82 this(); 83 this.object = object; 84 } 85 86 90 public DoesExistQuery(Call call) { 91 this(); 92 setCall(call); 93 } 94 95 100 public void assumeExistenceForDoesExist() { 101 setExistencePolicy(AssumeExistence); 102 } 103 104 109 public void assumeNonExistenceForDoesExist() { 110 setExistencePolicy(AssumeNonExistence); 111 } 112 113 120 public void checkCacheForDoesExist() { 121 setExistencePolicy(CheckCache); 122 } 123 124 128 public void checkDatabaseForDoesExist() { 129 setExistencePolicy(CheckDatabase); 130 } 131 132 138 public Object checkEarlyReturn(Object object, Vector primaryKey, AbstractSession session, AbstractRecord translationRow) { 139 buildSelectionCriteria(session); 142 ClassDescriptor descriptor = session.getDescriptor(object.getClass()); 143 if (object == null){ 145 return Boolean.FALSE; 146 }else if (primaryKey == null) { 147 primaryKey = this.getPrimaryKey(); 148 if ( primaryKey == null ){ 149 primaryKey = descriptor.getObjectBuilder().extractPrimaryKeyFromObject(object, session); 150 } 151 152 } 153 if ((primaryKey == null)|| (primaryKey.contains(null)) ) { 154 return Boolean.FALSE; 155 } 156 157 if (shouldCheckCacheForDoesExist() ||(checkCacheFirst)) { 159 160 if ( checkDatabaseIfInvalid && (session.isUnitOfWork() && 162 ((UnitOfWorkImpl)session).shouldReadFromDB() ) ){ 163 return null; 164 } 165 166 oracle.toplink.essentials.internal.identitymaps.CacheKey cacheKey; 167 Class objectClass = object.getClass(); 168 if (session.isUnitOfWork()){ 169 cacheKey = session.getIdentityMapAccessorInstance().getCacheKeyForObject(primaryKey,objectClass, descriptor); 170 if (cacheKey!=null){ return Boolean.TRUE; 172 } 173 cacheKey = ((UnitOfWorkImpl)session).getParent().getIdentityMapAccessorInstance().getCacheKeyForObject(primaryKey,objectClass, descriptor); 174 }else{ 175 cacheKey = session.getIdentityMapAccessorInstance().getCacheKeyForObject(primaryKey,objectClass, descriptor); 176 } 177 178 if ((cacheKey !=null)){ 179 boolean invalid; 181 if ( checkDatabaseIfInvalid ){ 182 long currentTimeInMillis = System.currentTimeMillis(); 183 invalid = session.getDescriptor(objectClass).getCacheInvalidationPolicy().isInvalidated(cacheKey, currentTimeInMillis); 184 }else { 185 invalid = false; 186 } 187 188 if (!invalid){ 189 Object objectFromCache = cacheKey.getObject(); 190 if ((session instanceof oracle.toplink.essentials.internal.ejb.cmp3.base.RepeatableWriteUnitOfWork)&& 191 (((oracle.toplink.essentials.internal.ejb.cmp3.base.RepeatableWriteUnitOfWork)session).getUnregisteredDeletedCloneForOriginal(objectFromCache)!=null)){ 192 if(shouldCheckCacheForDoesExist()){ 194 return Boolean.FALSE; 195 } 196 }else { 197 return Boolean.TRUE; 198 } 199 200 }else { 201 return null; 203 } 204 }else if(shouldCheckCacheForDoesExist()){ 205 return Boolean.FALSE; 207 } 208 } 209 if (shouldAssumeNonExistenceForDoesExist()) { 211 return Boolean.FALSE; 212 } 213 214 if (shouldAssumeExistenceForDoesExist()) { 216 return Boolean.TRUE; 217 } 218 219 return null; 220 } 221 222 227 public Object checkEarlyReturn(AbstractSession session, AbstractRecord translationRow) { 228 return checkEarlyReturn(getObject(), getPrimaryKey(), session, translationRow); 229 } 230 231 238 public Object executeDatabaseQuery() throws DatabaseException { 239 DatabaseField field = getDoesExistField(); 241 242 AbstractRecord databaseRow = getQueryMechanism().selectRowForDoesExist(field); 244 245 return new Boolean (databaseRow != null); 247 } 248 249 253 protected DatabaseField getDoesExistField() { 254 return (DatabaseField)(getDescriptor().getPrimaryKeyFields().get(0)); 255 } 256 257 261 public int getExistencePolicy() { 262 return this.existencePolicy; 263 } 264 265 269 public Object getObject() { 270 return object; 271 } 272 273 277 public Vector getPrimaryKey() { 278 return primaryKey; 279 } 280 281 284 public Class getReferenceClass() { 285 return getObject().getClass(); 286 } 287 288 294 public String getReferenceClassName() { 295 return getReferenceClass().getName(); 296 } 297 298 302 protected void prepare() throws QueryException { 303 if (getDescriptor() == null) { 304 setDescriptor(getSession().getDescriptor(getObject())); 306 } 307 308 if (getObject() != null) { setObject(getDescriptor().getObjectBuilder().unwrapObject(getObject(), getSession())); 310 } 311 312 super.prepare(); 313 314 getQueryMechanism().prepareDoesExist(getDoesExistField()); 316 } 317 318 322 public void prepareForExecution() throws QueryException { 323 super.prepareForExecution(); 324 325 if (getObject() == null) { 326 throw QueryException.objectToModifyNotSpecified(this); 327 } 328 setObject(getDescriptor().getObjectBuilder().unwrapObject(getObject(), getSession())); 329 330 if (getDescriptor() == null) { 331 setDescriptor(getSession().getDescriptor(getObject().getClass())); 332 } 333 334 if (getPrimaryKey() == null) { 335 setPrimaryKey(getDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(getObject(), getSession())); 336 } 337 338 if ((getTranslationRow() == null) || (getTranslationRow().isEmpty())) { 339 setTranslationRow(getDescriptor().getObjectBuilder().buildRowForTranslation(getObject(), getSession())); 340 } 341 } 342 343 347 public void setExistencePolicy(int existencePolicy) { 348 this.existencePolicy = existencePolicy; 349 } 350 351 355 public void setObject(Object object) { 356 this.object = object; 357 } 358 359 363 public void setPrimaryKey(Vector primaryKey) { 364 this.primaryKey = primaryKey; 365 } 366 367 372 public boolean shouldAssumeExistenceForDoesExist() { 373 return existencePolicy == AssumeExistence; 374 } 375 376 380 public boolean shouldAssumeNonExistenceForDoesExist() { 381 return existencePolicy == AssumeNonExistence; 382 } 383 384 389 public boolean shouldCheckCacheForDoesExist() { 390 return existencePolicy == CheckCache; 391 } 392 393 397 public boolean shouldCheckDatabaseForDoesExist() { 398 return existencePolicy == CheckDatabase; 399 } 400 401 407 public void setCheckCacheFirst(boolean checkCacheFirst){ 408 this.checkCacheFirst = checkCacheFirst; 409 } 410 414 public boolean getCheckCacheFirst(){ 415 return this.checkCacheFirst; 416 } 417 418 425 public void setCheckDatabaseIfInvalid(boolean checkCacheFirst){ 426 this.checkCacheFirst = checkCacheFirst; 427 } 428 432 public boolean getCheckDatabaseIfInvalid(){ 433 return this.checkCacheFirst; 434 } 435 } 436 | Popular Tags |