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 BaseMITListPeer 38 extends BasePeer 39 { 40 41 42 public static final String DATABASE_NAME = "scarab"; 43 44 45 public static final String TABLE_NAME = "SCARAB_MIT_LIST"; 46 47 52 public static MapBuilder getMapBuilder() 53 throws TorqueException 54 { 55 return getMapBuilder(MITListMapBuilder.CLASS_NAME); 56 } 57 58 59 public static final String LIST_ID; 60 61 public static final String NAME; 62 63 public static final String ACTIVE; 64 65 public static final String MODIFIABLE; 66 67 public static final String USER_ID; 68 69 static 70 { 71 LIST_ID = "SCARAB_MIT_LIST.LIST_ID"; 72 NAME = "SCARAB_MIT_LIST.NAME"; 73 ACTIVE = "SCARAB_MIT_LIST.ACTIVE"; 74 MODIFIABLE = "SCARAB_MIT_LIST.MODIFIABLE"; 75 USER_ID = "SCARAB_MIT_LIST.USER_ID"; 76 if (Torque.isInit()) 77 { 78 try 79 { 80 getMapBuilder(MITListMapBuilder.CLASS_NAME); 81 } 82 catch (Exception e) 83 { 84 log.error("Could not initialize Peer", e); 85 } 86 } 87 else 88 { 89 Torque.registerMapBuilder(MITListMapBuilder.CLASS_NAME); 90 } 91 } 92 93 94 public static final int numColumns = 5; 95 96 97 protected static final String CLASSNAME_DEFAULT = 98 "org.tigris.scarab.om.MITList"; 99 100 101 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 102 103 109 private static Class initClass(String className) 110 { 111 Class c = null; 112 try 113 { 114 c = Class.forName(className); 115 } 116 catch (Throwable t) 117 { 118 log.error("A FATAL ERROR has occurred which should not " 119 + "have happened under any circumstance. Please notify " 120 + "the Torque developers <torque-dev@db.apache.org> " 121 + "and give as many details as possible (including the error " 122 + "stack trace).", t); 123 124 if (t instanceof Error ) 126 { 127 throw (Error ) t.fillInStackTrace(); 128 } 129 } 130 return c; 131 } 132 133 143 public static List resultSet2Objects(java.sql.ResultSet results) 144 throws TorqueException 145 { 146 try 147 { 148 QueryDataSet qds = null; 149 List rows = null; 150 try 151 { 152 qds = new QueryDataSet(results); 153 rows = getSelectResults(qds); 154 } 155 finally 156 { 157 if (qds != null) 158 { 159 qds.close(); 160 } 161 } 162 163 return populateObjects(rows); 164 } 165 catch (SQLException e) 166 { 167 throw new TorqueException(e); 168 } 169 catch (DataSetException e) 170 { 171 throw new TorqueException(e); 172 } 173 } 174 175 176 177 184 public static ObjectKey doInsert(Criteria criteria) 185 throws TorqueException 186 { 187 return BaseMITListPeer 188 .doInsert(criteria, (Connection ) null); 189 } 190 191 201 public static ObjectKey doInsert(Criteria criteria, Connection con) 202 throws TorqueException 203 { 204 if (criteria.containsKey(ACTIVE)) 206 { 207 Object possibleBoolean = criteria.get(ACTIVE); 208 if (possibleBoolean instanceof Boolean ) 209 { 210 criteria.add(ACTIVE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 211 } 212 } 213 if (criteria.containsKey(MODIFIABLE)) 215 { 216 Object possibleBoolean = criteria.get(MODIFIABLE); 217 if (possibleBoolean instanceof Boolean ) 218 { 219 criteria.add(MODIFIABLE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 220 } 221 } 222 223 setDbName(criteria); 224 225 if (con == null) 226 { 227 return BasePeer.doInsert(criteria); 228 } 229 else 230 { 231 return BasePeer.doInsert(criteria, con); 232 } 233 } 234 235 242 public static void addSelectColumns(Criteria criteria) 243 throws TorqueException 244 { 245 criteria.addSelectColumn(LIST_ID); 246 criteria.addSelectColumn(NAME); 247 criteria.addSelectColumn(ACTIVE); 248 criteria.addSelectColumn(MODIFIABLE); 249 criteria.addSelectColumn(USER_ID); 250 } 251 252 261 public static MITList row2Object(Record row, 262 int offset, 263 Class cls) 264 throws TorqueException 265 { 266 try 267 { 268 MITList obj = (MITList) cls.newInstance(); 269 MITListPeer.populateObject(row, offset, obj); 270 obj.setModified(false); 271 obj.setNew(false); 272 273 return obj; 274 } 275 catch (InstantiationException e) 276 { 277 throw new TorqueException(e); 278 } 279 catch (IllegalAccessException e) 280 { 281 throw new TorqueException(e); 282 } 283 } 284 285 294 public static void populateObject(Record row, 295 int offset, 296 MITList obj) 297 throws TorqueException 298 { 299 try 300 { 301 obj.setListId(row.getValue(offset + 0).asLongObj()); 302 obj.setName(row.getValue(offset + 1).asString()); 303 obj.setActive(row.getValue(offset + 2).asBoolean()); 304 obj.setModifiable(row.getValue(offset + 3).asBoolean()); 305 obj.setUserId(row.getValue(offset + 4).asIntegerObj()); 306 } 307 catch (DataSetException e) 308 { 309 throw new TorqueException(e); 310 } 311 } 312 313 321 public static List doSelect(Criteria criteria) throws TorqueException 322 { 323 return populateObjects(doSelectVillageRecords(criteria)); 324 } 325 326 335 public static List doSelect(Criteria criteria, Connection con) 336 throws TorqueException 337 { 338 return populateObjects(doSelectVillageRecords(criteria, con)); 339 } 340 341 351 public static List doSelectVillageRecords(Criteria criteria) 352 throws TorqueException 353 { 354 return BaseMITListPeer 355 .doSelectVillageRecords(criteria, (Connection ) null); 356 } 357 358 367 public static List doSelectVillageRecords(Criteria criteria, Connection con) 368 throws TorqueException 369 { 370 if (criteria.getSelectColumns().size() == 0) 371 { 372 addSelectColumns(criteria); 373 } 374 375 if (criteria.containsKey(ACTIVE)) 377 { 378 Object possibleBoolean = criteria.get(ACTIVE); 379 if (possibleBoolean instanceof Boolean ) 380 { 381 criteria.add(ACTIVE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 382 } 383 } 384 if (criteria.containsKey(MODIFIABLE)) 386 { 387 Object possibleBoolean = criteria.get(MODIFIABLE); 388 if (possibleBoolean instanceof Boolean ) 389 { 390 criteria.add(MODIFIABLE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 391 } 392 } 393 394 setDbName(criteria); 395 396 if (con == null) 399 { 400 return BasePeer.doSelect(criteria); 401 } 402 else 403 { 404 return BasePeer.doSelect(criteria, con); 405 } 406 } 407 408 415 public static List populateObjects(List records) 416 throws TorqueException 417 { 418 List results = new ArrayList (records.size()); 419 420 for (int i = 0; i < records.size(); i++) 422 { 423 Record row = (Record) records.get(i); 424 results.add(MITListPeer.row2Object(row, 1, 425 MITListPeer.getOMClass())); 426 } 427 return results; 428 } 429 430 431 439 public static Class getOMClass() 440 throws TorqueException 441 { 442 return CLASS_DEFAULT; 443 } 444 445 453 public static void doUpdate(Criteria criteria) throws TorqueException 454 { 455 BaseMITListPeer 456 .doUpdate(criteria, (Connection ) null); 457 } 458 459 470 public static void doUpdate(Criteria criteria, Connection con) 471 throws TorqueException 472 { 473 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 474 selectCriteria.put(LIST_ID, criteria.remove(LIST_ID)); 475 if (criteria.containsKey(ACTIVE)) 477 { 478 Object possibleBoolean = criteria.get(ACTIVE); 479 if (possibleBoolean instanceof Boolean ) 480 { 481 criteria.add(ACTIVE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 482 } 483 } 484 if (criteria.containsKey(MODIFIABLE)) 486 { 487 Object possibleBoolean = criteria.get(MODIFIABLE); 488 if (possibleBoolean instanceof Boolean ) 489 { 490 criteria.add(MODIFIABLE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 491 } 492 } 493 494 setDbName(criteria); 495 496 if (con == null) 497 { 498 BasePeer.doUpdate(selectCriteria, criteria); 499 } 500 else 501 { 502 BasePeer.doUpdate(selectCriteria, criteria, con); 503 } 504 } 505 506 513 public static void doDelete(Criteria criteria) throws TorqueException 514 { 515 MITListPeer 516 .doDelete(criteria, (Connection ) null); 517 } 518 519 529 public static void doDelete(Criteria criteria, Connection con) 530 throws TorqueException 531 { 532 if (criteria.containsKey(ACTIVE)) 534 { 535 Object possibleBoolean = criteria.get(ACTIVE); 536 if (possibleBoolean instanceof Boolean ) 537 { 538 criteria.add(ACTIVE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 539 } 540 } 541 if (criteria.containsKey(MODIFIABLE)) 543 { 544 Object possibleBoolean = criteria.get(MODIFIABLE); 545 if (possibleBoolean instanceof Boolean ) 546 { 547 criteria.add(MODIFIABLE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 548 } 549 } 550 551 setDbName(criteria); 552 553 if (con == null) 554 { 555 BasePeer.doDelete(criteria); 556 } 557 else 558 { 559 BasePeer.doDelete(criteria, con); 560 } 561 } 562 563 569 public static List doSelect(MITList obj) throws TorqueException 570 { 571 return doSelect(buildSelectCriteria(obj)); 572 } 573 574 580 public static void doInsert(MITList obj) throws TorqueException 581 { 582 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 583 obj.setNew(false); 584 obj.setModified(false); 585 } 586 587 592 public static void doUpdate(MITList obj) throws TorqueException 593 { 594 doUpdate(buildCriteria(obj)); 595 obj.setModified(false); 596 } 597 598 603 public static void doDelete(MITList obj) throws TorqueException 604 { 605 doDelete(buildSelectCriteria(obj)); 606 } 607 608 618 public static void doInsert(MITList obj, Connection con) 619 throws TorqueException 620 { 621 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 622 obj.setNew(false); 623 obj.setModified(false); 624 } 625 626 636 public static void doUpdate(MITList obj, Connection con) 637 throws TorqueException 638 { 639 doUpdate(buildCriteria(obj), con); 640 obj.setModified(false); 641 } 642 643 653 public static void doDelete(MITList obj, Connection con) 654 throws TorqueException 655 { 656 doDelete(buildSelectCriteria(obj), con); 657 } 658 659 666 public static void doDelete(ObjectKey pk) throws TorqueException 667 { 668 BaseMITListPeer 669 .doDelete(pk, (Connection ) null); 670 } 671 672 682 public static void doDelete(ObjectKey pk, Connection con) 683 throws TorqueException 684 { 685 doDelete(buildCriteria(pk), con); 686 } 687 688 689 public static Criteria buildCriteria( ObjectKey pk ) 690 { 691 Criteria criteria = new Criteria(); 692 criteria.add(LIST_ID, pk); 693 return criteria; 694 } 695 696 697 public static Criteria buildCriteria( MITList obj ) 698 { 699 Criteria criteria = new Criteria(DATABASE_NAME); 700 if (!obj.isNew()) 701 criteria.add(LIST_ID, obj.getListId()); 702 criteria.add(NAME, obj.getName()); 703 criteria.add(ACTIVE, obj.getActive()); 704 criteria.add(MODIFIABLE, obj.getModifiable()); 705 criteria.add(USER_ID, obj.getUserId()); 706 return criteria; 707 } 708 709 710 public static Criteria buildSelectCriteria( MITList obj ) 711 { 712 Criteria criteria = new Criteria(DATABASE_NAME); 713 if (!obj.isNew()) 714 criteria.add(LIST_ID, obj.getListId()); 715 criteria.add(NAME, obj.getName()); 716 criteria.add(ACTIVE, obj.getActive()); 717 criteria.add(MODIFIABLE, obj.getModifiable()); 718 criteria.add(USER_ID, obj.getUserId()); 719 return criteria; 720 } 721 722 723 732 public static MITList retrieveByPK(Long pk) 733 throws TorqueException, NoRowsException, TooManyRowsException 734 { 735 return retrieveByPK(SimpleKey.keyFor(pk)); 736 } 737 738 748 public static MITList retrieveByPK(Long pk, Connection con) 749 throws TorqueException, NoRowsException, TooManyRowsException 750 { 751 return retrieveByPK(SimpleKey.keyFor(pk), con); 752 } 753 754 763 public static MITList retrieveByPK(ObjectKey pk) 764 throws TorqueException, NoRowsException, TooManyRowsException 765 { 766 Connection db = null; 767 MITList retVal = null; 768 try 769 { 770 db = Torque.getConnection(DATABASE_NAME); 771 retVal = retrieveByPK(pk, db); 772 } 773 finally 774 { 775 Torque.closeConnection(db); 776 } 777 return(retVal); 778 } 779 780 790 public static MITList retrieveByPK(ObjectKey pk, Connection con) 791 throws TorqueException, NoRowsException, TooManyRowsException 792 { 793 Criteria criteria = buildCriteria(pk); 794 List v = doSelect(criteria, con); 795 if (v.size() == 0) 796 { 797 throw new NoRowsException("Failed to select a row."); 798 } 799 else if (v.size() > 1) 800 { 801 throw new TooManyRowsException("Failed to select only one row."); 802 } 803 else 804 { 805 return (MITList)v.get(0); 806 } 807 } 808 809 816 public static List retrieveByPKs(List pks) 817 throws TorqueException 818 { 819 Connection db = null; 820 List retVal = null; 821 try 822 { 823 db = Torque.getConnection(DATABASE_NAME); 824 retVal = retrieveByPKs(pks, db); 825 } 826 finally 827 { 828 Torque.closeConnection(db); 829 } 830 return(retVal); 831 } 832 833 841 public static List retrieveByPKs( List pks, Connection dbcon ) 842 throws TorqueException 843 { 844 List objs = null; 845 if (pks == null || pks.size() == 0) 846 { 847 objs = new LinkedList (); 848 } 849 else 850 { 851 Criteria criteria = new Criteria(); 852 criteria.addIn( LIST_ID, pks ); 853 objs = doSelect(criteria, dbcon); 854 } 855 return objs; 856 } 857 858 859 860 861 862 863 864 865 866 867 878 protected static List doSelectJoinScarabUserImpl(Criteria criteria) 879 throws TorqueException 880 { 881 setDbName(criteria); 882 883 MITListPeer.addSelectColumns(criteria); 884 int offset = numColumns + 1; 885 ScarabUserImplPeer.addSelectColumns(criteria); 886 887 888 criteria.addJoin(MITListPeer.USER_ID, 889 ScarabUserImplPeer.USER_ID); 890 891 892 if (criteria.containsKey(ACTIVE)) 894 { 895 Object possibleBoolean = criteria.get(ACTIVE); 896 if (possibleBoolean instanceof Boolean ) 897 { 898 criteria.add(ACTIVE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 899 } 900 } 901 if (criteria.containsKey(MODIFIABLE)) 903 { 904 Object possibleBoolean = criteria.get(MODIFIABLE); 905 if (possibleBoolean instanceof Boolean ) 906 { 907 criteria.add(MODIFIABLE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 908 } 909 } 910 911 List rows = BasePeer.doSelect(criteria); 912 List results = new ArrayList (); 913 914 for (int i = 0; i < rows.size(); i++) 915 { 916 Record row = (Record) rows.get(i); 917 918 Class omClass = MITListPeer.getOMClass(); 919 MITList obj1 = (MITList) MITListPeer 920 .row2Object(row, 1, omClass); 921 omClass = ScarabUserImplPeer.getOMClass(); 922 ScarabUserImpl obj2 = (ScarabUserImpl)ScarabUserImplPeer 923 .row2Object(row, offset, omClass); 924 925 boolean newObject = true; 926 for (int j = 0; j < results.size(); j++) 927 { 928 MITList temp_obj1 = (MITList)results.get(j); 929 ScarabUserImpl temp_obj2 = (ScarabUserImpl)temp_obj1.getScarabUser(); 930 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 931 { 932 newObject = false; 933 temp_obj2.addMITList(obj1); 934 break; 935 } 936 } 937 if (newObject) 938 { 939 obj2.initMITLists(); 940 obj2.addMITList(obj1); 941 } 942 results.add(obj1); 943 } 944 return results; 945 } 946 947 948 949 950 957 protected static TableMap getTableMap() 958 throws TorqueException 959 { 960 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 961 } 962 963 private static void setDbName(Criteria crit) 964 { 965 if (crit.getDbName() == Torque.getDefaultDB()) 969 { 970 crit.setDbName(DATABASE_NAME); 971 } 972 } 973 } 974 | Popular Tags |