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 BaseIssueVotePeer 39 extends BasePeer 40 { 41 42 43 public static final String DATABASE_NAME = "scarab"; 44 45 46 public static final String TABLE_NAME = "SCARAB_ISSUE_VOTE"; 47 48 53 public static MapBuilder getMapBuilder() 54 throws TorqueException 55 { 56 return getMapBuilder(IssueVoteMapBuilder.CLASS_NAME); 57 } 58 59 60 public static final String ISSUE_ID; 61 62 public static final String USER_ID; 63 64 public static final String VOTES; 65 66 static 67 { 68 ISSUE_ID = "SCARAB_ISSUE_VOTE.ISSUE_ID"; 69 USER_ID = "SCARAB_ISSUE_VOTE.USER_ID"; 70 VOTES = "SCARAB_ISSUE_VOTE.VOTES"; 71 if (Torque.isInit()) 72 { 73 try 74 { 75 getMapBuilder(IssueVoteMapBuilder.CLASS_NAME); 76 } 77 catch (Exception e) 78 { 79 log.error("Could not initialize Peer", e); 80 } 81 } 82 else 83 { 84 Torque.registerMapBuilder(IssueVoteMapBuilder.CLASS_NAME); 85 } 86 } 87 88 89 public static final int numColumns = 3; 90 91 92 protected static final String CLASSNAME_DEFAULT = 93 "org.tigris.scarab.om.IssueVote"; 94 95 96 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 97 98 104 private static Class initClass(String className) 105 { 106 Class c = null; 107 try 108 { 109 c = Class.forName(className); 110 } 111 catch (Throwable t) 112 { 113 log.error("A FATAL ERROR has occurred which should not " 114 + "have happened under any circumstance. Please notify " 115 + "the Torque developers <torque-dev@db.apache.org> " 116 + "and give as many details as possible (including the error " 117 + "stack trace).", t); 118 119 if (t instanceof Error ) 121 { 122 throw (Error ) t.fillInStackTrace(); 123 } 124 } 125 return c; 126 } 127 128 138 public static List resultSet2Objects(java.sql.ResultSet results) 139 throws TorqueException 140 { 141 try 142 { 143 QueryDataSet qds = null; 144 List rows = null; 145 try 146 { 147 qds = new QueryDataSet(results); 148 rows = getSelectResults(qds); 149 } 150 finally 151 { 152 if (qds != null) 153 { 154 qds.close(); 155 } 156 } 157 158 return populateObjects(rows); 159 } 160 catch (SQLException e) 161 { 162 throw new TorqueException(e); 163 } 164 catch (DataSetException e) 165 { 166 throw new TorqueException(e); 167 } 168 } 169 170 171 172 179 public static ObjectKey doInsert(Criteria criteria) 180 throws TorqueException 181 { 182 return BaseIssueVotePeer 183 .doInsert(criteria, (Connection ) null); 184 } 185 186 196 public static ObjectKey doInsert(Criteria criteria, Connection con) 197 throws TorqueException 198 { 199 200 setDbName(criteria); 201 202 if (con == null) 203 { 204 return BasePeer.doInsert(criteria); 205 } 206 else 207 { 208 return BasePeer.doInsert(criteria, con); 209 } 210 } 211 212 219 public static void addSelectColumns(Criteria criteria) 220 throws TorqueException 221 { 222 criteria.addSelectColumn(ISSUE_ID); 223 criteria.addSelectColumn(USER_ID); 224 criteria.addSelectColumn(VOTES); 225 } 226 227 236 public static IssueVote row2Object(Record row, 237 int offset, 238 Class cls) 239 throws TorqueException 240 { 241 try 242 { 243 IssueVote obj = (IssueVote) cls.newInstance(); 244 IssueVotePeer.populateObject(row, offset, obj); 245 obj.setModified(false); 246 obj.setNew(false); 247 248 return obj; 249 } 250 catch (InstantiationException e) 251 { 252 throw new TorqueException(e); 253 } 254 catch (IllegalAccessException e) 255 { 256 throw new TorqueException(e); 257 } 258 } 259 260 269 public static void populateObject(Record row, 270 int offset, 271 IssueVote obj) 272 throws TorqueException 273 { 274 try 275 { 276 obj.setIssueId(row.getValue(offset + 0).asLongObj()); 277 obj.setUserId(row.getValue(offset + 1).asIntegerObj()); 278 obj.setVotes(row.getValue(offset + 2).asInt()); 279 } 280 catch (DataSetException e) 281 { 282 throw new TorqueException(e); 283 } 284 } 285 286 294 public static List doSelect(Criteria criteria) throws TorqueException 295 { 296 return populateObjects(doSelectVillageRecords(criteria)); 297 } 298 299 308 public static List doSelect(Criteria criteria, Connection con) 309 throws TorqueException 310 { 311 return populateObjects(doSelectVillageRecords(criteria, con)); 312 } 313 314 324 public static List doSelectVillageRecords(Criteria criteria) 325 throws TorqueException 326 { 327 return BaseIssueVotePeer 328 .doSelectVillageRecords(criteria, (Connection ) null); 329 } 330 331 340 public static List doSelectVillageRecords(Criteria criteria, Connection con) 341 throws TorqueException 342 { 343 if (criteria.getSelectColumns().size() == 0) 344 { 345 addSelectColumns(criteria); 346 } 347 348 349 setDbName(criteria); 350 351 if (con == null) 354 { 355 return BasePeer.doSelect(criteria); 356 } 357 else 358 { 359 return BasePeer.doSelect(criteria, con); 360 } 361 } 362 363 370 public static List populateObjects(List records) 371 throws TorqueException 372 { 373 List results = new ArrayList (records.size()); 374 375 for (int i = 0; i < records.size(); i++) 377 { 378 Record row = (Record) records.get(i); 379 results.add(IssueVotePeer.row2Object(row, 1, 380 IssueVotePeer.getOMClass())); 381 } 382 return results; 383 } 384 385 386 394 public static Class getOMClass() 395 throws TorqueException 396 { 397 return CLASS_DEFAULT; 398 } 399 400 408 public static void doUpdate(Criteria criteria) throws TorqueException 409 { 410 BaseIssueVotePeer 411 .doUpdate(criteria, (Connection ) null); 412 } 413 414 425 public static void doUpdate(Criteria criteria, Connection con) 426 throws TorqueException 427 { 428 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 429 selectCriteria.put(ISSUE_ID, criteria.remove(ISSUE_ID)); 430 selectCriteria.put(USER_ID, criteria.remove(USER_ID)); 431 432 setDbName(criteria); 433 434 if (con == null) 435 { 436 BasePeer.doUpdate(selectCriteria, criteria); 437 } 438 else 439 { 440 BasePeer.doUpdate(selectCriteria, criteria, con); 441 } 442 } 443 444 451 public static void doDelete(Criteria criteria) throws TorqueException 452 { 453 IssueVotePeer 454 .doDelete(criteria, (Connection ) null); 455 } 456 457 467 public static void doDelete(Criteria criteria, Connection con) 468 throws TorqueException 469 { 470 471 setDbName(criteria); 472 473 if (con == null) 474 { 475 BasePeer.doDelete(criteria); 476 } 477 else 478 { 479 BasePeer.doDelete(criteria, con); 480 } 481 } 482 483 489 public static List doSelect(IssueVote obj) throws TorqueException 490 { 491 return doSelect(buildSelectCriteria(obj)); 492 } 493 494 500 public static void doInsert(IssueVote obj) throws TorqueException 501 { 502 doInsert(buildCriteria(obj)); 503 obj.setNew(false); 504 obj.setModified(false); 505 } 506 507 512 public static void doUpdate(IssueVote obj) throws TorqueException 513 { 514 doUpdate(buildCriteria(obj)); 515 obj.setModified(false); 516 } 517 518 523 public static void doDelete(IssueVote obj) throws TorqueException 524 { 525 doDelete(buildSelectCriteria(obj)); 526 } 527 528 538 public static void doInsert(IssueVote obj, Connection con) 539 throws TorqueException 540 { 541 doInsert(buildCriteria(obj), con); 542 obj.setNew(false); 543 obj.setModified(false); 544 } 545 546 556 public static void doUpdate(IssueVote obj, Connection con) 557 throws TorqueException 558 { 559 doUpdate(buildCriteria(obj), con); 560 obj.setModified(false); 561 } 562 563 573 public static void doDelete(IssueVote obj, Connection con) 574 throws TorqueException 575 { 576 doDelete(buildSelectCriteria(obj), con); 577 } 578 579 586 public static void doDelete(ObjectKey pk) throws TorqueException 587 { 588 BaseIssueVotePeer 589 .doDelete(pk, (Connection ) null); 590 } 591 592 602 public static void doDelete(ObjectKey pk, Connection con) 603 throws TorqueException 604 { 605 doDelete(buildCriteria(pk), con); 606 } 607 608 609 public static Criteria buildCriteria( ObjectKey pk ) 610 { 611 Criteria criteria = new Criteria(); 612 SimpleKey[] keys = (SimpleKey[])pk.getValue(); 613 criteria.add(ISSUE_ID, keys[0]); 614 criteria.add(USER_ID, keys[1]); 615 return criteria; 616 } 617 618 619 public static Criteria buildCriteria( IssueVote obj ) 620 { 621 Criteria criteria = new Criteria(DATABASE_NAME); 622 criteria.add(ISSUE_ID, obj.getIssueId()); 623 criteria.add(USER_ID, obj.getUserId()); 624 criteria.add(VOTES, obj.getVotes()); 625 return criteria; 626 } 627 628 629 public static Criteria buildSelectCriteria( IssueVote obj ) 630 { 631 Criteria criteria = new Criteria(DATABASE_NAME); 632 criteria.add(ISSUE_ID, obj.getIssueId()); 633 criteria.add(USER_ID, obj.getUserId()); 634 criteria.add(VOTES, obj.getVotes()); 635 return criteria; 636 } 637 638 639 640 649 public static IssueVote retrieveByPK(ObjectKey pk) 650 throws TorqueException, NoRowsException, TooManyRowsException 651 { 652 Connection db = null; 653 IssueVote retVal = null; 654 try 655 { 656 db = Torque.getConnection(DATABASE_NAME); 657 retVal = retrieveByPK(pk, db); 658 } 659 finally 660 { 661 Torque.closeConnection(db); 662 } 663 return(retVal); 664 } 665 666 676 public static IssueVote retrieveByPK(ObjectKey pk, Connection con) 677 throws TorqueException, NoRowsException, TooManyRowsException 678 { 679 Criteria criteria = buildCriteria(pk); 680 List v = doSelect(criteria, con); 681 if (v.size() == 0) 682 { 683 throw new NoRowsException("Failed to select a row."); 684 } 685 else if (v.size() > 1) 686 { 687 throw new TooManyRowsException("Failed to select only one row."); 688 } 689 else 690 { 691 return (IssueVote)v.get(0); 692 } 693 } 694 695 702 public static List retrieveByPKs(List pks) 703 throws TorqueException 704 { 705 Connection db = null; 706 List retVal = null; 707 try 708 { 709 db = Torque.getConnection(DATABASE_NAME); 710 retVal = retrieveByPKs(pks, db); 711 } 712 finally 713 { 714 Torque.closeConnection(db); 715 } 716 return(retVal); 717 } 718 719 727 public static List retrieveByPKs( List pks, Connection dbcon ) 728 throws TorqueException 729 { 730 List objs = null; 731 if (pks == null || pks.size() == 0) 732 { 733 objs = new LinkedList (); 734 } 735 else 736 { 737 Criteria criteria = new Criteria(); 738 Iterator iter = pks.iterator(); 739 while (iter.hasNext()) 740 { 741 ObjectKey pk = (ObjectKey)iter.next(); 742 SimpleKey[] keys = (SimpleKey[])pk.getValue(); 743 Criteria.Criterion c0 = criteria.getNewCriterion( 744 ISSUE_ID, keys[0], Criteria.EQUAL); 745 Criteria.Criterion c1 = criteria.getNewCriterion( 746 USER_ID, keys[1], Criteria.EQUAL); 747 c0.and(c1); 748 criteria.or(c0); 749 } 750 objs = doSelect(criteria, dbcon); 751 } 752 return objs; 753 } 754 755 756 762 public static IssueVote retrieveByPK( 763 Long issue_id 764 , Integer user_id 765 ) throws TorqueException 766 { 767 Connection db = null; 768 IssueVote retVal = null; 769 try 770 { 771 db = Torque.getConnection(DATABASE_NAME); 772 retVal = retrieveByPK( 773 issue_id 774 , user_id 775 , db); 776 } 777 finally 778 { 779 Torque.closeConnection(db); 780 } 781 return(retVal); 782 } 783 784 791 public static IssueVote retrieveByPK( 792 Long issue_id 793 , Integer user_id 794 ,Connection con) throws TorqueException 795 { 796 797 Criteria criteria = new Criteria(5); 798 criteria.add(ISSUE_ID, issue_id); 799 criteria.add(USER_ID, user_id); 800 List v = doSelect(criteria, con); 801 if (v.size() != 1) 802 { 803 throw new TorqueException("Failed to select one and only one row."); 804 } 805 else 806 { 807 return (IssueVote) v.get(0); 808 } 809 } 810 811 812 813 814 815 816 817 818 829 protected static List doSelectJoinIssue(Criteria criteria) 830 throws TorqueException 831 { 832 setDbName(criteria); 833 834 IssueVotePeer.addSelectColumns(criteria); 835 int offset = numColumns + 1; 836 IssuePeer.addSelectColumns(criteria); 837 838 839 criteria.addJoin(IssueVotePeer.ISSUE_ID, 840 IssuePeer.ISSUE_ID); 841 842 843 844 List rows = BasePeer.doSelect(criteria); 845 List results = new ArrayList (); 846 847 for (int i = 0; i < rows.size(); i++) 848 { 849 Record row = (Record) rows.get(i); 850 851 Class omClass = IssueVotePeer.getOMClass(); 852 IssueVote obj1 = (IssueVote) IssueVotePeer 853 .row2Object(row, 1, omClass); 854 omClass = IssuePeer.getOMClass(); 855 Issue obj2 = (Issue)IssuePeer 856 .row2Object(row, offset, omClass); 857 858 boolean newObject = true; 859 for (int j = 0; j < results.size(); j++) 860 { 861 IssueVote temp_obj1 = (IssueVote)results.get(j); 862 Issue temp_obj2 = (Issue)temp_obj1.getIssue(); 863 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 864 { 865 newObject = false; 866 temp_obj2.addIssueVote(obj1); 867 break; 868 } 869 } 870 if (newObject) 871 { 872 obj2.initIssueVotes(); 873 obj2.addIssueVote(obj1); 874 } 875 results.add(obj1); 876 } 877 return results; 878 } 879 880 881 882 883 894 protected static List doSelectJoinScarabUserImpl(Criteria criteria) 895 throws TorqueException 896 { 897 setDbName(criteria); 898 899 IssueVotePeer.addSelectColumns(criteria); 900 int offset = numColumns + 1; 901 ScarabUserImplPeer.addSelectColumns(criteria); 902 903 904 criteria.addJoin(IssueVotePeer.USER_ID, 905 ScarabUserImplPeer.USER_ID); 906 907 908 909 List rows = BasePeer.doSelect(criteria); 910 List results = new ArrayList (); 911 912 for (int i = 0; i < rows.size(); i++) 913 { 914 Record row = (Record) rows.get(i); 915 916 Class omClass = IssueVotePeer.getOMClass(); 917 IssueVote obj1 = (IssueVote) IssueVotePeer 918 .row2Object(row, 1, omClass); 919 omClass = ScarabUserImplPeer.getOMClass(); 920 ScarabUserImpl obj2 = (ScarabUserImpl)ScarabUserImplPeer 921 .row2Object(row, offset, omClass); 922 923 boolean newObject = true; 924 for (int j = 0; j < results.size(); j++) 925 { 926 IssueVote temp_obj1 = (IssueVote)results.get(j); 927 ScarabUserImpl temp_obj2 = (ScarabUserImpl)temp_obj1.getScarabUser(); 928 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 929 { 930 newObject = false; 931 temp_obj2.addIssueVote(obj1); 932 break; 933 } 934 } 935 if (newObject) 936 { 937 obj2.initIssueVotes(); 938 obj2.addIssueVote(obj1); 939 } 940 results.add(obj1); 941 } 942 return results; 943 } 944 945 946 947 948 955 protected static TableMap getTableMap() 956 throws TorqueException 957 { 958 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 959 } 960 961 private static void setDbName(Criteria crit) 962 { 963 if (crit.getDbName() == Torque.getDefaultDB()) 967 { 968 crit.setDbName(DATABASE_NAME); 969 } 970 } 971 } 972 | Popular Tags |