1 2 41 42 43 package jspPhoneBook.data; 44 45 import java.io.*; 46 import java.sql.*; 47 import java.math.*; 48 import java.util.Hashtable ; 49 import java.util.Collection ; 50 import java.util.Properties ; 51 import java.io.FileInputStream ; 52 import java.util.Vector ; 53 import java.util.Map ; 54 import java.util.HashSet ; 55 import java.util.HashMap ; 56 import java.util.Iterator ; 57 import java.lang.reflect.Method ; 58 59 import com.lutris.logging.LogChannel; 60 import com.lutris.logging.Logger; 61 import org.enhydra.dods.DODS; 62 import com.lutris.util.Config; 63 import com.lutris.util.ConfigException; 64 import com.lutris.appserver.server.sql.*; 65 import com.lutris.appserver.server.sql.standard.*; 66 import com.lutris.dods.builder.generator.dataobject.*; 67 import com.lutris.dods.builder.generator.query.*; 68 import org.enhydra.dods.cache.Condition; 69 import org.enhydra.dods.cache.DataStructCache; 70 import org.enhydra.dods.cache.QueryCache; 71 import org.enhydra.dods.cache.QueryCacheImpl; 72 import org.enhydra.dods.cache.QueryResult; 73 import org.enhydra.dods.cache.ConfigurationAdministration; 74 import org.enhydra.dods.statistics.Statistics; 75 import com.lutris.classloader.MultiClassLoader; 76 import org.enhydra.xml.XMLConfig; 77 import org.enhydra.dods.Common; 78 import org.enhydra.dods.cache.CacheConstants; 79 import org.enhydra.dods.exceptions.AssertionDataObjectException; 80 import org.enhydra.dods.exceptions.CacheObjectException; 81 82 83 84 91 public class PersonDO extends com.lutris.dods.builder.generator.dataobject.GenericDO implements PersonDOI, java.io.Serializable { 92 128 static public final RDBTable table = new RDBTable( "person" ); 129 130 133 private HashMap refs = null; 134 135 136 139 protected String originDatabase = null; 140 141 142 149 public String getOriginDatabase() { 150 return get_OriginDatabase(); 151 } 152 153 159 public String get_OriginDatabase() { 160 return get_DataStruct().get_Database(); 161 } 162 163 174 public String getTableName() { 175 return "person"; 176 } 177 178 182 static public final RDBColumn PrimaryKey = new RDBColumn( table, get_primaryKeyName() ); 183 184 185 186 187 205 static protected final boolean notUsingOId = false; 206 207 208 215 private PersonDataStruct data = null; 216 217 222 private DBTransaction transaction = null; 223 224 230 public DBTransaction get_transaction() { 231 return transaction; 232 } 233 234 240 protected boolean setTransaction(DBTransaction trans) { 241 boolean isOK=false; 242 if (get_transaction() == null) { 243 transaction = trans; 244 isOK=true; 245 } else { 246 if(get_transaction().equals(trans)) 247 isOK=true; 248 } 249 return isOK; 250 } 251 252 253 259 public boolean isReadOnly() { 260 return getConfigurationAdministration().getTableConfiguration().isReadOnly(); 261 } 262 263 267 public void setData (Object data) { 268 set_Data(data); 269 } 270 271 274 public void set_Data (Object data) { 275 this.data = (PersonDataStruct)data; 276 } 277 278 281 public void originalData_set (Object data) { 282 originalData = (PersonDataStruct)data; 283 } 284 285 289 public Object getData () { 290 return get_Data(); 291 } 292 293 296 public Object get_Data () { 297 return (null != data)? data : originalData; 298 } 299 300 304 public PersonDataStruct getDataStruct () { 305 return get_DataStruct(); 306 } 307 308 311 public PersonDataStruct get_DataStruct () { 312 return (PersonDataStruct) get_Data(); 313 } 314 315 318 public Object originalData_get () { 319 return originalData; 320 } 321 322 public void checkDup () throws DatabaseManagerException, com.lutris.appserver.server.sql.ObjectIdException { 323 if (isDeletedFromDatabase) 324 throw new DatabaseManagerException("Object "+get_OId()+" is deleted"); 325 if (null == data) { 326 data = ((PersonDataStruct)originalData).duplicate(); 327 data.readOnly = false; 328 } 329 } 330 331 341 protected PersonDO ( boolean is_view ) 342 throws ObjectIdException, DatabaseManagerException { 343 super( is_view ); 344 if(isTransactionCheck()) { 345 String trace=""; 346 StackTraceElement [] traceElements= (new Throwable ()).getStackTrace(); 347 for(int i=0; i < traceElements.length; i++) 348 trace+=traceElements[i].toString()+"\n"; 349 DODS.getLogChannel().write(Logger.WARNING, "DO without transaction context is created :"+(is_view?"":" Database: "+get_OriginDatabase()+" PersonDO class, oid: "+get_OId()+", version: "+get_Version())+" \n"+trace); 350 } 351 } 352 363 protected PersonDO ( boolean is_view, DBTransaction dbTrans ) 364 throws ObjectIdException, DatabaseManagerException { 365 super( is_view ); 366 setTransaction(dbTrans); 367 if(dbTrans!=null) { 368 originDatabase = dbTrans.getDatabaseName(); 369 } 370 if(originDatabase==null) 371 originDatabase = get_logicalDBName(); 372 get_DataStruct().set_Database(originDatabase); 373 addToTransactionCache(); 374 375 } 376 377 378 386 protected PersonDO () 387 throws ObjectIdException, DatabaseManagerException { 388 super( notUsingOId ); 389 originDatabase = get_logicalDBName(); 390 get_DataStruct().set_Database(originDatabase); 391 if(isTransactionCheck()) { 392 String trace=""; 393 StackTraceElement [] traceElements= (new Throwable ()).getStackTrace(); 394 for(int i=0; i < traceElements.length; i++) 395 trace+=traceElements[i].toString()+"\n"; 396 DODS.getLogChannel().write(Logger.WARNING, "DO without transaction context is created : Database: "+get_OriginDatabase()+" PersonDO class, oid: "+get_OId()+", version: "+get_Version()+" \n"+trace); 397 } 398 if (autoSaveAllowed&&isAutoSaveCreateVirgin()&&null != transaction) { 399 try { 400 save(transaction,false); 401 } catch (Exception ex) { 402 DODS.getLogChannel().write(Logger.DEBUG,"Faild to AutoSave virgin DO: "+get_OriginDatabase()+" PersonDO class\n"); 403 } 404 } 405 } 406 407 415 protected PersonDO (DBTransaction dbTrans) 416 throws ObjectIdException, DatabaseManagerException { 417 super( notUsingOId ); 418 setTransaction(dbTrans); 419 if(dbTrans!=null) { 420 originDatabase = dbTrans.getDatabaseName(); 421 } 422 if(originDatabase==null) 423 originDatabase = get_logicalDBName(); 424 get_DataStruct().set_Database(originDatabase); 425 426 addToTransactionCache(); 427 if (autoSaveAllowed&&isAutoSaveCreateVirgin()&&null != transaction) { 428 try { 429 save(transaction,false); 430 } catch (Exception ex) { 431 DODS.getLogChannel().write(Logger.DEBUG,"Faild to AutoSave virgin DO: "+get_OriginDatabase()+" PersonDO class\n"); 432 } 433 } 434 } 435 436 437 438 444 public boolean isLoaded() { 445 return (null != originalData)&&(!get_DataStruct().isEmpty); 446 } 447 448 449 460 public void loadData() 461 throws SQLException, ObjectIdException, DataObjectException { 462 if (null == originalData&&!get_DataStruct().isEmpty) { 463 originalData = new PersonDataStruct (); 464 } 465 466 ObjectId id = get_OId(); 467 if ( null == id ) 468 return; 469 if ( ! isPersistent() ) return; 471 PersonQuery query; 473 474 query = new PersonQuery (get_transaction()); 475 476 query.setRefs(get_refs()); 479 query.setQueryOId( id ); 481 query.requireUniqueInstance(); 482 query.setLoadData(true); 483 PersonDO obj; 484 try { 485 obj = query.getNextDO(); 486 if ( null == obj ) 487 throw new DataObjectException("PersonDO DO not found for id=" + id ); 488 makeIdentical(obj); 489 set_Version( obj.get_Version() ); 490 get_DataStruct().isEmpty = false; 491 } catch ( NonUniqueQueryException e ) { 492 throw new ObjectIdException( "Duplicate ObjectId" ); 493 } 494 495 } 496 497 503 protected void checkLoad() 504 throws DataObjectException { 505 if (null == originalData||get_DataStruct().isEmpty) { 506 try { 507 loadData(); 508 } catch ( Exception e ) { 509 throw new DataObjectException("Unable to load data for PersonDO id=" + get_OId() + 510 ", error = ", e); 511 } 512 } 513 } 514 515 516 531 protected PersonDO( ObjectId id ) 532 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 533 super( id ); 534 originDatabase = get_logicalDBName(); 535 get_DataStruct().set_Database(originDatabase); 536 if(isTransactionCheck()) { 537 String trace=""; 538 StackTraceElement [] traceElements= (new Throwable ()).getStackTrace(); 539 for(int i=0; i < traceElements.length; i++) 540 trace+=traceElements[i].toString()+"\n"; 541 DODS.getLogChannel().write(Logger.WARNING, "DO without transaction context is created : Database: "+get_OriginDatabase()+" PersonDO class, oid: "+get_OId()+", version: "+get_Version()+" \n"+trace); 542 } 543 544 } 545 546 561 protected PersonDO( ObjectId id , DBTransaction dbTrans) 562 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 563 super( id ); 564 setTransaction(dbTrans); 565 if(dbTrans!=null) { 566 originDatabase = dbTrans.getDatabaseName(); 567 } 568 if(originDatabase==null) 569 originDatabase = get_logicalDBName(); 570 get_DataStruct().set_Database(originDatabase); 571 addToTransactionCache(); 572 } 573 574 575 576 577 580 protected static Statistics statistics; 581 582 587 public static Statistics get_statistics() { 588 statistics = cache.getStatistics(); 589 return statistics; 590 } 591 592 595 public static void refreshStatistics() { 596 cache.refreshStatistics(); 597 } 598 599 600 607 public static String [] getUsedLogicalDatabases() { 608 return get_UsedLogicalDatabases(); 609 } 610 611 617 public static String [] get_UsedLogicalDatabases() { 618 String [] str = { get_logicalDBName() }; 619 return str; 620 } 621 622 623 protected static DataStructCache cache; 625 629 public static void readCacheConfiguration(String database) throws CacheObjectException { 630 if (getConfigurationAdministration().isDisabled()) { 631 throw new CacheObjectException("Caching is disabled"); 632 } 633 Config tableConfig = null; 634 Config cacheConfig = null; 635 try { 636 tableConfig = (Config)DODS.getDatabaseManager().getConfig().getSection("DB."+database+".person"); 637 } catch (Exception ex) { 638 } 639 try { 640 cacheConfig = (Config)DODS.getDatabaseManager().getConfig().getSection("DB."+database+".person.cache"); 641 } catch ( Exception e ) { 642 } 643 644 cache.readConfiguration(tableConfig,cacheConfig, database); 645 } 646 647 648 653 public static String getCacheDodsTableName() { 654 return "person"; 655 } 656 657 658 663 public static ConfigurationAdministration getConfigurationAdministration() { 664 return cache; 665 } 666 667 683 public static void refreshCache() 684 throws java.sql.SQLException , DatabaseManagerException, ObjectIdException, DataObjectException { 685 getConfigurationAdministration().getCacheAdministration(CacheConstants.DATA_CACHE).refresh(); 686 String querySnt = cache.getInitialQueryCache(); 687 int maxSize = 0; 688 cache.checkFull(); 689 if (querySnt != null) { 690 PersonQuery query; 691 692 query = new PersonQuery ((DBTransaction)null); 693 query.hitDatabase(); 694 maxSize = cache.getCacheAdministration(CacheConstants.DATA_CACHE).getMaxCacheSize(); 695 if (maxSize > 0) 696 try { 697 query.setMaxRows(maxSize); 698 } 699 catch (NonUniqueQueryException nuEx){ 700 System.out.println ("NonUniqueQueryException in refreshCache() method : too many rows were found."); 701 } 702 if (!querySnt.equals("*") ) { 703 QueryBuilder builder = query.getQueryBuilder(); 704 builder.addWhere(querySnt); 705 } 706 PersonDO obj; 707 try { 708 query.setLoadData(true); 709 query.getNextDO(); 710 } catch ( NonUniqueQueryException ex ) { 711 throw new ObjectIdException( "Duplicate ObjectId" ); 714 } 715 716 } 717 cache.refreshStatistics(); 718 } 719 720 private static boolean isDisabledCaching = false; 721 722 725 public static void disableCaching() 726 throws java.sql.SQLException , DatabaseManagerException, ObjectIdException, DataObjectException { 727 isDisabledCaching = true; 728 getConfigurationAdministration().getCacheAdministration(CacheConstants.DATA_CACHE).disable(); 729 } 730 731 734 public static void enableCaching() 735 throws java.sql.SQLException , DatabaseManagerException, ObjectIdException, DataObjectException { 736 if (isDisabledCaching){ 737 getConfigurationAdministration().getCacheAdministration(CacheConstants.DATA_CACHE).enable(); 738 String querySnt = cache.getInitialQueryCache(); 739 if (querySnt != null) { 740 PersonQuery query; 741 742 query = new PersonQuery ((DBTransaction)null); 743 query.hitDatabase(); 744 if (!querySnt.equals("*") ) { 745 QueryBuilder builder = query.getQueryBuilder(); 746 builder.addWhere(querySnt); 747 } 748 PersonDO obj; 749 try { 750 query.getNextDO(); 751 } catch ( NonUniqueQueryException ex ) { 752 throw new ObjectIdException( "Duplicate ObjectId" ); 755 } 756 757 } 758 } 759 } 760 private static StandardDBTransaction _tr_(DBTransaction dbt) { 761 return (StandardDBTransaction)dbt; 762 } 763 764 774 private PersonDO addToTransactionCache( PersonDO newDO ) { 775 PersonDO ret = null; 776 if(get_transaction()!=null && _tr_(get_transaction()).getTransactionCache()!=null) { 777 ret = (PersonDO)_tr_(get_transaction()).getTransactionCache().addDO(newDO); 778 } 779 if (ret == null) 780 return newDO; 781 return ret; 782 } 783 784 792 public static synchronized PersonDataStruct addToCache( PersonDataStruct newDS ) { 793 PersonDataStruct ret = (PersonDataStruct)cache.addDataStruct(newDS); 794 if (ret == null) 795 return newDS; 796 return ret; 797 } 798 799 802 public void addToCache() { 803 addToCache((PersonDataStruct)this.originalData_get()); 804 } 805 806 812 private void addToTransactionCache() { 813 addToTransactionCache(this); 814 } 815 816 817 822 public static synchronized PersonDataStruct updateCache( PersonDataStruct updDS) { 823 PersonDataStruct ret = (PersonDataStruct)cache.updateDataStruct(updDS); 824 if (ret == null) 825 return updDS; 826 return ret; 827 } 828 829 832 public void updateCache() { 833 updateCache((PersonDataStruct)this.originalData_get()); 834 } 835 836 843 public static synchronized PersonDataStruct deleteFromCache( PersonDataStruct data ) { 844 cache.deleteDataStruct(data); 845 return data; 846 } 847 848 849 856 public static synchronized void removeFromCache(String dbName, String handle) { 857 String cacheHandle = dbName+"."+handle; 858 cache.removeDataStruct( cacheHandle ); 859 } 860 861 864 public void deleteFromCache() { 865 deleteFromCache((PersonDataStruct)this.originalData_get()); 866 } 867 868 869 877 public static synchronized PersonDataStruct removeFromCache( PersonDataStruct data ) { 878 return (PersonDataStruct)cache.removeDataStruct(data); 879 } 880 881 884 public void evict() { 885 if (!isPersistent()) 886 removeFromCache((PersonDataStruct)this.originalData_get()); 887 } 888 889 894 public static void evict(PersonDataStruct[] DSs) { 895 for (int i=0; i<DSs.length; i++) 896 removeFromCache((PersonDataStruct) DSs[i]); 897 } 898 899 907 public static void evict(String dbName, String [] handles) { 908 if (handles!=null) { 909 for (int i=0; i<handles.length; i++) 910 removeFromCache(dbName, handles[i]); 911 } 912 } 913 914 922 923 927 static { 928 try { 929 XMLConfig dodsConf = Common.getDodsConf(); 930 String cacheClassPath = null; 931 String cacheClassName = null; 932 try { 933 cacheClassPath = dodsConf.getText("CacheJar"); 934 cacheClassName = dodsConf.getText("CacheClassName"); 935 if (cacheClassPath != null && cacheClassName != null) { 936 MultiClassLoader loader = new MultiClassLoader(null); 937 loader.setClassPath(cacheClassPath); 938 Class cacheClass = loader.loadClass(cacheClassName); 939 cache = (DataStructCache)cacheClass.newInstance(); 940 cache = cache.newInstance(); 941 } else { 942 cache = new QueryCacheImpl(); 943 } 944 } catch ( Exception e ) { 945 } 946 if (cache == null) { 947 cache = new QueryCacheImpl(); 948 } 949 readCacheConfiguration(get_logicalDBName()); 950 get_statistics(); refreshCache(); 952 } catch ( Exception e ) { 953 } 955 } 956 957 962 public void refresh() throws DataObjectException { 963 try { 964 loadData(); 965 } catch ( Exception e ) { 966 throw new DataObjectException("Unable to load data for PersonDO id=" + get_OId() + 967 ", error = ", e); 968 } 969 } 970 971 978 public static void refresh(PersonDO[] DOs) throws DataObjectException { 979 for (int i=0; i<DOs.length; i++) 980 DOs[i].refresh(); 981 } 982 983 984 989 public static void refresh(String querySnt) throws QueryException { 990 try { 991 QueryBuilder qb = new QueryBuilder(); 992 qb.select( PersonDO.PrimaryKey); 993 qb.addWhere(querySnt); 994 BigDecimal objId; 995 String handle; 996 String database = get_logicalDBName(); 997 RDBRow row; 998 try { 999 while ( null != ( row = qb.getNextRow() ) ) { 1000 objId = row.get( PersonDO.PrimaryKey ).getBigDecimal(); 1001 handle = objId.toString(); 1002 removeFromCache(database, handle); 1003 } 1004 }catch ( Exception e ) { 1005 throw new QueryException(" Query Exception occured," ); 1006 } 1007 }catch (Exception ex){ 1008 System.out.println("Error in refresh(String) of DO object."); 1009 } 1010 } 1011 1012 1013 1028 public static PersonDO createVirgin() 1029 throws DatabaseManagerException, ObjectIdException { 1030 return new PersonDO (); 1031 } 1032 1033 1043 public static PersonDO createVirgin(DBTransaction dbTrans) 1044 throws DatabaseManagerException, ObjectIdException { 1045 return new PersonDO (dbTrans); 1046 } 1047 1048 1086 public static PersonDO createExisting( BigDecimal bd ) 1087 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1088 if ( null == bd ) 1089 return null; 1090 return createExisting( new ObjectId( bd ) ); 1091 } 1092 1093 1094 1113 public static PersonDO createExisting( BigDecimal bd, DBTransaction dbTrans) 1114 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1115 if ( null == bd ) 1116 return null; 1117 return createExisting( new ObjectId( bd ), dbTrans ); 1118 } 1119 1120 1141 public static PersonDO createExisting( BigDecimal bd, HashMap queryRefs, DBTransaction dbTrans) 1142 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1143 if ( null == bd ) 1144 return null; 1145 return createExisting( new ObjectId( bd ), queryRefs, dbTrans ); 1146 } 1147 1148 1149 1160 public static PersonDO createExisting( String handle ) { 1161 PersonDO ret = null; 1162 try { 1163 BigDecimal bd = new BigDecimal( handle ); 1164 ret = createExisting( bd ); 1165 } catch ( Exception e ) { 1166 } 1167 return ret; 1168 } 1169 1170 1171 1181 public static PersonDO createExisting( String handle, DBTransaction dbTrans ) { 1182 PersonDO ret = null; 1183 try { 1184 BigDecimal bd = new BigDecimal( handle ); 1185 ret = createExisting( bd, dbTrans ); 1186 } catch ( Exception e ) { 1187 } 1188 return ret; 1189 } 1190 1191 1192 1193 1213 protected static PersonDO createExisting( ObjectId id, DBTransaction dbTrans ) 1214 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1215 if ( null == id ) 1216 return null; 1217 return createExisting(id, null, dbTrans); 1218 } 1219 1220 1241 protected static PersonDO createExisting( ObjectId id, HashMap queryRefs, DBTransaction dbTrans ) 1242 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1243 if ( null == id ) 1244 return null; 1245 String cacheHandle = get_logicalDBName()+"."+id.toString(); 1246 PersonDO ret = null; 1247 PersonDataStruct data = null; 1248 if(queryRefs==null) 1249 queryRefs = new HashMap (); 1250 if(queryRefs.containsKey(cacheHandle)) { 1251 ret = (PersonDO)queryRefs.get(cacheHandle); 1252 return ret; 1253 } 1254 if(dbTrans!=null && _tr_(dbTrans).getTransactionCache()!= null) { 1255 ret = (PersonDO)_tr_(dbTrans).getTransactionCache().getDOByHandle( cacheHandle ); 1256 if(ret != null) 1257 return ret; 1258 } 1259 if (null != ret) 1260 return ret; 1261 data = (PersonDataStruct)findCachedObjectByHandle( cacheHandle ); 1262 if ( data != null ){ 1263 ret = (PersonDO)createDO (data.get_OId(), dbTrans); 1264 if(ret!=null) { 1265 ret.originalData_set(data); 1266 ret.setPersistent(true); 1267 } 1268 return ret; 1269 } 1270 ret = new PersonDO( id , dbTrans); 1272 ret.setPersistent( true ); 1283 if(queryRefs!=null) { 1284 ret.set_refs(queryRefs); 1285 ret.addRefs(cacheHandle, ret); 1286 } 1287 if (!cache.getTableConfiguration().isLazyLoading()) { ret.loadData(); 1289 } else { 1290 statistics.incrementLazyLoadingNum(); 1291 } 1292 ret.markClean(); 1294 1295 return ret; 1296 } 1297 1298 1299 1320 protected static PersonDO createExisting( ObjectId id ) 1321 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1322 if ( null == id ) 1323 return null; 1324 return createExisting(id, null); 1325 } 1326 1327 1349 protected static PersonDO createExisting( ResultSet rs ) 1350 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1351 if ( null == rs ) 1352 return null; 1353 PersonDO ret = null; 1354 if ( notUsingOId ) { 1355 ret = new PersonDO (); 1356 ret.initFromResultSet( rs ); 1357 } else { 1358 ret = new PersonDO ( rs ); 1359 } 1360 return ret; 1361 } 1362 1363 1364 1382 protected static PersonDO createExisting( ResultSet rs , DBTransaction dbTrans) 1383 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1384 if ( null == rs ) 1385 return null; 1386 PersonDO ret = null; 1387 if ( notUsingOId ) { 1388 ret = new PersonDO (dbTrans); 1389 ret.initFromResultSet( rs ); 1390 } else { 1391 ret = new PersonDO ( rs, dbTrans ); 1392 } 1393 return ret; 1394 } 1395 1396 1397 1420 protected static PersonDO createExisting( ResultSet rs , HashMap queryRefs) 1421 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1422 if ( null == rs ) 1423 return null; 1424 PersonDO ret = null; 1425 if ( notUsingOId ) { 1426 ret = new PersonDO (); 1427 ret.set_refs(queryRefs); 1428 ret.initFromResultSet( rs ); 1429 } else { 1430 ret = new PersonDO ( rs, queryRefs ); 1431 } 1432 return ret; 1433 } 1434 1435 1436 1455 protected static PersonDO createExisting( ResultSet rs , HashMap queryRefs, DBTransaction dbTrans) 1456 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1457 if ( null == rs ) 1458 return null; 1459 PersonDO ret = null; 1460 if ( notUsingOId ) { 1461 ret = new PersonDO (dbTrans); 1462 ret.set_refs(queryRefs); 1463 ret.initFromResultSet( rs ); 1464 } else { 1465 if(queryRefs==null) 1466 queryRefs = new HashMap (); 1467 String cacheHandle = get_logicalDBName()+"."+rs.getBigDecimal(get_OIdColumnName()); 1468 if(queryRefs.containsKey(cacheHandle)) { 1469 ret = (PersonDO)queryRefs.get(cacheHandle); 1470 if (!ret.isLoaded()) { 1471 ret.set_refs(queryRefs); 1472 ret.initFromResultSet(rs); 1473 } 1474 return ret; 1475 } 1476 ret = new PersonDO ( rs, queryRefs, dbTrans ); 1477 } 1478 return ret; 1479 } 1480 1481 1482 1505 protected static PersonDO createExisting( RDBRow row ) 1506 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1507 if ( null == row ) 1508 return null; 1509 RDBColumnValue pk = null; 1510 try { 1511 pk = row.get( PersonDO.PrimaryKey ); 1512 return createExisting( pk ); 1513 } catch ( Exception e ) { 1514 throw new DataObjectException("Cannot create PersonDO, row does not " + 1515 "contain PersonDO primary key." ); 1516 } 1517 } 1518 1519 1520 1542 protected static PersonDO createExisting( RDBRow row, DBTransaction dbTrans ) 1543 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1544 if ( null == row ) 1545 return null; 1546 RDBColumnValue pk = null; 1547 try { 1548 pk = row.get( PersonDO.PrimaryKey ); 1549 return createExisting( pk, dbTrans ); 1550 } catch ( Exception e ) { 1551 throw new DataObjectException("Cannot create PersonDO, row does not " + 1552 "contain PersonDO primary key." ); 1553 } 1554 } 1555 1556 1557 1580 protected static PersonDO createExisting( RDBColumnValue pk ) 1581 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1582 if ( null == pk ) 1583 return null; 1584 if ( ! pk.equals( PersonDO.PrimaryKey ) ) 1585 throw new DataObjectException("Cannot create PersonDO, " + 1586 "RDBColumnValue is not PersonDO.PrimaryKey." ); 1587 BigDecimal bd = null; 1588 try { 1589 bd = pk.getBigDecimal(); 1590 } catch ( Exception e ) { 1591 throw new DataObjectException("Cannot create PersonDO, bad primary key." ); 1592 } 1593 if ( null == bd ) 1594 return null; 1595 return createExisting( bd ); 1596 } 1597 1598 1599 1600 1622 protected static PersonDO createExisting( RDBColumnValue pk, DBTransaction dbTrans ) 1623 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 1624 if ( null == pk ) 1625 return null; 1626 if ( ! pk.equals( PersonDO.PrimaryKey ) ) 1627 throw new DataObjectException("Cannot create PersonDO, " + 1628 "RDBColumnValue is not PersonDO.PrimaryKey." ); 1629 BigDecimal bd = null; 1630 try { 1631 bd = pk.getBigDecimal(); 1632 } catch ( Exception e ) { 1633 throw new DataObjectException("Cannot create PersonDO, bad primary key." ); 1634 } 1635 if ( null == bd ) 1636 return null; 1637 return createExisting( bd , dbTrans); 1638 } 1639 1640 1641 1657 public static PersonDO createCopy( PersonDataStruct data ) 1658 throws DatabaseManagerException, ObjectIdException { 1659 PersonDO ret = new PersonDO (true); 1660 ret.originalData_set(data); 1661 return ret; 1662 } 1663 1664 1679 public static PersonDO createCopy( PersonDataStruct data, DBTransaction dbTrans ) 1680 throws DatabaseManagerException, ObjectIdException { 1681 PersonDO ret = new PersonDO (true, dbTrans); 1682 ret.originalData_set(data); 1683 ret.markClean(); 1684 return ret; 1685 } 1686 1687 1688 1689 1704 public static PersonDO createCopy( PersonDO orig ) 1705 throws DatabaseManagerException, ObjectIdException { 1706 if (null == orig) 1707 return null; 1708 PersonDO ret = new PersonDO (true); 1709 if (null != orig.originalData_get()) { 1710 ret.originalData_set(orig.originalData_get()); 1711 ret.markClean(); 1712 ret.transaction = orig.transaction; 1713 ret.setPersistent(orig.isPersistent()); 1714 } 1715 return ret; 1716 } 1717 1718 1729 public static PersonDO createCopy( PersonDO orig, DBTransaction dbTrans ) 1730 throws DatabaseManagerException, ObjectIdException { 1731 if (null == orig) 1732 return null; 1733 PersonDO ret = new PersonDO (true, dbTrans); 1734 if (null != orig.originalData_get()) { 1735 ret.originalData_set(orig.originalData_get()); 1736 ret.markClean(); 1737 ret.setPersistent(orig.isPersistent()); 1738 } 1739 return ret; 1740 1741 } 1742 1743 1744 1748 public void reload() { 1749 originalData = data = null; 1750 } 1751 1752 1775 public String getHandle() 1776 throws DatabaseManagerException { 1777 return get_Handle(); 1778 } 1779 1780 1781 1803 public String get_Handle() 1804 throws DatabaseManagerException { 1805 String ret = null; 1806 if ( null == get_OId() ) 1807 throw new DatabaseManagerException( "ID not set " ); 1808 ret = get_OId().toString(); 1809 return ret; 1810 } 1811 1812 1819 public String get_CacheHandle() 1820 throws DatabaseManagerException { 1821 String ret = get_OriginDatabase() + "." + get_Handle(); 1822 return ret; 1823 } 1824 1825 1832 public static GenericDO createDO(ObjectId oid) throws java.sql.SQLException , com.lutris.appserver.server.sql.ObjectIdException, com.lutris.dods.builder.generator.query.DataObjectException, com.lutris.appserver.server.sql.DatabaseManagerException{ 1833 return new PersonDO(oid); 1834 } 1835 1836 1837 1844 public static GenericDO createDO(ObjectId oid, DBTransaction dbTrans) throws java.sql.SQLException , com.lutris.appserver.server.sql.ObjectIdException, com.lutris.dods.builder.generator.query.DataObjectException, com.lutris.appserver.server.sql.DatabaseManagerException{ 1845 return new PersonDO(oid, dbTrans); 1846 } 1847 1848 1849 1860 public boolean hasMatchingHandle( String handle ) { 1861 boolean ret = false; 1862 if ( null == get_OId() ) 1863 return false; 1864 else { 1865 String thisHnadle = get_OId().toString(); 1866 ret = thisHnadle.equals( handle ); 1867 } 1868 return ret; 1869 } 1870 1871 1872 1885 public PersonDO findTransactionCachedObjectByHandle( String cacheHandle ){ 1886 if ( null == cacheHandle ) 1887 return null; 1888 if(get_transaction()!=null && _tr_(get_transaction()).getTransactionCache()!= null) 1889 return (PersonDO)_tr_(get_transaction()).getTransactionCache().getDOByHandle( cacheHandle ); 1890 else 1891 return null; 1892 } 1893 1894 1907 public static PersonDataStruct findCachedObjectByHandle( String cacheHandle ){ 1908 if ( null == cacheHandle ) 1909 return null; 1910 return ( PersonDataStruct ) cache.getDataStructByHandle( cacheHandle ); 1911 } 1912 1913 1914 1915 1922 protected void makeIdentical( PersonDO orig ) { 1923 super.makeIdentical(orig); 1924 originalData = orig.originalData; 1925 data = orig.data; 1926 } 1927 1928 1932 public int getVersion() { 1933 return get_Version(); 1934 } 1935 1936 1941 public int get_Version() { 1942 return (null != data)?data.get_Version():super.get_Version(); 1943 } 1944 1945 1951 public int getNewVersion() { 1952 if (null != data) 1953 return data.get_Version(); 1954 else 1955 return super.get_Version(); 1956 } 1957 1958 1963 public int get_NewVersion() { 1964 if (null != data) 1965 return data.get_Version(); 1966 else 1967 return super.get_Version(); 1968 } 1969 1970 1974 public void setVersion(int _ver) { 1975 set_Version(_ver); 1976 } 1977 1978 1982 public void set_Version(int _ver) { 1983 if(_ver < get_Version()) { 1984 new Throwable ("WOW, ("+get_OId()+") oldVer:"+get_Version()+", new one is "+_ver).printStackTrace(); 1985 } else if (null != data) 1986 data.set_Version(_ver); 1987 else 1988 super.set_Version(_ver); 1989 } 1990 1991 1995 public void setNewVersion(int _ver){} 1996 1997 2004 public void makeReadOnly() { 2005 if (null != data) { 2006 try{ 2007 checkDup(); 2008 }catch(Exception ex) { 2009 String trace=""; 2010 StackTraceElement [] traceElements= (new Throwable ()).getStackTrace(); 2011 for(int i=0; i < traceElements.length; i++) 2012 trace+=traceElements[i].toString()+"\n"; 2013 DODS.getLogChannel().write(Logger.DEBUG, " MakeReadOnly failed: Database: "+get_OriginDatabase()+" PersonDO class, oid: "+get_OId()+", version: "+get_Version()+" \n"+trace); 2014 } 2015 } 2016 data.readOnly = true; 2017 } 2018 2019 2024 public void makeReadWrite() { 2025 if (null != data) { 2026 try{ 2027 checkDup(); 2028 }catch(Exception ex) { 2029 String trace=""; 2030 StackTraceElement [] traceElements= (new Throwable ()).getStackTrace(); 2031 for(int i=0; i < traceElements.length; i++) 2032 trace+=traceElements[i].toString()+"\n"; 2033 DODS.getLogChannel().write(Logger.DEBUG, " MakeReadWrite failed: Database: "+get_OriginDatabase()+" PersonDO class, oid: "+get_OId()+", version: "+get_Version()+" \n"+trace); 2034 } 2035 } 2036 data.readOnly = false; 2037 } 2038 2040 2042 2046 private void set_refs(HashMap queryRefs) { 2047 refs = queryRefs; 2048 } 2049 2050 private Object getRefs(String key) { 2051 if(get_transaction()!=null && _tr_(get_transaction()).getTransactionCache()!= null) { 2052 return _tr_(get_transaction()).getTransactionCache().getDOByHandle(key); 2053 } else if (null == refs) { 2054 refs = new HashMap (); 2055 return null; 2056 } else { 2057 return refs.get(key); 2058 } 2059 } 2060 2061 private void addRefs(String key, Object newRefs) { 2062 if (null == refs) { 2063 refs = new HashMap (); 2064 } 2065 refs.put(key, newRefs); 2066 } 2067 2068 2072 private HashMap get_refs() { 2073 return refs; 2074 } 2075 2076 2080 public ObjectId getOId() { 2081 return get_OId(); 2082 } 2083 2084 2088 public ObjectId get_OId() { 2089 return get_DataStruct().get_OId(); 2090 } 2091 2092 2096 protected void setOId(ObjectId _oId) { 2097 set_OId(_oId); 2098 } 2099 2100 2104 protected void set_OId(ObjectId _oId) { 2105 if (get_DataStruct() == null) 2106 originalData = new PersonDataStruct(); 2107 get_DataStruct().set_OId(_oId); 2108 } 2109 2110 2119 public synchronized Object cloneUnique() 2120 throws DatabaseManagerException, ObjectIdException { 2121 2122 PersonDO _clone = createVirgin(get_transaction()); 2123 2124 try { 2125 PersonDataStruct toClone = (null != get_Data()) 2126 ?(PersonDataStruct)get_Data() 2127 :(PersonDataStruct)originalData_get(); 2128 if (null != toClone) { 2129 _clone.set_Data(toClone.duplicate()); 2130 ((PersonDataStruct)_clone.get_Data()).set_OId(((PersonDataStruct)_clone.originalData_get()).get_OId()); 2131 ((PersonDataStruct)_clone.get_Data()).set_Version(0); 2132 changedFlags_set(true); 2133 } 2134 } catch (Exception e) { 2135 String trace=""; 2136 StackTraceElement [] traceElements= (new Throwable ()).getStackTrace(); 2137 for(int i=0; i < traceElements.length; i++) 2138 trace+=traceElements[i].toString()+"\n"; 2139 DODS.getLogChannel().write(Logger.DEBUG," cloneUnique failed: Database: "+get_OriginDatabase()+" PersonDO class, oid: "+get_OId()+", version: "+get_Version()+" \n"+trace); 2140 } 2141 return _clone; 2142 } 2143 2144 protected boolean deleted; 2145 2146 2149 public boolean isDeleted() { 2150 return deleted; 2151 } 2152 2153 2156 public void setDeleted(boolean flag) { 2157 deleted = flag; 2158 } 2159 2160 2161 2166 public void finalizeInsert(boolean success) { 2167 if (!success && !isPersistent()) 2169 setDeleted(true); 2170 super.finalizeInsert(success); 2171 if (success) 2172 syncStructs(true); 2173 } 2174 2175 2180 public void finalizeUpdate(boolean success) { 2181 super.finalizeUpdate(success); 2182 if (success) 2183 syncStructs(true); 2184 } 2185 2186 2193 public void finalizeDelete(boolean success) { 2194 super.finalizeDelete(success); 2195 if (success) { 2196 deleteFromCache(); 2197 } 2198 } 2199 2200 2203 private synchronized void syncStructs(boolean _updateCache) { 2204 if (null != data) 2205 originalData = data; 2206 data = null; 2207 changedFlags_set(false); 2208 ((PersonDataStruct)originalData).readOnly = true; 2209 if (_updateCache) 2210 updateCache(); 2211 } 2212 2213 2216 public boolean isVirgin() { 2217 return !isPersistent(); 2218 } 2219 2220 2223 public void makeVisible () { 2224 try { 2225 ((QueryCacheImpl)cache).makeVisible(get_CacheHandle()); 2226 } catch (DatabaseManagerException dme) { 2227 System.err.println("makeVisible for "+super.toString()+"failed"); 2228 } 2229 } 2230 2231 2234 public void makeInvisible () { 2235 try { 2236 ((QueryCacheImpl)cache).makeInvisible(get_CacheHandle()); 2237 } catch (DatabaseManagerException dme) { 2238 System.err.println("makeInvisible for "+super.toString()+"failed"); 2239 } 2240 } 2241 2242 2249 public synchronized void executeInsert(DBConnection conn) 2250 throws SQLException, DBRowUpdateException { 2251 if (dirty) { 2252 super.executeInsert(conn); 2253 changedFlags_set(false); 2254 } 2255 } 2256 2257 2258 2260 protected boolean isAutoSave() 2261 { 2262 boolean flag = false; 2263 try { 2264 flag = ((StandardLogicalDatabase)(DODS.getDatabaseManager().findLogicalDatabase(get_OriginDatabase()))).getDatabaseConfiguration().getAutoSave(); 2265 } catch (Exception ex) { 2266 } 2267 return flag; 2268 } 2269 2270 2272 protected boolean isAutoSaveCreateVirgin() 2273 { 2274 boolean flag = false; 2275 try { 2276 flag = ((StandardLogicalDatabase)(DODS.getDatabaseManager().findLogicalDatabase(get_OriginDatabase()))).getDatabaseConfiguration().getAutoSaveCreateVirgin(); 2277 } catch (Exception ex) { 2278 } 2279 return flag; 2280 } 2281 2282 2284 protected boolean isTransactionCheck() 2285 { 2286 boolean flag = false; 2287 try { 2288 flag = ((StandardLogicalDatabase)(DODS.getDatabaseManager().findLogicalDatabase(get_OriginDatabase()))).getDatabaseConfiguration().getTransactionCheck(); 2289 } catch (Exception ex) { 2290 } 2291 return flag; 2292 } 2293 2294 2296 protected boolean isTransactionCaches() 2297 { 2298 boolean flag = false; 2299 try { 2300 flag = ((StandardLogicalDatabase)(DODS.getDatabaseManager().findLogicalDatabase(get_OriginDatabase()))).getDatabaseConfiguration().getTransactionCaches(); 2301 } catch (Exception ex) { 2302 } 2303 return flag; 2304 } 2305 2306 2308 protected boolean isDeleteCheckVersion() 2309 { 2310 boolean flag = false; 2311 try { 2312 flag = ((StandardLogicalDatabase)(DODS.getDatabaseManager().findLogicalDatabase(this.get_OriginDatabase()))).getDatabaseConfiguration().getDeleteCheckVersion(); 2313 } catch (Exception ex) { 2314 } 2315 return flag; 2316 } 2317 2318 2319 2321 protected static boolean isAllReadOnly() 2322 { 2323 boolean flag = false; 2324 try { 2325 flag = ((StandardLogicalDatabase)(DODS.getDatabaseManager().findLogicalDatabase(get_logicalDBName()))).getDatabaseConfiguration().isAllReadOnly(); 2326 } catch (Exception ex) { 2327 } 2328 return flag; 2329 } 2330 2331 2332 2333 2336 public void undo()throws com.lutris.dods.builder.generator.query.DataObjectException 2337 { 2338 try{ 2339 if(null != transaction){ 2340 if((data!=null) || (data==null && isDeleted())){ 2341 int tempVersion=get_Version(); 2342 if(isDeleted() && !isDeletedFromDatabase){ 2343 unDelete(transaction); 2344 }else if (isDeleted() && isDeletedFromDatabase){ 2345 data =((PersonDataStruct)originalData).duplicate(); 2346 set_Version(tempVersion); 2347 persistent=false; 2348 deleted=false; 2349 isDeletedFromDatabase=false; 2350 if (isAutoSave()) { 2351 save(transaction,false); 2352 } 2353 }else{ 2354 data =((PersonDataStruct)originalData).duplicate(); 2355 set_Version(tempVersion); 2356 if ( isAutoSave()) { 2357 save(transaction,false); 2358 } 2359 } 2360 } 2361 }else{ 2362 throw new DataObjectException("Error during Undo operation"); 2363 } 2364 }catch(Exception ex){ 2365 throw new DataObjectException("Error during Undo operation"); 2366 } 2367 2368 } 2369 2370 2371 2372 2373 2374 2375 2377 2381 static public final RDBColumn FirstName = new RDBColumn( table, "firstName", true); 2382 2383 private boolean changedFirstName = false; 2384 2385 2388 static public final int COLUMN_FIRSTNAME = 0; 2389 static public final int firstName_MaxLength = 32; 2390 2391 2399 public String getFirstName () 2400 throws DataObjectException { 2401 checkLoad(); 2402 2403 return get_DataStruct().getFirstName(); 2404 } 2405 2406 2407 2415 public String originalData_getFirstName () 2416 throws DataObjectException { 2417 checkLoad(); 2418 return ((PersonDataStruct)originalData_get()).getFirstName(); 2419 } 2420 2421 2422 2423 2431 public void setFirstName ( String firstName ) 2432 throws DataObjectException { 2433 _setFirstName ( firstName ); 2434 } 2435 2436 2446 protected void _setFirstName ( String firstName ) 2447 throws DataObjectException { 2448 checkLoad(); 2449 2450 try { 2451 checkDup(); 2452 } catch (Exception e) { 2453 throw new DataObjectException ("Coudn't duplicate DataStruct:", e); 2454 } 2455 if (data.isEmpty) 2456 data.isEmpty = false; 2457 data.setFirstName(markNewValue(get_DataStruct().getFirstName(), firstName, 0, firstName_MaxLength, false)); 2458 changedFirstName = colChanged; 2459 if (changedFirstName) { 2460 if (autoSaveAllowed&&isAutoSave()&&null != transaction) { 2461 try { 2462 save(transaction,false); 2463 } catch (Exception ex) { 2464 throw new DataObjectException("Error during transaction's writting data into database",ex); 2465 } 2466 } 2467 } 2468 } 2469 2470 2471 2472 2473 2475 2479 static public final RDBColumn LastName = new RDBColumn( table, "lastName", true); 2480 2481 private boolean changedLastName = false; 2482 2483 2486 static public final int COLUMN_LASTNAME = 1; 2487 static public final int lastName_MaxLength = 32; 2488 2489 2497 public String getLastName () 2498 throws DataObjectException { 2499 checkLoad(); 2500 2501 return get_DataStruct().getLastName(); 2502 } 2503 2504 2505 2513 public String originalData_getLastName () 2514 throws DataObjectException { 2515 checkLoad(); 2516 return ((PersonDataStruct)originalData_get()).getLastName(); 2517 } 2518 2519 2520 2521 2529 public void setLastName ( String lastName ) 2530 throws DataObjectException { 2531 _setLastName ( lastName ); 2532 } 2533 2534 2544 protected void _setLastName ( String lastName ) 2545 throws DataObjectException { 2546 checkLoad(); 2547 2548 try { 2549 checkDup(); 2550 } catch (Exception e) { 2551 throw new DataObjectException ("Coudn't duplicate DataStruct:", e); 2552 } 2553 if (data.isEmpty) 2554 data.isEmpty = false; 2555 data.setLastName(markNewValue(get_DataStruct().getLastName(), lastName, 0, lastName_MaxLength, false)); 2556 changedLastName = colChanged; 2557 if (changedLastName) { 2558 if (autoSaveAllowed&&isAutoSave()&&null != transaction) { 2559 try { 2560 save(transaction,false); 2561 } catch (Exception ex) { 2562 throw new DataObjectException("Error during transaction's writting data into database",ex); 2563 } 2564 } 2565 } 2566 } 2567 2568 2569 2570 2571 2573 2577 static public final RDBColumn PhoneNumber = new RDBColumn( table, "phoneNumber", true); 2578 2579 private boolean changedPhoneNumber = false; 2580 2581 2584 static public final int COLUMN_PHONENUMBER = 2; 2585 static public final int phoneNumber_MaxLength = 32; 2586 2587 2595 public String getPhoneNumber () 2596 throws DataObjectException { 2597 checkLoad(); 2598 2599 return get_DataStruct().getPhoneNumber(); 2600 } 2601 2602 2603 2611 public String originalData_getPhoneNumber () 2612 throws DataObjectException { 2613 checkLoad(); 2614 return ((PersonDataStruct)originalData_get()).getPhoneNumber(); 2615 } 2616 2617 2618 2619 2627 public void setPhoneNumber ( String phoneNumber ) 2628 throws DataObjectException { 2629 _setPhoneNumber ( phoneNumber ); 2630 } 2631 2632 2642 protected void _setPhoneNumber ( String phoneNumber ) 2643 throws DataObjectException { 2644 checkLoad(); 2645 2646 try { 2647 checkDup(); 2648 } catch (Exception e) { 2649 throw new DataObjectException ("Coudn't duplicate DataStruct:", e); 2650 } 2651 if (data.isEmpty) 2652 data.isEmpty = false; 2653 data.setPhoneNumber(markNewValue(get_DataStruct().getPhoneNumber(), phoneNumber, 0, phoneNumber_MaxLength, false)); 2654 changedPhoneNumber = colChanged; 2655 if (changedPhoneNumber) { 2656 if (autoSaveAllowed&&isAutoSave()&&null != transaction) { 2657 try { 2658 save(transaction,false); 2659 } catch (Exception ex) { 2660 throw new DataObjectException("Error during transaction's writting data into database",ex); 2661 } 2662 } 2663 } 2664 } 2665 2666 2667 2668 2675 public boolean compareCond(Condition cond) { 2676 try { 2677 switch(cond.getColumnIndex()) { 2678 case COLUMN_FIRSTNAME: 2679 return QueryBuilder.compare(getFirstName(),cond.getValue(),cond.getOperator()); 2680 case COLUMN_LASTNAME: 2681 return QueryBuilder.compare(getLastName(),cond.getValue(),cond.getOperator()); 2682 case COLUMN_PHONENUMBER: 2683 return QueryBuilder.compare(getPhoneNumber(),cond.getValue(),cond.getOperator()); 2684 } 2685 } catch (Exception e) { 2686 } 2687 return false; 2688 } 2689 2690 static { 2691 } 2692 2693 2698 static private String logicalDbName = null; 2699 2700 2711 static public synchronized void setLogicalDBName( String logicalDbNameInConfFile ) { 2712 if ( null != logicalDbNameInConfFile && 0 != logicalDbNameInConfFile.length() ) 2713 logicalDbName = logicalDbNameInConfFile; 2714 else 2715 logicalDbName = DODS.getDatabaseManager().getDefaultDB(); 2716 } 2717 2718 2725 static public synchronized String get_logicalDBName() { 2726 if (logicalDbName == null) 2727 logicalDbName = DODS.getDatabaseManager().getDefaultDB(); 2728 return logicalDbName; 2729 } 2730 2731 2745 static public DBTransaction createTransaction() 2746 throws DatabaseManagerException, SQLException { 2747 DBTransaction ret; 2748 try { 2749 ret = DODS.getDatabaseManager().createTransaction(get_logicalDBName()); 2750 ret.setDatabaseName(get_logicalDBName()); 2751 return ret; 2752 } catch ( DatabaseManagerException e ) { 2753 String err = ""; 2754 if ( null != get_logicalDBName() ) 2755 err = "ERROR: Could not create a DBTransaction. " + 2756 "PersonDO.logicalDbName='" + get_logicalDBName() + "'. "+ 2757 "The application .conf file must list this name in " + 2758 "DatabaseManager.Databases[], and there must be " + 2759 "DatabaseManager.DB." + get_logicalDBName() + " settings."; 2760 throw new DatabaseManagerException( err, e ); 2761 } 2762 } 2763 2764 2765 2780 static public DBQuery createQuery() 2781 throws DatabaseManagerException, SQLException { 2782 try { 2783 return DODS.getDatabaseManager().createQuery(get_logicalDBName()); 2784 } catch ( DatabaseManagerException e ) { 2785 String err = ""; 2786 if ( null != get_logicalDBName() ) 2787 err = "ERROR: Could not create a DBQuery. " + 2788 "PersonDO.logicalDBName='" + get_logicalDBName() + "'. "+ 2789 "The application .conf file must list this name in " + 2790 "DatabaseManager.Databases[], and there must be " + 2791 "DatabaseManager.DB." + get_logicalDBName() + " settings."; 2792 throw new DatabaseManagerException( err, e ); 2793 } 2794 } 2795 2796 2797 2811 static public DBQuery createQuery(DBTransaction trans) 2812 throws DatabaseManagerException, SQLException { 2813 2814 try { 2815 return (null==trans)?createQuery():trans.createQuery(); 2816 } catch ( DatabaseManagerException e ) { 2817 String err = ""; 2818 if ( null != get_logicalDBName() ) 2819 err = "ERROR: Could not create a DBQuery. " + 2820 "PersonDO.logicalDBName='" + get_logicalDBName() + "'. "+ 2821 "The application .conf file must list this name in " + 2822 "DatabaseManager.Databases[], and there must be " + 2823 "DatabaseManager.DB." + get_logicalDBName() + " settings."; 2824 throw new DatabaseManagerException( err, e ); 2825 } 2826 } 2827 2828 2829 2844 protected PersonDO(ResultSet rs) 2845 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 2846 super(rs); 2847 initFromResultSet( rs ); 2848 originDatabase = get_logicalDBName(); 2849 get_DataStruct().set_Database(originDatabase); 2850 set_OId(new ObjectId(rs.getBigDecimal(get_OIdColumnName()))); 2851 if ( versioning ) 2852 set_Version(rs.getInt(get_versionColumnName())); 2853 if(isTransactionCheck()) { 2854 String trace=""; 2855 StackTraceElement [] traceElements= (new Throwable ()).getStackTrace(); 2856 for(int i=0; i < traceElements.length; i++) 2857 trace+=traceElements[i].toString()+"\n"; 2858 DODS.getLogChannel().write(Logger.WARNING, "DO without transaction context is created : Database: "+get_OriginDatabase()+" PersonDO class, oid: "+get_OId()+", version: "+get_Version()+" \n"+trace); 2859 } 2860 } 2861 2862 2877 protected PersonDO(ResultSet rs, HashMap queryRefs) 2878 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 2879 this(rs,queryRefs,null); 2880 } 2881 2882 2897 protected PersonDO(ResultSet rs, HashMap queryRefs, DBTransaction dbTrans) 2898 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 2899 super(rs); 2900 set_refs(queryRefs); 2901 setTransaction(dbTrans); 2902 initFromResultSet( rs ); 2903 if(dbTrans!=null) 2904 originDatabase = dbTrans.getDatabaseName(); 2905 if(originDatabase==null) 2906 originDatabase = get_logicalDBName(); 2907 get_DataStruct().set_Database(originDatabase); 2908 addToTransactionCache(); 2909 if(dbTrans!=null) 2910 dbTrans.lockDO(this); 2911 } 2912 2913 2928 protected PersonDO(ResultSet rs, DBTransaction dbTrans) 2929 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 2930 this(rs, null, dbTrans); 2931 } 2932 2933 2934 2935 2936 2937 2940 private boolean autoSaveAllowed = true; 2941 2942 2949 private void initFromResultSet( ResultSet rs ) 2950 throws SQLException, ObjectIdException, DataObjectException, DatabaseManagerException { 2951 autoSaveAllowed = false; 2952 if (null == get_DataStruct()) 2955 originalData = new PersonDataStruct (); 2956 get_DataStruct().isEmpty = false; 2957 2961 setFirstName( rs.getString( "firstName" ) ); 2962 2963 setLastName( rs.getString( "lastName" ) ); 2964 2965 setPhoneNumber( rs.getString( "phoneNumber" ) ); 2966 2967 get_DataStruct().isEmpty = false; 2968 if ( versioning ) 2969 set_Version(rs.getInt(get_versionColumnName())); 2970 setPersistent(true); 2971 markClean(); 2972 syncStructs(false); 2973 autoSaveAllowed = true; 2975 } 2976 2977 2978 private int[] param = null; 2979 private boolean isDeletedFromDatabase = false; 2980 2990 public PreparedStatement getInsertStatement(DBConnection conn) 2991 throws SQLException { 2992 ObjectId oid; 2993 2994 if (isDeletedFromDatabase) 2995 throw new SQLException("Object "+get_OId()+" is already deleted"); 2996 2997 PreparedStatement stmt = conn.prepareStatement( 2998 "insert into person ( firstName, lastName, phoneNumber, " + get_OIdColumnName() + ", " + get_versionColumnName() + " )" + 2999 "values ( ?, ?, ?, ?, ? )" 3000 ); 3001 3002 param = new int[1]; param[0] = 1; 3003 try { 3008 setPrepStmtParam_String ( stmt, param, getFirstName()); 3009 setPrepStmtParam_String ( stmt, param, getLastName()); 3010 setPrepStmtParam_String ( stmt, param, getPhoneNumber()); 3011 3012 3013 setPrepStmtParam_BigDecimal( stmt, param, get_OId().toBigDecimal() ); 3014 setPrepStmtParam_int( stmt, param, get_NewVersion() ); 3015 3016 } catch ( Exception e ) { 3017 throw new SQLException( "Data Object error: " + e.getMessage() ); 3018 } 3019 statistics.incrementInsertNum(); 3020 return stmt; 3021 } 3022 3023 3024 3027 private boolean _lockDO = false; 3028 3029 3035 public void doCheck(boolean value) { 3036 _lockDO = value; 3037 } 3038 3039 3043 public void doLock() throws SQLException { 3044 if (null!=transaction) { 3045 boolean _ol = _lockDO; 3046 _lockDO = true; 3047 transaction.lockDO(this); 3048 _lockDO = _ol; 3049 } 3050 } 3051 3052 private boolean incrementVersionToo = true; 3053 3057 public void doTouch() 3058 throws SQLException, DatabaseManagerException, 3059 com.lutris.appserver.server.sql.ObjectIdException, DataObjectException { 3060 if (null!=transaction) { 3061 checkLoad(); 3062 checkDup(); 3063 markNewValue(); 3064 incrementVersionToo = true; 3065 boolean _ol = _lockDO; 3066 _lockDO = true; 3067 transaction.lockDO(this); 3068 _lockDO = _ol; 3069 } 3070 } 3071 3072 3080 public void executeLockingStatement(DBConnection conn) throws SQLException { 3081 if (!_lockDO) 3082 return; 3083 StringBuffer updateStmt = new StringBuffer (); 3084 PreparedStatement stmt = null; 3085 if (isDeletedFromDatabase) 3086 throw new SQLException("person (" 3087 +get_OId()+") is already deleted, " 3088 +"cannot lock it."); 3089 3090 param = new int[1]; param[0] = 1; 3091 try { 3092 updateStmt.append("Update person set "); 3093 updateStmt.append(get_versionColumnName()).append(" = ? "); 3094 updateStmt.append(" where " + get_OIdColumnName() + " = ? and " + get_versionColumnName() + " = ?"); 3095 3096 stmt = conn.prepareStatement(updateStmt.toString()); 3097 setPrepStmtParam_int(stmt, param, get_Version()+(incrementVersionToo?1:0)); 3098 setPrepStmtParam_BigDecimal( stmt, param, get_OId().toBigDecimal() ); 3099 setPrepStmtParam_int(stmt, param, get_Version()); 3100 if (null != stmt) { 3101 conn.executeUpdate(stmt, "execute update"); 3102 if (incrementVersionToo) { 3103 set_Version(get_Version()+1); 3104 } 3105 } 3106 } catch ( Exception e ) { 3107 e.printStackTrace(); 3108 throw new SQLException( "Data Object error: " + e.getMessage() ); 3109 } 3110 } 3111 3112 3113 3123 public PreparedStatement getUpdateStatement(DBConnection conn) 3124 throws SQLException { 3125 StringBuffer updateStmt = new StringBuffer (); 3126 PreparedStatement stmt; 3127 3128 if (isDeletedFromDatabase) 3129 throw new SQLException("Object "+get_OId()+" is already deleted"); 3130 3131 data.set_Version(get_Version()+1); 3132 param = new int[1]; param[0] = 1; 3133 try { 3134 updateStmt.append("Update person set "); 3135 updateStmt.append(get_versionColumnName()).append(" = ? "); 3136 3137 if (changedFirstName) 3138 updateStmt.append(", FirstName = ? "); 3139 if (changedLastName) 3140 updateStmt.append(", LastName = ? "); 3141 if (changedPhoneNumber) 3142 updateStmt.append(", PhoneNumber = ? "); 3143 updateStmt.append(" where " + get_OIdColumnName() + " = ? and " + get_versionColumnName() + " = ?"); 3144 3145 stmt = conn.prepareStatement(updateStmt.toString()); 3146 setPrepStmtParam_int(stmt, param, get_Version()); 3147 3148 if (changedFirstName) { 3149 setPrepStmtParam_String( stmt, param, getFirstName()); 3150 changedFirstName = false; 3151 } 3152 if (changedLastName) { 3153 setPrepStmtParam_String( stmt, param, getLastName()); 3154 changedLastName = false; 3155 } 3156 if (changedPhoneNumber) { 3157 setPrepStmtParam_String( stmt, param, getPhoneNumber()); 3158 changedPhoneNumber = false; 3159 } 3160 setPrepStmtParam_BigDecimal( stmt, param, get_OId().toBigDecimal() ); 3161 setPrepStmtParam_int(stmt, param, get_Version() - 1); 3162 } catch ( Exception e ) { 3163 e.printStackTrace(); 3164 throw new SQLException( "Data Object error: " + e.getMessage() ); 3165 } 3166 statistics.incrementUpdateNum(); 3167 return stmt; 3168 } 3169 3170 3180 public PreparedStatement getDeleteStatement(DBConnection conn) 3181 throws SQLException { 3182 String sql=""; 3183 if(isDeleteCheckVersion()) 3184 sql = 3185 "delete from person \n" + 3186 "where " + get_OIdColumnName() + " = ? and " + get_versionColumnName() + " = ?"; 3187 else 3188 sql = 3189 "delete from person \n" + 3190 "where " + get_OIdColumnName() + " = ?"; 3191 PreparedStatement stmt = conn.prepareStatement(sql); 3192 stmt.setBigDecimal(1, get_OId().toBigDecimal()); 3193 if(isDeleteCheckVersion()) { 3194 stmt.setInt(2, get_Version()); 3195 } 3196 statistics.incrementDeleteNum(); 3197 isDeletedFromDatabase = true; 3198 return stmt; 3199 } 3200 3201 3202 3203 3209 public String toString(){ 3210 return toString( 1 ); 3211 } 3212 3213 public String toString( int indentCount ){ 3214 String indent = ""; 3215 for ( int i = 0; i < indentCount; i++ ) { 3216 indent += ". "; 3217 } 3218 StringBuffer sb = new StringBuffer (); 3219 sb.append(indent + "PersonDO:"); 3220 3221 ObjectId oid = get_OId(); 3222 String id = "virgin"; 3223 if ( null != oid ) 3224 id = oid.toString(); 3225 sb.append(" OID=" + id + ",VERSION=" + get_Version()); 3226 if (isLoaded()) { 3227 sb.append("\n" + indent + "firstName=" + get_DataStruct().getFirstName()); 3228 3229 sb.append("\n" + indent + "lastName=" + get_DataStruct().getLastName()); 3230 3231 sb.append("\n" + indent + "phoneNumber=" + get_DataStruct().getPhoneNumber()); 3232; 3233 sb.append("\n" + indent + "SUPER=" + super.toString( indentCount )); 3234 } 3235 return sb.toString(); 3236 } 3237 3238 3244 protected void okToCommit() 3245 throws RefAssertionException { 3246 } 3247 3248 3254 protected void okToDelete() throws RefAssertionException { } 3255 3256 3265 public void commit() 3266 throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException, DBRowUpdateException, QueryException { 3267 commit(null); 3268 } 3269 3270 3284 public void commit(DBTransaction dbt) 3285 throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException, DBRowUpdateException, QueryException { 3286 if (cache.getTableConfiguration().isReadOnly()) 3287 throw new AssertionDataObjectException("PersonDO's cache is read-only. Therefore, DML opertions are not allowed."); 3288 try { 3290 DBTransaction dbtlocal = dbt; 3291 boolean needToCommit = false; 3292 if (dbtlocal == null) { 3293 if( get_transaction()==null) { 3294 dbtlocal = DODS.getDatabaseManager().createTransaction(get_OriginDatabase()); 3295 dbtlocal.setDatabaseName(get_OriginDatabase()); 3296 needToCommit = true; 3297 }else 3298 dbtlocal=transaction; 3299 } else { 3300 if(get_transaction()!=null) { 3301 if(!get_transaction().equals(dbt)) 3302 throw new DatabaseManagerException("DO doesn't belong this transaction."); 3303 } 3304 } 3305 modifyDO( dbtlocal, false ); 3306 if (needToCommit) { 3307 dbtlocal.commit(); 3308 dbtlocal.release(); 3309 } 3310 } catch (DataObjectException e) { 3311 modifyDO( dbt, false ); 3312 } 3313 } 3315 3316 3325 public void save() 3326 throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException, DBRowUpdateException, QueryException { 3327 save(null,true); 3328 } 3329 3330 3341 public void save(boolean references) 3342 throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException, DBRowUpdateException, QueryException { 3343 save(null,references); 3344 } 3345 3346 3360 public void save(DBTransaction dbt) 3361 throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException, DBRowUpdateException, QueryException { 3362 save(dbt, true); 3363 } 3364 3365 3380 public void save(DBTransaction dbt, boolean references) 3381 throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException, DBRowUpdateException, QueryException { 3382 if (cache.getTableConfiguration().isReadOnly()) { 3383 throw new AssertionDataObjectException("PersonDO's cache is read-only. Therefore, DML opertions are not allowed."); 3384 } 3385 try { 3388 DBTransaction dbtlocal = dbt; 3391 boolean needToCommit = false; 3392 3393 if (dbtlocal == null) { 3394 if( get_transaction()==null) { 3395 dbtlocal = DODS.getDatabaseManager().createTransaction(get_OriginDatabase()); 3396 dbtlocal.setDatabaseName(get_OriginDatabase()); 3397 needToCommit = true; 3398 } 3399 else 3400 dbtlocal=transaction; 3401 } else { 3402 if(get_transaction()!=null) { 3403 if(!get_transaction().equals(dbt)) 3404 throw new DatabaseManagerException("DO doesn't belong this transaction."); 3405 } 3406 } 3407 3408 modifyDO( dbtlocal, false, references ); 3410 if (needToCommit) { 3411 dbtlocal.commit(); 3412 dbtlocal.release(); 3413 } 3414 } catch (DataObjectException e) { 3415 modifyDO( dbt, false ); 3416 } 3417 } 3419 3420 3427 public void delete() 3428 throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException, DBRowUpdateException, QueryException { 3429 delete((DBTransaction)null); 3430 } 3431 3432 3433 3440 public void unDelete() 3441 throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException, DBRowUpdateException, QueryException { 3442 unDelete((DBTransaction)null); 3443 } 3444 3445 3457 public void delete(DBTransaction dbt) 3458 throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException, DBRowUpdateException, QueryException { 3459 if (cache.getTableConfiguration().isReadOnly()) 3460 throw new AssertionDataObjectException("PersonDO's cache is read-only. Therefore, DML opertions are not allowed."); 3461 try { 3464 DBTransaction dbtlocal = dbt; 3465 boolean needToCommit = false; 3466 if (dbtlocal == null) { 3467 if(get_transaction()==null) { 3468 dbtlocal = DODS.getDatabaseManager().createTransaction(get_OriginDatabase()); 3469 dbtlocal.setDatabaseName(get_OriginDatabase()); 3470 needToCommit = true; 3471 } 3472 else 3473 dbtlocal=transaction; 3474 } else { 3475 if(get_transaction()!=null) { 3476 if(!get_transaction().equals(dbtlocal)) 3477 throw new DatabaseManagerException("DO doesn't belong this transaction."); 3478 } 3479 } 3480 modifyDO( dbtlocal, true ); 3482 if (needToCommit) { 3484 dbtlocal.commit(); 3485 dbtlocal.release(); 3486 } 3487 } catch (DataObjectException e) { 3488 modifyDO( dbt, true ); 3489 } 3490 } 3491 3492 3502 public void unDelete(DBTransaction dbt) 3503 throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException, DBRowUpdateException, QueryException { 3504 3505 if (cache.getTableConfiguration().isReadOnly()) 3506 throw new AssertionDataObjectException("PersonDO's cache is read-only. Therefore, DML opertions are not allowed."); 3507 try { 3510 DBTransaction dbtlocal = dbt; 3511 boolean needToCommit = false; 3512 if (dbtlocal == null) { 3513 if(get_transaction()==null) { 3514 dbtlocal = DODS.getDatabaseManager().createTransaction(get_OriginDatabase()); 3515 dbtlocal.setDatabaseName(get_OriginDatabase()); 3516 needToCommit = true; 3517 } 3518 else 3519 dbtlocal=transaction; 3520 } else { 3521 if(get_transaction()!=null) { 3522 if(!get_transaction().equals(dbtlocal)) 3523 throw new DatabaseManagerException("DO didn't belong this transaction."); 3524 } 3525 } 3526 setDeleted(false); 3527 isDeletedFromDatabase = false; 3528 persistent=false; 3529 modifyDO( dbtlocal, false ); 3530 if (needToCommit) { 3531 dbtlocal.commit(); 3532 dbtlocal.release(); 3533 } 3534 isDeletedFromDatabase = false; 3535 } catch (DataObjectException e) { 3536 persistent=true; 3537 setDeleted(true); 3538 } 3539 } 3540 3541 3542 3559 protected void modifyDO( DBTransaction dbt, boolean delete) 3560 throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException, DBRowUpdateException, QueryException { 3561 modifyDO( dbt, delete, true); 3562 } 3563 3564 3582 protected void modifyDO( DBTransaction dbt, boolean delete, boolean references ) 3583 throws SQLException, DatabaseManagerException, DataObjectException, RefAssertionException, DBRowUpdateException, QueryException { 3584 if ( delete ) 3585 okToDelete(); 3586 else 3587 okToCommit(); 3588 boolean ownTransaction = false; 3589 try { 3590 if ( null == dbt ) { 3591 if( null==get_transaction()) { 3592 DatabaseManager dbm = DODS.getDatabaseManager(); 3593 dbt = dbm.createTransaction(get_OriginDatabase()); dbt.setDatabaseName(get_OriginDatabase()); 3595 ownTransaction = true; 3596 } else { 3597 dbt=transaction; 3598 } 3599 } else { 3600 3601 if(get_transaction()!=null) { 3602 if(!get_transaction().equals(dbt)) 3603 throw new DatabaseManagerException("DO doesn't belong this transaction."); 3604 } 3605 CoreDO foundDO = _tr_(dbt).getDO(this); 3608 if (foundDO!=null) 3609 return; 3610 3611 } 3612 if ( null == dbt ) 3614 throw new DatabaseManagerException("DatabaseManager.createTransaction returned null." ); 3615 if ( delete ) { 3616 _tr_(dbt).addDeletedDO(this); 3617 3620 if ( false ) 3623 throw new QueryException("XXX"); 3624 } else { 3625 if (references) { 3628 3631 } 3634 } 3636 if ( false ) { 3637 throw new DataObjectException( "foo" ); 3642 } 3643 if ( delete ) { 3644 dbt.delete( this ); 3645 setDeleted(true); 3646 _tr_(dbt).resetDeletedDOs(); 3647 } else { 3648 if ( isLoaded() ) 3649 dbt.insert( this ); } 3651 if (ownTransaction) { 3652 dbt.commit(); } 3654 } catch (SQLException sqle) { 3655 StringBuffer message = new StringBuffer ("Failed to insert/update DO: "); 3656 message.append(sqle.getMessage()); 3657 if (ownTransaction) { 3659 try { 3660 dbt.rollback(); 3661 } catch (SQLException sqle2) { 3662 message.insert(0,"\n"); 3663 message.insert(0,sqle2.getMessage()); 3664 message.insert(0,"Rollback failed: "); 3665 } 3666 } 3667 throw new SQLException(message.toString()); 3668 } finally { 3669 if (ownTransaction) { 3671 dbt.release(); 3672 } 3673 } 3674 } 3675 3676 private void changedFlags_set(boolean value) { 3677 changedFirstName = value; 3678 changedLastName = value; 3679 changedPhoneNumber = value; 3680 } 3681 3684} 3685
| Popular Tags
|