KickJava   Java API By Example, From Geeks To Geeks.

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


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, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.queryframework;
23
24 import oracle.toplink.essentials.exceptions.*;
25 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl;
26
27 /**
28  * <p><b>Purpose</b>:
29  * Used for inserting or updating objects
30  * WriteObjectQuery determines whether to perform a insert or an update on the database.
31  *
32  * <p><b>Responsibilities</b>:
33  * <ul>
34  * <li> Determines whether to perform a insert or an update on the database.
35  * <li> Stores object in identity map for insert if required.
36  * </ul>
37  *
38  * @author Yvon Lavoie
39  * @since TOPLink/Java 1.0
40  */

41 public class WriteObjectQuery extends ObjectLevelModifyQuery {
42     public WriteObjectQuery() {
43         super();
44     }
45
46     public WriteObjectQuery(Object JavaDoc objectToWrite) {
47         this();
48         setObject(objectToWrite);
49     }
50
51     public WriteObjectQuery(Call call) {
52         this();
53         setCall(call);
54     }
55
56     /**
57      * INTERNAL:
58      * Perform a does exist check to decide whether to perform an insert or update and
59      * delegate the work to the mechanism. Does exists check will also perform an
60      * optimistic lock check if required.
61      * @exception DatabaseException - an error has occurred on the database
62      * @exception OptimisticLockException - an error has occurred using the optimistic lock feature
63      * @return object - the object being written.
64      */

65     public Object JavaDoc executeDatabaseQuery() throws DatabaseException, OptimisticLockException {
66         if (getObjectChangeSet() != null) {
67             return getQueryMechanism().executeWriteWithChangeSet();
68         } else {
69             return getQueryMechanism().executeWrite();
70         }
71     }
72
73     /**
74      * INTERNAL:
75      * Perform a does exist check to decide whether to perform an insert or update and
76      * delegate the work to the mechanism.
77      */

78     public void executeCommit() throws DatabaseException, OptimisticLockException {
79         boolean doesExist;
80
81         if (getSession().isUnitOfWork()) {
82             doesExist = !((UnitOfWorkImpl)getSession()).isCloneNewObject(getObject());
83             if (doesExist) {
84                 doesExist = ((UnitOfWorkImpl)getSession()).isObjectRegistered(getObject());
85             }
86         } else {
87             //Initialize does exist query
88
DoesExistQuery existQuery = (DoesExistQuery)getDescriptor().getQueryManager().getDoesExistQuery().clone();
89             existQuery.setObject(getObject());
90             existQuery.setPrimaryKey(getPrimaryKey());
91             existQuery.setDescriptor(getDescriptor());
92             existQuery.setTranslationRow(getTranslationRow());
93
94             doesExist = ((Boolean JavaDoc)getSession().executeQuery(existQuery)).booleanValue();
95         }
96
97         // Do insert of update
98
if (doesExist) {
99             // Must do an update
100
getQueryMechanism().updateObjectForWrite();
101         } else {
102             // Must do an insert
103
getQueryMechanism().insertObjectForWrite();
104         }
105     }
106
107     /**
108      * INTERNAL:
109      * Perform a does exist check to decide whether to perform an insert or update and
110      * delegate the work to the mechanism.
111      */

112     public void executeCommitWithChangeSet() throws DatabaseException, OptimisticLockException {
113         // Do insert of update
114
if (!getObjectChangeSet().isNew()) {
115             // Must do an update
116
if (!getSession().getCommitManager().isCommitInPreModify(objectChangeSet)) {
117                 //If the changeSet is in the PreModify then it is in the process of being written
118
getQueryMechanism().updateObjectForWriteWithChangeSet();
119             }
120         } else {
121             // check whether the object is already being committed -
122
// if it is and it is new, then a shallow insert must be done
123
if (getSession().getCommitManager().isCommitInPreModify(objectChangeSet)) {
124                 // a shallow insert must be performed
125
this.dontCascadeParts();
126                 getQueryMechanism().insertObjectForWriteWithChangeSet();
127                 getSession().getCommitManager().markShallowCommit(object);
128             } else {
129                 // Must do an insert
130
getQueryMechanism().insertObjectForWriteWithChangeSet();
131             }
132         }
133     }
134
135     /**
136      * PUBLIC:
137      * Return if this is a write object query.
138      */

139     public boolean isWriteObjectQuery() {
140         return true;
141     }
142
143     /**
144      * INTERNAL:
145      * Prepare the receiver for execution in a session.
146      */

147     public void prepareForExecution() throws QueryException {
148         super.prepareForExecution();
149
150         // Set the tranlation row, it may already be set in the custom query situation.
151
if ((getTranslationRow() == null) || (getTranslationRow().isEmpty())) {
152             setTranslationRow(getDescriptor().getObjectBuilder().buildRowForTranslation(getObject(), getSession()));
153         }
154     }
155 }
156
Popular Tags