1 21 package oracle.toplink.essentials.queryframework; 23 24 import java.util.*; 25 import oracle.toplink.essentials.exceptions.*; 26 import oracle.toplink.essentials.expressions.*; 27 import oracle.toplink.essentials.descriptors.DescriptorEvent; 28 import oracle.toplink.essentials.descriptors.DescriptorEventManager; 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 import oracle.toplink.essentials.descriptors.ClassDescriptor; 33 34 47 public class DeleteAllQuery extends ModifyAllQuery { 48 49 50 protected Vector objects; 51 52 55 public DeleteAllQuery() { 56 super(); 57 } 58 59 63 public DeleteAllQuery(Class referenceClass) { 64 super(referenceClass); 65 } 66 67 72 public DeleteAllQuery(Class referenceClass, Expression selectionCriteria) { 73 super(referenceClass, selectionCriteria); 74 } 75 76 80 public boolean isDeleteAllQuery() { 81 return true; 82 } 83 84 96 public Object executeInUnitOfWork(UnitOfWorkImpl unitOfWork, AbstractRecord translationRow) throws DatabaseException, OptimisticLockException { 97 if (getObjects() != null) { 98 if (unitOfWork.isAfterWriteChangesButBeforeCommit()) { 99 throw ValidationException.illegalOperationForUnitOfWorkLifecycle(unitOfWork.getLifecycle(), "executeQuery(DeleteAllQuery)"); 100 } 101 102 if (!unitOfWork.getCommitManager().isActive()) { 104 return unitOfWork.getParent().executeQuery(this, translationRow); 105 } 106 result = (Integer )super.execute(unitOfWork, translationRow); 107 return result; 108 } else { 109 return super.executeInUnitOfWork(unitOfWork, translationRow); 110 } 111 } 112 113 120 public Object executeDatabaseQuery() throws DatabaseException { 121 if (getObjects() != null) { 123 124 if(isExpressionQuery() && getSelectionCriteria() == null) { 125 throw QueryException.deleteAllQuerySpecifiesObjectsButNotSelectionCriteria(getDescriptor(), this, getObjects().toString()); 127 } 128 129 try { 131 getSession().beginTransaction(); 132 133 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 136 for (Enumeration deletedObjectsEnum = getObjects().elements(); 137 deletedObjectsEnum.hasMoreElements();) { 138 DescriptorEvent event = new DescriptorEvent(deletedObjectsEnum.nextElement()); 139 event.setEventCode(DescriptorEventManager.PreDeleteEvent); 140 event.setSession(getSession()); 141 event.setQuery(this); 142 getDescriptor().getEventManager().executeEvent(event); 143 } 144 } 145 146 result = getQueryMechanism().deleteAll(); 147 148 if (getDescriptor().getEventManager().hasAnyEventListeners()) { 151 for (Enumeration deletedObjectsEnum = getObjects().elements(); 152 deletedObjectsEnum.hasMoreElements();) { 153 DescriptorEvent event = new DescriptorEvent(deletedObjectsEnum.nextElement()); 154 event.setEventCode(DescriptorEventManager.PostDeleteEvent); 155 event.setSession(getSession()); 156 event.setQuery(this); 157 getDescriptor().getEventManager().executeEvent(event); 158 } 159 } 160 161 if (shouldMaintainCache()) { 162 for (Enumeration objectsEnum = getObjects().elements(); 164 objectsEnum.hasMoreElements();) { 165 Object deleted = objectsEnum.nextElement(); 166 if (getSession().isUnitOfWork()) { 167 deleted = getDescriptor().getObjectBuilder().unwrapObject(deleted, getSession()); 169 ((UnitOfWorkImpl)getSession()).addObjectDeletedDuringCommit(deleted, getDescriptor()); 170 } else { 171 getSession().getIdentityMapAccessor().removeFromIdentityMap(deleted); 172 } 173 } 174 } 175 176 getSession().commitTransaction(); 177 178 } catch (RuntimeException exception) { 179 getSession().rollbackTransaction(); 180 throw exception; 181 } 182 } else { 183 result = getQueryMechanism().deleteAll(); mergeChangesIntoSharedCache(); 185 } 186 187 return result; 188 } 189 190 194 public void executeDeleteAll(AbstractSession session, AbstractRecord translationRow, Vector objects) throws DatabaseException { 195 this.checkPrepare(session, translationRow); 196 DeleteAllQuery queryToExecute = (DeleteAllQuery)clone(); 197 198 queryToExecute.setTranslationRow(translationRow); 200 queryToExecute.setSession(session); 201 queryToExecute.setObjects(objects); 202 queryToExecute.prepareForExecution(); 203 queryToExecute.executeDatabaseQuery(); 204 } 205 206 210 public Vector getObjects() { 211 return objects; 212 } 213 214 218 protected void prepare() throws QueryException { 219 super.prepare(); 220 221 if (getReferenceClass() == null) { 222 throw QueryException.referenceClassMissing(this); 223 } 224 225 if (getDescriptor() == null) { 226 ClassDescriptor referenceDescriptor = getSession().getDescriptor(getReferenceClass()); 227 if (referenceDescriptor == null) { 228 throw QueryException.descriptorIsMissing(getReferenceClass(), this); 229 } 230 setDescriptor(referenceDescriptor); 231 } 232 233 if (getDescriptor().isAggregateDescriptor()) { 234 throw QueryException.aggregateObjectCannotBeDeletedOrWritten(getDescriptor(), this); 235 } 236 237 getQueryMechanism().prepareDeleteAll(); 238 } 239 240 261 public void setObjects(Vector objectCollection) { 262 objects = objectCollection; 263 } 264 } 265 | Popular Tags |