KickJava   Java API By Example, From Geeks To Geeks.

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


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.sequencing;
23
24 import java.util.Vector JavaDoc;
25 import oracle.toplink.essentials.internal.sessions.AbstractSession;
26 import oracle.toplink.essentials.internal.databaseaccess.Accessor;
27
28 /**
29  * <p>
30  * <b>Purpose</b>: Reference to the default sequence
31  * <p>
32  */

33 public class DefaultSequence extends Sequence {
34     protected Sequence defaultSequence;
35
36     public DefaultSequence() {
37         super();
38     }
39
40     // Created with this constructor,
41
// DefaultSequence uses preallocation size of defaultSequence.
42
public DefaultSequence(String JavaDoc name) {
43         super(name, 0);
44     }
45
46     public DefaultSequence(String JavaDoc name, int size) {
47         super(name, size);
48     }
49
50     public DefaultSequence(String JavaDoc name, int size, int initialValue) {
51         super(name, size, initialValue);
52     }
53     
54     public Sequence getDefaultSequence() {
55         return getDatasourcePlatform().getDefaultSequence();
56     }
57
58     public boolean hasPreallocationSize() {
59         return size != 0;
60     }
61
62     public int getPreallocationSize() {
63         if ((size != 0) || (getDefaultSequence() == null)) {
64             return size;
65         } else {
66             return getDefaultSequence().getPreallocationSize();
67         }
68     }
69
70     public int getInitialValue() {
71         if ((initialValue != 0) || (getDefaultSequence() == null)) {
72             return initialValue;
73         } else {
74             return getDefaultSequence().getInitialValue();
75         }
76     }
77
78     public boolean equals(Object JavaDoc obj) {
79         if (obj instanceof DefaultSequence) {
80             return equalNameAndSize(this, (DefaultSequence)obj);
81         } else {
82             return false;
83         }
84     }
85
86     /**
87     * INTERNAL:
88     * Indicates whether sequencing value should be acquired after INSERT.
89     * Note that preallocation could be used only in case sequencing values
90     * should be acquired before insert (this method returns false).
91     * In default implementation, it is true for table sequencing and native
92     * sequencing on Oracle platform, false for native sequencing on other platforms.
93     */

94     public boolean shouldAcquireValueAfterInsert() {
95         return getDefaultSequence().shouldAcquireValueAfterInsert();
96     }
97
98     /**
99     * INTERNAL:
100     * Indicates whether TopLink should internally call beginTransaction() before
101     * getGeneratedValue/Vector, and commitTransaction after.
102     * In default implementation, it is true for table sequencing and
103     * false for native sequencing.
104     */

105     public boolean shouldUseTransaction() {
106         return getDefaultSequence().shouldUseTransaction();
107     }
108
109     /**
110     * INTERNAL:
111     * Indicates whether existing attribute value should be overridden.
112     * This method is called in case an attribute mapped to PK of sequencing-using
113     * descriptor contains non-null value.
114     * @param seqName String is sequencing number field name
115     * @param existingValue Object is a non-null value of PK-mapped attribute.
116     */

117     public boolean shouldOverrideExistingValue(String JavaDoc seqName, Object JavaDoc existingValue) {
118         return getDefaultSequence().shouldOverrideExistingValue(seqName, existingValue);
119     }
120
121     /**
122     * INTERNAL:
123     * Return the newly-generated sequencing value.
124     * Used only in case preallocation is not used (shouldUsePreallocation()==false).
125     * Accessor may be non-null only in case shouldUseSeparateConnection()==true.
126     * Even in this case accessor could be null - if SequencingControl().shouldUseSeparateConnection()==false;
127     * Therefore in case shouldUseSeparateConnection()==true, implementation should handle
128     * both cases: use a separate connection if provided (accessor != null), or get by
129     * without it (accessor == null).
130     * @param accessor Accessor is a separate sequencing accessor (may be null);
131     * @param writeSession Session is a Session used for writing (either ClientSession or DatabaseSession);
132     * @param seqName String is sequencing number field name
133     */

134     public Object JavaDoc getGeneratedValue(Accessor accessor, AbstractSession writeSession, String JavaDoc seqName) {
135         return getDefaultSequence().getGeneratedValue(accessor, writeSession, seqName);
136     }
137
138     /**
139     * INTERNAL:
140     * Return a Vector of newly-generated sequencing values.
141     * Used only in case preallocation is used (shouldUsePreallocation()==true).
142     * Accessor may be non-null only in case shouldUseSeparateConnection()==true.
143     * Even in this case accessor could be null - if SequencingControl().shouldUseSeparateConnection()==false;
144     * Therefore in case shouldUseSeparateConnection()==true, implementation should handle
145     * both cases: use a separate connection if provided (accessor != null), or get by
146     * without it (accessor == null).
147     * @param accessor Accessor is a separate sequencing accessor (may be null);
148     * @param writeSession Session is a Session used for writing (either ClientSession or DatabaseSession);
149     * @param seqName String is sequencing number field name
150     * @param size int number of values to preallocate (output Vector size).
151     */

152     public Vector JavaDoc getGeneratedVector(Accessor accessor, AbstractSession writeSession, String JavaDoc name, int size) {
153         return getDefaultSequence().getGeneratedVector(accessor, writeSession, name, size);
154     }
155
156     /**
157     * INTERNAL:
158     * This method is called when Sequencing object is created.
159     * It's a chance to do initialization.
160     * @param ownerSession DatabaseSession
161     */

162     protected void onConnect() {
163         // nothing to do
164
}
165
166     /**
167     * INTERNAL:
168     * This method is called when Sequencing object is destroyed..
169     * It's a chance to do deinitialization.
170     */

171     public void onDisconnect() {
172         // nothing to do
173
}
174 }
175
Popular Tags