1 23 package com.lutris.appserver.server.sql; 24 25 import java.io.Serializable ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 30 import com.lutris.dods.builder.generator.query.DataObjectException; 31 32 38 abstract public class CoreDO implements Transaction, Serializable { 39 private static boolean transactionCheck = false; 40 private static boolean deleteCheckVersion = false; 41 private static boolean autoSave = false; 42 public static boolean versioning = true; 43 44 49 public static void disableVersioning() { 50 versioning = false; 51 } 52 protected boolean persistent = false; 54 protected CoreDataStruct originalData = null; 55 56 59 public CoreDO() { 60 persistent = false; 61 } 62 63 71 public CoreDO(ResultSet rs) throws SQLException , ObjectIdException { 72 persistent = true; 73 set_OId(new ObjectId(rs.getBigDecimal(get_OIdColumnName()))); 74 if (versioning) { 75 set_Version(rs.getInt(get_versionColumnName())); 76 } 77 } 78 79 83 public static String get_OIdColumnName() { 84 return CoreDataStruct.get_OIdColumnName(); 85 } 86 87 92 public static String getOIdColumnName() { 93 return get_OIdColumnName(); 94 } 95 96 100 public static void set_OIdColumnName(String _oidColumnName) { 101 CoreDataStruct.set_OIdColumnName(_oidColumnName); 102 } 103 104 109 public static void setOIdColumnName(String _oidColumnName) { 110 set_OIdColumnName(_oidColumnName); 111 } 112 113 117 public static String get_versionColumnName() { 118 return CoreDataStruct.get_versionColumnName(); 119 } 120 121 126 public static String getVersionColumnName() { 127 return get_versionColumnName(); 128 } 129 130 134 public static void set_versionColumnName(String _versionColumnName) { 135 CoreDataStruct.set_versionColumnName(_versionColumnName); 136 } 137 138 143 public static void setVersionColumnName(String _versionColumnName) { 144 set_versionColumnName(_versionColumnName); 145 } 146 147 151 public ObjectId getOId() { 152 return get_OId(); 153 } 154 155 159 public ObjectId get_OId() { 160 if (originalData != null) { 161 return originalData.get_OId(); 162 } else { 163 return null; 164 } 165 } 166 167 172 protected void setOId(ObjectId oId) { 173 set_OId(oId); 174 } 175 176 180 protected void set_OId(ObjectId oId) { 181 if (originalData != null) { 182 originalData.set_OId(oId); 183 } 184 } 185 186 190 protected void setVersion(int version) { 191 set_Version(version); 192 } 193 194 198 protected void set_Version(int version) { 199 if (originalData != null) { 200 originalData.set_Version(version); 201 } 202 } 203 204 208 protected int getVersion() { 209 return get_Version(); 210 } 211 212 216 protected int get_Version() { 217 if (originalData != null) { 218 return originalData.get_Version(); 219 } else { 220 return 0; 221 } 222 } 223 224 229 protected void setNewVersion(int newVersion) { 230 if (originalData != null) { 231 originalData.newVersion = newVersion; 232 } 233 } 234 235 239 protected void set_NewVersion(int newVersion) { 240 if (originalData != null) { 241 originalData.newVersion = newVersion; 242 } 243 } 244 245 250 protected int getNewVersion() { 251 if (originalData != null) { 252 return originalData.newVersion; 253 } else { 254 return 0; 255 } 256 } 257 258 262 protected int get_NewVersion() { 263 if (originalData != null) { 264 return originalData.newVersion; 265 } else { 266 return 0; 267 } 268 } 269 270 279 public boolean isPersistent() { 280 return this.persistent; 281 } 282 283 288 public void setPersistent(boolean persistent) { 289 this.persistent = persistent; 290 } 291 292 301 public abstract PreparedStatement getInsertStatement(DBConnection conn) 302 throws SQLException ; 303 304 313 public abstract PreparedStatement getUpdateStatement(DBConnection conn) 314 throws SQLException ; 315 316 325 public abstract PreparedStatement getDeleteStatement(DBConnection conn) 326 throws SQLException ; 327 328 335 public synchronized void executeInsert(DBConnection conn) 336 throws SQLException , DBRowUpdateException { 337 PreparedStatement stmt; 338 339 if (persistent) { 340 executeUpdate(conn); 341 } else { 342 stmt = getInsertStatement(conn); 343 if (stmt != null) { 344 try { 345 conn.executeUpdate(stmt, "executeInsert"); 346 } catch (SQLException e) { 347 throw e; 348 } finally{ 349 if(((ExtendedDBConnection)conn).getMaxPreparedStmts()==0){ 350 stmt.close(); 351 } 352 } 354 } 355 persistent = true; 356 } 357 } 358 359 365 public void finalizeInsert(boolean success) { 366 persistent = success; 367 376 } 377 378 385 public synchronized void executeUpdate(DBConnection conn) 386 throws SQLException , DBRowUpdateException { 387 boolean mustUpdate = (get_NewVersion() == get_Version()); 388 393 PreparedStatement stmt = getUpdateStatement(conn); 394 395 if (stmt != null) { 396 try{ 397 if ((conn.executeUpdate(stmt, "execute update") == 0) && mustUpdate) { 398 stmt.close(); 404 stmt=null; 405 seeWhatHappened(conn,"Update"); 406 } 407 } catch (SQLException e) { 408 throw e; 409 } finally{ 410 if(((ExtendedDBConnection)conn).getMaxPreparedStmts()==0 && (stmt!= null)){ 411 stmt.close(); 412 } 413 } 415 } 416 } 417 418 private void seeWhatHappened(DBConnection conn, 419 String action) 420 throws SQLException , DBRowUpdateException { 421 ResultSet rs=null; 422 PreparedStatement stmt=null; 423 try { 424 String table = this.getTableName(); 425 426 stmt = conn.prepareStatement("select " + get_versionColumnName() 427 + " from " + table + " where " + get_OIdColumnName() + " = " 428 + get_OId()); 429 rs = stmt.executeQuery(); 430 431 if (false == rs.next()) { 432 throw new DBRowUpdateException(action + " failed: Table " 433 + table + " contains no row with id=" + get_OId()); 434 } 435 int v = rs.getInt(1); 436 437 if (get_Version() != v) { 438 throw new DBRowUpdateException(action + " failed: Table " 439 + table + " id=" + get_OId() + " has version " + v 440 + " where object has version " + get_Version() 441 + (versioning ? "." : ", and versioning is disabled!")); 442 } 443 throw new SQLException (action + " failed, but Table " + table 444 + " id=" + get_OId() + " does exist with version=" + v); 445 } catch (DBRowUpdateException re) { 446 throw re; 447 } catch (SQLException se) { 448 throw se; 449 } catch (Exception e) { 450 throw new SQLException ("Error determining cause of update failure.", 451 e.getMessage()); 452 }finally{ 453 if (rs!=null){ 454 rs.close(); 455 } 456 if(stmt!=null){ 457 stmt.close(); 458 } 459 } 460 } 461 462 469 protected String getTableName()throws SQLException { 470 throw new SQLException ("getTableName() not implemented. DO classes need to be regenerated"); 472 } 473 474 483 public void finalizeUpdate(boolean success) { 484 493 } 494 495 502 public void executeDelete(DBConnection conn) 503 throws SQLException { 504 PreparedStatement stmt = getDeleteStatement(conn); 505 506 if (stmt != null) { 507 try{ 508 if (conn.executeUpdate(stmt, "execute delete") == 0) { 509 try { 510 stmt.close(); 511 stmt=null; 512 seeWhatHappened(conn, "Delete"); 513 } catch (DBRowUpdateException de) { 514 SQLException sqle = new SQLException (de.toString()); 515 throw sqle; 516 } 517 } 518 }catch( SQLException e){ 519 throw e; 520 }finally{ 521 if(((ExtendedDBConnection)conn).getMaxPreparedStmts()==0 && (stmt!=null)){ 522 stmt.close(); 523 } 524 } 526 } 527 } 528 529 536 public void finalizeDelete(boolean success) { 537 if (persistent) { 538 persistent = !success; 539 } 540 } 541 542 546 public Object originalData_get() { 547 return null; 548 } 549 550 554 public void updateCache() {} 555 556 560 public void deleteFromCache() {} 561 562 566 public void addToCache() {} 567 568 572 public void evict() {} 573 574 580 public void refresh() throws DataObjectException {} 581 582 585 public int getOriginalVersion() { 586 if (originalData != null) { 587 return originalData.get_Version(); 588 } else { 589 return 0; 590 } 591 } 592 593 596 public void makeVisible() {} 597 598 601 public void makeInvisible() {} 602 603 606 public void executeLockingStatement(DBConnection conn) throws SQLException {} 607 608 611 abstract public void dumpData(boolean incrementVersion); 612 } 613 614 | Popular Tags |