1 21 package oracle.toplink.essentials.sequencing; 23 24 import java.util.Vector ; 25 import java.io.Serializable ; 26 import oracle.toplink.essentials.internal.sessions.AbstractSession; 27 import oracle.toplink.essentials.internal.databaseaccess.Platform; 28 import oracle.toplink.essentials.internal.databaseaccess.DatasourcePlatform; 29 import oracle.toplink.essentials.internal.databaseaccess.Accessor; 30 import oracle.toplink.essentials.exceptions.ValidationException; 31 32 57 public abstract class Sequence implements Serializable , Cloneable { 58 protected String name = ""; 60 61 protected int size = 50; 63 64 protected Platform platform; 66 67 protected int initialValue = 1; 68 69 protected int depth; 71 72 public Sequence() { 73 super(); 74 } 75 76 public Sequence(String name) { 77 this(); 78 setName(name); 79 } 80 81 public Sequence(String name, int size) { 82 this(); 83 setName(name); 84 setPreallocationSize(size); 85 } 86 87 public Sequence(String name, int size, int initialValue) { 88 this(); 89 setName(name); 90 setPreallocationSize(size); 91 setInitialValue(initialValue); 92 } 93 94 public String getName() { 95 return name; 96 } 97 98 public void setName(String name) { 99 this.name = name; 100 } 101 102 public int getPreallocationSize() { 103 return size; 104 } 105 106 public void setPreallocationSize(int size) { 107 this.size = size; 108 } 109 110 public int getInitialValue() { 111 return initialValue; 112 } 113 114 public void setInitialValue(int initialValue) { 115 this.initialValue = initialValue; 116 } 117 118 public Object clone() { 119 try { 120 Sequence clone = (Sequence)super.clone(); 121 if (isConnected()) { 122 clone.depth = 1; 123 clone.onDisconnect(getDatasourcePlatform()); 124 } 125 return clone; 126 } catch (Exception exception) { 127 throw new InternalError ("Clone failed"); 128 } 129 } 130 131 public boolean equals(Object obj) { 132 if (obj instanceof Sequence) { 133 return equalNameAndSize(this, (Sequence)obj); 134 } else { 135 return false; 136 } 137 } 138 139 public static boolean equalNameAndSize(Sequence seq1, Sequence seq2) { 140 if (seq1 == seq2) { 141 return true; 142 } 143 return seq1.getName().equals(seq2.getName()) && (seq1.getPreallocationSize() == seq2.getPreallocationSize()); 144 } 145 146 protected void setDatasourcePlatform(Platform platform) { 147 this.platform = platform; 148 } 149 150 public Platform getDatasourcePlatform() { 151 return platform; 152 } 153 154 162 public abstract boolean shouldAcquireValueAfterInsert(); 163 164 172 public boolean shouldUsePreallocation() { 173 return !shouldAcquireValueAfterInsert(); 174 } 175 176 183 public abstract boolean shouldUseTransaction(); 184 185 193 public abstract boolean shouldOverrideExistingValue(String seqName, Object existingValue); 194 195 202 public boolean shouldOverrideExistingValue(Object existingValue) { 203 return shouldOverrideExistingValue(getName(), existingValue); 204 } 205 206 219 public abstract Object getGeneratedValue(Accessor accessor, AbstractSession writeSession, String seqName); 220 221 233 public Object getGeneratedValue(Accessor accessor, AbstractSession writeSession) { 234 return getGeneratedValue(accessor, writeSession, getName()); 235 } 236 237 251 public abstract Vector getGeneratedVector(Accessor accessor, AbstractSession writeSession, String seqName, int size); 252 253 265 public Vector getGeneratedVector(Accessor accessor, AbstractSession writeSession) { 266 return getGeneratedVector(accessor, writeSession, getName(), getPreallocationSize()); 267 } 268 269 275 public void onConnect(Platform platform) { 276 if (isConnected()) { 277 verifyPlatform(platform); 278 } else { 279 setDatasourcePlatform(platform); 280 onConnect(); 281 } 282 depth++; 283 } 284 285 291 protected abstract void onConnect(); 292 293 298 public void onDisconnect(Platform platform) { 299 if (isConnected()) { 300 depth--; 301 if (depth == 0) { 302 onDisconnect(); 303 setDatasourcePlatform(null); 304 } 305 } 306 } 307 308 313 protected abstract void onDisconnect(); 314 315 319 public boolean isConnected() { 320 return platform != null; 321 } 322 323 327 protected void verifyPlatform(Platform otherPlatform) { 328 if (getDatasourcePlatform() != otherPlatform) { 329 String hashCode1 = Integer.toString(System.identityHashCode(getDatasourcePlatform())); 330 String name1 = ((DatasourcePlatform)getDatasourcePlatform()).toString() + '(' + hashCode1 + ')'; 331 332 String hashCode2 = Integer.toString(System.identityHashCode(otherPlatform)); 333 String name2 = ((DatasourcePlatform)otherPlatform).toString() + '(' + hashCode2 + ')'; 334 335 throw ValidationException.sequenceCannotBeConnectedToTwoPlatforms(getName(), name1, name2); 336 } 337 } 338 } 339 | Popular Tags |