1 package org.apache.jetspeed.om.apps.coffees; 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.apache.jetspeed.om.apps.coffees.map.*; 32 33 34 40 public abstract class BaseCoffeesPeer 41 extends BasePeer 42 { 43 44 45 public static final String DATABASE_NAME = "default"; 46 47 48 public static final String TABLE_NAME = "COFFEES"; 49 50 55 public static MapBuilder getMapBuilder() 56 throws TorqueException 57 { 58 return getMapBuilder(CoffeesMapBuilder.CLASS_NAME); 59 } 60 61 62 public static final String COFFEE_ID; 63 64 public static final String COFFEE_NAME; 65 66 public static final String SUPPLIER_ID; 67 68 public static final String PRICE; 69 70 public static final String SALES; 71 72 public static final String TOTAL; 73 74 static 75 { 76 COFFEE_ID = "COFFEES.COFFEE_ID"; 77 COFFEE_NAME = "COFFEES.COFFEE_NAME"; 78 SUPPLIER_ID = "COFFEES.SUPPLIER_ID"; 79 PRICE = "COFFEES.PRICE"; 80 SALES = "COFFEES.SALES"; 81 TOTAL = "COFFEES.TOTAL"; 82 if (Torque.isInit()) 83 { 84 try 85 { 86 getMapBuilder(); 87 } 88 catch (Exception e) 89 { 90 log.error("Could not initialize Peer", e); 91 } 92 } 93 else 94 { 95 Torque.registerMapBuilder(CoffeesMapBuilder.CLASS_NAME); 96 } 97 } 98 99 100 public static final int numColumns = 6; 101 102 103 protected static final String CLASSNAME_DEFAULT = 104 "org.apache.jetspeed.om.apps.coffees.Coffees"; 105 106 107 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 108 109 115 private static Class initClass(String className) 116 { 117 Class c = null; 118 try 119 { 120 c = Class.forName(className); 121 } 122 catch (Throwable t) 123 { 124 log.error("A FATAL ERROR has occurred which should not " 125 + "have happened under any circumstance. Please notify " 126 + "the Torque developers <turbine-torque-dev@jakarta.apache.org> " 127 + "and give as many details as possible (including the error " 128 + "stack trace).", t); 129 130 if (t instanceof Error ) 132 { 133 throw (Error ) t.fillInStackTrace(); 134 } 135 } 136 return c; 137 } 138 139 149 public static List resultSet2Objects(java.sql.ResultSet results) 150 throws TorqueException 151 { 152 try 153 { 154 QueryDataSet qds = null; 155 List rows = null; 156 try 157 { 158 qds = new QueryDataSet(results); 159 rows = getSelectResults(qds); 160 } 161 finally 162 { 163 if (qds != null) 164 { 165 qds.close(); 166 } 167 } 168 169 return populateObjects(rows); 170 } 171 catch (SQLException e) 172 { 173 throw new TorqueException(e); 174 } 175 catch (DataSetException e) 176 { 177 throw new TorqueException(e); 178 } 179 } 180 181 182 183 190 public static ObjectKey doInsert(Criteria criteria) 191 throws TorqueException 192 { 193 return BaseCoffeesPeer 194 .doInsert(criteria, (Connection ) null); 195 } 196 197 207 public static ObjectKey doInsert(Criteria criteria, Connection con) 208 throws TorqueException 209 { 210 211 if (criteria.getDbName() == Torque.getDefaultDB()) 215 { 216 criteria.setDbName(DATABASE_NAME); 217 } 218 if (con == null) 219 { 220 return BasePeer.doInsert(criteria); 221 } 222 else 223 { 224 return BasePeer.doInsert(criteria, con); 225 } 226 } 227 228 235 public static void addSelectColumns(Criteria criteria) 236 throws TorqueException 237 { 238 criteria.addSelectColumn(COFFEE_ID); 239 criteria.addSelectColumn(COFFEE_NAME); 240 criteria.addSelectColumn(SUPPLIER_ID); 241 criteria.addSelectColumn(PRICE); 242 criteria.addSelectColumn(SALES); 243 criteria.addSelectColumn(TOTAL); 244 } 245 246 255 public static Coffees row2Object(Record row, 256 int offset, 257 Class cls) 258 throws TorqueException 259 { 260 try 261 { 262 Coffees obj = (Coffees) cls.newInstance(); 263 CoffeesPeer.populateObject(row, offset, obj); 264 obj.setModified(false); 265 obj.setNew(false); 266 267 return obj; 268 } 269 catch (InstantiationException e) 270 { 271 throw new TorqueException(e); 272 } 273 catch (IllegalAccessException e) 274 { 275 throw new TorqueException(e); 276 } 277 } 278 279 288 public static void populateObject(Record row, 289 int offset, 290 Coffees obj) 291 throws TorqueException 292 { 293 try 294 { 295 obj.setCoffeeId(row.getValue(offset + 0).asInt()); 296 obj.setCoffeeName(row.getValue(offset + 1).asString()); 297 obj.setSupplierId(row.getValue(offset + 2).asInt()); 298 obj.setPrice(row.getValue(offset + 3).asDouble()); 299 obj.setSales(row.getValue(offset + 4).asInt()); 300 obj.setTotal(row.getValue(offset + 5).asInt()); 301 } 302 catch (DataSetException e) 303 { 304 throw new TorqueException(e); 305 } 306 } 307 308 316 public static List doSelect(Criteria criteria) throws TorqueException 317 { 318 return populateObjects(doSelectVillageRecords(criteria)); 319 } 320 321 330 public static List doSelect(Criteria criteria, Connection con) 331 throws TorqueException 332 { 333 return populateObjects(doSelectVillageRecords(criteria, con)); 334 } 335 336 346 public static List doSelectVillageRecords(Criteria criteria) 347 throws TorqueException 348 { 349 return BaseCoffeesPeer 350 .doSelectVillageRecords(criteria, (Connection ) null); 351 } 352 353 361 public static List doSelectVillageRecords(Criteria criteria, Connection con) 362 throws TorqueException 363 { 364 if (criteria.getSelectColumns().size() == 0) 365 { 366 addSelectColumns(criteria); 367 } 368 369 370 if (criteria.getDbName() == Torque.getDefaultDB()) 374 { 375 criteria.setDbName(DATABASE_NAME); 376 } 377 if (con == null) 380 { 381 return BasePeer.doSelect(criteria); 382 } 383 else 384 { 385 return BasePeer.doSelect(criteria, con); 386 } 387 } 388 389 396 public static List populateObjects(List records) 397 throws TorqueException 398 { 399 List results = new ArrayList (records.size()); 400 401 for (int i = 0; i < records.size(); i++) 403 { 404 Record row = (Record) records.get(i); 405 results.add(CoffeesPeer.row2Object(row, 1, 406 CoffeesPeer.getOMClass())); 407 } 408 return results; 409 } 410 411 412 420 public static Class getOMClass() 421 throws TorqueException 422 { 423 return CLASS_DEFAULT; 424 } 425 426 434 public static void doUpdate(Criteria criteria) throws TorqueException 435 { 436 BaseCoffeesPeer 437 .doUpdate(criteria, (Connection ) null); 438 } 439 440 451 public static void doUpdate(Criteria criteria, Connection con) 452 throws TorqueException 453 { 454 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 455 selectCriteria.put(COFFEE_ID, criteria.remove(COFFEE_ID)); 456 457 if (criteria.getDbName() == Torque.getDefaultDB()) 461 { 462 criteria.setDbName(DATABASE_NAME); 463 } 464 if (con == null) 465 { 466 BasePeer.doUpdate(selectCriteria, criteria); 467 } 468 else 469 { 470 BasePeer.doUpdate(selectCriteria, criteria, con); 471 } 472 } 473 474 481 public static void doDelete(Criteria criteria) throws TorqueException 482 { 483 BaseCoffeesPeer 484 .doDelete(criteria, (Connection ) null); 485 } 486 487 497 public static void doDelete(Criteria criteria, Connection con) 498 throws TorqueException 499 { 500 501 if (criteria.getDbName() == Torque.getDefaultDB()) 505 { 506 criteria.setDbName(DATABASE_NAME); 507 } 508 if (con == null) 509 { 510 BasePeer.doDelete(criteria); 511 } 512 else 513 { 514 BasePeer.doDelete(criteria, con); 515 } 516 } 517 518 524 public static List doSelect(Coffees obj) throws TorqueException 525 { 526 return doSelect(buildCriteria(obj)); 527 } 528 529 535 public static void doInsert(Coffees obj) throws TorqueException 536 { 537 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 538 obj.setNew(false); 539 obj.setModified(false); 540 } 541 542 547 public static void doUpdate(Coffees obj) throws TorqueException 548 { 549 doUpdate(buildCriteria(obj)); 550 obj.setModified(false); 551 } 552 553 558 public static void doDelete(Coffees obj) throws TorqueException 559 { 560 doDelete(buildCriteria(obj)); 561 } 562 563 573 public static void doInsert(Coffees obj, Connection con) 574 throws TorqueException 575 { 576 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 577 obj.setNew(false); 578 obj.setModified(false); 579 } 580 581 591 public static void doUpdate(Coffees obj, Connection con) 592 throws TorqueException 593 { 594 doUpdate(buildCriteria(obj), con); 595 obj.setModified(false); 596 } 597 598 608 public static void doDelete(Coffees obj, Connection con) 609 throws TorqueException 610 { 611 doDelete(buildCriteria(obj), con); 612 } 613 614 621 public static void doDelete(ObjectKey pk) throws TorqueException 622 { 623 BaseCoffeesPeer 624 .doDelete(pk, (Connection ) null); 625 } 626 627 637 public static void doDelete(ObjectKey pk, Connection con) 638 throws TorqueException 639 { 640 doDelete(buildCriteria(pk), con); 641 } 642 643 644 public static Criteria buildCriteria( ObjectKey pk ) 645 { 646 Criteria criteria = new Criteria(); 647 criteria.add(COFFEE_ID, pk); 648 return criteria; 649 } 650 651 652 public static Criteria buildCriteria( Coffees obj ) 653 { 654 Criteria criteria = new Criteria(DATABASE_NAME); 655 if (!obj.isNew()) 656 criteria.add(COFFEE_ID, obj.getCoffeeId()); 657 criteria.add(COFFEE_NAME, obj.getCoffeeName()); 658 criteria.add(SUPPLIER_ID, obj.getSupplierId()); 659 criteria.add(PRICE, obj.getPrice()); 660 criteria.add(SALES, obj.getSales()); 661 criteria.add(TOTAL, obj.getTotal()); 662 return criteria; 663 } 664 665 666 675 public static Coffees retrieveByPK(int pk) 676 throws TorqueException, NoRowsException, TooManyRowsException 677 { 678 return retrieveByPK(SimpleKey.keyFor(pk)); 679 } 680 681 690 public static Coffees retrieveByPK(ObjectKey pk) 691 throws TorqueException, NoRowsException, TooManyRowsException 692 { 693 Connection db = null; 694 Coffees retVal = null; 695 try 696 { 697 db = Torque.getConnection(DATABASE_NAME); 698 retVal = retrieveByPK(pk, db); 699 } 700 finally 701 { 702 Torque.closeConnection(db); 703 } 704 return(retVal); 705 } 706 707 717 public static Coffees retrieveByPK(ObjectKey pk, Connection con) 718 throws TorqueException, NoRowsException, TooManyRowsException 719 { 720 Criteria criteria = buildCriteria(pk); 721 List v = doSelect(criteria, con); 722 if (v.size() == 0) 723 { 724 throw new NoRowsException("Failed to select a row."); 725 } 726 else if (v.size() > 1) 727 { 728 throw new TooManyRowsException("Failed to select only one row."); 729 } 730 else 731 { 732 return (Coffees)v.get(0); 733 } 734 } 735 736 743 public static List retrieveByPKs(List pks) 744 throws TorqueException 745 { 746 Connection db = null; 747 List retVal = null; 748 try 749 { 750 db = Torque.getConnection(DATABASE_NAME); 751 retVal = retrieveByPKs(pks, db); 752 } 753 finally 754 { 755 Torque.closeConnection(db); 756 } 757 return(retVal); 758 } 759 760 768 public static List retrieveByPKs( List pks, Connection dbcon ) 769 throws TorqueException 770 { 771 List objs = null; 772 if (pks == null || pks.size() == 0) 773 { 774 objs = new LinkedList (); 775 } 776 else 777 { 778 Criteria criteria = new Criteria(); 779 criteria.addIn( COFFEE_ID, pks ); 780 objs = doSelect(criteria, dbcon); 781 } 782 return objs; 783 } 784 785 786 787 788 789 790 791 792 793 794 801 protected static TableMap getTableMap() 802 throws TorqueException 803 { 804 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 805 } 806 } 807 | Popular Tags |