001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.queryframework;
038:
039: import oracle.toplink.essentials.exceptions.*;
040: import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl;
041:
042: /**
043: * <p><b>Purpose</b>:
044: * Used for inserting or updating objects
045: * WriteObjectQuery determines whether to perform a insert or an update on the database.
046: *
047: * <p><b>Responsibilities</b>:
048: * <ul>
049: * <li> Determines whether to perform a insert or an update on the database.
050: * <li> Stores object in identity map for insert if required.
051: * </ul>
052: *
053: * @author Yvon Lavoie
054: * @since TOPLink/Java 1.0
055: */
056: public class WriteObjectQuery extends ObjectLevelModifyQuery {
057: public WriteObjectQuery() {
058: super ();
059: }
060:
061: public WriteObjectQuery(Object objectToWrite) {
062: this ();
063: setObject(objectToWrite);
064: }
065:
066: public WriteObjectQuery(Call call) {
067: this ();
068: setCall(call);
069: }
070:
071: /**
072: * INTERNAL:
073: * Return if the object exists on the database or not.
074: * This first checks existence in the chache, then on the database.
075: */
076: protected boolean doesObjectExist() {
077: boolean doesExist;
078:
079: if (getSession().isUnitOfWork()) {
080: doesExist = !((UnitOfWorkImpl) getSession())
081: .isCloneNewObject(getObject());
082: if (doesExist) {
083: doesExist = ((UnitOfWorkImpl) getSession())
084: .isObjectRegistered(getObject());
085: }
086: } else {
087: //Initialize does exist query
088: DoesExistQuery existQuery = (DoesExistQuery) getDescriptor()
089: .getQueryManager().getDoesExistQuery().clone();
090: existQuery.setObject(getObject());
091: existQuery.setPrimaryKey(getPrimaryKey());
092: existQuery.setDescriptor(getDescriptor());
093: existQuery.setTranslationRow(getTranslationRow());
094:
095: doesExist = ((Boolean) getSession()
096: .executeQuery(existQuery)).booleanValue();
097: }
098:
099: return doesExist;
100: }
101:
102: /**
103: * INTERNAL:
104: * Perform a does exist check to decide whether to perform an insert or update and
105: * delegate the work to the mechanism. Does exists check will also perform an
106: * optimistic lock check if required.
107: * @exception DatabaseException - an error has occurred on the database
108: * @exception OptimisticLockException - an error has occurred using the optimistic lock feature
109: * @return object - the object being written.
110: */
111: public Object executeDatabaseQuery() throws DatabaseException,
112: OptimisticLockException {
113: if (getObjectChangeSet() != null) {
114: return getQueryMechanism().executeWriteWithChangeSet();
115: } else {
116: return getQueryMechanism().executeWrite();
117: }
118: }
119:
120: /**
121: * INTERNAL:
122: * Decide whether to perform an insert, update or delete and
123: * delegate the work to the mechanism.
124: */
125: public void executeCommit() throws DatabaseException,
126: OptimisticLockException {
127: boolean doesExist = doesObjectExist();
128: boolean shouldBeDeleted = shouldObjectBeDeleted();
129:
130: // Do insert, update or delete
131: if (doesExist) {
132: if (shouldBeDeleted) {
133: // Must do a delete
134: getQueryMechanism().deleteObjectForWrite();
135: } else {
136: // Must do an update
137: getQueryMechanism().updateObjectForWrite();
138: }
139: } else if (!shouldBeDeleted) {
140: // Must do an insert
141: getQueryMechanism().insertObjectForWrite();
142: }
143: }
144:
145: /**
146: * INTERNAL:
147: * Perform a does exist check to decide whether to perform an insert or update and
148: * delegate the work to the mechanism.
149: */
150: public void executeCommitWithChangeSet() throws DatabaseException,
151: OptimisticLockException {
152: // Do insert of update
153: if (!getObjectChangeSet().isNew()) {
154: // Must do an update
155: if (!getSession().getCommitManager().isCommitInPreModify(
156: objectChangeSet)) {
157: //If the changeSet is in the PreModify then it is in the process of being written
158: getQueryMechanism().updateObjectForWriteWithChangeSet();
159: }
160: } else {
161: // check whether the object is already being committed -
162: // if it is and it is new, then a shallow insert must be done
163: if (getSession().getCommitManager().isCommitInPreModify(
164: objectChangeSet)) {
165: // a shallow insert must be performed
166: this .dontCascadeParts();
167: getQueryMechanism().insertObjectForWriteWithChangeSet();
168: getSession().getCommitManager().markShallowCommit(
169: object);
170: } else {
171: // Must do an insert
172: getQueryMechanism().insertObjectForWriteWithChangeSet();
173: }
174: }
175: }
176:
177: /**
178: * INTERNAL:
179: * Perform a shallow write. The decision, which shallow action should be
180: * executed is based on the existence of the associated object. If
181: * the object exists, perform a shallow delete. Do a shallow
182: * insert otherwise.
183: * Note that there currently is *no* shallow update operation.
184: * If shallow updates become necessary, the decision logic must
185: * also perform a delete check as in {@link this.executeCommit}.
186: */
187: public void executeShallowWrite() {
188: boolean doesExist = doesObjectExist();
189:
190: // Shallow writes only occur for inserts or deletes
191: if (doesExist) {
192: getQueryMechanism().shallowDeleteObjectForWrite(
193: getObject(), this , getSession().getCommitManager());
194: } else {
195: getQueryMechanism().shallowInsertObjectForWrite(
196: getObject(), this , getSession().getCommitManager());
197: }
198: }
199:
200: /**
201: * PUBLIC:
202: * Return if this is a write object query.
203: */
204: public boolean isWriteObjectQuery() {
205: return true;
206: }
207:
208: /**
209: * INTERNAL:
210: * Prepare the receiver for execution in a session.
211: */
212: public void prepareForExecution() throws QueryException {
213: super .prepareForExecution();
214:
215: // Set the tranlation row, it may already be set in the custom query situation.
216: if ((getTranslationRow() == null)
217: || (getTranslationRow().isEmpty())) {
218: setTranslationRow(getDescriptor().getObjectBuilder()
219: .buildRowForTranslation(getObject(), getSession()));
220: }
221: }
222:
223: /**
224: * INTERNAL:
225: * Return whether a dependent object should be deleted from the database or not.
226: * Dependent objects should not be removed if not already scheduled for removal in a UoW.
227: * Returns "true" outside a UoW. Used by relationship mappings when cascading a delete operation.
228: */
229: public boolean shouldDependentObjectBeDeleted(Object object) {
230: boolean shouldBeDeleted;
231:
232: if (getSession().isUnitOfWork()) {
233: shouldBeDeleted = ((UnitOfWorkImpl) getSession())
234: .isObjectDeleted(object);
235: } else {
236: // Deletes are cascaded outside a UoW
237: shouldBeDeleted = true;
238: }
239:
240: return shouldBeDeleted;
241: }
242:
243: /**
244: * INTERNAL:
245: * Return if the attached object should be deleted from the database or not.
246: * This information is available only, if the session is a UoW. Returns "false" outside a UoW.
247: * In this case an existence check should be performed and either an insert or update executed.
248: * Only used internally.
249: */
250: protected boolean shouldObjectBeDeleted() {
251: boolean shouldBeDeleted;
252:
253: if (getSession().isUnitOfWork()) {
254: shouldBeDeleted = ((UnitOfWorkImpl) getSession())
255: .isObjectDeleted(getObject());
256: } else {
257: // Deletes must be explicitly user defined outside a UoW
258: shouldBeDeleted = false;
259: }
260:
261: return shouldBeDeleted;
262: }
263: }
|