1 package org.apache.fulcrum.security.impl.db.entity; 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.fulcrum.security.impl.db.entity.map.*; 32 33 34 35 36 38 public abstract class BaseTurbineRolePermissionPeer 39 extends BasePeer 40 { 41 42 43 public static final String DATABASE_NAME = "scarab"; 44 45 46 public static final String TABLE_NAME = "TURBINE_ROLE_PERMISSION"; 47 48 53 public static MapBuilder getMapBuilder() 54 throws TorqueException 55 { 56 return getMapBuilder(TurbineRolePermissionMapBuilder.CLASS_NAME); 57 } 58 59 60 public static final String ROLE_ID; 61 62 public static final String PERMISSION_ID; 63 64 static 65 { 66 ROLE_ID = "TURBINE_ROLE_PERMISSION.ROLE_ID"; 67 PERMISSION_ID = "TURBINE_ROLE_PERMISSION.PERMISSION_ID"; 68 if (Torque.isInit()) 69 { 70 try 71 { 72 getMapBuilder(TurbineRolePermissionMapBuilder.CLASS_NAME); 73 } 74 catch (Exception e) 75 { 76 log.error("Could not initialize Peer", e); 77 } 78 } 79 else 80 { 81 Torque.registerMapBuilder(TurbineRolePermissionMapBuilder.CLASS_NAME); 82 } 83 } 84 85 86 public static final int numColumns = 2; 87 88 89 protected static final String CLASSNAME_DEFAULT = 90 "org.apache.fulcrum.security.impl.db.entity.TurbineRolePermission"; 91 92 93 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 94 95 101 private static Class initClass(String className) 102 { 103 Class c = null; 104 try 105 { 106 c = Class.forName(className); 107 } 108 catch (Throwable t) 109 { 110 log.error("A FATAL ERROR has occurred which should not " 111 + "have happened under any circumstance. Please notify " 112 + "the Torque developers <torque-dev@db.apache.org> " 113 + "and give as many details as possible (including the error " 114 + "stack trace).", t); 115 116 if (t instanceof Error ) 118 { 119 throw (Error ) t.fillInStackTrace(); 120 } 121 } 122 return c; 123 } 124 125 135 public static List resultSet2Objects(java.sql.ResultSet results) 136 throws TorqueException 137 { 138 try 139 { 140 QueryDataSet qds = null; 141 List rows = null; 142 try 143 { 144 qds = new QueryDataSet(results); 145 rows = getSelectResults(qds); 146 } 147 finally 148 { 149 if (qds != null) 150 { 151 qds.close(); 152 } 153 } 154 155 return populateObjects(rows); 156 } 157 catch (SQLException e) 158 { 159 throw new TorqueException(e); 160 } 161 catch (DataSetException e) 162 { 163 throw new TorqueException(e); 164 } 165 } 166 167 168 169 176 public static ObjectKey doInsert(Criteria criteria) 177 throws TorqueException 178 { 179 return BaseTurbineRolePermissionPeer 180 .doInsert(criteria, (Connection ) null); 181 } 182 183 193 public static ObjectKey doInsert(Criteria criteria, Connection con) 194 throws TorqueException 195 { 196 197 if (criteria.getDbName() == Torque.getDefaultDB()) 201 { 202 criteria.setDbName(DATABASE_NAME); 203 } 204 if (con == null) 205 { 206 return BasePeer.doInsert(criteria); 207 } 208 else 209 { 210 return BasePeer.doInsert(criteria, con); 211 } 212 } 213 214 221 public static void addSelectColumns(Criteria criteria) 222 throws TorqueException 223 { 224 criteria.addSelectColumn(ROLE_ID); 225 criteria.addSelectColumn(PERMISSION_ID); 226 } 227 228 237 public static TurbineRolePermission row2Object(Record row, 238 int offset, 239 Class cls) 240 throws TorqueException 241 { 242 try 243 { 244 TurbineRolePermission obj = (TurbineRolePermission) cls.newInstance(); 245 TurbineRolePermissionPeer.populateObject(row, offset, obj); 246 obj.setModified(false); 247 obj.setNew(false); 248 249 return obj; 250 } 251 catch (InstantiationException e) 252 { 253 throw new TorqueException(e); 254 } 255 catch (IllegalAccessException e) 256 { 257 throw new TorqueException(e); 258 } 259 } 260 261 270 public static void populateObject(Record row, 271 int offset, 272 TurbineRolePermission obj) 273 throws TorqueException 274 { 275 try 276 { 277 obj.setRoleId(row.getValue(offset + 0).asIntegerObj()); 278 obj.setPermissionId(row.getValue(offset + 1).asIntegerObj()); 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 BaseTurbineRolePermissionPeer 328 .doSelectVillageRecords(criteria, (Connection ) null); 329 } 330 331 339 public static List doSelectVillageRecords(Criteria criteria, Connection con) 340 throws TorqueException 341 { 342 if (criteria.getSelectColumns().size() == 0) 343 { 344 addSelectColumns(criteria); 345 } 346 347 348 if (criteria.getDbName() == Torque.getDefaultDB()) 352 { 353 criteria.setDbName(DATABASE_NAME); 354 } 355 if (con == null) 358 { 359 return BasePeer.doSelect(criteria); 360 } 361 else 362 { 363 return BasePeer.doSelect(criteria, con); 364 } 365 } 366 367 374 public static List populateObjects(List records) 375 throws TorqueException 376 { 377 List results = new ArrayList (records.size()); 378 379 for (int i = 0; i < records.size(); i++) 381 { 382 Record row = (Record) records.get(i); 383 results.add(TurbineRolePermissionPeer.row2Object(row, 1, 384 TurbineRolePermissionPeer.getOMClass())); 385 } 386 return results; 387 } 388 389 390 398 public static Class getOMClass() 399 throws TorqueException 400 { 401 return CLASS_DEFAULT; 402 } 403 404 412 public static void doUpdate(Criteria criteria) throws TorqueException 413 { 414 BaseTurbineRolePermissionPeer 415 .doUpdate(criteria, (Connection ) null); 416 } 417 418 429 public static void doUpdate(Criteria criteria, Connection con) 430 throws TorqueException 431 { 432 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 433 selectCriteria.put(ROLE_ID, criteria.remove(ROLE_ID)); 434 selectCriteria.put(PERMISSION_ID, criteria.remove(PERMISSION_ID)); 435 436 if (criteria.getDbName() == Torque.getDefaultDB()) 440 { 441 criteria.setDbName(DATABASE_NAME); 442 } 443 if (con == null) 444 { 445 BasePeer.doUpdate(selectCriteria, criteria); 446 } 447 else 448 { 449 BasePeer.doUpdate(selectCriteria, criteria, con); 450 } 451 } 452 453 460 public static void doDelete(Criteria criteria) throws TorqueException 461 { 462 BaseTurbineRolePermissionPeer 463 .doDelete(criteria, (Connection ) null); 464 } 465 466 476 public static void doDelete(Criteria criteria, Connection con) 477 throws TorqueException 478 { 479 480 if (criteria.getDbName() == Torque.getDefaultDB()) 484 { 485 criteria.setDbName(DATABASE_NAME); 486 } 487 if (con == null) 488 { 489 BasePeer.doDelete(criteria); 490 } 491 else 492 { 493 BasePeer.doDelete(criteria, con); 494 } 495 } 496 497 503 public static List doSelect(TurbineRolePermission obj) throws TorqueException 504 { 505 return doSelect(buildCriteria(obj)); 506 } 507 508 514 public static void doInsert(TurbineRolePermission obj) throws TorqueException 515 { 516 doInsert(buildCriteria(obj)); 517 obj.setNew(false); 518 obj.setModified(false); 519 } 520 521 526 public static void doUpdate(TurbineRolePermission obj) throws TorqueException 527 { 528 doUpdate(buildCriteria(obj)); 529 obj.setModified(false); 530 } 531 532 537 public static void doDelete(TurbineRolePermission obj) throws TorqueException 538 { 539 doDelete(buildCriteria(obj)); 540 } 541 542 552 public static void doInsert(TurbineRolePermission obj, Connection con) 553 throws TorqueException 554 { 555 doInsert(buildCriteria(obj), con); 556 obj.setNew(false); 557 obj.setModified(false); 558 } 559 560 570 public static void doUpdate(TurbineRolePermission obj, Connection con) 571 throws TorqueException 572 { 573 doUpdate(buildCriteria(obj), con); 574 obj.setModified(false); 575 } 576 577 587 public static void doDelete(TurbineRolePermission obj, Connection con) 588 throws TorqueException 589 { 590 doDelete(buildCriteria(obj), con); 591 } 592 593 600 public static void doDelete(ObjectKey pk) throws TorqueException 601 { 602 BaseTurbineRolePermissionPeer 603 .doDelete(pk, (Connection ) null); 604 } 605 606 616 public static void doDelete(ObjectKey pk, Connection con) 617 throws TorqueException 618 { 619 doDelete(buildCriteria(pk), con); 620 } 621 622 623 public static Criteria buildCriteria( ObjectKey pk ) 624 { 625 Criteria criteria = new Criteria(); 626 SimpleKey[] keys = (SimpleKey[])pk.getValue(); 627 criteria.add(ROLE_ID, keys[0]); 628 criteria.add(PERMISSION_ID, keys[1]); 629 return criteria; 630 } 631 632 633 public static Criteria buildCriteria( TurbineRolePermission obj ) 634 { 635 Criteria criteria = new Criteria(DATABASE_NAME); 636 criteria.add(ROLE_ID, obj.getRoleId()); 637 criteria.add(PERMISSION_ID, obj.getPermissionId()); 638 return criteria; 639 } 640 641 642 643 652 public static TurbineRolePermission retrieveByPK(ObjectKey pk) 653 throws TorqueException, NoRowsException, TooManyRowsException 654 { 655 Connection db = null; 656 TurbineRolePermission retVal = null; 657 try 658 { 659 db = Torque.getConnection(DATABASE_NAME); 660 retVal = retrieveByPK(pk, db); 661 } 662 finally 663 { 664 Torque.closeConnection(db); 665 } 666 return(retVal); 667 } 668 669 679 public static TurbineRolePermission retrieveByPK(ObjectKey pk, Connection con) 680 throws TorqueException, NoRowsException, TooManyRowsException 681 { 682 Criteria criteria = buildCriteria(pk); 683 List v = doSelect(criteria, con); 684 if (v.size() == 0) 685 { 686 throw new NoRowsException("Failed to select a row."); 687 } 688 else if (v.size() > 1) 689 { 690 throw new TooManyRowsException("Failed to select only one row."); 691 } 692 else 693 { 694 return (TurbineRolePermission)v.get(0); 695 } 696 } 697 698 705 public static List retrieveByPKs(List pks) 706 throws TorqueException 707 { 708 Connection db = null; 709 List retVal = null; 710 try 711 { 712 db = Torque.getConnection(DATABASE_NAME); 713 retVal = retrieveByPKs(pks, db); 714 } 715 finally 716 { 717 Torque.closeConnection(db); 718 } 719 return(retVal); 720 } 721 722 730 public static List retrieveByPKs( List pks, Connection dbcon ) 731 throws TorqueException 732 { 733 List objs = null; 734 if (pks == null || pks.size() == 0) 735 { 736 objs = new LinkedList (); 737 } 738 else 739 { 740 Criteria criteria = new Criteria(); 741 Iterator iter = pks.iterator(); 742 while (iter.hasNext()) 743 { 744 ObjectKey pk = (ObjectKey)iter.next(); 745 SimpleKey[] keys = (SimpleKey[])pk.getValue(); 746 Criteria.Criterion c0 = criteria.getNewCriterion( 747 ROLE_ID, keys[0], Criteria.EQUAL); 748 Criteria.Criterion c1 = criteria.getNewCriterion( 749 PERMISSION_ID, keys[1], Criteria.EQUAL); 750 c0.and(c1); 751 criteria.or(c0); 752 } 753 objs = doSelect(criteria, dbcon); 754 } 755 return objs; 756 } 757 758 759 765 public static TurbineRolePermission retrieveByPK( 766 Integer role_id 767 , Integer permission_id 768 ) throws TorqueException 769 { 770 Connection db = null; 771 TurbineRolePermission retVal = null; 772 try 773 { 774 db = Torque.getConnection(DATABASE_NAME); 775 retVal = retrieveByPK( 776 role_id 777 , permission_id 778 , db); 779 } 780 finally 781 { 782 Torque.closeConnection(db); 783 } 784 return(retVal); 785 } 786 787 794 public static TurbineRolePermission retrieveByPK( 795 Integer role_id 796 , Integer permission_id 797 ,Connection con) throws TorqueException 798 { 799 800 Criteria criteria = new Criteria(5); 801 criteria.add(ROLE_ID, role_id); 802 criteria.add(PERMISSION_ID, permission_id); 803 List v = doSelect(criteria, con); 804 if (v.size() != 1) 805 { 806 throw new TorqueException("Failed to select one and only one row."); 807 } 808 else 809 { 810 return (TurbineRolePermission) v.get(0); 811 } 812 } 813 814 815 816 817 818 819 820 821 832 protected static List doSelectJoinTurbineRole(Criteria c) 833 throws TorqueException 834 { 835 if (c.getDbName() == Torque.getDefaultDB()) 839 { 840 c.setDbName(DATABASE_NAME); 841 } 842 843 TurbineRolePermissionPeer.addSelectColumns(c); 844 int offset = numColumns + 1; 845 TurbineRolePeer.addSelectColumns(c); 846 847 848 c.addJoin(TurbineRolePermissionPeer.ROLE_ID, 849 TurbineRolePeer.ROLE_ID); 850 851 852 853 List rows = BasePeer.doSelect(c); 854 List results = new ArrayList (); 855 856 for (int i = 0; i < rows.size(); i++) 857 { 858 Record row = (Record) rows.get(i); 859 860 Class omClass = TurbineRolePermissionPeer.getOMClass(); 861 TurbineRolePermission obj1 = (TurbineRolePermission) TurbineRolePermissionPeer 862 .row2Object(row, 1, omClass); 863 omClass = TurbineRolePeer.getOMClass(); 864 TurbineRole obj2 = (TurbineRole)TurbineRolePeer 865 .row2Object(row, offset, omClass); 866 867 boolean newObject = true; 868 for (int j = 0; j < results.size(); j++) 869 { 870 TurbineRolePermission temp_obj1 = (TurbineRolePermission)results.get(j); 871 TurbineRole temp_obj2 = (TurbineRole)temp_obj1.getTurbineRole(); 872 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 873 { 874 newObject = false; 875 temp_obj2.addTurbineRolePermission(obj1); 876 break; 877 } 878 } 879 if (newObject) 880 { 881 obj2.initTurbineRolePermissions(); 882 obj2.addTurbineRolePermission(obj1); 883 } 884 results.add(obj1); 885 } 886 return results; 887 } 888 889 890 891 892 903 protected static List doSelectJoinTurbinePermission(Criteria c) 904 throws TorqueException 905 { 906 if (c.getDbName() == Torque.getDefaultDB()) 910 { 911 c.setDbName(DATABASE_NAME); 912 } 913 914 TurbineRolePermissionPeer.addSelectColumns(c); 915 int offset = numColumns + 1; 916 TurbinePermissionPeer.addSelectColumns(c); 917 918 919 c.addJoin(TurbineRolePermissionPeer.PERMISSION_ID, 920 TurbinePermissionPeer.PERMISSION_ID); 921 922 923 924 List rows = BasePeer.doSelect(c); 925 List results = new ArrayList (); 926 927 for (int i = 0; i < rows.size(); i++) 928 { 929 Record row = (Record) rows.get(i); 930 931 Class omClass = TurbineRolePermissionPeer.getOMClass(); 932 TurbineRolePermission obj1 = (TurbineRolePermission) TurbineRolePermissionPeer 933 .row2Object(row, 1, omClass); 934 omClass = TurbinePermissionPeer.getOMClass(); 935 TurbinePermission obj2 = (TurbinePermission)TurbinePermissionPeer 936 .row2Object(row, offset, omClass); 937 938 boolean newObject = true; 939 for (int j = 0; j < results.size(); j++) 940 { 941 TurbineRolePermission temp_obj1 = (TurbineRolePermission)results.get(j); 942 TurbinePermission temp_obj2 = (TurbinePermission)temp_obj1.getTurbinePermission(); 943 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 944 { 945 newObject = false; 946 temp_obj2.addTurbineRolePermission(obj1); 947 break; 948 } 949 } 950 if (newObject) 951 { 952 obj2.initTurbineRolePermissions(); 953 obj2.addTurbineRolePermission(obj1); 954 } 955 results.add(obj1); 956 } 957 return results; 958 } 959 960 961 962 963 970 protected static TableMap getTableMap() 971 throws TorqueException 972 { 973 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 974 } 975 } 976 | Popular Tags |