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 BaseUserPreferencePeer 38 extends BasePeer 39 { 40 41 42 public static final String DATABASE_NAME = "scarab"; 43 44 45 public static final String TABLE_NAME = "SCARAB_USER_PREFERENCE"; 46 47 52 public static MapBuilder getMapBuilder() 53 throws TorqueException 54 { 55 return getMapBuilder(UserPreferenceMapBuilder.CLASS_NAME); 56 } 57 58 59 public static final String USER_ID; 60 61 public static final String PASSWORD_EXPIRE; 62 63 public static final String ENTER_ISSUE_REDIRECT; 64 65 public static final String HOME_PAGE; 66 67 public static final String LOCALE; 68 69 static 70 { 71 USER_ID = "SCARAB_USER_PREFERENCE.USER_ID"; 72 PASSWORD_EXPIRE = "SCARAB_USER_PREFERENCE.PASSWORD_EXPIRE"; 73 ENTER_ISSUE_REDIRECT = "SCARAB_USER_PREFERENCE.ENTER_ISSUE_REDIRECT"; 74 HOME_PAGE = "SCARAB_USER_PREFERENCE.HOME_PAGE"; 75 LOCALE = "SCARAB_USER_PREFERENCE.LOCALE"; 76 if (Torque.isInit()) 77 { 78 try 79 { 80 getMapBuilder(UserPreferenceMapBuilder.CLASS_NAME); 81 } 82 catch (Exception e) 83 { 84 log.error("Could not initialize Peer", e); 85 } 86 } 87 else 88 { 89 Torque.registerMapBuilder(UserPreferenceMapBuilder.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.UserPreference"; 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 BaseUserPreferencePeer 188 .doInsert(criteria, (Connection ) null); 189 } 190 191 201 public static ObjectKey doInsert(Criteria criteria, Connection con) 202 throws TorqueException 203 { 204 205 setDbName(criteria); 206 207 if (con == null) 208 { 209 return BasePeer.doInsert(criteria); 210 } 211 else 212 { 213 return BasePeer.doInsert(criteria, con); 214 } 215 } 216 217 224 public static void addSelectColumns(Criteria criteria) 225 throws TorqueException 226 { 227 criteria.addSelectColumn(USER_ID); 228 criteria.addSelectColumn(PASSWORD_EXPIRE); 229 criteria.addSelectColumn(ENTER_ISSUE_REDIRECT); 230 criteria.addSelectColumn(HOME_PAGE); 231 criteria.addSelectColumn(LOCALE); 232 } 233 234 243 public static UserPreference row2Object(Record row, 244 int offset, 245 Class cls) 246 throws TorqueException 247 { 248 try 249 { 250 UserPreference obj = (UserPreference) cls.newInstance(); 251 UserPreferencePeer.populateObject(row, offset, obj); 252 obj.setModified(false); 253 obj.setNew(false); 254 255 return obj; 256 } 257 catch (InstantiationException e) 258 { 259 throw new TorqueException(e); 260 } 261 catch (IllegalAccessException e) 262 { 263 throw new TorqueException(e); 264 } 265 } 266 267 276 public static void populateObject(Record row, 277 int offset, 278 UserPreference obj) 279 throws TorqueException 280 { 281 try 282 { 283 obj.setUserId(row.getValue(offset + 0).asIntegerObj()); 284 obj.setPasswordExpire(row.getValue(offset + 1).asUtilDate()); 285 obj.setEnterIssueRedirect(row.getValue(offset + 2).asInt()); 286 obj.setHomePage(row.getValue(offset + 3).asString()); 287 obj.setLocale(row.getValue(offset + 4).asString()); 288 } 289 catch (DataSetException e) 290 { 291 throw new TorqueException(e); 292 } 293 } 294 295 303 public static List doSelect(Criteria criteria) throws TorqueException 304 { 305 return populateObjects(doSelectVillageRecords(criteria)); 306 } 307 308 317 public static List doSelect(Criteria criteria, Connection con) 318 throws TorqueException 319 { 320 return populateObjects(doSelectVillageRecords(criteria, con)); 321 } 322 323 333 public static List doSelectVillageRecords(Criteria criteria) 334 throws TorqueException 335 { 336 return BaseUserPreferencePeer 337 .doSelectVillageRecords(criteria, (Connection ) null); 338 } 339 340 349 public static List doSelectVillageRecords(Criteria criteria, Connection con) 350 throws TorqueException 351 { 352 if (criteria.getSelectColumns().size() == 0) 353 { 354 addSelectColumns(criteria); 355 } 356 357 358 setDbName(criteria); 359 360 if (con == null) 363 { 364 return BasePeer.doSelect(criteria); 365 } 366 else 367 { 368 return BasePeer.doSelect(criteria, con); 369 } 370 } 371 372 379 public static List populateObjects(List records) 380 throws TorqueException 381 { 382 List results = new ArrayList (records.size()); 383 384 for (int i = 0; i < records.size(); i++) 386 { 387 Record row = (Record) records.get(i); 388 results.add(UserPreferencePeer.row2Object(row, 1, 389 UserPreferencePeer.getOMClass())); 390 } 391 return results; 392 } 393 394 395 403 public static Class getOMClass() 404 throws TorqueException 405 { 406 return CLASS_DEFAULT; 407 } 408 409 417 public static void doUpdate(Criteria criteria) throws TorqueException 418 { 419 BaseUserPreferencePeer 420 .doUpdate(criteria, (Connection ) null); 421 } 422 423 434 public static void doUpdate(Criteria criteria, Connection con) 435 throws TorqueException 436 { 437 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 438 selectCriteria.put(USER_ID, criteria.remove(USER_ID)); 439 440 setDbName(criteria); 441 442 if (con == null) 443 { 444 BasePeer.doUpdate(selectCriteria, criteria); 445 } 446 else 447 { 448 BasePeer.doUpdate(selectCriteria, criteria, con); 449 } 450 } 451 452 459 public static void doDelete(Criteria criteria) throws TorqueException 460 { 461 UserPreferencePeer 462 .doDelete(criteria, (Connection ) null); 463 } 464 465 475 public static void doDelete(Criteria criteria, Connection con) 476 throws TorqueException 477 { 478 479 setDbName(criteria); 480 481 if (con == null) 482 { 483 BasePeer.doDelete(criteria); 484 } 485 else 486 { 487 BasePeer.doDelete(criteria, con); 488 } 489 } 490 491 497 public static List doSelect(UserPreference obj) throws TorqueException 498 { 499 return doSelect(buildSelectCriteria(obj)); 500 } 501 502 508 public static void doInsert(UserPreference obj) throws TorqueException 509 { 510 doInsert(buildCriteria(obj)); 511 obj.setNew(false); 512 obj.setModified(false); 513 } 514 515 520 public static void doUpdate(UserPreference obj) throws TorqueException 521 { 522 doUpdate(buildCriteria(obj)); 523 obj.setModified(false); 524 } 525 526 531 public static void doDelete(UserPreference obj) throws TorqueException 532 { 533 doDelete(buildSelectCriteria(obj)); 534 } 535 536 546 public static void doInsert(UserPreference obj, Connection con) 547 throws TorqueException 548 { 549 doInsert(buildCriteria(obj), con); 550 obj.setNew(false); 551 obj.setModified(false); 552 } 553 554 564 public static void doUpdate(UserPreference obj, Connection con) 565 throws TorqueException 566 { 567 doUpdate(buildCriteria(obj), con); 568 obj.setModified(false); 569 } 570 571 581 public static void doDelete(UserPreference obj, Connection con) 582 throws TorqueException 583 { 584 doDelete(buildSelectCriteria(obj), con); 585 } 586 587 594 public static void doDelete(ObjectKey pk) throws TorqueException 595 { 596 BaseUserPreferencePeer 597 .doDelete(pk, (Connection ) null); 598 } 599 600 610 public static void doDelete(ObjectKey pk, Connection con) 611 throws TorqueException 612 { 613 doDelete(buildCriteria(pk), con); 614 } 615 616 617 public static Criteria buildCriteria( ObjectKey pk ) 618 { 619 Criteria criteria = new Criteria(); 620 criteria.add(USER_ID, pk); 621 return criteria; 622 } 623 624 625 public static Criteria buildCriteria( UserPreference obj ) 626 { 627 Criteria criteria = new Criteria(DATABASE_NAME); 628 criteria.add(USER_ID, obj.getUserId()); 629 criteria.add(PASSWORD_EXPIRE, obj.getPasswordExpire()); 630 criteria.add(ENTER_ISSUE_REDIRECT, obj.getEnterIssueRedirect()); 631 criteria.add(HOME_PAGE, obj.getHomePage()); 632 criteria.add(LOCALE, obj.getLocale()); 633 return criteria; 634 } 635 636 637 public static Criteria buildSelectCriteria( UserPreference obj ) 638 { 639 Criteria criteria = new Criteria(DATABASE_NAME); 640 criteria.add(USER_ID, obj.getUserId()); 641 criteria.add(PASSWORD_EXPIRE, obj.getPasswordExpire()); 642 criteria.add(ENTER_ISSUE_REDIRECT, obj.getEnterIssueRedirect()); 643 criteria.add(HOME_PAGE, obj.getHomePage()); 644 criteria.add(LOCALE, obj.getLocale()); 645 return criteria; 646 } 647 648 649 658 public static UserPreference retrieveByPK(Integer pk) 659 throws TorqueException, NoRowsException, TooManyRowsException 660 { 661 return retrieveByPK(SimpleKey.keyFor(pk)); 662 } 663 664 674 public static UserPreference retrieveByPK(Integer pk, Connection con) 675 throws TorqueException, NoRowsException, TooManyRowsException 676 { 677 return retrieveByPK(SimpleKey.keyFor(pk), con); 678 } 679 680 689 public static UserPreference retrieveByPK(ObjectKey pk) 690 throws TorqueException, NoRowsException, TooManyRowsException 691 { 692 Connection db = null; 693 UserPreference retVal = null; 694 try 695 { 696 db = Torque.getConnection(DATABASE_NAME); 697 retVal = retrieveByPK(pk, db); 698 } 699 finally 700 { 701 Torque.closeConnection(db); 702 } 703 return(retVal); 704 } 705 706 716 public static UserPreference retrieveByPK(ObjectKey pk, Connection con) 717 throws TorqueException, NoRowsException, TooManyRowsException 718 { 719 Criteria criteria = buildCriteria(pk); 720 List v = doSelect(criteria, con); 721 if (v.size() == 0) 722 { 723 throw new NoRowsException("Failed to select a row."); 724 } 725 else if (v.size() > 1) 726 { 727 throw new TooManyRowsException("Failed to select only one row."); 728 } 729 else 730 { 731 return (UserPreference)v.get(0); 732 } 733 } 734 735 742 public static List retrieveByPKs(List pks) 743 throws TorqueException 744 { 745 Connection db = null; 746 List retVal = null; 747 try 748 { 749 db = Torque.getConnection(DATABASE_NAME); 750 retVal = retrieveByPKs(pks, db); 751 } 752 finally 753 { 754 Torque.closeConnection(db); 755 } 756 return(retVal); 757 } 758 759 767 public static List retrieveByPKs( List pks, Connection dbcon ) 768 throws TorqueException 769 { 770 List objs = null; 771 if (pks == null || pks.size() == 0) 772 { 773 objs = new LinkedList (); 774 } 775 else 776 { 777 Criteria criteria = new Criteria(); 778 criteria.addIn( USER_ID, pks ); 779 objs = doSelect(criteria, dbcon); 780 } 781 return objs; 782 } 783 784 785 786 787 788 789 790 791 792 793 804 protected static List doSelectJoinScarabUserImpl(Criteria criteria) 805 throws TorqueException 806 { 807 setDbName(criteria); 808 809 UserPreferencePeer.addSelectColumns(criteria); 810 int offset = numColumns + 1; 811 ScarabUserImplPeer.addSelectColumns(criteria); 812 813 814 criteria.addJoin(UserPreferencePeer.USER_ID, 815 ScarabUserImplPeer.USER_ID); 816 817 818 819 List rows = BasePeer.doSelect(criteria); 820 List results = new ArrayList (); 821 822 for (int i = 0; i < rows.size(); i++) 823 { 824 Record row = (Record) rows.get(i); 825 826 Class omClass = UserPreferencePeer.getOMClass(); 827 UserPreference obj1 = (UserPreference) UserPreferencePeer 828 .row2Object(row, 1, omClass); 829 omClass = ScarabUserImplPeer.getOMClass(); 830 ScarabUserImpl obj2 = (ScarabUserImpl)ScarabUserImplPeer 831 .row2Object(row, offset, omClass); 832 833 boolean newObject = true; 834 for (int j = 0; j < results.size(); j++) 835 { 836 UserPreference temp_obj1 = (UserPreference)results.get(j); 837 ScarabUserImpl temp_obj2 = (ScarabUserImpl)temp_obj1.getScarabUser(); 838 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 839 { 840 newObject = false; 841 temp_obj2.addUserPreference(obj1); 842 break; 843 } 844 } 845 if (newObject) 846 { 847 obj2.initUserPreferences(); 848 obj2.addUserPreference(obj1); 849 } 850 results.add(obj1); 851 } 852 return results; 853 } 854 855 856 857 858 865 protected static TableMap getTableMap() 866 throws TorqueException 867 { 868 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 869 } 870 871 private static void setDbName(Criteria crit) 872 { 873 if (crit.getDbName() == Torque.getDefaultDB()) 877 { 878 crit.setDbName(DATABASE_NAME); 879 } 880 } 881 } 882 | Popular Tags |