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 BaseDependTypePeer 37 extends BasePeer 38 { 39 40 41 public static final String DATABASE_NAME = "scarab"; 42 43 44 public static final String TABLE_NAME = "SCARAB_DEPEND_TYPE"; 45 46 51 public static MapBuilder getMapBuilder() 52 throws TorqueException 53 { 54 return getMapBuilder(DependTypeMapBuilder.CLASS_NAME); 55 } 56 57 58 public static final String DEPEND_TYPE_ID; 59 60 public static final String DEPEND_TYPE_NAME; 61 62 static 63 { 64 DEPEND_TYPE_ID = "SCARAB_DEPEND_TYPE.DEPEND_TYPE_ID"; 65 DEPEND_TYPE_NAME = "SCARAB_DEPEND_TYPE.DEPEND_TYPE_NAME"; 66 if (Torque.isInit()) 67 { 68 try 69 { 70 getMapBuilder(DependTypeMapBuilder.CLASS_NAME); 71 } 72 catch (Exception e) 73 { 74 log.error("Could not initialize Peer", e); 75 } 76 } 77 else 78 { 79 Torque.registerMapBuilder(DependTypeMapBuilder.CLASS_NAME); 80 } 81 } 82 83 84 public static final int numColumns = 2; 85 86 87 protected static final String CLASSNAME_DEFAULT = 88 "org.tigris.scarab.om.DependType"; 89 90 91 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 92 93 99 private static Class initClass(String className) 100 { 101 Class c = null; 102 try 103 { 104 c = Class.forName(className); 105 } 106 catch (Throwable t) 107 { 108 log.error("A FATAL ERROR has occurred which should not " 109 + "have happened under any circumstance. Please notify " 110 + "the Torque developers <torque-dev@db.apache.org> " 111 + "and give as many details as possible (including the error " 112 + "stack trace).", t); 113 114 if (t instanceof Error ) 116 { 117 throw (Error ) t.fillInStackTrace(); 118 } 119 } 120 return c; 121 } 122 123 133 public static List resultSet2Objects(java.sql.ResultSet results) 134 throws TorqueException 135 { 136 try 137 { 138 QueryDataSet qds = null; 139 List rows = null; 140 try 141 { 142 qds = new QueryDataSet(results); 143 rows = getSelectResults(qds); 144 } 145 finally 146 { 147 if (qds != null) 148 { 149 qds.close(); 150 } 151 } 152 153 return populateObjects(rows); 154 } 155 catch (SQLException e) 156 { 157 throw new TorqueException(e); 158 } 159 catch (DataSetException e) 160 { 161 throw new TorqueException(e); 162 } 163 } 164 165 166 167 174 public static ObjectKey doInsert(Criteria criteria) 175 throws TorqueException 176 { 177 return BaseDependTypePeer 178 .doInsert(criteria, (Connection ) null); 179 } 180 181 191 public static ObjectKey doInsert(Criteria criteria, Connection con) 192 throws TorqueException 193 { 194 195 setDbName(criteria); 196 197 if (con == null) 198 { 199 return BasePeer.doInsert(criteria); 200 } 201 else 202 { 203 return BasePeer.doInsert(criteria, con); 204 } 205 } 206 207 214 public static void addSelectColumns(Criteria criteria) 215 throws TorqueException 216 { 217 criteria.addSelectColumn(DEPEND_TYPE_ID); 218 criteria.addSelectColumn(DEPEND_TYPE_NAME); 219 } 220 221 230 public static DependType row2Object(Record row, 231 int offset, 232 Class cls) 233 throws TorqueException 234 { 235 try 236 { 237 DependType obj = (DependType) cls.newInstance(); 238 DependTypePeer.populateObject(row, offset, obj); 239 obj.setModified(false); 240 obj.setNew(false); 241 242 return obj; 243 } 244 catch (InstantiationException e) 245 { 246 throw new TorqueException(e); 247 } 248 catch (IllegalAccessException e) 249 { 250 throw new TorqueException(e); 251 } 252 } 253 254 263 public static void populateObject(Record row, 264 int offset, 265 DependType obj) 266 throws TorqueException 267 { 268 try 269 { 270 obj.setDependTypeId(row.getValue(offset + 0).asIntegerObj()); 271 obj.setName(row.getValue(offset + 1).asString()); 272 } 273 catch (DataSetException e) 274 { 275 throw new TorqueException(e); 276 } 277 } 278 279 287 public static List doSelect(Criteria criteria) throws TorqueException 288 { 289 return populateObjects(doSelectVillageRecords(criteria)); 290 } 291 292 301 public static List doSelect(Criteria criteria, Connection con) 302 throws TorqueException 303 { 304 return populateObjects(doSelectVillageRecords(criteria, con)); 305 } 306 307 317 public static List doSelectVillageRecords(Criteria criteria) 318 throws TorqueException 319 { 320 return BaseDependTypePeer 321 .doSelectVillageRecords(criteria, (Connection ) null); 322 } 323 324 333 public static List doSelectVillageRecords(Criteria criteria, Connection con) 334 throws TorqueException 335 { 336 if (criteria.getSelectColumns().size() == 0) 337 { 338 addSelectColumns(criteria); 339 } 340 341 342 setDbName(criteria); 343 344 if (con == null) 347 { 348 return BasePeer.doSelect(criteria); 349 } 350 else 351 { 352 return BasePeer.doSelect(criteria, con); 353 } 354 } 355 356 363 public static List populateObjects(List records) 364 throws TorqueException 365 { 366 List results = new ArrayList (records.size()); 367 368 for (int i = 0; i < records.size(); i++) 370 { 371 Record row = (Record) records.get(i); 372 results.add(DependTypePeer.row2Object(row, 1, 373 DependTypePeer.getOMClass())); 374 } 375 return results; 376 } 377 378 379 387 public static Class getOMClass() 388 throws TorqueException 389 { 390 return CLASS_DEFAULT; 391 } 392 393 401 public static void doUpdate(Criteria criteria) throws TorqueException 402 { 403 BaseDependTypePeer 404 .doUpdate(criteria, (Connection ) null); 405 } 406 407 418 public static void doUpdate(Criteria criteria, Connection con) 419 throws TorqueException 420 { 421 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 422 selectCriteria.put(DEPEND_TYPE_ID, criteria.remove(DEPEND_TYPE_ID)); 423 424 setDbName(criteria); 425 426 if (con == null) 427 { 428 BasePeer.doUpdate(selectCriteria, criteria); 429 } 430 else 431 { 432 BasePeer.doUpdate(selectCriteria, criteria, con); 433 } 434 } 435 436 443 public static void doDelete(Criteria criteria) throws TorqueException 444 { 445 DependTypePeer 446 .doDelete(criteria, (Connection ) null); 447 } 448 449 459 public static void doDelete(Criteria criteria, Connection con) 460 throws TorqueException 461 { 462 463 setDbName(criteria); 464 465 if (con == null) 466 { 467 BasePeer.doDelete(criteria); 468 } 469 else 470 { 471 BasePeer.doDelete(criteria, con); 472 } 473 } 474 475 481 public static List doSelect(DependType obj) throws TorqueException 482 { 483 return doSelect(buildSelectCriteria(obj)); 484 } 485 486 492 public static void doInsert(DependType obj) throws TorqueException 493 { 494 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 495 obj.setNew(false); 496 obj.setModified(false); 497 } 498 499 504 public static void doUpdate(DependType obj) throws TorqueException 505 { 506 doUpdate(buildCriteria(obj)); 507 obj.setModified(false); 508 } 509 510 515 public static void doDelete(DependType obj) throws TorqueException 516 { 517 doDelete(buildSelectCriteria(obj)); 518 } 519 520 530 public static void doInsert(DependType obj, Connection con) 531 throws TorqueException 532 { 533 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 534 obj.setNew(false); 535 obj.setModified(false); 536 } 537 538 548 public static void doUpdate(DependType obj, Connection con) 549 throws TorqueException 550 { 551 doUpdate(buildCriteria(obj), con); 552 obj.setModified(false); 553 } 554 555 565 public static void doDelete(DependType obj, Connection con) 566 throws TorqueException 567 { 568 doDelete(buildSelectCriteria(obj), con); 569 } 570 571 578 public static void doDelete(ObjectKey pk) throws TorqueException 579 { 580 BaseDependTypePeer 581 .doDelete(pk, (Connection ) null); 582 } 583 584 594 public static void doDelete(ObjectKey pk, Connection con) 595 throws TorqueException 596 { 597 doDelete(buildCriteria(pk), con); 598 } 599 600 601 public static Criteria buildCriteria( ObjectKey pk ) 602 { 603 Criteria criteria = new Criteria(); 604 criteria.add(DEPEND_TYPE_ID, pk); 605 return criteria; 606 } 607 608 609 public static Criteria buildCriteria( DependType obj ) 610 { 611 Criteria criteria = new Criteria(DATABASE_NAME); 612 if (!obj.isNew()) 613 criteria.add(DEPEND_TYPE_ID, obj.getDependTypeId()); 614 criteria.add(DEPEND_TYPE_NAME, obj.getName()); 615 return criteria; 616 } 617 618 619 public static Criteria buildSelectCriteria( DependType obj ) 620 { 621 Criteria criteria = new Criteria(DATABASE_NAME); 622 if (!obj.isNew()) 623 criteria.add(DEPEND_TYPE_ID, obj.getDependTypeId()); 624 criteria.add(DEPEND_TYPE_NAME, obj.getName()); 625 return criteria; 626 } 627 628 629 638 public static DependType retrieveByPK(Integer pk) 639 throws TorqueException, NoRowsException, TooManyRowsException 640 { 641 return retrieveByPK(SimpleKey.keyFor(pk)); 642 } 643 644 654 public static DependType retrieveByPK(Integer pk, Connection con) 655 throws TorqueException, NoRowsException, TooManyRowsException 656 { 657 return retrieveByPK(SimpleKey.keyFor(pk), con); 658 } 659 660 669 public static DependType retrieveByPK(ObjectKey pk) 670 throws TorqueException, NoRowsException, TooManyRowsException 671 { 672 Connection db = null; 673 DependType retVal = null; 674 try 675 { 676 db = Torque.getConnection(DATABASE_NAME); 677 retVal = retrieveByPK(pk, db); 678 } 679 finally 680 { 681 Torque.closeConnection(db); 682 } 683 return(retVal); 684 } 685 686 696 public static DependType retrieveByPK(ObjectKey pk, Connection con) 697 throws TorqueException, NoRowsException, TooManyRowsException 698 { 699 Criteria criteria = buildCriteria(pk); 700 List v = doSelect(criteria, con); 701 if (v.size() == 0) 702 { 703 throw new NoRowsException("Failed to select a row."); 704 } 705 else if (v.size() > 1) 706 { 707 throw new TooManyRowsException("Failed to select only one row."); 708 } 709 else 710 { 711 return (DependType)v.get(0); 712 } 713 } 714 715 722 public static List retrieveByPKs(List pks) 723 throws TorqueException 724 { 725 Connection db = null; 726 List retVal = null; 727 try 728 { 729 db = Torque.getConnection(DATABASE_NAME); 730 retVal = retrieveByPKs(pks, db); 731 } 732 finally 733 { 734 Torque.closeConnection(db); 735 } 736 return(retVal); 737 } 738 739 747 public static List retrieveByPKs( List pks, Connection dbcon ) 748 throws TorqueException 749 { 750 List objs = null; 751 if (pks == null || pks.size() == 0) 752 { 753 objs = new LinkedList (); 754 } 755 else 756 { 757 Criteria criteria = new Criteria(); 758 criteria.addIn( DEPEND_TYPE_ID, pks ); 759 objs = doSelect(criteria, dbcon); 760 } 761 return objs; 762 } 763 764 765 766 767 768 769 770 771 772 773 780 protected static TableMap getTableMap() 781 throws TorqueException 782 { 783 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 784 } 785 786 private static void setDbName(Criteria crit) 787 { 788 if (crit.getDbName() == Torque.getDefaultDB()) 792 { 793 crit.setDbName(DATABASE_NAME); 794 } 795 } 796 } 797 | Popular Tags |