1 21 package oracle.toplink.essentials.sequencing; 23 24 import java.util.Vector ; 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 36 public abstract class StandardSequence extends Sequence { 37 public StandardSequence() { 38 super(); 39 } 40 41 public StandardSequence(String name) { 42 super(name); 43 } 44 45 public StandardSequence(String name, int size) { 46 super(name, size); 47 } 48 49 public StandardSequence(String name, int size, int initialValue) { 50 super(name, size, initialValue); 51 } 52 53 public void onConnect() { 54 } 56 57 public void onDisconnect() { 58 } 60 61 protected abstract Number updateAndSelectSequence(Accessor accessor, AbstractSession writeSession, String seqName, int size); 62 63 public abstract boolean shouldAcquireValueAfterInsert(); 64 65 public abstract boolean shouldUseTransaction(); 66 67 public Object getGeneratedValue(Accessor accessor, AbstractSession writeSession, String seqName) { 68 if (shouldUsePreallocation()) { 69 return null; 70 } else { 71 Number value = updateAndSelectSequence(accessor, writeSession, seqName, 1); 72 if (value == null) { 73 throw DatabaseException.errorPreallocatingSequenceNumbers(); 74 } 75 return value; 76 } 77 } 78 79 public Vector getGeneratedVector(Accessor accessor, AbstractSession writeSession, String seqName, int size) { 80 if (shouldUsePreallocation()) { 81 Number 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 101 public boolean shouldOverrideExistingValue(String seqName, Object existingValue) { 102 if (shouldAcquireValueAfterInsert()) { 103 return true; 104 } else { 105 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 else if (existingValue instanceof Number && (((Number )existingValue).longValue() <= 0)) { 118 return true; 119 } 120 121 return false; 122 } 123 } 124 125 132 protected Vector createVector(Number sequence, String 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 sequencesForName = new Vector (size); 143 144 nextSequence = nextSequence.subtract(new BigDecimal(size)); 145 146 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 |