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 BaseScopePeer 37 extends BasePeer 38 { 39 40 41 public static final String DATABASE_NAME = "scarab"; 42 43 44 public static final String TABLE_NAME = "SCARAB_SCOPE"; 45 46 51 public static MapBuilder getMapBuilder() 52 throws TorqueException 53 { 54 return getMapBuilder(ScopeMapBuilder.CLASS_NAME); 55 } 56 57 58 public static final String SCOPE_ID; 59 60 public static final String SCOPE_NAME; 61 62 static 63 { 64 SCOPE_ID = "SCARAB_SCOPE.SCOPE_ID"; 65 SCOPE_NAME = "SCARAB_SCOPE.SCOPE_NAME"; 66 if (Torque.isInit()) 67 { 68 try 69 { 70 getMapBuilder(ScopeMapBuilder.CLASS_NAME); 71 } 72 catch (Exception e) 73 { 74 log.error("Could not initialize Peer", e); 75 } 76 } 77 else 78 { 79 Torque.registerMapBuilder(ScopeMapBuilder.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.Scope"; 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 BaseScopePeer 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(SCOPE_ID); 218 criteria.addSelectColumn(SCOPE_NAME); 219 } 220 221 230 public static Scope row2Object(Record row, 231 int offset, 232 Class cls) 233 throws TorqueException 234 { 235 try 236 { 237 Scope obj = (Scope) cls.newInstance(); 238 ScopePeer.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 Scope obj) 266 throws TorqueException 267 { 268 try 269 { 270 obj.setScopeId(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 BaseScopePeer 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(ScopePeer.row2Object(row, 1, 373 ScopePeer.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 BaseScopePeer 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(SCOPE_ID, criteria.remove(SCOPE_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 ScopePeer 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(Scope obj) throws TorqueException 482 { 483 return doSelect(buildSelectCriteria(obj)); 484 } 485 486 492 public static void doInsert(Scope obj) throws TorqueException 493 { 494 doInsert(buildCriteria(obj)); 495 obj.setNew(false); 496 obj.setModified(false); 497 } 498 499 504 public static void doUpdate(Scope obj) throws TorqueException 505 { 506 doUpdate(buildCriteria(obj)); 507 obj.setModified(false); 508 } 509 510 515 public static void doDelete(Scope obj) throws TorqueException 516 { 517 doDelete(buildSelectCriteria(obj)); 518 } 519 520 530 public static void doInsert(Scope obj, Connection con) 531 throws TorqueException 532 { 533 doInsert(buildCriteria(obj), con); 534 obj.setNew(false); 535 obj.setModified(false); 536 } 537 538 548 public static void doUpdate(Scope obj, Connection con) 549 throws TorqueException 550 { 551 doUpdate(buildCriteria(obj), con); 552 obj.setModified(false); 553 } 554 555 565 public static void doDelete(Scope 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 BaseScopePeer 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(SCOPE_ID, pk); 605 return criteria; 606 } 607 608 609 public static Criteria buildCriteria( Scope obj ) 610 { 611 Criteria criteria = new Criteria(DATABASE_NAME); 612 criteria.add(SCOPE_ID, obj.getScopeId()); 613 criteria.add(SCOPE_NAME, obj.getName()); 614 return criteria; 615 } 616 617 618 public static Criteria buildSelectCriteria( Scope obj ) 619 { 620 Criteria criteria = new Criteria(DATABASE_NAME); 621 criteria.add(SCOPE_ID, obj.getScopeId()); 622 criteria.add(SCOPE_NAME, obj.getName()); 623 return criteria; 624 } 625 626 627 636 public static Scope retrieveByPK(Integer pk) 637 throws TorqueException, NoRowsException, TooManyRowsException 638 { 639 return retrieveByPK(SimpleKey.keyFor(pk)); 640 } 641 642 652 public static Scope retrieveByPK(Integer pk, Connection con) 653 throws TorqueException, NoRowsException, TooManyRowsException 654 { 655 return retrieveByPK(SimpleKey.keyFor(pk), con); 656 } 657 658 667 public static Scope retrieveByPK(ObjectKey pk) 668 throws TorqueException, NoRowsException, TooManyRowsException 669 { 670 Connection db = null; 671 Scope retVal = null; 672 try 673 { 674 db = Torque.getConnection(DATABASE_NAME); 675 retVal = retrieveByPK(pk, db); 676 } 677 finally 678 { 679 Torque.closeConnection(db); 680 } 681 return(retVal); 682 } 683 684 694 public static Scope retrieveByPK(ObjectKey pk, Connection con) 695 throws TorqueException, NoRowsException, TooManyRowsException 696 { 697 Criteria criteria = buildCriteria(pk); 698 List v = doSelect(criteria, con); 699 if (v.size() == 0) 700 { 701 throw new NoRowsException("Failed to select a row."); 702 } 703 else if (v.size() > 1) 704 { 705 throw new TooManyRowsException("Failed to select only one row."); 706 } 707 else 708 { 709 return (Scope)v.get(0); 710 } 711 } 712 713 720 public static List retrieveByPKs(List pks) 721 throws TorqueException 722 { 723 Connection db = null; 724 List retVal = null; 725 try 726 { 727 db = Torque.getConnection(DATABASE_NAME); 728 retVal = retrieveByPKs(pks, db); 729 } 730 finally 731 { 732 Torque.closeConnection(db); 733 } 734 return(retVal); 735 } 736 737 745 public static List retrieveByPKs( List pks, Connection dbcon ) 746 throws TorqueException 747 { 748 List objs = null; 749 if (pks == null || pks.size() == 0) 750 { 751 objs = new LinkedList (); 752 } 753 else 754 { 755 Criteria criteria = new Criteria(); 756 criteria.addIn( SCOPE_ID, pks ); 757 objs = doSelect(criteria, dbcon); 758 } 759 return objs; 760 } 761 762 763 764 765 766 767 768 769 770 771 778 protected static TableMap getTableMap() 779 throws TorqueException 780 { 781 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 782 } 783 784 private static void setDbName(Criteria crit) 785 { 786 if (crit.getDbName() == Torque.getDefaultDB()) 790 { 791 crit.setDbName(DATABASE_NAME); 792 } 793 } 794 } 795 | Popular Tags |