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 35 37 public abstract class BaseGlobalParameterPeer 38 extends BasePeer 39 { 40 41 42 public static final String DATABASE_NAME = "scarab"; 43 44 45 public static final String TABLE_NAME = "SCARAB_GLOBAL_PARAMETER"; 46 47 52 public static MapBuilder getMapBuilder() 53 throws TorqueException 54 { 55 return getMapBuilder(GlobalParameterMapBuilder.CLASS_NAME); 56 } 57 58 59 public static final String PARAMETER_ID; 60 61 public static final String NAME; 62 63 public static final String VALUE; 64 65 public static final String MODULE_ID; 66 67 static 68 { 69 PARAMETER_ID = "SCARAB_GLOBAL_PARAMETER.PARAMETER_ID"; 70 NAME = "SCARAB_GLOBAL_PARAMETER.NAME"; 71 VALUE = "SCARAB_GLOBAL_PARAMETER.VALUE"; 72 MODULE_ID = "SCARAB_GLOBAL_PARAMETER.MODULE_ID"; 73 if (Torque.isInit()) 74 { 75 try 76 { 77 getMapBuilder(GlobalParameterMapBuilder.CLASS_NAME); 78 } 79 catch (Exception e) 80 { 81 log.error("Could not initialize Peer", e); 82 } 83 } 84 else 85 { 86 Torque.registerMapBuilder(GlobalParameterMapBuilder.CLASS_NAME); 87 } 88 } 89 90 91 public static final int numColumns = 4; 92 93 94 protected static final String CLASSNAME_DEFAULT = 95 "org.tigris.scarab.om.GlobalParameter"; 96 97 98 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 99 100 106 private static Class initClass(String className) 107 { 108 Class c = null; 109 try 110 { 111 c = Class.forName(className); 112 } 113 catch (Throwable t) 114 { 115 log.error("A FATAL ERROR has occurred which should not " 116 + "have happened under any circumstance. Please notify " 117 + "the Torque developers <torque-dev@db.apache.org> " 118 + "and give as many details as possible (including the error " 119 + "stack trace).", t); 120 121 if (t instanceof Error ) 123 { 124 throw (Error ) t.fillInStackTrace(); 125 } 126 } 127 return c; 128 } 129 130 140 public static List resultSet2Objects(java.sql.ResultSet results) 141 throws TorqueException 142 { 143 try 144 { 145 QueryDataSet qds = null; 146 List rows = null; 147 try 148 { 149 qds = new QueryDataSet(results); 150 rows = getSelectResults(qds); 151 } 152 finally 153 { 154 if (qds != null) 155 { 156 qds.close(); 157 } 158 } 159 160 return populateObjects(rows); 161 } 162 catch (SQLException e) 163 { 164 throw new TorqueException(e); 165 } 166 catch (DataSetException e) 167 { 168 throw new TorqueException(e); 169 } 170 } 171 172 173 174 181 public static ObjectKey doInsert(Criteria criteria) 182 throws TorqueException 183 { 184 return BaseGlobalParameterPeer 185 .doInsert(criteria, (Connection ) null); 186 } 187 188 198 public static ObjectKey doInsert(Criteria criteria, Connection con) 199 throws TorqueException 200 { 201 202 setDbName(criteria); 203 204 if (con == null) 205 { 206 return BasePeer.doInsert(criteria); 207 } 208 else 209 { 210 return BasePeer.doInsert(criteria, con); 211 } 212 } 213 214 221 public static void addSelectColumns(Criteria criteria) 222 throws TorqueException 223 { 224 criteria.addSelectColumn(PARAMETER_ID); 225 criteria.addSelectColumn(NAME); 226 criteria.addSelectColumn(VALUE); 227 criteria.addSelectColumn(MODULE_ID); 228 } 229 230 239 public static GlobalParameter row2Object(Record row, 240 int offset, 241 Class cls) 242 throws TorqueException 243 { 244 try 245 { 246 GlobalParameter obj = (GlobalParameter) cls.newInstance(); 247 GlobalParameterPeer.populateObject(row, offset, obj); 248 obj.setModified(false); 249 obj.setNew(false); 250 251 return obj; 252 } 253 catch (InstantiationException e) 254 { 255 throw new TorqueException(e); 256 } 257 catch (IllegalAccessException e) 258 { 259 throw new TorqueException(e); 260 } 261 } 262 263 272 public static void populateObject(Record row, 273 int offset, 274 GlobalParameter obj) 275 throws TorqueException 276 { 277 try 278 { 279 obj.setParameterId(row.getValue(offset + 0).asIntegerObj()); 280 obj.setName(row.getValue(offset + 1).asString()); 281 obj.setValue(row.getValue(offset + 2).asString()); 282 obj.setModuleId(row.getValue(offset + 3).asIntegerObj()); 283 } 284 catch (DataSetException e) 285 { 286 throw new TorqueException(e); 287 } 288 } 289 290 298 public static List doSelect(Criteria criteria) throws TorqueException 299 { 300 return populateObjects(doSelectVillageRecords(criteria)); 301 } 302 303 312 public static List doSelect(Criteria criteria, Connection con) 313 throws TorqueException 314 { 315 return populateObjects(doSelectVillageRecords(criteria, con)); 316 } 317 318 328 public static List doSelectVillageRecords(Criteria criteria) 329 throws TorqueException 330 { 331 return BaseGlobalParameterPeer 332 .doSelectVillageRecords(criteria, (Connection ) null); 333 } 334 335 344 public static List doSelectVillageRecords(Criteria criteria, Connection con) 345 throws TorqueException 346 { 347 if (criteria.getSelectColumns().size() == 0) 348 { 349 addSelectColumns(criteria); 350 } 351 352 353 setDbName(criteria); 354 355 if (con == null) 358 { 359 return BasePeer.doSelect(criteria); 360 } 361 else 362 { 363 return BasePeer.doSelect(criteria, con); 364 } 365 } 366 367 374 public static List populateObjects(List records) 375 throws TorqueException 376 { 377 List results = new ArrayList (records.size()); 378 379 for (int i = 0; i < records.size(); i++) 381 { 382 Record row = (Record) records.get(i); 383 results.add(GlobalParameterPeer.row2Object(row, 1, 384 GlobalParameterPeer.getOMClass())); 385 } 386 return results; 387 } 388 389 390 398 public static Class getOMClass() 399 throws TorqueException 400 { 401 return CLASS_DEFAULT; 402 } 403 404 412 public static void doUpdate(Criteria criteria) throws TorqueException 413 { 414 BaseGlobalParameterPeer 415 .doUpdate(criteria, (Connection ) null); 416 } 417 418 429 public static void doUpdate(Criteria criteria, Connection con) 430 throws TorqueException 431 { 432 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 433 selectCriteria.put(PARAMETER_ID, criteria.remove(PARAMETER_ID)); 434 435 setDbName(criteria); 436 437 if (con == null) 438 { 439 BasePeer.doUpdate(selectCriteria, criteria); 440 } 441 else 442 { 443 BasePeer.doUpdate(selectCriteria, criteria, con); 444 } 445 } 446 447 454 public static void doDelete(Criteria criteria) throws TorqueException 455 { 456 GlobalParameterPeer 457 .doDelete(criteria, (Connection ) null); 458 } 459 460 470 public static void doDelete(Criteria criteria, Connection con) 471 throws TorqueException 472 { 473 474 setDbName(criteria); 475 476 if (con == null) 477 { 478 BasePeer.doDelete(criteria); 479 } 480 else 481 { 482 BasePeer.doDelete(criteria, con); 483 } 484 } 485 486 492 public static List doSelect(GlobalParameter obj) throws TorqueException 493 { 494 return doSelect(buildSelectCriteria(obj)); 495 } 496 497 503 public static void doInsert(GlobalParameter obj) throws TorqueException 504 { 505 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 506 obj.setNew(false); 507 obj.setModified(false); 508 } 509 510 515 public static void doUpdate(GlobalParameter obj) throws TorqueException 516 { 517 doUpdate(buildCriteria(obj)); 518 obj.setModified(false); 519 } 520 521 526 public static void doDelete(GlobalParameter obj) throws TorqueException 527 { 528 doDelete(buildSelectCriteria(obj)); 529 } 530 531 541 public static void doInsert(GlobalParameter obj, Connection con) 542 throws TorqueException 543 { 544 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 545 obj.setNew(false); 546 obj.setModified(false); 547 } 548 549 559 public static void doUpdate(GlobalParameter obj, Connection con) 560 throws TorqueException 561 { 562 doUpdate(buildCriteria(obj), con); 563 obj.setModified(false); 564 } 565 566 576 public static void doDelete(GlobalParameter obj, Connection con) 577 throws TorqueException 578 { 579 doDelete(buildSelectCriteria(obj), con); 580 } 581 582 589 public static void doDelete(ObjectKey pk) throws TorqueException 590 { 591 BaseGlobalParameterPeer 592 .doDelete(pk, (Connection ) null); 593 } 594 595 605 public static void doDelete(ObjectKey pk, Connection con) 606 throws TorqueException 607 { 608 doDelete(buildCriteria(pk), con); 609 } 610 611 612 public static Criteria buildCriteria( ObjectKey pk ) 613 { 614 Criteria criteria = new Criteria(); 615 criteria.add(PARAMETER_ID, pk); 616 return criteria; 617 } 618 619 620 public static Criteria buildCriteria( GlobalParameter obj ) 621 { 622 Criteria criteria = new Criteria(DATABASE_NAME); 623 if (!obj.isNew()) 624 criteria.add(PARAMETER_ID, obj.getParameterId()); 625 criteria.add(NAME, obj.getName()); 626 criteria.add(VALUE, obj.getValue()); 627 criteria.add(MODULE_ID, obj.getModuleId()); 628 return criteria; 629 } 630 631 632 public static Criteria buildSelectCriteria( GlobalParameter obj ) 633 { 634 Criteria criteria = new Criteria(DATABASE_NAME); 635 if (!obj.isNew()) 636 criteria.add(PARAMETER_ID, obj.getParameterId()); 637 criteria.add(NAME, obj.getName()); 638 criteria.add(VALUE, obj.getValue()); 639 criteria.add(MODULE_ID, obj.getModuleId()); 640 return criteria; 641 } 642 643 644 653 public static GlobalParameter retrieveByPK(Integer pk) 654 throws TorqueException, NoRowsException, TooManyRowsException 655 { 656 return retrieveByPK(SimpleKey.keyFor(pk)); 657 } 658 659 669 public static GlobalParameter retrieveByPK(Integer pk, Connection con) 670 throws TorqueException, NoRowsException, TooManyRowsException 671 { 672 return retrieveByPK(SimpleKey.keyFor(pk), con); 673 } 674 675 684 public static GlobalParameter retrieveByPK(ObjectKey pk) 685 throws TorqueException, NoRowsException, TooManyRowsException 686 { 687 Connection db = null; 688 GlobalParameter retVal = null; 689 try 690 { 691 db = Torque.getConnection(DATABASE_NAME); 692 retVal = retrieveByPK(pk, db); 693 } 694 finally 695 { 696 Torque.closeConnection(db); 697 } 698 return(retVal); 699 } 700 701 711 public static GlobalParameter retrieveByPK(ObjectKey pk, Connection con) 712 throws TorqueException, NoRowsException, TooManyRowsException 713 { 714 Criteria criteria = buildCriteria(pk); 715 List v = doSelect(criteria, con); 716 if (v.size() == 0) 717 { 718 throw new NoRowsException("Failed to select a row."); 719 } 720 else if (v.size() > 1) 721 { 722 throw new TooManyRowsException("Failed to select only one row."); 723 } 724 else 725 { 726 return (GlobalParameter)v.get(0); 727 } 728 } 729 730 737 public static List retrieveByPKs(List pks) 738 throws TorqueException 739 { 740 Connection db = null; 741 List retVal = null; 742 try 743 { 744 db = Torque.getConnection(DATABASE_NAME); 745 retVal = retrieveByPKs(pks, db); 746 } 747 finally 748 { 749 Torque.closeConnection(db); 750 } 751 return(retVal); 752 } 753 754 762 public static List retrieveByPKs( List pks, Connection dbcon ) 763 throws TorqueException 764 { 765 List objs = null; 766 if (pks == null || pks.size() == 0) 767 { 768 objs = new LinkedList (); 769 } 770 else 771 { 772 Criteria criteria = new Criteria(); 773 criteria.addIn( PARAMETER_ID, pks ); 774 objs = doSelect(criteria, dbcon); 775 } 776 return objs; 777 } 778 779 780 781 782 783 784 785 786 787 788 799 protected static List doSelectJoinScarabModule(Criteria criteria) 800 throws TorqueException 801 { 802 setDbName(criteria); 803 804 GlobalParameterPeer.addSelectColumns(criteria); 805 int offset = numColumns + 1; 806 ScarabModulePeer.addSelectColumns(criteria); 807 808 809 criteria.addJoin(GlobalParameterPeer.MODULE_ID, 810 ScarabModulePeer.MODULE_ID); 811 812 813 814 List rows = BasePeer.doSelect(criteria); 815 List results = new ArrayList (); 816 817 for (int i = 0; i < rows.size(); i++) 818 { 819 Record row = (Record) rows.get(i); 820 821 Class omClass = GlobalParameterPeer.getOMClass(); 822 GlobalParameter obj1 = (GlobalParameter) GlobalParameterPeer 823 .row2Object(row, 1, omClass); 824 omClass = ScarabModulePeer.getOMClass(row, offset); 825 ScarabModule obj2 = (ScarabModule)ScarabModulePeer 826 .row2Object(row, offset, omClass); 827 828 boolean newObject = true; 829 for (int j = 0; j < results.size(); j++) 830 { 831 GlobalParameter temp_obj1 = (GlobalParameter)results.get(j); 832 ScarabModule temp_obj2 = (ScarabModule)temp_obj1.getModule(); 833 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 834 { 835 newObject = false; 836 temp_obj2.addGlobalParameter(obj1); 837 break; 838 } 839 } 840 if (newObject) 841 { 842 obj2.initGlobalParameters(); 843 obj2.addGlobalParameter(obj1); 844 } 845 results.add(obj1); 846 } 847 return results; 848 } 849 850 851 852 853 860 protected static TableMap getTableMap() 861 throws TorqueException 862 { 863 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 864 } 865 866 private static void setDbName(Criteria crit) 867 { 868 if (crit.getDbName() == Torque.getDefaultDB()) 872 { 873 crit.setDbName(DATABASE_NAME); 874 } 875 } 876 } 877 | Popular Tags |