KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.math.*;
26 import oracle.toplink.essentials.internal.sessions.AbstractSession;
27 import oracle.toplink.essentials.internal.databaseaccess.Accessor;
28 import oracle.toplink.essentials.exceptions.ValidationException;
29 import oracle.toplink.essentials.exceptions.DatabaseException;
30
31 /**
32  * <p>
33  * <b>Purpose</b>: An abstract class providing default sequence behavior.
34  * <p>
35  */

36 public abstract class StandardSequence extends Sequence {
37     public StandardSequence() {
38         super();
39     }
40
41     public StandardSequence(String JavaDoc name) {
42         super(name);
43     }
44
45     public StandardSequence(String JavaDoc name, int size) {
46         super(name, size);
47     }
48
49     public StandardSequence(String JavaDoc name, int size, int initialValue) {
50         super(name, size, initialValue);
51     }
52     
53     public void onConnect() {
54         // does nothing
55
}
56
57     public void onDisconnect() {
58         // does nothing
59
}
60
61     protected abstract Number JavaDoc updateAndSelectSequence(Accessor accessor, AbstractSession writeSession, String JavaDoc seqName, int size);
62
63     public abstract boolean shouldAcquireValueAfterInsert();
64
65     public abstract boolean shouldUseTransaction();
66
67     public Object JavaDoc getGeneratedValue(Accessor accessor, AbstractSession writeSession, String JavaDoc seqName) {
68         if (shouldUsePreallocation()) {
69             return null;
70         } else {
71             Number JavaDoc value = updateAndSelectSequence(accessor, writeSession, seqName, 1);
72             if (value == null) {
73                 throw DatabaseException.errorPreallocatingSequenceNumbers();
74             }
75             return value;
76         }
77     }
78
79     public Vector JavaDoc getGeneratedVector(Accessor accessor, AbstractSession writeSession, String JavaDoc seqName, int size) {
80         if (shouldUsePreallocation()) {
81             Number JavaDoc value = updateAndSelectSequence(accessor, writeSession, seqName, size);
82             if (value == null) {
83                 throw DatabaseException.errorPreallocatingSequenceNumbers();
84             }
85             return createVector(value, seqName, size);
86         } else {
87             return null;
88         }
89     }
90
91     /**
92     * INTERNAL:
93     * Indicates whether existing attribute value should be overridden.
94     * This method is called in case an attribute mapped to PK of sequencing-using
95     * descriptor contains non-null value.
96     * Default implementation is: Always override for "after insert" sequencing,
97     * override non-positive Numbers for "before insert" sequencing.
98     * @param seqName String is sequencing number field name
99     * @param existingValue Object is a non-null value of PK-mapped attribute.
100     */

101     public boolean shouldOverrideExistingValue(String JavaDoc seqName, Object JavaDoc existingValue) {
102         if (shouldAcquireValueAfterInsert()) {
103             return true;
104         } else {
105             // Check if the stored number is greater than zero (a valid sequence number)
106
if (existingValue instanceof BigDecimal) {
107                 if (((BigDecimal)existingValue).signum() <= 0) {
108                     return true;
109                 }
110             } else if (existingValue instanceof BigInteger) {
111                 if (((BigInteger)existingValue).signum() <= 0) {
112                     return true;
113                 }
114             }
115             //CR#2645: Fix for String ClassCastException. Now we don't assume
116
//anything which is not a BigDecimal to be a Number.
117
else if (existingValue instanceof Number JavaDoc && (((Number JavaDoc)existingValue).longValue() <= 0)) {
118                 return true;
119             }
120
121             return false;
122         }
123     }
124
125     /**
126     * INTERNAL:
127     * given sequence = 10, size = 5 will create Vector (6,7,8,9,10)
128     * @param seqName String is sequencing number field name
129     * @param existingValue Object is a non-null value of PK-mapped attribute.
130     * @param size int size of Vector to create.
131     */

132     protected Vector JavaDoc createVector(Number JavaDoc sequence, String JavaDoc seqName, int size) {
133         BigDecimal nextSequence;
134         BigDecimal increment = new BigDecimal(1);
135
136         if (sequence instanceof BigDecimal) {
137             nextSequence = (BigDecimal)sequence;
138         } else {
139             nextSequence = new BigDecimal(sequence.doubleValue());
140         }
141
142         Vector JavaDoc sequencesForName = new Vector JavaDoc(size);
143
144         nextSequence = nextSequence.subtract(new BigDecimal(size));
145
146         // Check for incorrect values return to validate that the sequence is setup correctly.
147
// PRS 36451 intvalue would wrap
148
if (nextSequence.doubleValue() < -1) {
149             throw ValidationException.sequenceSetupIncorrectly(seqName);
150         }
151
152         for (int index = size; index > 0; index--) {
153             nextSequence = nextSequence.add(increment);
154             sequencesForName.addElement(nextSequence);
155         }
156         return sequencesForName;
157     }
158 }
159
Popular Tags