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 36 38 public abstract class BaseUserVotePeer 39 extends BasePeer 40 { 41 42 43 public static final String DATABASE_NAME = "scarab"; 44 45 46 public static final String TABLE_NAME = "SCARAB_USER_VOTE"; 47 48 53 public static MapBuilder getMapBuilder() 54 throws TorqueException 55 { 56 return getMapBuilder(UserVoteMapBuilder.CLASS_NAME); 57 } 58 59 60 public static final String MODULE_ID; 61 62 public static final String USER_ID; 63 64 public static final String TOTAL_VOTES; 65 66 public static final String USED_VOTES; 67 68 static 69 { 70 MODULE_ID = "SCARAB_USER_VOTE.MODULE_ID"; 71 USER_ID = "SCARAB_USER_VOTE.USER_ID"; 72 TOTAL_VOTES = "SCARAB_USER_VOTE.TOTAL_VOTES"; 73 USED_VOTES = "SCARAB_USER_VOTE.USED_VOTES"; 74 if (Torque.isInit()) 75 { 76 try 77 { 78 getMapBuilder(UserVoteMapBuilder.CLASS_NAME); 79 } 80 catch (Exception e) 81 { 82 log.error("Could not initialize Peer", e); 83 } 84 } 85 else 86 { 87 Torque.registerMapBuilder(UserVoteMapBuilder.CLASS_NAME); 88 } 89 } 90 91 92 public static final int numColumns = 4; 93 94 95 protected static final String CLASSNAME_DEFAULT = 96 "org.tigris.scarab.om.UserVote"; 97 98 99 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 100 101 107 private static Class initClass(String className) 108 { 109 Class c = null; 110 try 111 { 112 c = Class.forName(className); 113 } 114 catch (Throwable t) 115 { 116 log.error("A FATAL ERROR has occurred which should not " 117 + "have happened under any circumstance. Please notify " 118 + "the Torque developers <torque-dev@db.apache.org> " 119 + "and give as many details as possible (including the error " 120 + "stack trace).", t); 121 122 if (t instanceof Error ) 124 { 125 throw (Error ) t.fillInStackTrace(); 126 } 127 } 128 return c; 129 } 130 131 141 public static List resultSet2Objects(java.sql.ResultSet results) 142 throws TorqueException 143 { 144 try 145 { 146 QueryDataSet qds = null; 147 List rows = null; 148 try 149 { 150 qds = new QueryDataSet(results); 151 rows = getSelectResults(qds); 152 } 153 finally 154 { 155 if (qds != null) 156 { 157 qds.close(); 158 } 159 } 160 161 return populateObjects(rows); 162 } 163 catch (SQLException e) 164 { 165 throw new TorqueException(e); 166 } 167 catch (DataSetException e) 168 { 169 throw new TorqueException(e); 170 } 171 } 172 173 174 175 182 public static ObjectKey doInsert(Criteria criteria) 183 throws TorqueException 184 { 185 return BaseUserVotePeer 186 .doInsert(criteria, (Connection ) null); 187 } 188 189 199 public static ObjectKey doInsert(Criteria criteria, Connection con) 200 throws TorqueException 201 { 202 203 setDbName(criteria); 204 205 if (con == null) 206 { 207 return BasePeer.doInsert(criteria); 208 } 209 else 210 { 211 return BasePeer.doInsert(criteria, con); 212 } 213 } 214 215 222 public static void addSelectColumns(Criteria criteria) 223 throws TorqueException 224 { 225 criteria.addSelectColumn(MODULE_ID); 226 criteria.addSelectColumn(USER_ID); 227 criteria.addSelectColumn(TOTAL_VOTES); 228 criteria.addSelectColumn(USED_VOTES); 229 } 230 231 240 public static UserVote row2Object(Record row, 241 int offset, 242 Class cls) 243 throws TorqueException 244 { 245 try 246 { 247 UserVote obj = (UserVote) cls.newInstance(); 248 UserVotePeer.populateObject(row, offset, obj); 249 obj.setModified(false); 250 obj.setNew(false); 251 252 return obj; 253 } 254 catch (InstantiationException e) 255 { 256 throw new TorqueException(e); 257 } 258 catch (IllegalAccessException e) 259 { 260 throw new TorqueException(e); 261 } 262 } 263 264 273 public static void populateObject(Record row, 274 int offset, 275 UserVote obj) 276 throws TorqueException 277 { 278 try 279 { 280 obj.setModuleId(row.getValue(offset + 0).asIntegerObj()); 281 obj.setUserId(row.getValue(offset + 1).asIntegerObj()); 282 obj.setTotalVotes(row.getValue(offset + 2).asInt()); 283 obj.setUsedVotes(row.getValue(offset + 3).asInt()); 284 } 285 catch (DataSetException e) 286 { 287 throw new TorqueException(e); 288 } 289 } 290 291 299 public static List doSelect(Criteria criteria) throws TorqueException 300 { 301 return populateObjects(doSelectVillageRecords(criteria)); 302 } 303 304 313 public static List doSelect(Criteria criteria, Connection con) 314 throws TorqueException 315 { 316 return populateObjects(doSelectVillageRecords(criteria, con)); 317 } 318 319 329 public static List doSelectVillageRecords(Criteria criteria) 330 throws TorqueException 331 { 332 return BaseUserVotePeer 333 .doSelectVillageRecords(criteria, (Connection ) null); 334 } 335 336 345 public static List doSelectVillageRecords(Criteria criteria, Connection con) 346 throws TorqueException 347 { 348 if (criteria.getSelectColumns().size() == 0) 349 { 350 addSelectColumns(criteria); 351 } 352 353 354 setDbName(criteria); 355 356 if (con == null) 359 { 360 return BasePeer.doSelect(criteria); 361 } 362 else 363 { 364 return BasePeer.doSelect(criteria, con); 365 } 366 } 367 368 375 public static List populateObjects(List records) 376 throws TorqueException 377 { 378 List results = new ArrayList (records.size()); 379 380 for (int i = 0; i < records.size(); i++) 382 { 383 Record row = (Record) records.get(i); 384 results.add(UserVotePeer.row2Object(row, 1, 385 UserVotePeer.getOMClass())); 386 } 387 return results; 388 } 389 390 391 399 public static Class getOMClass() 400 throws TorqueException 401 { 402 return CLASS_DEFAULT; 403 } 404 405 413 public static void doUpdate(Criteria criteria) throws TorqueException 414 { 415 BaseUserVotePeer 416 .doUpdate(criteria, (Connection ) null); 417 } 418 419 430 public static void doUpdate(Criteria criteria, Connection con) 431 throws TorqueException 432 { 433 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 434 selectCriteria.put(MODULE_ID, criteria.remove(MODULE_ID)); 435 selectCriteria.put(USER_ID, criteria.remove(USER_ID)); 436 437 setDbName(criteria); 438 439 if (con == null) 440 { 441 BasePeer.doUpdate(selectCriteria, criteria); 442 } 443 else 444 { 445 BasePeer.doUpdate(selectCriteria, criteria, con); 446 } 447 } 448 449 456 public static void doDelete(Criteria criteria) throws TorqueException 457 { 458 UserVotePeer 459 .doDelete(criteria, (Connection ) null); 460 } 461 462 472 public static void doDelete(Criteria criteria, Connection con) 473 throws TorqueException 474 { 475 476 setDbName(criteria); 477 478 if (con == null) 479 { 480 BasePeer.doDelete(criteria); 481 } 482 else 483 { 484 BasePeer.doDelete(criteria, con); 485 } 486 } 487 488 494 public static List doSelect(UserVote obj) throws TorqueException 495 { 496 return doSelect(buildSelectCriteria(obj)); 497 } 498 499 505 public static void doInsert(UserVote obj) throws TorqueException 506 { 507 doInsert(buildCriteria(obj)); 508 obj.setNew(false); 509 obj.setModified(false); 510 } 511 512 517 public static void doUpdate(UserVote obj) throws TorqueException 518 { 519 doUpdate(buildCriteria(obj)); 520 obj.setModified(false); 521 } 522 523 528 public static void doDelete(UserVote obj) throws TorqueException 529 { 530 doDelete(buildSelectCriteria(obj)); 531 } 532 533 543 public static void doInsert(UserVote obj, Connection con) 544 throws TorqueException 545 { 546 doInsert(buildCriteria(obj), con); 547 obj.setNew(false); 548 obj.setModified(false); 549 } 550 551 561 public static void doUpdate(UserVote obj, Connection con) 562 throws TorqueException 563 { 564 doUpdate(buildCriteria(obj), con); 565 obj.setModified(false); 566 } 567 568 578 public static void doDelete(UserVote obj, Connection con) 579 throws TorqueException 580 { 581 doDelete(buildSelectCriteria(obj), con); 582 } 583 584 591 public static void doDelete(ObjectKey pk) throws TorqueException 592 { 593 BaseUserVotePeer 594 .doDelete(pk, (Connection ) null); 595 } 596 597 607 public static void doDelete(ObjectKey pk, Connection con) 608 throws TorqueException 609 { 610 doDelete(buildCriteria(pk), con); 611 } 612 613 614 public static Criteria buildCriteria( ObjectKey pk ) 615 { 616 Criteria criteria = new Criteria(); 617 SimpleKey[] keys = (SimpleKey[])pk.getValue(); 618 criteria.add(MODULE_ID, keys[0]); 619 criteria.add(USER_ID, keys[1]); 620 return criteria; 621 } 622 623 624 public static Criteria buildCriteria( UserVote obj ) 625 { 626 Criteria criteria = new Criteria(DATABASE_NAME); 627 criteria.add(MODULE_ID, obj.getModuleId()); 628 criteria.add(USER_ID, obj.getUserId()); 629 criteria.add(TOTAL_VOTES, obj.getTotalVotes()); 630 criteria.add(USED_VOTES, obj.getUsedVotes()); 631 return criteria; 632 } 633 634 635 public static Criteria buildSelectCriteria( UserVote obj ) 636 { 637 Criteria criteria = new Criteria(DATABASE_NAME); 638 criteria.add(MODULE_ID, obj.getModuleId()); 639 criteria.add(USER_ID, obj.getUserId()); 640 criteria.add(TOTAL_VOTES, obj.getTotalVotes()); 641 criteria.add(USED_VOTES, obj.getUsedVotes()); 642 return criteria; 643 } 644 645 646 647 656 public static UserVote retrieveByPK(ObjectKey pk) 657 throws TorqueException, NoRowsException, TooManyRowsException 658 { 659 Connection db = null; 660 UserVote retVal = null; 661 try 662 { 663 db = Torque.getConnection(DATABASE_NAME); 664 retVal = retrieveByPK(pk, db); 665 } 666 finally 667 { 668 Torque.closeConnection(db); 669 } 670 return(retVal); 671 } 672 673 683 public static UserVote retrieveByPK(ObjectKey pk, Connection con) 684 throws TorqueException, NoRowsException, TooManyRowsException 685 { 686 Criteria criteria = buildCriteria(pk); 687 List v = doSelect(criteria, con); 688 if (v.size() == 0) 689 { 690 throw new NoRowsException("Failed to select a row."); 691 } 692 else if (v.size() > 1) 693 { 694 throw new TooManyRowsException("Failed to select only one row."); 695 } 696 else 697 { 698 return (UserVote)v.get(0); 699 } 700 } 701 702 709 public static List retrieveByPKs(List pks) 710 throws TorqueException 711 { 712 Connection db = null; 713 List retVal = null; 714 try 715 { 716 db = Torque.getConnection(DATABASE_NAME); 717 retVal = retrieveByPKs(pks, db); 718 } 719 finally 720 { 721 Torque.closeConnection(db); 722 } 723 return(retVal); 724 } 725 726 734 public static List retrieveByPKs( List pks, Connection dbcon ) 735 throws TorqueException 736 { 737 List objs = null; 738 if (pks == null || pks.size() == 0) 739 { 740 objs = new LinkedList (); 741 } 742 else 743 { 744 Criteria criteria = new Criteria(); 745 Iterator iter = pks.iterator(); 746 while (iter.hasNext()) 747 { 748 ObjectKey pk = (ObjectKey)iter.next(); 749 SimpleKey[] keys = (SimpleKey[])pk.getValue(); 750 Criteria.Criterion c0 = criteria.getNewCriterion( 751 MODULE_ID, keys[0], Criteria.EQUAL); 752 Criteria.Criterion c1 = criteria.getNewCriterion( 753 USER_ID, keys[1], Criteria.EQUAL); 754 c0.and(c1); 755 criteria.or(c0); 756 } 757 objs = doSelect(criteria, dbcon); 758 } 759 return objs; 760 } 761 762 763 769 public static UserVote retrieveByPK( 770 Integer module_id 771 , Integer user_id 772 ) throws TorqueException 773 { 774 Connection db = null; 775 UserVote retVal = null; 776 try 777 { 778 db = Torque.getConnection(DATABASE_NAME); 779 retVal = retrieveByPK( 780 module_id 781 , user_id 782 , db); 783 } 784 finally 785 { 786 Torque.closeConnection(db); 787 } 788 return(retVal); 789 } 790 791 798 public static UserVote retrieveByPK( 799 Integer module_id 800 , Integer user_id 801 ,Connection con) throws TorqueException 802 { 803 804 Criteria criteria = new Criteria(5); 805 criteria.add(MODULE_ID, module_id); 806 criteria.add(USER_ID, user_id); 807 List v = doSelect(criteria, con); 808 if (v.size() != 1) 809 { 810 throw new TorqueException("Failed to select one and only one row."); 811 } 812 else 813 { 814 return (UserVote) v.get(0); 815 } 816 } 817 818 819 820 821 822 823 824 825 836 protected static List doSelectJoinScarabModule(Criteria criteria) 837 throws TorqueException 838 { 839 setDbName(criteria); 840 841 UserVotePeer.addSelectColumns(criteria); 842 int offset = numColumns + 1; 843 ScarabModulePeer.addSelectColumns(criteria); 844 845 846 criteria.addJoin(UserVotePeer.MODULE_ID, 847 ScarabModulePeer.MODULE_ID); 848 849 850 851 List rows = BasePeer.doSelect(criteria); 852 List results = new ArrayList (); 853 854 for (int i = 0; i < rows.size(); i++) 855 { 856 Record row = (Record) rows.get(i); 857 858 Class omClass = UserVotePeer.getOMClass(); 859 UserVote obj1 = (UserVote) UserVotePeer 860 .row2Object(row, 1, omClass); 861 omClass = ScarabModulePeer.getOMClass(row, offset); 862 ScarabModule obj2 = (ScarabModule)ScarabModulePeer 863 .row2Object(row, offset, omClass); 864 865 boolean newObject = true; 866 for (int j = 0; j < results.size(); j++) 867 { 868 UserVote temp_obj1 = (UserVote)results.get(j); 869 ScarabModule temp_obj2 = (ScarabModule)temp_obj1.getModule(); 870 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 871 { 872 newObject = false; 873 temp_obj2.addUserVote(obj1); 874 break; 875 } 876 } 877 if (newObject) 878 { 879 obj2.initUserVotes(); 880 obj2.addUserVote(obj1); 881 } 882 results.add(obj1); 883 } 884 return results; 885 } 886 887 888 889 890 901 protected static List doSelectJoinScarabUserImpl(Criteria criteria) 902 throws TorqueException 903 { 904 setDbName(criteria); 905 906 UserVotePeer.addSelectColumns(criteria); 907 int offset = numColumns + 1; 908 ScarabUserImplPeer.addSelectColumns(criteria); 909 910 911 criteria.addJoin(UserVotePeer.USER_ID, 912 ScarabUserImplPeer.USER_ID); 913 914 915 916 List rows = BasePeer.doSelect(criteria); 917 List results = new ArrayList (); 918 919 for (int i = 0; i < rows.size(); i++) 920 { 921 Record row = (Record) rows.get(i); 922 923 Class omClass = UserVotePeer.getOMClass(); 924 UserVote obj1 = (UserVote) UserVotePeer 925 .row2Object(row, 1, omClass); 926 omClass = ScarabUserImplPeer.getOMClass(); 927 ScarabUserImpl obj2 = (ScarabUserImpl)ScarabUserImplPeer 928 .row2Object(row, offset, omClass); 929 930 boolean newObject = true; 931 for (int j = 0; j < results.size(); j++) 932 { 933 UserVote temp_obj1 = (UserVote)results.get(j); 934 ScarabUserImpl temp_obj2 = (ScarabUserImpl)temp_obj1.getScarabUser(); 935 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 936 { 937 newObject = false; 938 temp_obj2.addUserVote(obj1); 939 break; 940 } 941 } 942 if (newObject) 943 { 944 obj2.initUserVotes(); 945 obj2.addUserVote(obj1); 946 } 947 results.add(obj1); 948 } 949 return results; 950 } 951 952 953 954 955 962 protected static TableMap getTableMap() 963 throws TorqueException 964 { 965 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 966 } 967 968 private static void setDbName(Criteria crit) 969 { 970 if (crit.getDbName() == Torque.getDefaultDB()) 974 { 975 crit.setDbName(DATABASE_NAME); 976 } 977 } 978 } 979 | Popular Tags |