1 21 package oracle.toplink.essentials.descriptors; 23 24 import oracle.toplink.essentials.internal.sessions.*; 25 import java.util.*; 26 import java.sql.Timestamp ; 27 import oracle.toplink.essentials.expressions.*; 28 import oracle.toplink.essentials.internal.helper.*; 29 import oracle.toplink.essentials.queryframework.*; 30 import oracle.toplink.essentials.exceptions.*; 31 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 32 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 33 import oracle.toplink.essentials.internal.sessions.AbstractSession; 34 35 40 public class TimestampLockingPolicy extends VersionLockingPolicy { 41 protected int retrieveTimeFrom; 42 public final static int SERVER_TIME = 1; 43 public final static int LOCAL_TIME = 2; 44 45 50 public TimestampLockingPolicy() { 51 super(); 52 this.useServerTime(); 53 } 54 55 61 public TimestampLockingPolicy(String fieldName) { 62 super(fieldName); 63 this.useServerTime(); 64 } 65 66 72 public TimestampLockingPolicy(DatabaseField field) { 73 super(field); 74 this.useServerTime(); 75 } 76 77 89 public int compareWriteLockValues(Object value1, Object value2) { 90 java.sql.Timestamp timestampValue1 = (java.sql.Timestamp )value1; 91 java.sql.Timestamp timestampValue2 = (java.sql.Timestamp )value2; 92 return timestampValue1.compareTo(timestampValue2); 93 } 94 95 99 protected Class getDefaultLockingFieldType() { 100 return ClassConstants.TIMESTAMP; 101 } 102 103 108 public Object getBaseValue(){ 109 return new Timestamp (0); 110 } 111 112 116 protected Object getInitialWriteValue(AbstractSession session) { 117 if (usesLocalTime()) { 118 return new Timestamp (System.currentTimeMillis()); 119 } 120 if (usesServerTime()) { 121 AbstractSession readSession = session.getSessionForClass(getDescriptor().getJavaClass()); 122 while (readSession.isUnitOfWork()) { 123 readSession = ((UnitOfWorkImpl)readSession).getParent().getSessionForClass(getDescriptor().getJavaClass()); 124 } 125 126 return readSession.getDatasourceLogin().getDatasourcePlatform().getTimestampFromServer(session, readSession.getName()); 127 } 128 return null; 129 130 } 131 132 136 public Object getNewLockValue(ModifyQuery query) { 137 return getInitialWriteValue(query.getSession()); 138 } 139 140 145 public Object getValueToPutInCache(AbstractRecord row, AbstractSession session) { 146 if (isStoredInCache()) { 147 return session.getDatasourcePlatform().convertObject(row.get(getWriteLockField()), ClassConstants.TIMESTAMP); 148 } else { 149 return null; 150 } 151 } 152 153 157 public int getVersionDifference(Object currentValue, Object domainObject, Vector primaryKeys, AbstractSession session) { 158 java.sql.Timestamp writeLockFieldValue; 159 java.sql.Timestamp newWriteLockFieldValue = (java.sql.Timestamp )currentValue; 160 if (newWriteLockFieldValue == null) { 161 return 0; } 163 if (isStoredInCache()) { 164 writeLockFieldValue = (java.sql.Timestamp )session.getIdentityMapAccessor().getWriteLockValue(primaryKeys, domainObject.getClass()); 165 } else { 166 writeLockFieldValue = (java.sql.Timestamp )lockValueFromObject(domainObject); 167 } 168 if ((writeLockFieldValue != null) && !(newWriteLockFieldValue.after(writeLockFieldValue))) { 169 return 0; 170 } 171 172 return 2; 175 } 176 177 181 public Object getWriteLockValue(Object domainObject, java.util.Vector primaryKey, AbstractSession session) { 182 java.sql.Timestamp writeLockFieldValue = null; 183 if (isStoredInCache()) { 184 writeLockFieldValue = (java.sql.Timestamp )session.getIdentityMapAccessor().getWriteLockValue(primaryKey, domainObject.getClass()); 185 } else { 186 Object lockValue = lockValueFromObject(domainObject); 188 if (lockValue != null) { 189 if (lockValue instanceof java.sql.Timestamp ) { 190 writeLockFieldValue = (java.sql.Timestamp )lockValueFromObject(domainObject); 191 } else { 192 throw OptimisticLockException.needToMapJavaSqlTimestampWhenStoredInObject(); 193 } 194 } 195 } 196 return writeLockFieldValue; 197 } 198 199 203 public Expression getWriteLockUpdateExpression(ExpressionBuilder builder) { 204 return builder.value(getInitialWriteValue(builder.getSession())); 205 } 206 207 211 protected Number incrementWriteLockValue(Number numberValue) { 212 return null; 213 } 214 215 219 public boolean isChildWriteLockValueGreater(AbstractSession session, java.util.Vector primaryKey, Class original, ObjectChangeSet changeSet) { 220 if (isStoredInCache()) { 221 java.sql.Timestamp writeLockValue = (java.sql.Timestamp )changeSet.getWriteLockValue(); 224 java.sql.Timestamp parentValue = (java.sql.Timestamp )session.getIdentityMapAccessor().getWriteLockValue(primaryKey, original); 225 if (writeLockValue != null) { if ((parentValue == null) || parentValue.before(writeLockValue)) { return true; 228 } 229 } 230 } 231 232 return false; 233 } 234 235 239 public boolean isChildWriteLockValueGreater(UnitOfWorkImpl uow, java.util.Vector primaryKey, Class original) { 240 if (isStoredInCache()) { 241 java.sql.Timestamp writeLockValue = (java.sql.Timestamp )uow.getIdentityMapAccessor().getWriteLockValue(primaryKey, original); 244 java.sql.Timestamp parentValue = (java.sql.Timestamp )uow.getParent().getIdentityMapAccessor().getWriteLockValue(primaryKey, original); 245 if (writeLockValue != null) { if ((parentValue == null) || parentValue.before(writeLockValue)) { return true; 248 } 249 } 250 } 251 252 return false; 253 } 254 255 260 public boolean isNewerVersion(Object currentValue, Object domainObject, java.util.Vector primaryKey, AbstractSession session) { 261 java.sql.Timestamp writeLockFieldValue; 262 java.sql.Timestamp newWriteLockFieldValue = (java.sql.Timestamp )currentValue; 263 if (isStoredInCache()) { 264 writeLockFieldValue = (java.sql.Timestamp )session.getIdentityMapAccessor().getWriteLockValue(primaryKey, domainObject.getClass()); 265 } else { 266 writeLockFieldValue = (java.sql.Timestamp )lockValueFromObject(domainObject); 267 } 268 if ((writeLockFieldValue != null) && !(newWriteLockFieldValue.after(writeLockFieldValue))) { 269 return false; 270 } 271 return true; 272 } 273 274 279 public boolean isNewerVersion(AbstractRecord databaseRow, Object domainObject, java.util.Vector primaryKey, AbstractSession session) { 280 java.sql.Timestamp writeLockFieldValue; 281 java.sql.Timestamp newWriteLockFieldValue = (java.sql.Timestamp )session.getDatasourcePlatform().convertObject(databaseRow.get(getWriteLockField()), ClassConstants.TIMESTAMP); 282 if (isStoredInCache()) { 283 writeLockFieldValue = (java.sql.Timestamp )session.getIdentityMapAccessor().getWriteLockValue(primaryKey, domainObject.getClass()); 284 } else { 285 writeLockFieldValue = (java.sql.Timestamp )lockValueFromObject(domainObject); 286 } 287 if ((writeLockFieldValue != null) && !(newWriteLockFieldValue.after(writeLockFieldValue))) { 288 return false; 289 } 290 return true; 291 } 292 293 297 public void setUsesServerTime(boolean usesServerTime) { 298 if (usesServerTime) { 299 useServerTime(); 300 } else { 301 useLocalTime(); 302 } 303 } 304 305 309 public void useLocalTime() { 310 retrieveTimeFrom = LOCAL_TIME; 311 } 312 313 317 public void useServerTime() { 318 retrieveTimeFrom = SERVER_TIME; 319 } 320 321 325 public boolean usesLocalTime() { 326 return (retrieveTimeFrom == LOCAL_TIME); 327 } 328 329 333 public boolean usesServerTime() { 334 return (retrieveTimeFrom == SERVER_TIME); 335 } 336 } 337 | Popular Tags |