| 1 23 24 37 package com.lutris.dods.builder.generator.dataobject; 38 39 import java.io.ByteArrayInputStream ; 40 import java.io.IOException ; 41 import java.math.BigDecimal ; 42 import java.sql.PreparedStatement ; 43 import java.sql.ResultSet ; 44 import java.sql.SQLException ; 45 import java.sql.Types ; 46 import com.lutris.logging.Logger; 47 import org.enhydra.dods.DODS; 48 import org.enhydra.dods.cache.Condition; 49 import com.lutris.appserver.server.sql.CloneableDO; 50 import com.lutris.appserver.server.sql.CoreDO; 51 import com.lutris.appserver.server.sql.DBConnection; 52 import com.lutris.appserver.server.sql.DBRowUpdateException; 53 import com.lutris.appserver.server.sql.DatabaseManagerException; 54 import com.lutris.appserver.server.sql.ObjectId; 55 import com.lutris.appserver.server.sql.ObjectIdException; 56 import com.lutris.appserver.server.sql.standard.DriverSpecificConstants; 57 import com.lutris.appserver.server.sql.standard.StandardLogicalDatabase; 58 import com.lutris.dods.builder.generator.query.DataObjectException; 59 import com.lutris.dods.builder.generator.query.QueryException; 60 import com.lutris.dods.builder.generator.query.RefAssertionException; 61 62 abstract public class GenericDO extends CloneableDO { 63 static public void printMsg(int level, String s) { 64 try { 65 DODS.getLogChannel().write(level, s); 66 } catch (Exception e) { 67 System.out.println(level + " " + s); 68 } 69 } 70 protected boolean dirty = false; 73 private boolean notUsingOId = false; 75 76 private static Boolean setNullAsVarchar = null; 77 78 private static Boolean setBytesAsLongvarbinary = null; 79 80 private static Boolean setBytesAsBinaryStream = null; 81 82 private static Boolean setBooleanAsString = null; 83 84 85 86 private static boolean isSetNullAsVarchar() { 87 if (setNullAsVarchar==null) { 88 setNullAsVarchar = new Boolean (DriverSpecificConstants.DEFAULT_SET_NULL_AS_VARCHAR); 89 try { 90 String setNullAsVarcharStr = ((StandardLogicalDatabase)DODS.getDatabaseManager() 91 .findLogicalDatabase(DODS.getDatabaseManager().getDefaultDB())) 92 .getDriverProperty(DriverSpecificConstants.PARAMNAME_SET_NULL_AS_VARCHAR); 93 if(setNullAsVarcharStr!=null){ 94 if(setNullAsVarcharStr.equalsIgnoreCase("true")){ 95 setNullAsVarchar=new Boolean (true); 96 }else if(setNullAsVarcharStr.equalsIgnoreCase("false")){ 97 setNullAsVarchar=new Boolean (false); 98 }else{ 99 DODS.getLogChannel().write(Logger.DEBUG,"Illegal value for SetNullAsVarchar parameter. Using default"); 100 } 101 } 102 } catch (DatabaseManagerException e){ 103 DODS.getLogChannel().write(Logger.DEBUG," Unable to read configuration for SetNullAsVarchar. Using default. "); 104 } 105 } 106 return setNullAsVarchar.booleanValue(); 107 } 108 109 private static boolean isSetBooleanAsString() { 110 if (setBooleanAsString == null) { 111 setBooleanAsString = new Boolean (DriverSpecificConstants.DEFAULT_SET_BOOLEAN_AS_STRING); 112 try { 113 String setBooleanAsStringStr = ((StandardLogicalDatabase)DODS.getDatabaseManager() 114 .findLogicalDatabase(DODS.getDatabaseManager().getDefaultDB())) 115 .getDriverProperty(DriverSpecificConstants.PARAMNAME_SET_BOOLEAN_AS_STRING); 116 if(setBooleanAsStringStr!=null){ 117 if(setBooleanAsStringStr.equalsIgnoreCase("true")){ 118 setBooleanAsString=new Boolean (true); 119 }else if(setBooleanAsStringStr.equalsIgnoreCase("false")){ 120 setBooleanAsString=new Boolean (false); 121 }else{ 122 DODS.getLogChannel().write(Logger.DEBUG,"Illegal value for SetBooleanAsString parameter. Using default ('true')."); 123 } 124 } 125 } catch (DatabaseManagerException e){ 126 DODS.getLogChannel().write(Logger.DEBUG," Unable to read configuration for SetBooleanAsString. Using default ('true'). "); 127 } 128 } 129 return setBooleanAsString.booleanValue(); 130 } 131 132 133 private static boolean isSetBytesAsBinaryStream() { 134 if (setBytesAsBinaryStream == null) { 135 setBytesAsBinaryStream = new Boolean (DriverSpecificConstants.DEFAULT_SET_BYTES_AS_BINARY_STREAM); 136 try { 137 String setBytesAsBinaryStreamStr = ((StandardLogicalDatabase)DODS.getDatabaseManager() 138 .findLogicalDatabase(DODS.getDatabaseManager().getDefaultDB())) 139 .getDriverProperty(DriverSpecificConstants.PARAMNAME_SET_BYTES_AS_BINARY_STREAM); 140 if(setBytesAsBinaryStreamStr!=null){ 141 if(setBytesAsBinaryStreamStr.equalsIgnoreCase("true")){ 142 setBytesAsBinaryStream=new Boolean (true); 143 }else if(setBytesAsBinaryStreamStr.equalsIgnoreCase("false")){ 144 setBytesAsBinaryStream=new Boolean (false); 145 }else{ 146 DODS.getLogChannel().write(Logger.DEBUG,"Illegal value for SetBytesAsBinaryStream parameter. Using default"); 147 } 148 } 149 } catch (DatabaseManagerException e){ 150 DODS.getLogChannel().write(Logger.DEBUG," Unable to read configuration for SetBytesAsBinaryStream. Using default. "); 151 } 152 } 153 return setBytesAsBinaryStream.booleanValue(); 154 } 155 156 private static boolean isSetBytesAsLongvarbinary() { 157 if (setBytesAsLongvarbinary == null) { 158 setBytesAsLongvarbinary = new Boolean (DriverSpecificConstants.DEFAULT_SET_BYTES_AS_LONGVARBINARY); 159 try { 160 String setBytesAsLongvarbinaryStr = ((StandardLogicalDatabase)DODS.getDatabaseManager() 161 .findLogicalDatabase(DODS.getDatabaseManager().getDefaultDB())) 162 .getDriverProperty(DriverSpecificConstants.PARAMNAME_SET_BYTES_AS_LONGVARBINARY); 163 if(setBytesAsLongvarbinaryStr!=null){ 164 if(setBytesAsLongvarbinaryStr.equalsIgnoreCase("true")){ 165 setBytesAsLongvarbinary=new Boolean (true); 166 }else if(setBytesAsLongvarbinaryStr.equalsIgnoreCase("false")){ 167 setBytesAsLongvarbinary=new Boolean (false); 168 }else{ 169 DODS.getLogChannel().write(Logger.DEBUG,"Illegal value for SetBytesAsLongvarchar parameter. Using default"); 170 } 171 } 172 } catch (DatabaseManagerException e){ 173 DODS.getLogChannel().write(Logger.DEBUG," Unable to read configuration for SetBytesAsLongvarchar. Using default. "); 174 } 175 } 176 return setBytesAsLongvarbinary.booleanValue(); 177 } 178 179 180 181 184 public GenericDO() 185 throws ObjectIdException, DatabaseManagerException { 186 super(); 187 set_OId(DODS.getDatabaseManager().allocateObjectId()); 188 markNewValue(); 189 } 190 191 194 public GenericDO(String dbName) 195 throws ObjectIdException, DatabaseManagerException { 196 super(); 197 set_OId(DODS.getDatabaseManager().allocateObjectId(dbName)); 198 markNewValue(); 199 } 200 201 204 public GenericDO(boolean notUsingOId) 205 throws ObjectIdException, DatabaseManagerException { 206 super(); 207 this.notUsingOId = notUsingOId; 208 if (!notUsingOId) { 209 set_OId(DODS.getDatabaseManager().allocateObjectId()); 210 } 211 markNewValue(); 212 } 213 214 217 public GenericDO(String dbName, boolean notUsingOId) 218 throws ObjectIdException, DatabaseManagerException { 219 super(); 220 this.notUsingOId = notUsingOId; 221 if (!notUsingOId) { 222 set_OId(DODS.getDatabaseManager().allocateObjectId(dbName)); 223 } 224 markNewValue(); 225 } 226 227 230 public GenericDO(ObjectId id) 231 throws ObjectIdException, DatabaseManagerException { 232 super(); 233 set_OId(id); 234 } 235 236 239 public GenericDO(String dbName, ObjectId id) 240 throws ObjectIdException, DatabaseManagerException { 241 super(); 242 set_OId(id); 243 } 244 245 255 public GenericDO(ResultSet rs) 256 throws SQLException , ObjectIdException, DatabaseManagerException { 257 super(rs); 258 } 260 261 271 public GenericDO(String dbName, ResultSet rs) 272 throws SQLException , ObjectIdException, DatabaseManagerException { 273 super(rs); 274 } 276 277 286 public synchronized Object cloneUnique() 287 throws DatabaseManagerException, ObjectIdException { 288 GenericDO dataObj = (GenericDO) super.cloneUnique(); 289 290 dataObj.set_Version(0); 294 dataObj.markNewValue(); return dataObj; 296 } 297 298 307 protected void makeIdentical(GenericDO obj) {} 308 309 319 public synchronized void executeUpdate(DBConnection conn) 320 throws SQLException , DBRowUpdateException { 321 if (dirty) { 322 if (!notUsingOId) { 323 printMsg(Logger.DEBUG, 324 getClass().getName() + ".executeUpdate:" + persistent 325 + ": " + getClass().getName() + ": " 326 + get_OId().toString() + ": " + get_Version()); 327 } 328 super.executeUpdate(conn); 329 } 330 } 331 332 342 public synchronized void executeInsert(DBConnection conn) 343 throws SQLException , DBRowUpdateException { 344 345 346 if (!notUsingOId) { 347 printMsg(Logger.DEBUG, 348 getClass().getName() + ".executeInsert():" + persistent 349 + ": " + get_OId().toString() + ": " + get_Version() + ": " 350 + get_NewVersion()); 351 } 352 super.executeInsert(conn); 353 354 356 } 357 358 365 public void executeDelete(DBConnection conn) 366 throws SQLException { 367 369 if (isPersistent()) { 370 if (!notUsingOId) { 371 printMsg(Logger.DEBUG, 372 getClass().getName() + ".executeDelete(): " + persistent 373 + ": " + get_OId().toString() + ": " + get_Version()); 374 } 375 super.executeDelete(conn); 376 } 377 } 378 379 384 public void finalizeInsert(boolean success) { 385 if (!notUsingOId) { 386 printMsg(Logger.DEBUG, 387 getClass().getName() + ".finalizeInsert(" + success + ") " 388 + "oid=" + get_OId().toString() + " " + "version=" 389 + get_Version()); 390 } 391 boolean p = persistent; 392 393 super.finalizeInsert(success); 394 if (p == true) { 395 persistent = true; } 397 if (success) { 398 dirty = false; 399 } 400 } 401 402 407 public void finalizeUpdate(boolean success) { 408 if (!notUsingOId) { 409 printMsg(Logger.DEBUG, 410 getClass().getName() + ".finalizeUpdate:" + success + ": " 411 + get_OId().toString() + ": " + get_Version()); 412 } 413 super.finalizeUpdate(success); 414 if (success) { 415 dirty = false; 416 } 417 } 419 420 428 static protected String get_primaryKeyName() { 429 return CoreDO.get_OIdColumnName(); 430 } 431 432 441 static protected String getPrimaryKeyName() { 442 return get_primaryKeyName(); 443 } 444 445 449 protected void setOId(ObjectId oId) { 450 set_OId(oId); 451 } 452 453 457 protected void set_OId(ObjectId oId) { 458 if (!notUsingOId) { 459 super.set_OId(markNewValue(get_OId(), oId)); 460 } 461 } 462 463 467 public boolean isDirty() { 468 return dirty; 469 } 470 471 479 protected void markClean() { 480 dirty = false; 481 } 482 483 493 protected void markNewValue() { 494 dirty = true; 495 } 496 497 522 protected String markNewValue(String current_string, String new_string, 523 int max_length, 524 boolean nullOK) { 525 526 540 return markNewValue(current_string, new_string, 0, max_length, nullOK); 542 } 543 544 575 protected String markNewValue(String current_string, String new_string, 576 int min_length, int max_length, 577 boolean nullOK) { 578 colChanged = false; 579 if (!nullOK && current_string == null) { current_string = ""; 581 } 582 if (new_string == null) { 583 if (!nullOK) { return current_string; 585 } else { 586 if (current_string != null) { 587 colChanged = dirty = true; 588 } 589 return null; 590 } 591 } 592 if (min_length >= 0 && new_string.length() < min_length) { 593 new_string = current_string; 594 } if (max_length > 00 && new_string.length() > max_length) { 596 new_string = new_string.substring(0, max_length); 597 } 598 if (!new_string.equals(current_string)) { 599 colChanged = dirty = true; 600 } 601 return new_string; 602 } 603 protected boolean colChanged = false; 604 622 protected ObjectId markNewValue(ObjectId current_OId, ObjectId new_OId) { 623 colChanged = false; 624 if (current_OId == null) { 625 if (new_OId != null) { 626 colChanged = dirty = true; 627 } 628 } else { 629 if (new_OId == null) { 630 colChanged = dirty = true; 631 } else if (!current_OId.toString().equals(new_OId.toString())) { 632 colChanged = dirty = true; 633 } 634 } 635 return new_OId; 636 } 637 638 656 protected java.util.Date markNewValue( 657 java.util.Date current_date, java.util.Date new_date) { 658 colChanged = false; 659 if (current_date == null) { 660 if (new_date != null) { 661 colChanged = dirty = true; 662 } 663 } else { 664 if (new_date == null) { 665 colChanged = dirty = true; 666 } else if (!current_date.toString().equals(new_date.toString())) { 667 colChanged = dirty = true; 668 } 669 } 670 return new_date; 671 } 672 673 691 protected BigDecimal markNewValue(BigDecimal current_bd, BigDecimal new_bd) { 692 colChanged = false; 693 if (current_bd == null) { 694 if (new_bd != null) { 695 colChanged = dirty = true; 696 } 697 } else { 698 if (new_bd == null) { 699 colChanged = dirty = true; 700 } else if (!current_bd.equals(new_bd)) { 701 colChanged = dirty = true; 702 } 703 } 704 return new_bd; 705 } 706 707 725 protected byte[] markNewValue(byte[] current_val, byte[] new_val) { 726 colChanged = false; 727 if (current_val == null) { 728 if (new_val != null) { 729 colChanged = dirty = true; 730 } 731 } else { 732 if (new_val == null) { 733 colChanged = dirty = true; 734 } else { 735 if (current_val.length != new_val.length) { colChanged = dirty = true; 737 } else { 738 for (int i = 0; i < current_val.length; i++) { 739 if (current_val[i] != new_val[i]) { 740 colChanged = dirty = true; 741 break; 742 } 743 } 744 } 745 } 746 } 747 return new_val; 748 } 749 750 768 protected java.sql.Date markNewValue( 769 java.sql.Date current_val, java.sql.Date new_val) { 770 colChanged = false; 771 if (current_val == null) { 772 if (new_val != null) { 773 colChanged = dirty = true; 774 } 775 } else { 776 if (new_val == null) { 777 colChanged = dirty = true; 778 } else if (!current_val.equals(new_val)) { 779 colChanged = dirty = true; 780 } 781 } 782 return new_val; 783 } 784 785 803 protected java.sql.Time markNewValue( 804 java.sql.Time current_val, java.sql.Time new_val) { 805 colChanged = false; 806 if (current_val == null) { 807 if (new_val != null) { 808 colChanged = dirty = true; 809 } 810 } else { 811 if (new_val == null) { 812 colChanged = dirty = true; 813 } else if (!cu
|