1 21 package oracle.toplink.essentials.queryframework; 23 24 import java.util.*; 25 import oracle.toplink.essentials.internal.helper.*; 26 import oracle.toplink.essentials.internal.sessions.*; 27 import oracle.toplink.essentials.exceptions.*; 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 import oracle.toplink.essentials.descriptors.ClassDescriptor; 32 33 46 public abstract class ObjectLevelModifyQuery extends ModifyQuery { 47 48 49 protected Vector primaryKey; 50 51 52 protected Object object; 53 54 55 protected ObjectChangeSet objectChangeSet; 56 57 58 protected Object backupClone; 59 60 64 public ObjectLevelModifyQuery() { 65 this.cascadePolicy = CascadePrivateParts; 66 } 67 68 72 public void checkDescriptor(AbstractSession session) throws QueryException { 73 if (getDescriptor() == null) { 74 if (getObject() == null) { 75 throw QueryException.objectToModifyNotSpecified(this); 76 } 77 78 ClassDescriptor referenceDescriptor = session.getDescriptor(getObject()); 80 if (referenceDescriptor == null) { 81 throw QueryException.descriptorIsMissing(getObject().getClass(), this); 82 } 83 setDescriptor(referenceDescriptor); 84 } 85 } 86 87 91 public Object executeInUnitOfWork(UnitOfWorkImpl unitOfWork, AbstractRecord translationRow) throws DatabaseException { 92 if (unitOfWork.isAfterWriteChangesButBeforeCommit()) { 93 throw ValidationException.illegalOperationForUnitOfWorkLifecycle(unitOfWork.getLifecycle(), "executeQuery(ObjectLevelModifyQuery)"); 94 } 95 return executeInUnitOfWorkObjectLevelModifyQuery(unitOfWork, translationRow); 96 } 97 98 107 protected Object executeInUnitOfWorkObjectLevelModifyQuery(UnitOfWorkImpl unitOfWork, AbstractRecord translationRow) throws DatabaseException, OptimisticLockException { 108 if (!unitOfWork.getCommitManager().isActive()) { 109 throw QueryException.invalidQuery(this); 110 } 111 112 if ((getObject() != null) && (unitOfWork.isClassReadOnly(getObject().getClass()))) { 113 return getObject(); 114 } 115 116 if (unitOfWork.shouldPerformNoValidation() && unitOfWork.getUnregisteredExistingObjects().containsKey(getObject())) { 118 return null; 121 } 122 123 return super.executeInUnitOfWork(unitOfWork, translationRow); 124 } 125 126 130 public Object getBackupClone() { 131 if ((backupClone == null) && getSession().isUnitOfWork()) { 134 setBackupClone(((UnitOfWorkImpl)getSession()).getBackupCloneForCommit(getObject())); 135 } 136 return backupClone; 137 } 138 139 143 public Object getObject() { 144 return object; 145 } 146 147 151 public ObjectChangeSet getObjectChangeSet() { 152 return this.objectChangeSet; 153 } 154 155 159 public Vector getPrimaryKey() { 160 return primaryKey; 161 } 162 163 166 public Class getReferenceClass() { 167 return getObject().getClass(); 168 } 169 170 176 public String getReferenceClassName() { 177 return getReferenceClass().getName(); 178 } 179 180 184 public boolean isObjectLevelModifyQuery() { 185 return true; 186 } 187 188 193 protected void prepare() throws QueryException { 194 checkDescriptor(getSession()); 195 196 if (getObject() != null) { setObject(getDescriptor().getObjectBuilder().unwrapObject(getObject(), getSession())); 198 } 199 200 if (getDescriptor().isAggregateDescriptor()) { 201 throw QueryException.aggregateObjectCannotBeDeletedOrWritten(getDescriptor(), this); 202 } 203 204 super.prepare(); 205 } 206 207 212 public void prepareForExecution() throws QueryException { 213 super.prepareForExecution(); 214 215 if (getObject() == null) { 216 throw QueryException.objectToModifyNotSpecified(this); 217 } 218 219 setObject(getDescriptor().getObjectBuilder().unwrapObject(getObject(), getSession())); 220 221 if (getPrimaryKey() == null) { 222 if (getObjectChangeSet() != null) { 223 setPrimaryKey(getObjectChangeSet().getPrimaryKeys()); 224 } else { 225 setPrimaryKey(getSession().keyFromObject(getObject())); 226 } 227 } 228 } 229 230 234 public void setBackupClone(Object backupClone) { 235 this.backupClone = backupClone; 236 } 237 238 242 public void setObject(Object object) { 243 this.object = object; 244 } 245 246 250 public void setObjectChangeSet(ObjectChangeSet changeSet) { 251 this.objectChangeSet = changeSet; 252 } 253 254 258 public void setPrimaryKey(Vector primaryKey) { 259 this.primaryKey = primaryKey; 260 } 261 262 public String toString() { 263 return Helper.getShortClassName(getClass()) + "(" + String.valueOf(getObject()) + ")"; 264 } 265 } 266 | Popular Tags |