KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > queryframework > DeleteObjectQuery


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
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 /**
33  * <p><b>Purpose</b>: Used for deleting objects.
34  * <p>
35  * <p><b>Responsibilities</b>:
36  * Extract primary key from object and delete it.
37  *
38  * @author Yvon Lavoie
39  * @since TOPLink/Java 1.0
40  */

41 public class DeleteObjectQuery extends ObjectLevelModifyQuery {
42     public DeleteObjectQuery() {
43         super();
44     }
45
46     public DeleteObjectQuery(Object JavaDoc objectToDelete) {
47         this();
48         setObject(objectToDelete);
49     }
50
51     public DeleteObjectQuery(Call call) {
52         this();
53         setCall(call);
54     }
55
56     /**
57      * INTERNAL:
58      * Check to see if a custom query should be used for this query.
59      * This is done before the query is copied and prepared/executed.
60      * null means there is none.
61      */

62     protected DatabaseQuery checkForCustomQuery(AbstractSession session, AbstractRecord translationRow) {
63         checkDescriptor(session);
64
65         // check if user defined a custom query
66
DescriptorQueryManager queryManager = getDescriptor().getQueryManager();
67         if ((!isCallQuery())// this is not a hand-coded (custom SQL, SDK etc.) call
68
&&(!isUserDefined())// and this is not a user-defined query (in the query manager)
69
&&queryManager.hasDeleteQuery()) {// and there is a user-defined query (in the query manager)
70
return queryManager.getDeleteQuery();
71         }
72
73         return null;
74     }
75
76     /**
77      * INTERNAL:
78      * Code was moved from UnitOfWork.internalExecuteQuery
79      *
80      * @param unitOfWork
81      * @param translationRow
82      * @return
83      * @throws oracle.toplink.essentials.exceptions.DatabaseException
84      * @throws oracle.toplink.essentials.exceptions.OptimisticLockException
85      */

86     protected Object JavaDoc executeInUnitOfWorkObjectLevelModifyQuery(UnitOfWorkImpl unitOfWork, AbstractRecord translationRow) throws DatabaseException, OptimisticLockException {
87         Object JavaDoc result = unitOfWork.processDeleteObjectQuery(this);
88         if (result != null) {
89             // if the above method returned something then the unit of work
90
//was not writing so the object has been stored to delete later
91
//so return the object. See the above method for the cases
92
//where this object will be returned.
93
return result;
94         }
95         return super.executeInUnitOfWorkObjectLevelModifyQuery(unitOfWork, translationRow);
96     }
97
98     /**
99      * INTERNAL:
100      * Perform the work to delete an object.
101      * @return object - the object being deleted.
102      */

103     public Object JavaDoc executeDatabaseQuery() throws DatabaseException, OptimisticLockException {
104         try {
105             // Check if the object has already been commited, then no work is required
106
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             // PERF: Avoid events if no listeners.
114
if (getDescriptor().getEventManager().hasAnyEventListeners()) {
115                 // Need to run pre-delete selector if available
116
getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PreDeleteEvent, this));
117             }
118
119             // Verify if deep shallow modify is turned on
120
if (shouldCascadeParts()) {
121                 getDescriptor().getQueryManager().preDelete(this);
122             }
123
124             // CR#2660080 missing aboutToDelete event.
125
// PERF: Avoid events if no listeners.
126
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             // Verify if deep shallow modify is turned on
144
if (shouldCascadeParts()) {
145                 getDescriptor().getQueryManager().postDelete(this);
146             }
147
148             // PERF: Avoid events if no listeners.
149
if (getDescriptor().getEventManager().hasAnyEventListeners()) {
150                 // Need to run post-delete selector if available
151
getDescriptor().getEventManager().executeEvent(new DescriptorEvent(DescriptorEventManager.PostDeleteEvent, this));
152             }
153
154             getSession().commitTransaction();
155             getSession().getCommitManager().markCommitCompleted(getObject());
156
157             // CR3510313, avoid removing aggregate collections from cache (maintain cache is false).
158
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 JavaDoc exception) {
168             getSession().rollbackTransaction();
169             getSession().getCommitManager().markCommitCompleted(getObject());
170             throw exception;
171         }
172     }
173
174     /**
175      * PUBLIC:
176      * Return if this is a delete object query.
177      */

178     public boolean isDeleteObjectQuery() {
179         return true;
180     }
181
182     /**
183      * INTERNAL:
184      * Prepare the receiver for execution in a session.
185      */

186     protected void prepare() {
187         super.prepare();
188
189         getQueryMechanism().prepareDeleteObject();
190     }
191
192     /**
193      * INTERNAL:
194      * Set the properties needed to be cascaded into the custom query.
195      */

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     /**
206      * INTERNAL:
207      * Prepare the receiver for execution in a session. In particular,
208      * verify that the object is not null and contains a valid primary key.
209      */

210     public void prepareForExecution() throws QueryException {
211         super.prepareForExecution();
212
213         // Set the tranlation row
214
if ((getTranslationRow() == null) || (getTranslationRow().isEmpty())) {
215             setTranslationRow(getDescriptor().getObjectBuilder().buildRowForTranslation(getObject(), getSession()));
216         }
217
218         // Add the write lock field if required
219
if (getDescriptor().usesOptimisticLocking()) {
220             getDescriptor().getOptimisticLockingPolicy().addLockValuesToTranslationRow(this);
221         }
222     }
223 }
224
Popular Tags