oracle.toplink.essentials.queryframework.DeleteObjectQuery Maven / Gradle / Ivy
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* glassfish/bootstrap/legal/CDDLv1.0.txt or
* https://glassfish.dev.java.net/public/CDDLv1.0.html.
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
* add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your
* own identifying information: Portions Copyright [yyyy]
* [name of copyright owner]
*/
// Copyright (c) 1998, 2006, Oracle. All rights reserved.
package oracle.toplink.essentials.queryframework;
import oracle.toplink.essentials.exceptions.*;
import oracle.toplink.essentials.descriptors.DescriptorEvent;
import oracle.toplink.essentials.descriptors.DescriptorEventManager;
import oracle.toplink.essentials.descriptors.DescriptorQueryManager;
import oracle.toplink.essentials.internal.sessions.AbstractRecord;
import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl;
import oracle.toplink.essentials.internal.sessions.AbstractSession;
/**
* Purpose: Used for deleting objects.
*
*
Responsibilities:
* Extract primary key from object and delete it.
*
* @author Yvon Lavoie
* @since TOPLink/Java 1.0
*/
public class DeleteObjectQuery extends ObjectLevelModifyQuery {
public DeleteObjectQuery() {
super();
}
public DeleteObjectQuery(Object objectToDelete) {
this();
setObject(objectToDelete);
}
public DeleteObjectQuery(Call call) {
this();
setCall(call);
}
/**
* INTERNAL:
* Check to see if a custom query should be used for this query.
* This is done before the query is copied and prepared/executed.
* null means there is none.
*/
protected DatabaseQuery checkForCustomQuery(AbstractSession session, AbstractRecord translationRow) {
checkDescriptor(session);
// check if user defined a custom query
DescriptorQueryManager queryManager = getDescriptor().getQueryManager();
if ((!isCallQuery())// this is not a hand-coded (custom SQL, SDK etc.) call
&&(!isUserDefined())// and this is not a user-defined query (in the query manager)
&&queryManager.hasDeleteQuery()) {// and there is a user-defined query (in the query manager)
return queryManager.getDeleteQuery();
}
return null;
}
/**
* INTERNAL:
* Code was moved from UnitOfWork.internalExecuteQuery
*
* @param unitOfWork
* @param translationRow
* @return
* @throws oracle.toplink.essentials.exceptions.DatabaseException
* @throws oracle.toplink.essentials.exceptions.OptimisticLockException
*/
protected Object executeInUnitOfWorkObjectLevelModifyQuery(UnitOfWorkImpl unitOfWork, AbstractRecord translationRow) throws DatabaseException, OptimisticLockException {
Object result = unitOfWork.processDeleteObjectQuery(this);
if (result != null) {
// if the above method returned something then the unit of work
//was not writing so the object has been stored to delete later
//so return the object. See the above method for the cases
//where this object will be returned.
return result;
}
return super.executeInUnitOfWorkObjectLevelModifyQuery(unitOfWork, translationRow);
}
/**
* INTERNAL:
* Perform the work to delete an object.
* @return object - the object being deleted.
*/
public Object executeDatabaseQuery() throws DatabaseException, OptimisticLockException {
try {
// Check if the object has already been commited, then no work is required
if (getSession().getCommitManager().isCommitCompleted(getObject()) || getSession().getCommitManager().isCommitInPostModify(getObject()) || getSession().getCommitManager().isCommitInPreModify(getObject())) {
return object;
}
getSession().getCommitManager().markPreModifyCommitInProgress(getObject());
getSession().beginTransaction();
// PERF: Avoid events if no listeners.
if (getDescriptor().getEventManager().hasAnyEventListeners()) {
// Need to run pre-delete selector if available
getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PreDeleteEvent, this));
}
// Verify if deep shallow modify is turned on
if (shouldCascadeParts()) {
getDescriptor().getQueryManager().preDelete(this);
}
// CR#2660080 missing aboutToDelete event.
// PERF: Avoid events if no listeners.
if (getDescriptor().getEventManager().hasAnyEventListeners()) {
DescriptorEvent event = new DescriptorEvent(DescriptorEventManager.AboutToDeleteEvent, this);
event.setRecord(getModifyRow());
getDescriptor().getEventManager().executeEvent(event);
}
int rowCount = getQueryMechanism().deleteObject().intValue();
if (rowCount < 1) {
getSession().getEventManager().noRowsModified(this, object);
}
if (getDescriptor().usesOptimisticLocking()) {
getDescriptor().getOptimisticLockingPolicy().validateDelete(rowCount, object, this);
}
getSession().getCommitManager().markPostModifyCommitInProgress(getObject());
// Verify if deep shallow modify is turned on
if (shouldCascadeParts()) {
getDescriptor().getQueryManager().postDelete(this);
}
// PERF: Avoid events if no listeners.
if (getDescriptor().getEventManager().hasAnyEventListeners()) {
// Need to run post-delete selector if available
getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PostDeleteEvent, this));
}
getSession().commitTransaction();
getSession().getCommitManager().markCommitCompleted(getObject());
// CR3510313, avoid removing aggregate collections from cache (maintain cache is false).
if (shouldMaintainCache()) {
if (getSession().isUnitOfWork()) {
((UnitOfWorkImpl)getSession()).addObjectDeletedDuringCommit(getObject(), getDescriptor());
} else {
session.getIdentityMapAccessorInstance().removeFromIdentityMap(getPrimaryKey(), getDescriptor().getJavaClass(), getDescriptor());
}
}
return getObject();
} catch (RuntimeException exception) {
getSession().rollbackTransaction();
getSession().getCommitManager().markCommitCompleted(getObject());
throw exception;
}
}
/**
* PUBLIC:
* Return if this is a delete object query.
*/
public boolean isDeleteObjectQuery() {
return true;
}
/**
* INTERNAL:
* Prepare the receiver for execution in a session.
*/
protected void prepare() {
super.prepare();
getQueryMechanism().prepareDeleteObject();
}
/**
* INTERNAL:
* Set the properties needed to be cascaded into the custom query.
*/
protected void prepareCustomQuery(DatabaseQuery customQuery) {
DeleteObjectQuery customDeleteQuery = (DeleteObjectQuery)customQuery;
customDeleteQuery.setObject(getObject());
customDeleteQuery.setObjectChangeSet(getObjectChangeSet());
customDeleteQuery.setCascadePolicy(getCascadePolicy());
customDeleteQuery.setShouldMaintainCache(shouldMaintainCache());
customDeleteQuery.setTranslationRow(customDeleteQuery.getDescriptor().getObjectBuilder().buildRow(getObject(), customDeleteQuery.getSession()));
}
/**
* INTERNAL:
* Prepare the receiver for execution in a session. In particular,
* verify that the object is not null and contains a valid primary key.
*/
public void prepareForExecution() throws QueryException {
super.prepareForExecution();
// Set the tranlation row
if ((getTranslationRow() == null) || (getTranslationRow().isEmpty())) {
setTranslationRow(getDescriptor().getObjectBuilder().buildRowForTranslation(getObject(), getSession()));
}
// Add the write lock field if required
if (getDescriptor().usesOptimisticLocking()) {
getDescriptor().getOptimisticLockingPolicy().addLockValuesToTranslationRow(this);
}
}
}