1 20 21 package com.methodhead.persistable; 22 23 import java.sql.ResultSet ; 24 import java.sql.SQLException ; 25 import java.sql.Timestamp ; 26 27 import java.util.Date ; 28 import java.util.List ; 29 import java.util.ArrayList ; 30 import java.text.DateFormat ; 31 import java.text.SimpleDateFormat ; 32 import org.apache.commons.beanutils.BasicDynaBean; 33 import org.apache.commons.beanutils.DynaBean; 34 import org.apache.commons.beanutils.DynaClass; 35 import org.apache.commons.beanutils.DynaProperty; 36 import java.text.ParseException ; 37 import com.methodhead.persistable.ConnectionSingleton; 38 import org.apache.commons.lang.exception.ExceptionUtils; 39 import com.methodhead.persistable.ConnectionSingleton; 40 import org.apache.log4j.Logger; 41 import com.methodhead.test.TestUtils; 42 43 87 public class Persistable 88 extends BasicDynaBean { 89 90 92 public Persistable( DynaClass dynaClass ) { 93 super( dynaClass ); 94 } 95 96 98 100 102 105 public String getString( 106 String property ) { 107 108 return ( String )get( property ); 109 } 110 111 114 public void setString( 115 String property, 116 String value ) { 117 118 set( property, value ); 119 } 120 121 125 public int getInt( 126 String property ) { 127 128 Integer i = ( Integer )get( property ); 129 130 if ( i == null ) 131 return 0; 132 133 return i.intValue(); 134 } 135 136 139 public void setInt( 140 String property, 141 int value ) { 142 143 set( property, new Integer ( value ) ); 144 } 145 146 150 public boolean getBoolean( 151 String property ) { 152 153 Boolean b = ( Boolean )get( property ); 154 155 if ( b == null ) 156 return false; 157 158 return b.booleanValue(); 159 } 160 161 164 public void setBoolean( 165 String property, 166 boolean value ) { 167 168 set( property, new Boolean ( value ) ); 169 } 170 171 175 public double getDouble( 176 String property ) { 177 178 Double d = ( Double )get( property ); 179 180 if ( d == null ) 181 return 0.0; 182 183 return d.doubleValue(); 184 } 185 186 189 public void setDouble( 190 String property, 191 double value ) { 192 193 set( property, new Double ( value ) ); 194 } 195 196 199 public Date getDate( 200 String property ) { 201 202 return ( Date )get( property ); 203 } 204 205 208 public void setDate( 209 String property, 210 Date value ) { 211 212 set( property, value ); 213 } 214 215 251 public void setAsString( 252 String property, 253 String value ) { 254 255 Class c = getDynaClass().getDynaProperty( property ).getType(); 256 257 if ( c == String .class ) { 258 if ( value == null ) 259 set( property, "" ); 260 else 261 set( property, value ); 262 } 263 264 else if ( c == Integer .class ) { 265 if ( ( value == null ) || value.equals( "" ) ) 266 set( property, new Integer ( 0 ) ); 267 else 268 set( property, new Integer ( value ) ); 269 } 270 271 else if ( c == Boolean .class ) { 272 273 if ( value == null ) 274 set( property, new Boolean ( false ) ); 275 else { 276 value = value.toLowerCase(); 277 278 if ( value.equals( "true" ) || 279 value.equals( "yes" ) || 280 value.equals( "on" ) ) 281 set( property, new Boolean ( true ) ); 282 else 283 set( property, new Boolean ( false ) ); 284 } 285 } 286 287 else if ( c == Double .class ) { 288 if ( ( value == null ) || value.equals( "" ) ) 289 set( property, new Double ( 0.0 ) ); 290 else 291 set( property, new Double ( value ) ); 292 } 293 294 else if ( c == java.util.Date .class ) { 295 if ( ( value == null ) || value.equals( "" ) ) 296 set( property, TestUtils.getCurrentDate() ); 297 else { 298 Date d = null; 299 DateFormat fmt = 300 DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT ); 301 302 try { 303 d = fmt.parse( value ); 304 } 305 catch ( ParseException e ) { 306 307 fmt =DateFormat.getDateInstance( DateFormat.SHORT ); 308 try { 309 d = fmt.parse( value ); 310 } 311 catch ( ParseException e2 ) { 312 throw new PersistableException( 313 "Couldn't parse date from \"" + value + "\"" ); 314 } 315 } 316 317 set( property, d ); 318 } 319 } 320 } 321 322 326 public void setAsObject( 327 String property, 328 Object value ) { 329 330 if ( value == null ) 331 setAsString( property, null ); 332 else 333 setAsString( property, value.toString() ); 334 } 335 336 341 public static String getSqlLiteral( String value ) { 342 return "'" + value.replaceAll( "'", "''" ) + "'"; 343 } 344 345 349 public static String getSqlLiteral( Boolean value ) { 350 if ( value.booleanValue() ) 351 return "'1'"; 352 else 353 return "'0'"; 354 } 355 356 360 public static String getSqlLiteral( Date value ) { 361 DateFormat format = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ); 362 return "'" + format.format( value ) + "'"; 363 } 364 365 369 protected String getSqlLiteral( 370 DynaProperty dynaProperty ) { 371 372 if ( get( dynaProperty.getName() ) == null ) 373 return "NULL"; 374 375 String property = dynaProperty.getName(); 376 377 if ( dynaProperty.getType() == String .class ) 378 return getSqlLiteral( get( property ).toString() ); 379 else if ( dynaProperty.getType() == Integer .class ) 380 return get( property ).toString(); 381 else if ( dynaProperty.getType() == Boolean .class ) 382 return getSqlLiteral( ( Boolean )get( property ) ); 383 else if ( dynaProperty.getType() == Double .class ) 384 return get( property ).toString(); 385 else if ( dynaProperty.getType() == java.util.Date .class ) 386 return getSqlLiteral( ( Date )get( property ) ); 387 388 return null; 389 } 390 391 395 protected static void setProperty( 396 DynaBean persistable, 397 DynaProperty dynaProperty, 398 ResultSet rs) { 399 400 try { 401 if ( dynaProperty.getType() == String .class ) 402 persistable.set( 403 dynaProperty.getName(), 404 rs.getString( dynaProperty.getName() ) ); 405 else if ( dynaProperty.getType() == Integer .class ) 406 persistable.set( 407 dynaProperty.getName(), 408 new Integer ( rs.getInt( dynaProperty.getName() ) ) ); 409 else if ( dynaProperty.getType() == Boolean .class ) 410 persistable.set( 411 dynaProperty.getName(), 412 new Boolean ( rs.getBoolean( dynaProperty.getName() ) ) ); 413 else if ( dynaProperty.getType() == Double .class ) 414 persistable.set( 415 dynaProperty.getName(), 416 new Double ( rs.getDouble( dynaProperty.getName() ) ) ); 417 else if ( dynaProperty.getType() == java.util.Date .class ) 418 persistable.set( 419 dynaProperty.getName(), 420 rs.getTimestamp( dynaProperty.getName() ) ); 421 } 422 catch ( SQLException e ) { 423 throw new PersistableException( 424 "Unexpected SQLException while setting property \"" + 425 dynaProperty.getName() + "\":\n" + e.getMessage() ); 426 } 427 } 428 429 432 public void saveNew() { 433 434 DynaClass dynaClass = getDynaClass(); 435 436 StringBuffer buf = new StringBuffer (); 440 441 buf.append( "INSERT INTO " ); 442 buf.append( dynaClass.getName() ); 443 buf.append( " (" ); 444 445 DynaProperty[] dyanProperties = dynaClass.getDynaProperties(); 449 for ( int i = 0; i < dyanProperties.length; i++ ) { 450 buf.append( dyanProperties[ i ].getName() ); 451 452 if ( i < dyanProperties.length - 1 ) 453 buf.append( "," ); 454 } 455 456 buf.append( ") VALUES (" ); 460 461 for ( int i = 0; i < dyanProperties.length; i++ ) { 462 buf.append( getSqlLiteral( dyanProperties[ i ] ) ); 463 464 if ( i < dyanProperties.length - 1 ) 465 buf.append( "," ); 466 } 467 468 buf.append( ")" ); 469 470 try { 474 ConnectionSingleton.runUpdate( buf.toString() ); 475 } 476 catch ( SQLException e ) { 477 throw new PersistableException( 478 "Unexpected SQLException: " + e.getMessage() ); 479 } 480 } 481 482 488 public void save( 489 String whereClause ) 490 throws 491 PersistableException { 492 493 StringBuffer buf = new StringBuffer (); 497 498 buf.append( "UPDATE " ); 499 buf.append( getDynaClass().getName() ); 500 buf.append( " SET " ); 501 502 DynaProperty[] dynaProperties = getDynaClass().getDynaProperties(); 503 504 for ( int i = 0; i < dynaProperties.length; i++ ) { 508 buf.append( dynaProperties[ i ].getName() ); 509 buf.append( "=" ); 510 buf.append( getSqlLiteral( dynaProperties[ i ] ) ); 511 512 if ( i < dynaProperties.length - 1 ) 513 buf.append( "," ); 514 } 515 516 if ( whereClause != null ) 520 buf.append( " WHERE " + whereClause ); 521 522 try { 526 ConnectionSingleton.runUpdate( buf.toString() ); 527 } 528 catch ( SQLException e ) { 529 throw new PersistableException( 530 "Unexpected SQLException: " + e.getMessage() + " while executing \"" + 531 buf.toString() + "\"" ); 532 } 533 } 534 535 540 public void load( 541 String whereClause ) { 542 543 DynaProperty[] dynaProperties = getDynaClass().getDynaProperties(); 544 545 StringBuffer buf = new StringBuffer (); 546 buf.append( "SELECT " ); 547 548 for ( int i = 0; i < dynaProperties.length; i++ ) { 549 buf.append( dynaProperties[ i ].getName() ); 550 if ( i < dynaProperties.length - 1 ) 551 buf.append( "," ); 552 } 553 554 buf.append( " FROM " ); 555 buf.append( getDynaClass().getName() ); 556 buf.append( " WHERE " ); 557 buf.append( whereClause ); 558 559 ResultSet rs = null; 560 try { 561 rs = ConnectionSingleton.runQuery( buf.toString() ); 562 563 if ( rs == null ) 564 throw new PersistableException( 565 "Couldn't execute statement: " + buf.toString() ); 566 567 if ( !rs.next() ) { 568 throw new PersistableException( 569 "No records for where clause \"" + whereClause + "\"." ); 570 } 571 572 for ( int i = 0; i < dynaProperties.length; i++ ) 573 setProperty( this, dynaProperties[ i ], rs ); 574 } 575 catch ( SQLException e ) { 576 String msg = "Loading for whereClause \"" + whereClause + "\". " + ExceptionUtils.getStackTrace( e ); 577 logger_.error( msg ); 578 throw new RuntimeException ( msg ); 579 } 580 finally { 581 ConnectionSingleton.close( rs ); 582 } 583 } 584 585 594 public static List loadAll( 595 DynaClass dynaClass, 596 String whereClause, 597 String orderByClause ) { 598 599 DynaProperty[] dynaProperties = dynaClass.getDynaProperties(); 600 601 StringBuffer buf = new StringBuffer (); 602 buf.append( "SELECT " ); 603 604 for ( int i = 0; i < dynaProperties.length; i++ ) { 605 buf.append( dynaProperties[ i ].getName() ); 606 if ( i < dynaProperties.length - 1 ) 607 buf.append( "," ); 608 } 609 610 buf.append( " FROM " ); 611 buf.append( dynaClass.getName() ); 612 613 if ( whereClause != null ) { 614 buf.append( " WHERE " ); 615 buf.append( whereClause ); 616 } 617 618 if ( orderByClause != null ) { 619 buf.append( " ORDER BY " ); 620 buf.append( orderByClause ); 621 } 622 623 ResultSet rs = null; 624 try { 625 rs = ConnectionSingleton.runQuery( buf.toString() ); 626 627 if ( rs == null ) 628 throw new PersistableException( 629 "Couldn't execute statement: " + buf.toString() ); 630 631 List persistables = new ArrayList (); 632 633 while ( rs.next() ) { 634 DynaBean persistable = ( DynaBean )dynaClass.newInstance(); 635 636 for ( int i = 0; i < dynaProperties.length; i++ ) 637 setProperty( persistable, dynaProperties[ i ], rs ); 638 639 persistables.add( persistable ); 640 } 641 642 ConnectionSingleton.close( rs ); 643 644 return persistables; 645 } 646 catch ( SQLException e ) { 647 if ( rs != null ) 648 ConnectionSingleton.close( rs ); 649 throw new PersistableException( 650 "Unexpected SQLException: " + e.getMessage() ); 651 } 652 catch ( IllegalAccessException e ) { 653 throw new PersistableException( 654 "Unexpected IllegalAccessException: " + e.getMessage() ); 655 } 656 catch ( InstantiationException e ) { 657 throw new PersistableException( 658 "Unexpected InstantiationException: " + e.getMessage() ); 659 } 660 } 661 662 671 public List loadAll( 672 String whereClause, 673 String orderByClause ) { 674 675 return loadAll( getDynaClass(), whereClause, orderByClause ); 676 } 677 678 684 public static void deleteAll( 685 DynaClass dynaClass, 686 String whereClause ) 687 throws 688 PersistableException { 689 690 StringBuffer buf = new StringBuffer (); 691 buf.append( "DELETE FROM " ); 692 buf.append( dynaClass.getName() ); 693 694 if ( whereClause != null ) { 695 buf.append( " WHERE " ); 696 buf.append( whereClause ); 697 } 698 699 try { 700 ConnectionSingleton.runUpdate( buf.toString() ); 701 } 702 catch ( SQLException e ) { 703 throw new PersistableException( 704 "Unexpected SQLException: " + e.getMessage() ); 705 } 706 } 707 708 714 public void deleteAll( 715 String whereClause ) 716 throws 717 PersistableException { 718 719 deleteAll( getDynaClass(), whereClause ); 720 } 721 722 724 726 private static Logger logger_ = Logger.getLogger( Persistable.class ); 727 } 728 | Popular Tags |