KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
34  * <p><b>Purpose</b>:
35  * Abstract class for all object modify queries.
36  *
37  * <p><b>Responsibilities</b>:
38  * <ul>
39  * <li> Stores & retrieves the object to modify.
40  * <li> Stores & retrieves the primary key of the objects.
41  * </ul>
42  *
43  * @author Yvon Lavoie
44  * @since TOPLink/Java 1.0
45  */

46 public abstract class ObjectLevelModifyQuery extends ModifyQuery {
47
48     /* Primary key of the object to be modified. */
49     protected Vector primaryKey;
50
51     /* The object being modified. */
52     protected Object JavaDoc object;
53
54     /* A changeSet representing the object being modified */
55     protected ObjectChangeSet objectChangeSet;
56
57     /* The clone of the object being modified from unit of work. */
58     protected Object JavaDoc backupClone;
59
60     /**
61      * PUBLIC:
62      * Initialize the state of the query.
63      */

64     public ObjectLevelModifyQuery() {
65         this.cascadePolicy = CascadePrivateParts;
66     }
67
68     /**
69      * INTERNAL:
70      * Ensure that the descriptor has been set.
71      */

72     public void checkDescriptor(AbstractSession session) throws QueryException {
73         if (getDescriptor() == null) {
74             if (getObject() == null) {
75                 throw QueryException.objectToModifyNotSpecified(this);
76             }
77
78             //Bug#3947714 Pass the object instead of class in case object is proxy
79
ClassDescriptor referenceDescriptor = session.getDescriptor(getObject());
80             if (referenceDescriptor == null) {
81                 throw QueryException.descriptorIsMissing(getObject().getClass(), this);
82             }
83             setDescriptor(referenceDescriptor);
84         }
85     }
86
87     /**
88      * INTERNAL:
89      * All have done is move code from UnitOfWork.internalExecuteQuery
90      */

91     public Object JavaDoc 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     /**
99      * INTERNAL:
100      * This code was moved from UnitOfWork.internalExecuteQuery
101      * @param unitOfWork
102      * @param translationRow
103      * @return
104      * @throws oracle.toplink.essentials.exceptions.DatabaseException
105      * @throws oracle.toplink.essentials.exceptions.OptimisticLockException
106      */

107     protected Object JavaDoc 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         // CR#3216 - Apply check to ObjectLevelModifyQuery not just WriteObjectQuery
117
if (unitOfWork.shouldPerformNoValidation() && unitOfWork.getUnregisteredExistingObjects().containsKey(getObject())) {
118             //if the object is an unregistered existing object then skip it. This
119
// Will only be in the collection if validation is turned off
120
return null;
121         }
122
123         return super.executeInUnitOfWork(unitOfWork, translationRow);
124     }
125
126     /**
127      * INTERNAL:
128      * Return the backup clone of the object from the unit of work.
129      */

130     public Object JavaDoc getBackupClone() {
131         // PERF: A backup clone is only required for the old commit,
132
// So avoid its creation for normal commit.
133
if ((backupClone == null) && getSession().isUnitOfWork()) {
134             setBackupClone(((UnitOfWorkImpl)getSession()).getBackupCloneForCommit(getObject()));
135         }
136         return backupClone;
137     }
138
139     /**
140      * PUBLIC:
141      * Return the object required for modification.
142      */

143     public Object JavaDoc getObject() {
144         return object;
145     }
146
147     /**
148      * PUBLIC:
149      * Return the ObjectChangeSet representing the object being changed
150      */

151     public ObjectChangeSet getObjectChangeSet() {
152         return this.objectChangeSet;
153     }
154
155     /**
156      * INTERNAL:
157      * Get the primary key for the query
158      */

159     public Vector getPrimaryKey() {
160         return primaryKey;
161     }
162
163     /**
164      * Return the domain class associated with this query.
165      */

166     public Class JavaDoc getReferenceClass() {
167         return getObject().getClass();
168     }
169
170     /**
171      * INTERNAL:
172      * Return the reference class for a query
173      * Note: Although the API is designed to avoid classpath dependancies for the MW, since the object
174      * is specified at runtime, this will not be an issue.
175      */

176     public String JavaDoc getReferenceClassName() {
177         return getReferenceClass().getName();
178     }
179
180     /**
181      * PUBLIC:
182      * Return if this is an object level modify query.
183      */

184     public boolean isObjectLevelModifyQuery() {
185         return true;
186     }
187
188     /**
189      * INTERNAL:
190      * Prepare the receiver for execution in a session.
191      * In particular check that the tables on the descriptor are set.
192      */

193     protected void prepare() throws QueryException {
194         checkDescriptor(getSession());
195
196         if (getObject() != null) {// Prepare can be called without the object set yet.
197
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     /**
208      * INTERNAL:
209      * Prepare the receiver for execution in a session.
210      * In particular check that the tables on the descriptor are set.
211      */

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     /**
231      * INTERNAL:
232      * Set the backup clone of the object from the unit of work.
233      */

234     public void setBackupClone(Object JavaDoc backupClone) {
235         this.backupClone = backupClone;
236     }
237
238     /**
239      * PUBLIC (REQUIRED):
240      * Set the object required for modification.
241      */

242     public void setObject(Object JavaDoc object) {
243         this.object = object;
244     }
245
246     /**
247      * INTERNAL:
248      * Set the ObjectChangeSet representing the object to be written
249      */

250     public void setObjectChangeSet(ObjectChangeSet changeSet) {
251         this.objectChangeSet = changeSet;
252     }
253
254     /**
255      * INTERNAL:
256      * Set the primary key for the query.
257      */

258     public void setPrimaryKey(Vector primaryKey) {
259         this.primaryKey = primaryKey;
260     }
261
262     public String JavaDoc toString() {
263         return Helper.getShortClassName(getClass()) + "(" + String.valueOf(getObject()) + ")";
264     }
265 }
266
Popular Tags