1 package org.tigris.scarab.om; 2 3 import java.math.BigDecimal ; 4 import java.sql.Connection ; 5 import java.sql.SQLException ; 6 import java.util.ArrayList ; 7 import java.util.Date ; 8 import java.util.Iterator ; 9 import java.util.LinkedList ; 10 import java.util.List ; 11 12 import org.apache.torque.NoRowsException; 13 import org.apache.torque.TooManyRowsException; 14 import org.apache.torque.Torque; 15 import org.apache.torque.TorqueException; 16 import org.apache.torque.map.MapBuilder; 17 import org.apache.torque.map.TableMap; 18 import org.apache.torque.om.DateKey; 19 import org.apache.torque.om.NumberKey; 20 import org.apache.torque.om.StringKey; 21 import org.apache.torque.om.ObjectKey; 22 import org.apache.torque.om.SimpleKey; 23 import org.apache.torque.util.BasePeer; 24 import org.apache.torque.util.Criteria; 25 26 import com.workingdogs.village.DataSetException; 27 import com.workingdogs.village.QueryDataSet; 28 import com.workingdogs.village.Record; 29 30 import org.tigris.scarab.om.map.*; 32 33 34 36 public abstract class BaseAttributeClassPeer 37 extends BasePeer 38 { 39 40 41 public static final String DATABASE_NAME = "scarab"; 42 43 44 public static final String TABLE_NAME = "SCARAB_ATTRIBUTE_CLASS"; 45 46 51 public static MapBuilder getMapBuilder() 52 throws TorqueException 53 { 54 return getMapBuilder(AttributeClassMapBuilder.CLASS_NAME); 55 } 56 57 58 public static final String ATTRIBUTE_CLASS_ID; 59 60 public static final String ATTRIBUTE_CLASS_NAME; 61 62 public static final String ATTRIBUTE_CLASS_DESC; 63 64 public static final String JAVA_CLASS_NAME; 65 66 static 67 { 68 ATTRIBUTE_CLASS_ID = "SCARAB_ATTRIBUTE_CLASS.ATTRIBUTE_CLASS_ID"; 69 ATTRIBUTE_CLASS_NAME = "SCARAB_ATTRIBUTE_CLASS.ATTRIBUTE_CLASS_NAME"; 70 ATTRIBUTE_CLASS_DESC = "SCARAB_ATTRIBUTE_CLASS.ATTRIBUTE_CLASS_DESC"; 71 JAVA_CLASS_NAME = "SCARAB_ATTRIBUTE_CLASS.JAVA_CLASS_NAME"; 72 if (Torque.isInit()) 73 { 74 try 75 { 76 getMapBuilder(AttributeClassMapBuilder.CLASS_NAME); 77 } 78 catch (Exception e) 79 { 80 log.error("Could not initialize Peer", e); 81 } 82 } 83 else 84 { 85 Torque.registerMapBuilder(AttributeClassMapBuilder.CLASS_NAME); 86 } 87 } 88 89 90 public static final int numColumns = 4; 91 92 93 protected static final String CLASSNAME_DEFAULT = 94 "org.tigris.scarab.om.AttributeClass"; 95 96 97 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 98 99 105 private static Class initClass(String className) 106 { 107 Class c = null; 108 try 109 { 110 c = Class.forName(className); 111 } 112 catch (Throwable t) 113 { 114 log.error("A FATAL ERROR has occurred which should not " 115 + "have happened under any circumstance. Please notify " 116 + "the Torque developers <torque-dev@db.apache.org> " 117 + "and give as many details as possible (including the error " 118 + "stack trace).", t); 119 120 if (t instanceof Error ) 122 { 123 throw (Error ) t.fillInStackTrace(); 124 } 125 } 126 return c; 127 } 128 129 139 public static List resultSet2Objects(java.sql.ResultSet results) 140 throws TorqueException 141 { 142 try 143 { 144 QueryDataSet qds = null; 145 List rows = null; 146 try 147 { 148 qds = new QueryDataSet(results); 149 rows = getSelectResults(qds); 150 } 151 finally 152 { 153 if (qds != null) 154 { 155 qds.close(); 156 } 157 } 158 159 return populateObjects(rows); 160 } 161 catch (SQLException e) 162 { 163 throw new TorqueException(e); 164 } 165 catch (DataSetException e) 166 { 167 throw new TorqueException(e); 168 } 169 } 170 171 172 173 180 public static ObjectKey doInsert(Criteria criteria) 181 throws TorqueException 182 { 183 return BaseAttributeClassPeer 184 .doInsert(criteria, (Connection ) null); 185 } 186 187 197 public static ObjectKey doInsert(Criteria criteria, Connection con) 198 throws TorqueException 199 { 200 201 setDbName(criteria); 202 203 if (con == null) 204 { 205 return BasePeer.doInsert(criteria); 206 } 207 else 208 { 209 return BasePeer.doInsert(criteria, con); 210 } 211 } 212 213 220 public static void addSelectColumns(Criteria criteria) 221 throws TorqueException 222 { 223 criteria.addSelectColumn(ATTRIBUTE_CLASS_ID); 224 criteria.addSelectColumn(ATTRIBUTE_CLASS_NAME); 225 criteria.addSelectColumn(ATTRIBUTE_CLASS_DESC); 226 criteria.addSelectColumn(JAVA_CLASS_NAME); 227 } 228 229 238 public static AttributeClass row2Object(Record row, 239 int offset, 240 Class cls) 241 throws TorqueException 242 { 243 try 244 { 245 AttributeClass obj = (AttributeClass) cls.newInstance(); 246 AttributeClassPeer.populateObject(row, offset, obj); 247 obj.setModified(false); 248 obj.setNew(false); 249 250 return obj; 251 } 252 catch (InstantiationException e) 253 { 254 throw new TorqueException(e); 255 } 256 catch (IllegalAccessException e) 257 { 258 throw new TorqueException(e); 259 } 260 } 261 262 271 public static void populateObject(Record row, 272 int offset, 273 AttributeClass obj) 274 throws TorqueException 275 { 276 try 277 { 278 obj.setAttributeClassId(row.getValue(offset + 0).asIntegerObj()); 279 obj.setName(row.getValue(offset + 1).asString()); 280 obj.setDesc(row.getValue(offset + 2).asString()); 281 obj.setJavaClassName(row.getValue(offset + 3).asString()); 282 } 283 catch (DataSetException e) 284 { 285 throw new TorqueException(e); 286 } 287 } 288 289 297 public static List doSelect(Criteria criteria) throws TorqueException 298 { 299 return populateObjects(doSelectVillageRecords(criteria)); 300 } 301 302 311 public static List doSelect(Criteria criteria, Connection con) 312 throws TorqueException 313 { 314 return populateObjects(doSelectVillageRecords(criteria, con)); 315 } 316 317 327 public static List doSelectVillageRecords(Criteria criteria) 328 throws TorqueException 329 { 330 return BaseAttributeClassPeer 331 .doSelectVillageRecords(criteria, (Connection ) null); 332 } 333 334 343 public static List doSelectVillageRecords(Criteria criteria, Connection con) 344 throws TorqueException 345 { 346 if (criteria.getSelectColumns().size() == 0) 347 { 348 addSelectColumns(criteria); 349 } 350 351 352 setDbName(criteria); 353 354 if (con == null) 357 { 358 return BasePeer.doSelect(criteria); 359 } 360 else 361 { 362 return BasePeer.doSelect(criteria, con); 363 } 364 } 365 366 373 public static List populateObjects(List records) 374 throws TorqueException 375 { 376 List results = new ArrayList (records.size()); 377 378 for (int i = 0; i < records.size(); i++) 380 { 381 Record row = (Record) records.get(i); 382 results.add(AttributeClassPeer.row2Object(row, 1, 383 AttributeClassPeer.getOMClass())); 384 } 385 return results; 386 } 387 388 389 397 public static Class getOMClass() 398 throws TorqueException 399 { 400 return CLASS_DEFAULT; 401 } 402 403 411 public static void doUpdate(Criteria criteria) throws TorqueException 412 { 413 BaseAttributeClassPeer 414 .doUpdate(criteria, (Connection ) null); 415 } 416 417 428 public static void doUpdate(Criteria criteria, Connection con) 429 throws TorqueException 430 { 431 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 432 selectCriteria.put(ATTRIBUTE_CLASS_ID, criteria.remove(ATTRIBUTE_CLASS_ID)); 433 434 setDbName(criteria); 435 436 if (con == null) 437 { 438 BasePeer.doUpdate(selectCriteria, criteria); 439 } 440 else 441 { 442 BasePeer.doUpdate(selectCriteria, criteria, con); 443 } 444 } 445 446 453 public static void doDelete(Criteria criteria) throws TorqueException 454 { 455 AttributeClassPeer 456 .doDelete(criteria, (Connection ) null); 457 } 458 459 469 public static void doDelete(Criteria criteria, Connection con) 470 throws TorqueException 471 { 472 473 setDbName(criteria); 474 475 if (con == null) 476 { 477 BasePeer.doDelete(criteria); 478 } 479 else 480 { 481 BasePeer.doDelete(criteria, con); 482 } 483 } 484 485 491 public static List doSelect(AttributeClass obj) throws TorqueException 492 { 493 return doSelect(buildSelectCriteria(obj)); 494 } 495 496 502 public static void doInsert(AttributeClass obj) throws TorqueException 503 { 504 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 505 obj.setNew(false); 506 obj.setModified(false); 507 } 508 509 514 public static void doUpdate(AttributeClass obj) throws TorqueException 515 { 516 doUpdate(buildCriteria(obj)); 517 obj.setModified(false); 518 } 519 520 525 public static void doDelete(AttributeClass obj) throws TorqueException 526 { 527 doDelete(buildSelectCriteria(obj)); 528 } 529 530 540 public static void doInsert(AttributeClass obj, Connection con) 541 throws TorqueException 542 { 543 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 544 obj.setNew(false); 545 obj.setModified(false); 546 } 547 548 558 public static void doUpdate(AttributeClass obj, Connection con) 559 throws TorqueException 560 { 561 doUpdate(buildCriteria(obj), con); 562 obj.setModified(false); 563 } 564 565 575 public static void doDelete(AttributeClass obj, Connection con) 576 throws TorqueException 577 { 578 doDelete(buildSelectCriteria(obj), con); 579 } 580 581 588 public static void doDelete(ObjectKey pk) throws TorqueException 589 { 590 BaseAttributeClassPeer 591 .doDelete(pk, (Connection ) null); 592 } 593 594 604 public static void doDelete(ObjectKey pk, Connection con) 605 throws TorqueException 606 { 607 doDelete(buildCriteria(pk), con); 608 } 609 610 611 public static Criteria buildCriteria( ObjectKey pk ) 612 { 613 Criteria criteria = new Criteria(); 614 criteria.add(ATTRIBUTE_CLASS_ID, pk); 615 return criteria; 616 } 617 618 619 public static Criteria buildCriteria( AttributeClass obj ) 620 { 621 Criteria criteria = new Criteria(DATABASE_NAME); 622 if (!obj.isNew()) 623 criteria.add(ATTRIBUTE_CLASS_ID, obj.getAttributeClassId()); 624 criteria.add(ATTRIBUTE_CLASS_NAME, obj.getName()); 625 criteria.add(ATTRIBUTE_CLASS_DESC, obj.getDesc()); 626 criteria.add(JAVA_CLASS_NAME, obj.getJavaClassName()); 627 return criteria; 628 } 629 630 631 public static Criteria buildSelectCriteria( AttributeClass obj ) 632 { 633 Criteria criteria = new Criteria(DATABASE_NAME); 634 if (!obj.isNew()) 635 criteria.add(ATTRIBUTE_CLASS_ID, obj.getAttributeClassId()); 636 criteria.add(ATTRIBUTE_CLASS_NAME, obj.getName()); 637 criteria.add(ATTRIBUTE_CLASS_DESC, obj.getDesc()); 638 criteria.add(JAVA_CLASS_NAME, obj.getJavaClassName()); 639 return criteria; 640 } 641 642 643 652 public static AttributeClass retrieveByPK(Integer pk) 653 throws TorqueException, NoRowsException, TooManyRowsException 654 { 655 return retrieveByPK(SimpleKey.keyFor(pk)); 656 } 657 658 668 public static AttributeClass retrieveByPK(Integer pk, Connection con) 669 throws TorqueException, NoRowsException, TooManyRowsException 670 { 671 return retrieveByPK(SimpleKey.keyFor(pk), con); 672 } 673 674 683 public static AttributeClass retrieveByPK(ObjectKey pk) 684 throws TorqueException, NoRowsException, TooManyRowsException 685 { 686 Connection db = null; 687 AttributeClass retVal = null; 688 try 689 { 690 db = Torque.getConnection(DATABASE_NAME); 691 retVal = retrieveByPK(pk, db); 692 } 693 finally 694 { 695 Torque.closeConnection(db); 696 } 697 return(retVal); 698 } 699 700 710 public static AttributeClass retrieveByPK(ObjectKey pk, Connection con) 711 throws TorqueException, NoRowsException, TooManyRowsException 712 { 713 Criteria criteria = buildCriteria(pk); 714 List v = doSelect(criteria, con); 715 if (v.size() == 0) 716 { 717 throw new NoRowsException("Failed to select a row."); 718 } 719 else if (v.size() > 1) 720 { 721 throw new TooManyRowsException("Failed to select only one row."); 722 } 723 else 724 { 725 return (AttributeClass)v.get(0); 726 } 727 } 728 729 736 public static List retrieveByPKs(List pks) 737 throws TorqueException 738 { 739 Connection db = null; 740 List retVal = null; 741 try 742 { 743 db = Torque.getConnection(DATABASE_NAME); 744 retVal = retrieveByPKs(pks, db); 745 } 746 finally 747 { 748 Torque.closeConnection(db); 749 } 750 return(retVal); 751 } 752 753 761 public static List retrieveByPKs( List pks, Connection dbcon ) 762 throws TorqueException 763 { 764 List objs = null; 765 if (pks == null || pks.size() == 0) 766 { 767 objs = new LinkedList (); 768 } 769 else 770 { 771 Criteria criteria = new Criteria(); 772 criteria.addIn( ATTRIBUTE_CLASS_ID, pks ); 773 objs = doSelect(criteria, dbcon); 774 } 775 return objs; 776 } 777 778 779 780 781 782 783 784 785 786 787 794 protected static TableMap getTableMap() 795 throws TorqueException 796 { 797 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 798 } 799 800 private static void setDbName(Criteria crit) 801 { 802 if (crit.getDbName() == Torque.getDefaultDB()) 806 { 807 crit.setDbName(DATABASE_NAME); 808 } 809 } 810 } 811 | Popular Tags |