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 BaseModificationPeer 37 extends BasePeer 38 { 39 40 41 public static final String DATABASE_NAME = "scarab"; 42 43 44 public static final String TABLE_NAME = "SCARAB_MODIFICATION"; 45 46 51 public static MapBuilder getMapBuilder() 52 throws TorqueException 53 { 54 return getMapBuilder(ModificationMapBuilder.CLASS_NAME); 55 } 56 57 58 public static final String TABLE_ID; 59 60 public static final String COLUMN_ID; 61 62 public static final String MODIFIED_BY; 63 64 public static final String CREATED_BY; 65 66 public static final String MODIFIED_DATE; 67 68 public static final String CREATED_DATE; 69 70 static 71 { 72 TABLE_ID = "SCARAB_MODIFICATION.TABLE_ID"; 73 COLUMN_ID = "SCARAB_MODIFICATION.COLUMN_ID"; 74 MODIFIED_BY = "SCARAB_MODIFICATION.MODIFIED_BY"; 75 CREATED_BY = "SCARAB_MODIFICATION.CREATED_BY"; 76 MODIFIED_DATE = "SCARAB_MODIFICATION.MODIFIED_DATE"; 77 CREATED_DATE = "SCARAB_MODIFICATION.CREATED_DATE"; 78 if (Torque.isInit()) 79 { 80 try 81 { 82 getMapBuilder(ModificationMapBuilder.CLASS_NAME); 83 } 84 catch (Exception e) 85 { 86 log.error("Could not initialize Peer", e); 87 } 88 } 89 else 90 { 91 Torque.registerMapBuilder(ModificationMapBuilder.CLASS_NAME); 92 } 93 } 94 95 96 public static final int numColumns = 6; 97 98 99 protected static final String CLASSNAME_DEFAULT = 100 "org.tigris.scarab.om.Modification"; 101 102 103 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 104 105 111 private static Class initClass(String className) 112 { 113 Class c = null; 114 try 115 { 116 c = Class.forName(className); 117 } 118 catch (Throwable t) 119 { 120 log.error("A FATAL ERROR has occurred which should not " 121 + "have happened under any circumstance. Please notify " 122 + "the Torque developers <torque-dev@db.apache.org> " 123 + "and give as many details as possible (including the error " 124 + "stack trace).", t); 125 126 if (t instanceof Error ) 128 { 129 throw (Error ) t.fillInStackTrace(); 130 } 131 } 132 return c; 133 } 134 135 145 public static List resultSet2Objects(java.sql.ResultSet results) 146 throws TorqueException 147 { 148 try 149 { 150 QueryDataSet qds = null; 151 List rows = null; 152 try 153 { 154 qds = new QueryDataSet(results); 155 rows = getSelectResults(qds); 156 } 157 finally 158 { 159 if (qds != null) 160 { 161 qds.close(); 162 } 163 } 164 165 return populateObjects(rows); 166 } 167 catch (SQLException e) 168 { 169 throw new TorqueException(e); 170 } 171 catch (DataSetException e) 172 { 173 throw new TorqueException(e); 174 } 175 } 176 177 178 179 186 public static ObjectKey doInsert(Criteria criteria) 187 throws TorqueException 188 { 189 return BaseModificationPeer 190 .doInsert(criteria, (Connection ) null); 191 } 192 193 203 public static ObjectKey doInsert(Criteria criteria, Connection con) 204 throws TorqueException 205 { 206 207 setDbName(criteria); 208 209 if (con == null) 210 { 211 return BasePeer.doInsert(criteria); 212 } 213 else 214 { 215 return BasePeer.doInsert(criteria, con); 216 } 217 } 218 219 226 public static void addSelectColumns(Criteria criteria) 227 throws TorqueException 228 { 229 criteria.addSelectColumn(TABLE_ID); 230 criteria.addSelectColumn(COLUMN_ID); 231 criteria.addSelectColumn(MODIFIED_BY); 232 criteria.addSelectColumn(CREATED_BY); 233 criteria.addSelectColumn(MODIFIED_DATE); 234 criteria.addSelectColumn(CREATED_DATE); 235 } 236 237 246 public static Modification row2Object(Record row, 247 int offset, 248 Class cls) 249 throws TorqueException 250 { 251 try 252 { 253 Modification obj = (Modification) cls.newInstance(); 254 ModificationPeer.populateObject(row, offset, obj); 255 obj.setModified(false); 256 obj.setNew(false); 257 258 return obj; 259 } 260 catch (InstantiationException e) 261 { 262 throw new TorqueException(e); 263 } 264 catch (IllegalAccessException e) 265 { 266 throw new TorqueException(e); 267 } 268 } 269 270 279 public static void populateObject(Record row, 280 int offset, 281 Modification obj) 282 throws TorqueException 283 { 284 try 285 { 286 obj.setTableId(row.getValue(offset + 0).asIntegerObj()); 287 obj.setColumnId(row.getValue(offset + 1).asIntegerObj()); 288 obj.setModifiedBy(row.getValue(offset + 2).asIntegerObj()); 289 obj.setCreatedBy(row.getValue(offset + 3).asIntegerObj()); 290 obj.setModifiedDate(row.getValue(offset + 4).asUtilDate()); 291 obj.setCreatedDate(row.getValue(offset + 5).asUtilDate()); 292 } 293 catch (DataSetException e) 294 { 295 throw new TorqueException(e); 296 } 297 } 298 299 307 public static List doSelect(Criteria criteria) throws TorqueException 308 { 309 return populateObjects(doSelectVillageRecords(criteria)); 310 } 311 312 321 public static List doSelect(Criteria criteria, Connection con) 322 throws TorqueException 323 { 324 return populateObjects(doSelectVillageRecords(criteria, con)); 325 } 326 327 337 public static List doSelectVillageRecords(Criteria criteria) 338 throws TorqueException 339 { 340 return BaseModificationPeer 341 .doSelectVillageRecords(criteria, (Connection ) null); 342 } 343 344 353 public static List doSelectVillageRecords(Criteria criteria, Connection con) 354 throws TorqueException 355 { 356 if (criteria.getSelectColumns().size() == 0) 357 { 358 addSelectColumns(criteria); 359 } 360 361 362 setDbName(criteria); 363 364 if (con == null) 367 { 368 return BasePeer.doSelect(criteria); 369 } 370 else 371 { 372 return BasePeer.doSelect(criteria, con); 373 } 374 } 375 376 383 public static List populateObjects(List records) 384 throws TorqueException 385 { 386 List results = new ArrayList (records.size()); 387 388 for (int i = 0; i < records.size(); i++) 390 { 391 Record row = (Record) records.get(i); 392 results.add(ModificationPeer.row2Object(row, 1, 393 ModificationPeer.getOMClass())); 394 } 395 return results; 396 } 397 398 399 407 public static Class getOMClass() 408 throws TorqueException 409 { 410 return CLASS_DEFAULT; 411 } 412 413 421 public static void doUpdate(Criteria criteria) throws TorqueException 422 { 423 BaseModificationPeer 424 .doUpdate(criteria, (Connection ) null); 425 } 426 427 438 public static void doUpdate(Criteria criteria, Connection con) 439 throws TorqueException 440 { 441 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 442 selectCriteria.put(TABLE_ID, criteria.remove(TABLE_ID)); 443 selectCriteria.put(COLUMN_ID, criteria.remove(COLUMN_ID)); 444 445 setDbName(criteria); 446 447 if (con == null) 448 { 449 BasePeer.doUpdate(selectCriteria, criteria); 450 } 451 else 452 { 453 BasePeer.doUpdate(selectCriteria, criteria, con); 454 } 455 } 456 457 464 public static void doDelete(Criteria criteria) throws TorqueException 465 { 466 ModificationPeer 467 .doDelete(criteria, (Connection ) null); 468 } 469 470 480 public static void doDelete(Criteria criteria, Connection con) 481 throws TorqueException 482 { 483 484 setDbName(criteria); 485 486 if (con == null) 487 { 488 BasePeer.doDelete(criteria); 489 } 490 else 491 { 492 BasePeer.doDelete(criteria, con); 493 } 494 } 495 496 502 public static List doSelect(Modification obj) throws TorqueException 503 { 504 return doSelect(buildSelectCriteria(obj)); 505 } 506 507 513 public static void doInsert(Modification obj) throws TorqueException 514 { 515 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 516 obj.setNew(false); 517 obj.setModified(false); 518 } 519 520 525 public static void doUpdate(Modification obj) throws TorqueException 526 { 527 doUpdate(buildCriteria(obj)); 528 obj.setModified(false); 529 } 530 531 536 public static void doDelete(Modification obj) throws TorqueException 537 { 538 doDelete(buildSelectCriteria(obj)); 539 } 540 541 551 public static void doInsert(Modification obj, Connection con) 552 throws TorqueException 553 { 554 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 555 obj.setNew(false); 556 obj.setModified(false); 557 } 558 559 569 public static void doUpdate(Modification obj, Connection con) 570 throws TorqueException 571 { 572 doUpdate(buildCriteria(obj), con); 573 obj.setModified(false); 574 } 575 576 586 public static void doDelete(Modification obj, Connection con) 587 throws TorqueException 588 { 589 doDelete(buildSelectCriteria(obj), con); 590 } 591 592 599 public static void doDelete(ObjectKey pk) throws TorqueException 600 { 601 BaseModificationPeer 602 .doDelete(pk, (Connection ) null); 603 } 604 605 615 public static void doDelete(ObjectKey pk, Connection con) 616 throws TorqueException 617 { 618 doDelete(buildCriteria(pk), con); 619 } 620 621 622 public static Criteria buildCriteria( ObjectKey pk ) 623 { 624 Criteria criteria = new Criteria(); 625 SimpleKey[] keys = (SimpleKey[])pk.getValue(); 626 criteria.add(TABLE_ID, keys[0]); 627 criteria.add(COLUMN_ID, keys[1]); 628 return criteria; 629 } 630 631 632 public static Criteria buildCriteria( Modification obj ) 633 { 634 Criteria criteria = new Criteria(DATABASE_NAME); 635 if (!obj.isNew()) 636 criteria.add(TABLE_ID, obj.getTableId()); 637 if (!obj.isNew()) 638 criteria.add(COLUMN_ID, obj.getColumnId()); 639 criteria.add(MODIFIED_BY, obj.getModifiedBy()); 640 criteria.add(CREATED_BY, obj.getCreatedBy()); 641 criteria.add(MODIFIED_DATE, obj.getModifiedDate()); 642 criteria.add(CREATED_DATE, obj.getCreatedDate()); 643 return criteria; 644 } 645 646 647 public static Criteria buildSelectCriteria( Modification obj ) 648 { 649 Criteria criteria = new Criteria(DATABASE_NAME); 650 if (!obj.isNew()) 651 criteria.add(TABLE_ID, obj.getTableId()); 652 if (!obj.isNew()) 653 criteria.add(COLUMN_ID, obj.getColumnId()); 654 criteria.add(MODIFIED_BY, obj.getModifiedBy()); 655 criteria.add(CREATED_BY, obj.getCreatedBy()); 656 criteria.add(MODIFIED_DATE, obj.getModifiedDate()); 657 criteria.add(CREATED_DATE, obj.getCreatedDate()); 658 return criteria; 659 } 660 661 662 663 672 public static Modification retrieveByPK(ObjectKey pk) 673 throws TorqueException, NoRowsException, TooManyRowsException 674 { 675 Connection db = null; 676 Modification retVal = null; 677 try 678 { 679 db = Torque.getConnection(DATABASE_NAME); 680 retVal = retrieveByPK(pk, db); 681 } 682 finally 683 { 684 Torque.closeConnection(db); 685 } 686 return(retVal); 687 } 688 689 699 public static Modification retrieveByPK(ObjectKey pk, Connection con) 700 throws TorqueException, NoRowsException, TooManyRowsException 701 { 702 Criteria criteria = buildCriteria(pk); 703 List v = doSelect(criteria, con); 704 if (v.size() == 0) 705 { 706 throw new NoRowsException("Failed to select a row."); 707 } 708 else if (v.size() > 1) 709 { 710 throw new TooManyRowsException("Failed to select only one row."); 711 } 712 else 713 { 714 return (Modification)v.get(0); 715 } 716 } 717 718 725 public static List retrieveByPKs(List pks) 726 throws TorqueException 727 { 728 Connection db = null; 729 List retVal = null; 730 try 731 { 732 db = Torque.getConnection(DATABASE_NAME); 733 retVal = retrieveByPKs(pks, db); 734 } 735 finally 736 { 737 Torque.closeConnection(db); 738 } 739 return(retVal); 740 } 741 742 750 public static List retrieveByPKs( List pks, Connection dbcon ) 751 throws TorqueException 752 { 753 List objs = null; 754 if (pks == null || pks.size() == 0) 755 { 756 objs = new LinkedList (); 757 } 758 else 759 { 760 Criteria criteria = new Criteria(); 761 Iterator iter = pks.iterator(); 762 while (iter.hasNext()) 763 { 764 ObjectKey pk = (ObjectKey)iter.next(); 765 SimpleKey[] keys = (SimpleKey[])pk.getValue(); 766 Criteria.Criterion c0 = criteria.getNewCriterion( 767 TABLE_ID, keys[0], Criteria.EQUAL); 768 Criteria.Criterion c1 = criteria.getNewCriterion( 769 COLUMN_ID, keys[1], Criteria.EQUAL); 770 c0.and(c1); 771 criteria.or(c0); 772 } 773 objs = doSelect(criteria, dbcon); 774 } 775 return objs; 776 } 777 778 779 785 public static Modification retrieveByPK( 786 Integer table_id 787 , Integer column_id 788 ) throws TorqueException 789 { 790 Connection db = null; 791 Modification retVal = null; 792 try 793 { 794 db = Torque.getConnection(DATABASE_NAME); 795 retVal = retrieveByPK( 796 table_id 797 , column_id 798 , db); 799 } 800 finally 801 { 802 Torque.closeConnection(db); 803 } 804 return(retVal); 805 } 806 807 814 public static Modification retrieveByPK( 815 Integer table_id 816 , Integer column_id 817 ,Connection con) throws TorqueException 818 { 819 820 Criteria criteria = new Criteria(5); 821 criteria.add(TABLE_ID, table_id); 822 criteria.add(COLUMN_ID, column_id); 823 List v = doSelect(criteria, con); 824 if (v.size() != 1) 825 { 826 throw new TorqueException("Failed to select one and only one row."); 827 } 828 else 829 { 830 return (Modification) v.get(0); 831 } 832 } 833 834 835 836 837 838 839 840 841 848 protected static TableMap getTableMap() 849 throws TorqueException 850 { 851 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 852 } 853 854 private static void setDbName(Criteria crit) 855 { 856 if (crit.getDbName() == Torque.getDefaultDB()) 860 { 861 crit.setDbName(DATABASE_NAME); 862 } 863 } 864 } 865 | Popular Tags |