KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > sequencing > QuerySequence


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.sequencing;
23
24 import java.util.Vector JavaDoc;
25 import java.math.BigDecimal JavaDoc;
26 import oracle.toplink.essentials.sessions.Record;
27 import oracle.toplink.essentials.queryframework.*;
28 import oracle.toplink.essentials.internal.databaseaccess.Accessor;
29 import oracle.toplink.essentials.internal.sessions.AbstractSession;
30
31 /**
32  * <p>
33  * <b>Purpose</b>: An abstract class adding queries to StandardSequence
34  * <p>
35  */

36 public class QuerySequence extends StandardSequence {
37     protected ValueReadQuery selectQuery;
38     protected DataModifyQuery updateQuery;
39     protected boolean shouldAcquireValueAfterInsert;
40     protected boolean shouldUseTransaction;
41     protected boolean shouldSkipUpdate;
42     protected boolean shouldSelectBeforeUpdate;
43     protected boolean wasSelectQueryCreated;
44     protected boolean wasUpdateQueryCreated;
45
46     public QuerySequence() {
47         super();
48     }
49
50     public QuerySequence(String JavaDoc name) {
51         super(name);
52     }
53
54     public QuerySequence(String JavaDoc name, int size) {
55         super(name, size);
56     }
57
58     public QuerySequence(String JavaDoc name, int size, int initialValue) {
59         super(name, size, initialValue);
60     }
61     
62     public QuerySequence(boolean shouldAcquireValueAfterInsert, boolean shouldUseTransaction) {
63         super();
64         setShouldAcquireValueAfterInsert(shouldAcquireValueAfterInsert);
65         setShouldUseTransaction(shouldUseTransaction);
66     }
67
68     public QuerySequence(String JavaDoc name, boolean shouldAcquireValueAfterInsert, boolean shouldUseTransaction) {
69         super(name);
70         setShouldAcquireValueAfterInsert(shouldAcquireValueAfterInsert);
71         setShouldUseTransaction(shouldUseTransaction);
72     }
73
74     public QuerySequence(String JavaDoc name, int size, boolean shouldAcquireValueAfterInsert, boolean shouldUseTransaction) {
75         super(name, size);
76         setShouldAcquireValueAfterInsert(shouldAcquireValueAfterInsert);
77         setShouldUseTransaction(shouldUseTransaction);
78     }
79     
80     public QuerySequence(String JavaDoc name, int size, int initialValue,
81             boolean shouldAcquireValueAfterInsert, boolean shouldUseTransaction) {
82         super(name, size, initialValue);
83         setShouldAcquireValueAfterInsert(shouldAcquireValueAfterInsert);
84         setShouldUseTransaction(shouldUseTransaction);
85     }
86
87     public boolean equals(Object JavaDoc obj) {
88         if (obj instanceof QuerySequence && super.equals(obj)) {
89             QuerySequence other = (QuerySequence)obj;
90             return (getSelectQuery() == other.getSelectQuery()) && (getUpdateQuery() == other.getUpdateQuery()) && (shouldAcquireValueAfterInsert() == other.shouldAcquireValueAfterInsert()) && (shouldUseTransaction() == other.shouldUseTransaction()) && (shouldSkipUpdate() == other.shouldSkipUpdate()) && (shouldSelectBeforeUpdate() == other.shouldSelectBeforeUpdate());
91
92         } else {
93             return false;
94         }
95     }
96
97     /**
98     * PUBLIC:
99     */

100     public boolean shouldAcquireValueAfterInsert() {
101         return shouldAcquireValueAfterInsert;
102     }
103
104     /**
105     * PUBLIC:
106     */

107     public void setShouldAcquireValueAfterInsert(boolean shouldAcquireValueAfterInsert) {
108         this.shouldAcquireValueAfterInsert = shouldAcquireValueAfterInsert;
109     }
110
111     /**
112     * PUBLIC:
113     */

114     public boolean shouldUseTransaction() {
115         return shouldUseTransaction;
116     }
117
118     /**
119     * PUBLIC:
120     */

121     public void setShouldUseTransaction(boolean shouldUseTransaction) {
122         this.shouldUseTransaction = shouldUseTransaction;
123     }
124
125     /**
126     * PUBLIC:
127     */

128     public void setSelectQuery(ValueReadQuery query) {
129         selectQuery = query;
130     }
131
132     /**
133     * PUBLIC:
134     */

135     public ValueReadQuery getSelectQuery() {
136         return selectQuery;
137     }
138
139     /**
140     * PUBLIC:
141     */

142     public void setUpdateQuery(DataModifyQuery query) {
143         updateQuery = query;
144     }
145
146     /**
147     * PUBLIC:
148     */

149     public DataModifyQuery getUpdateQuery() {
150         return updateQuery;
151     }
152
153     /**
154     * PUBLIC:
155     */

156     public void setShouldSkipUpdate(boolean shouldSkipUpdate) {
157         this.shouldSkipUpdate = shouldSkipUpdate;
158     }
159
160     /**
161     * PUBLIC:
162     */

163     public boolean shouldSkipUpdate() {
164         return shouldSkipUpdate;
165     }
166
167     /**
168     * PUBLIC:
169     */

170     public void setShouldSelectBeforeUpdate(boolean shouldSelectBeforeUpdate) {
171         this.shouldSelectBeforeUpdate = shouldSelectBeforeUpdate;
172     }
173
174     /**
175     * PUBLIC:
176     */

177     public boolean shouldSelectBeforeUpdate() {
178         return shouldSelectBeforeUpdate;
179     }
180
181     /**
182     * INTERNAL:
183     */

184     protected ValueReadQuery buildSelectQuery() {
185         return null;
186     }
187
188     /**
189     * INTERNAL:
190     */

191     protected DataModifyQuery buildUpdateQuery() {
192         return null;
193     }
194
195     /**
196     * INTERNAL:
197     */

198     protected ValueReadQuery buildSelectQuery(String JavaDoc seqName, Integer JavaDoc size) {
199         return null;
200     }
201
202     /**
203     * INTERNAL:
204     */

205     protected DataModifyQuery buildUpdateQuery(String JavaDoc seqName, Number JavaDoc sizeOrNewValue) {
206         return null;
207     }
208
209     /**
210     * INTERNAL:
211     */

212     public void onConnect() {
213         super.onConnect();
214         if (getSelectQuery() == null) {
215             setSelectQuery(buildSelectQuery());
216             wasSelectQueryCreated = getSelectQuery() != null;
217         }
218         if ((getUpdateQuery() == null) && !shouldSkipUpdate()) {
219             setUpdateQuery(buildUpdateQuery());
220             wasUpdateQueryCreated = getUpdateQuery() != null;
221         }
222     }
223
224     /**
225     * INTERNAL:
226     */

227     public void onDisconnect() {
228         if (wasSelectQueryCreated) {
229             setSelectQuery(null);
230             wasSelectQueryCreated = false;
231         }
232         if (wasUpdateQueryCreated) {
233             setUpdateQuery(null);
234             wasUpdateQueryCreated = false;
235         }
236         super.onDisconnect();
237     }
238
239     /**
240     * INTERNAL:
241     */

242     protected Number JavaDoc updateAndSelectSequence(Accessor accessor, AbstractSession writeSession, String JavaDoc seqName, int size) {
243         Integer JavaDoc sizeInteger = new Integer JavaDoc(size);
244         if (shouldSkipUpdate()) {
245             return (Number JavaDoc)select(accessor, writeSession, seqName, sizeInteger);
246         } else {
247             if (shouldSelectBeforeUpdate()) {
248                 Object JavaDoc result = select(accessor, writeSession, seqName, sizeInteger);
249                 BigDecimal JavaDoc currentValue;
250                 if (result instanceof Number JavaDoc) {
251                     currentValue = new BigDecimal JavaDoc(((Number JavaDoc)result).longValue());
252                 } else if (result instanceof String JavaDoc) {
253                     currentValue = new BigDecimal JavaDoc((String JavaDoc)result);
254                 } else if (result instanceof Record) {
255                     Object JavaDoc val = ((Record)result).get("text()");
256                     currentValue = new BigDecimal JavaDoc((String JavaDoc)val);
257                 } else {
258                     // DatabaseException.errorPreallocatingSequenceNumbers() is thrown by the superclass
259
return null;
260                 }
261
262                 // Increment value
263
BigDecimal JavaDoc newValue = currentValue.add(new BigDecimal JavaDoc(size));
264
265                 update(accessor, writeSession, seqName, newValue);
266                 return newValue;
267             } else {
268                 update(accessor, writeSession, seqName, sizeInteger);
269                 return (Number JavaDoc)select(accessor, writeSession, seqName, sizeInteger);
270             }
271         }
272     }
273
274     /**
275     * INTERNAL:
276     */

277     protected Object JavaDoc select(Accessor accessor, AbstractSession writeSession, String JavaDoc seqName, Integer JavaDoc size) {
278         ValueReadQuery query = getSelectQuery();
279         if (query != null) {
280             if (accessor != null) {
281                 // PERF: Prepare the query before being cloned.
282
// Also BUG: SQLCall could not be prepared concurrently by different queries.
283
// Setting user define allow custom SQL query to be prepared without translation row.
284
query.setIsUserDefined(true);
285                 query.checkPrepare(writeSession, null);
286                 query = (ValueReadQuery)query.clone();
287                 query.setAccessor(accessor);
288             }
289         } else {
290             query = buildSelectQuery(seqName, size);
291             if (accessor != null) {
292                 query.setAccessor(accessor);
293             }
294         }
295         Vector JavaDoc args = createArguments(query, seqName, size);
296         if (args != null) {
297             return writeSession.executeQuery(query, args);
298         } else {
299             return writeSession.executeQuery(query);
300         }
301     }
302
303     /**
304     * INTERNAL:
305     */

306     protected void update(Accessor accessor, AbstractSession writeSession, String JavaDoc seqName, Number JavaDoc sizeOrNewValue) {
307         DataModifyQuery query = getUpdateQuery();
308         if (query != null) {
309             if (accessor != null) {
310                 // PERF: Prepare the query before being cloned.
311
// Also BUG: SQLCall could not be prepared concurrently by different queries.
312
// Setting user define allow custom SQL query to be prepared without translation row.
313
query.setIsUserDefined(true);
314                 query.checkPrepare(writeSession, null);
315                 query = (DataModifyQuery)query.clone();
316                 query.setAccessor(accessor);
317             }
318         } else {
319             query = buildUpdateQuery(seqName, sizeOrNewValue);
320             if (query == null) {
321                 return;
322             }
323             if (accessor != null) {
324                 query.setAccessor(accessor);
325             }
326         }
327         Vector JavaDoc args = createArguments(query, seqName, sizeOrNewValue);
328         if (args != null) {
329             writeSession.executeQuery(query, args);
330         } else {
331             writeSession.executeQuery(query);
332         }
333     }
334
335     /**
336     * INTERNAL:
337     */

338     protected Vector JavaDoc createArguments(DatabaseQuery query, String JavaDoc seqName, Number JavaDoc sizeOrNewValue) {
339         int nArgs = query.getArguments().size();
340         if (nArgs > 0) {
341             Vector JavaDoc args = new Vector JavaDoc(nArgs);
342             args.addElement(seqName);
343             if (nArgs > 1) {
344                 args.addElement(sizeOrNewValue);
345             }
346             return args;
347         } else {
348             return null;
349         }
350     }
351 }
352
Popular Tags