1 package org.apache.jetspeed.om.security.turbine; 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.apache.jetspeed.om.security.turbine.map.*; 32 33 34 40 public abstract class BaseTurbineUserPeer 41 extends BasePeer 42 { 43 44 45 public static final String DATABASE_NAME = "default"; 46 47 48 public static final String TABLE_NAME = "TURBINE_USER"; 49 50 55 public static MapBuilder getMapBuilder() 56 throws TorqueException 57 { 58 return getMapBuilder(TurbineUserMapBuilder.CLASS_NAME); 59 } 60 61 62 public static final String USER_ID; 63 64 public static final String LOGIN_NAME; 65 66 public static final String PASSWORD_VALUE; 67 68 public static final String FIRST_NAME; 69 70 public static final String LAST_NAME; 71 72 public static final String EMAIL; 73 74 public static final String CONFIRM_VALUE; 75 76 public static final String MODIFIED; 77 78 public static final String CREATED; 79 80 public static final String LAST_LOGIN; 81 82 public static final String DISABLED; 83 84 public static final String OBJECTDATA; 85 86 public static final String PASSWORD_CHANGED; 87 88 static 89 { 90 USER_ID = "TURBINE_USER.USER_ID"; 91 LOGIN_NAME = "TURBINE_USER.LOGIN_NAME"; 92 PASSWORD_VALUE = "TURBINE_USER.PASSWORD_VALUE"; 93 FIRST_NAME = "TURBINE_USER.FIRST_NAME"; 94 LAST_NAME = "TURBINE_USER.LAST_NAME"; 95 EMAIL = "TURBINE_USER.EMAIL"; 96 CONFIRM_VALUE = "TURBINE_USER.CONFIRM_VALUE"; 97 MODIFIED = "TURBINE_USER.MODIFIED"; 98 CREATED = "TURBINE_USER.CREATED"; 99 LAST_LOGIN = "TURBINE_USER.LAST_LOGIN"; 100 DISABLED = "TURBINE_USER.DISABLED"; 101 OBJECTDATA = "TURBINE_USER.OBJECTDATA"; 102 PASSWORD_CHANGED = "TURBINE_USER.PASSWORD_CHANGED"; 103 if (Torque.isInit()) 104 { 105 try 106 { 107 getMapBuilder(); 108 } 109 catch (Exception e) 110 { 111 log.error("Could not initialize Peer", e); 112 } 113 } 114 else 115 { 116 Torque.registerMapBuilder(TurbineUserMapBuilder.CLASS_NAME); 117 } 118 } 119 120 121 public static final int numColumns = 13; 122 123 124 protected static final String CLASSNAME_DEFAULT = 125 "org.apache.jetspeed.om.security.turbine.TurbineUser"; 126 127 128 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 129 130 136 private static Class initClass(String className) 137 { 138 Class c = null; 139 try 140 { 141 c = Class.forName(className); 142 } 143 catch (Throwable t) 144 { 145 log.error("A FATAL ERROR has occurred which should not " 146 + "have happened under any circumstance. Please notify " 147 + "the Torque developers <turbine-torque-dev@jakarta.apache.org> " 148 + "and give as many details as possible (including the error " 149 + "stack trace).", t); 150 151 if (t instanceof Error ) 153 { 154 throw (Error ) t.fillInStackTrace(); 155 } 156 } 157 return c; 158 } 159 160 170 public static List resultSet2Objects(java.sql.ResultSet results) 171 throws TorqueException 172 { 173 try 174 { 175 QueryDataSet qds = null; 176 List rows = null; 177 try 178 { 179 qds = new QueryDataSet(results); 180 rows = getSelectResults(qds); 181 } 182 finally 183 { 184 if (qds != null) 185 { 186 qds.close(); 187 } 188 } 189 190 return populateObjects(rows); 191 } 192 catch (SQLException e) 193 { 194 throw new TorqueException(e); 195 } 196 catch (DataSetException e) 197 { 198 throw new TorqueException(e); 199 } 200 } 201 202 203 204 211 public static ObjectKey doInsert(Criteria criteria) 212 throws TorqueException 213 { 214 return BaseTurbineUserPeer 215 .doInsert(criteria, (Connection ) null); 216 } 217 218 228 public static ObjectKey doInsert(Criteria criteria, Connection con) 229 throws TorqueException 230 { 231 232 if (criteria.getDbName() == Torque.getDefaultDB()) 236 { 237 criteria.setDbName(DATABASE_NAME); 238 } 239 if (con == null) 240 { 241 return BasePeer.doInsert(criteria); 242 } 243 else 244 { 245 return BasePeer.doInsert(criteria, con); 246 } 247 } 248 249 256 public static void addSelectColumns(Criteria criteria) 257 throws TorqueException 258 { 259 criteria.addSelectColumn(USER_ID); 260 criteria.addSelectColumn(LOGIN_NAME); 261 criteria.addSelectColumn(PASSWORD_VALUE); 262 criteria.addSelectColumn(FIRST_NAME); 263 criteria.addSelectColumn(LAST_NAME); 264 criteria.addSelectColumn(EMAIL); 265 criteria.addSelectColumn(CONFIRM_VALUE); 266 criteria.addSelectColumn(MODIFIED); 267 criteria.addSelectColumn(CREATED); 268 criteria.addSelectColumn(LAST_LOGIN); 269 criteria.addSelectColumn(DISABLED); 270 criteria.addSelectColumn(OBJECTDATA); 271 criteria.addSelectColumn(PASSWORD_CHANGED); 272 } 273 274 283 public static TurbineUser row2Object(Record row, 284 int offset, 285 Class cls) 286 throws TorqueException 287 { 288 try 289 { 290 TurbineUser obj = (TurbineUser) cls.newInstance(); 291 TurbineUserPeer.populateObject(row, offset, obj); 292 obj.setModified(false); 293 obj.setNew(false); 294 295 return obj; 296 } 297 catch (InstantiationException e) 298 { 299 throw new TorqueException(e); 300 } 301 catch (IllegalAccessException e) 302 { 303 throw new TorqueException(e); 304 } 305 } 306 307 316 public static void populateObject(Record row, 317 int offset, 318 TurbineUser obj) 319 throws TorqueException 320 { 321 try 322 { 323 obj.setUserId(row.getValue(offset + 0).asInt()); 324 obj.setLoginName(row.getValue(offset + 1).asString()); 325 obj.setPasswordValue(row.getValue(offset + 2).asString()); 326 obj.setFirstName(row.getValue(offset + 3).asString()); 327 obj.setLastName(row.getValue(offset + 4).asString()); 328 obj.setEmail(row.getValue(offset + 5).asString()); 329 obj.setConfirmValue(row.getValue(offset + 6).asString()); 330 obj.setModified(row.getValue(offset + 7).asUtilDate()); 331 obj.setCreated(row.getValue(offset + 8).asUtilDate()); 332 obj.setLastLogin(row.getValue(offset + 9).asUtilDate()); 333 obj.setDisabled(row.getValue(offset + 10).asString()); 334 obj.setObjectdata(row.getValue(offset + 11).asBytes()); 335 obj.setPasswordChanged(row.getValue(offset + 12).asUtilDate()); 336 } 337 catch (DataSetException e) 338 { 339 throw new TorqueException(e); 340 } 341 } 342 343 351 public static List doSelect(Criteria criteria) throws TorqueException 352 { 353 return populateObjects(doSelectVillageRecords(criteria)); 354 } 355 356 365 public static List doSelect(Criteria criteria, Connection con) 366 throws TorqueException 367 { 368 return populateObjects(doSelectVillageRecords(criteria, con)); 369 } 370 371 381 public static List doSelectVillageRecords(Criteria criteria) 382 throws TorqueException 383 { 384 return BaseTurbineUserPeer 385 .doSelectVillageRecords(criteria, (Connection ) null); 386 } 387 388 396 public static List doSelectVillageRecords(Criteria criteria, Connection con) 397 throws TorqueException 398 { 399 if (criteria.getSelectColumns().size() == 0) 400 { 401 addSelectColumns(criteria); 402 } 403 404 405 if (criteria.getDbName() == Torque.getDefaultDB()) 409 { 410 criteria.setDbName(DATABASE_NAME); 411 } 412 if (con == null) 415 { 416 return BasePeer.doSelect(criteria); 417 } 418 else 419 { 420 return BasePeer.doSelect(criteria, con); 421 } 422 } 423 424 431 public static List populateObjects(List records) 432 throws TorqueException 433 { 434 List results = new ArrayList (records.size()); 435 436 for (int i = 0; i < records.size(); i++) 438 { 439 Record row = (Record) records.get(i); 440 results.add(TurbineUserPeer.row2Object(row, 1, 441 TurbineUserPeer.getOMClass())); 442 } 443 return results; 444 } 445 446 447 455 public static Class getOMClass() 456 throws TorqueException 457 { 458 return CLASS_DEFAULT; 459 } 460 461 469 public static void doUpdate(Criteria criteria) throws TorqueException 470 { 471 BaseTurbineUserPeer 472 .doUpdate(criteria, (Connection ) null); 473 } 474 475 486 public static void doUpdate(Criteria criteria, Connection con) 487 throws TorqueException 488 { 489 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 490 selectCriteria.put(USER_ID, criteria.remove(USER_ID)); 491 492 if (criteria.getDbName() == Torque.getDefaultDB()) 496 { 497 criteria.setDbName(DATABASE_NAME); 498 } 499 if (con == null) 500 { 501 BasePeer.doUpdate(selectCriteria, criteria); 502 } 503 else 504 { 505 BasePeer.doUpdate(selectCriteria, criteria, con); 506 } 507 } 508 509 516 public static void doDelete(Criteria criteria) throws TorqueException 517 { 518 BaseTurbineUserPeer 519 .doDelete(criteria, (Connection ) null); 520 } 521 522 532 public static void doDelete(Criteria criteria, Connection con) 533 throws TorqueException 534 { 535 536 if (criteria.getDbName() == Torque.getDefaultDB()) 540 { 541 criteria.setDbName(DATABASE_NAME); 542 } 543 if (con == null) 544 { 545 BasePeer.doDelete(criteria); 546 } 547 else 548 { 549 BasePeer.doDelete(criteria, con); 550 } 551 } 552 553 559 public static List doSelect(TurbineUser obj) throws TorqueException 560 { 561 return doSelect(buildCriteria(obj)); 562 } 563 564 570 public static void doInsert(TurbineUser obj) throws TorqueException 571 { 572 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 573 obj.setNew(false); 574 obj.setModified(false); 575 } 576 577 582 public static void doUpdate(TurbineUser obj) throws TorqueException 583 { 584 doUpdate(buildCriteria(obj)); 585 obj.setModified(false); 586 } 587 588 593 public static void doDelete(TurbineUser obj) throws TorqueException 594 { 595 doDelete(buildCriteria(obj)); 596 } 597 598 608 public static void doInsert(TurbineUser obj, Connection con) 609 throws TorqueException 610 { 611 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 612 obj.setNew(false); 613 obj.setModified(false); 614 } 615 616 626 public static void doUpdate(TurbineUser obj, Connection con) 627 throws TorqueException 628 { 629 doUpdate(buildCriteria(obj), con); 630 obj.setModified(false); 631 } 632 633 643 public static void doDelete(TurbineUser obj, Connection con) 644 throws TorqueException 645 { 646 doDelete(buildCriteria(obj), con); 647 } 648 649 656 public static void doDelete(ObjectKey pk) throws TorqueException 657 { 658 BaseTurbineUserPeer 659 .doDelete(pk, (Connection ) null); 660 } 661 662 672 public static void doDelete(ObjectKey pk, Connection con) 673 throws TorqueException 674 { 675 doDelete(buildCriteria(pk), con); 676 } 677 678 679 public static Criteria buildCriteria( ObjectKey pk ) 680 { 681 Criteria criteria = new Criteria(); 682 criteria.add(USER_ID, pk); 683 return criteria; 684 } 685 686 687 public static Criteria buildCriteria( TurbineUser obj ) 688 { 689 Criteria criteria = new Criteria(DATABASE_NAME); 690 if (!obj.isNew()) 691 criteria.add(USER_ID, obj.getUserId()); 692 criteria.add(LOGIN_NAME, obj.getLoginName()); 693 criteria.add(PASSWORD_VALUE, obj.getPasswordValue()); 694 criteria.add(FIRST_NAME, obj.getFirstName()); 695 criteria.add(LAST_NAME, obj.getLastName()); 696 criteria.add(EMAIL, obj.getEmail()); 697 criteria.add(CONFIRM_VALUE, obj.getConfirmValue()); 698 criteria.add(MODIFIED, obj.getModified()); 699 criteria.add(CREATED, obj.getCreated()); 700 criteria.add(LAST_LOGIN, obj.getLastLogin()); 701 criteria.add(DISABLED, obj.getDisabled()); 702 criteria.add(OBJECTDATA, obj.getObjectdata()); 703 criteria.add(PASSWORD_CHANGED, obj.getPasswordChanged()); 704 return criteria; 705 } 706 707 708 717 public static TurbineUser retrieveByPK(int pk) 718 throws TorqueException, NoRowsException, TooManyRowsException 719 { 720 return retrieveByPK(SimpleKey.keyFor(pk)); 721 } 722 723 732 public static TurbineUser retrieveByPK(ObjectKey pk) 733 throws TorqueException, NoRowsException, TooManyRowsException 734 { 735 Connection db = null; 736 TurbineUser retVal = null; 737 try 738 { 739 db = Torque.getConnection(DATABASE_NAME); 740 retVal = retrieveByPK(pk, db); 741 } 742 finally 743 { 744 Torque.closeConnection(db); 745 } 746 return(retVal); 747 } 748 749 759 public static TurbineUser retrieveByPK(ObjectKey pk, Connection con) 760 throws TorqueException, NoRowsException, TooManyRowsException 761 { 762 Criteria criteria = buildCriteria(pk); 763 List v = doSelect(criteria, con); 764 if (v.size() == 0) 765 { 766 throw new NoRowsException("Failed to select a row."); 767 } 768 else if (v.size() > 1) 769 { 770 throw new TooManyRowsException("Failed to select only one row."); 771 } 772 else 773 { 774 return (TurbineUser)v.get(0); 775 } 776 } 777 778 785 public static List retrieveByPKs(List pks) 786 throws TorqueException 787 { 788 Connection db = null; 789 List retVal = null; 790 try 791 { 792 db = Torque.getConnection(DATABASE_NAME); 793 retVal = retrieveByPKs(pks, db); 794 } 795 finally 796 { 797 Torque.closeConnection(db); 798 } 799 return(retVal); 800 } 801 802 810 public static List retrieveByPKs( List pks, Connection dbcon ) 811 throws TorqueException 812 { 813 List objs = null; 814 if (pks == null || pks.size() == 0) 815 { 816 objs = new LinkedList (); 817 } 818 else 819 { 820 Criteria criteria = new Criteria(); 821 criteria.addIn( USER_ID, pks ); 822 objs = doSelect(criteria, dbcon); 823 } 824 return objs; 825 } 826 827 828 829 830 831 832 833 834 835 836 843 protected static TableMap getTableMap() 844 throws TorqueException 845 { 846 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 847 } 848 } 849 | Popular Tags |