1 21 package oracle.toplink.essentials.queryframework; 23 24 import oracle.toplink.essentials.exceptions.*; 25 import oracle.toplink.essentials.descriptors.DescriptorEvent; 26 import oracle.toplink.essentials.descriptors.DescriptorEventManager; 27 import oracle.toplink.essentials.descriptors.DescriptorQueryManager; 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 41 public class DeleteObjectQuery extends ObjectLevelModifyQuery { 42 public DeleteObjectQuery() { 43 super(); 44 } 45 46 public DeleteObjectQuery(Object objectToDelete) { 47 this(); 48 setObject(objectToDelete); 49 } 50 51 public DeleteObjectQuery(Call call) { 52 this(); 53 setCall(call); 54 } 55 56 62 protected DatabaseQuery checkForCustomQuery(AbstractSession session, AbstractRecord translationRow) { 63 checkDescriptor(session); 64 65 DescriptorQueryManager queryManager = getDescriptor().getQueryManager(); 67 if ((!isCallQuery()) &&(!isUserDefined()) &&queryManager.hasDeleteQuery()) { return queryManager.getDeleteQuery(); 71 } 72 73 return null; 74 } 75 76 86 protected Object executeInUnitOfWorkObjectLevelModifyQuery(UnitOfWorkImpl unitOfWork, AbstractRecord translationRow) throws DatabaseException, OptimisticLockException { 87 Object result = unitOfWork.processDeleteObjectQuery(this); 88 if (result != null) { 89 return result; 94 } 95 return super.executeInUnitOfWorkObjectLevelModifyQuery(unitOfWork, translationRow); 96 } 97 98 103 public Object executeDatabaseQuery() throws DatabaseException, OptimisticLockException { 104 try { 105 if (getSession().getCommitManager().isCommitCompleted(getObject()) || getSession().getCommitManager().isCommitInPostModify(getObject()) || getSession().getCommitManager().isCommitInPreModify(getObject())) { 107 return object; 108 } 109 110 getSession().getCommitManager().markPreModifyCommitInProgress(getObject()); 111 getSession().beginTransaction(); 112 113 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 115 getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PreDeleteEvent, this)); 117 } 118 119 if (shouldCascadeParts()) { 121 getDescriptor().getQueryManager().preDelete(this); 122 } 123 124 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 127 DescriptorEvent event = new DescriptorEvent(DescriptorEventManager.AboutToDeleteEvent, this); 128 event.setRecord(getModifyRow()); 129 getDescriptor().getEventManager().executeEvent(event); 130 } 131 132 int rowCount = getQueryMechanism().deleteObject().intValue(); 133 134 if (rowCount < 1) { 135 getSession().getEventManager().noRowsModified(this, object); 136 } 137 138 if (getDescriptor().usesOptimisticLocking()) { 139 getDescriptor().getOptimisticLockingPolicy().validateDelete(rowCount, object, this); 140 } 141 142 getSession().getCommitManager().markPostModifyCommitInProgress(getObject()); 143 if (shouldCascadeParts()) { 145 getDescriptor().getQueryManager().postDelete(this); 146 } 147 148 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 150 getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PostDeleteEvent, this)); 152 } 153 154 getSession().commitTransaction(); 155 getSession().getCommitManager().markCommitCompleted(getObject()); 156 157 if (shouldMaintainCache()) { 159 if (getSession().isUnitOfWork()) { 160 ((UnitOfWorkImpl)getSession()).addObjectDeletedDuringCommit(getObject(), getDescriptor()); 161 } else { 162 session.getIdentityMapAccessorInstance().removeFromIdentityMap(getPrimaryKey(), getDescriptor().getJavaClass(), getDescriptor()); 163 } 164 } 165 return getObject(); 166 167 } catch (RuntimeException exception) { 168 getSession().rollbackTransaction(); 169 getSession().getCommitManager().markCommitCompleted(getObject()); 170 throw exception; 171 } 172 } 173 174 178 public boolean isDeleteObjectQuery() { 179 return true; 180 } 181 182 186 protected void prepare() { 187 super.prepare(); 188 189 getQueryMechanism().prepareDeleteObject(); 190 } 191 192 196 protected void prepareCustomQuery(DatabaseQuery customQuery) { 197 DeleteObjectQuery customDeleteQuery = (DeleteObjectQuery)customQuery; 198 customDeleteQuery.setObject(getObject()); 199 customDeleteQuery.setObjectChangeSet(getObjectChangeSet()); 200 customDeleteQuery.setCascadePolicy(getCascadePolicy()); 201 customDeleteQuery.setShouldMaintainCache(shouldMaintainCache()); 202 customDeleteQuery.setTranslationRow(customDeleteQuery.getDescriptor().getObjectBuilder().buildRow(getObject(), customDeleteQuery.getSession())); 203 } 204 205 210 public void prepareForExecution() throws QueryException { 211 super.prepareForExecution(); 212 213 if ((getTranslationRow() == null) || (getTranslationRow().isEmpty())) { 215 setTranslationRow(getDescriptor().getObjectBuilder().buildRowForTranslation(getObject(), getSession())); 216 } 217 218 if (getDescriptor().usesOptimisticLocking()) { 220 getDescriptor().getOptimisticLockingPolicy().addLockValuesToTranslationRow(this); 221 } 222 } 223 } 224 | Popular Tags |