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 35 36 42 public abstract class BaseTurbineRolePermissionPeer 43 extends BasePeer 44 { 45 46 47 public static final String DATABASE_NAME = "default"; 48 49 50 public static final String TABLE_NAME = "TURBINE_ROLE_PERMISSION"; 51 52 57 public static MapBuilder getMapBuilder() 58 throws TorqueException 59 { 60 return getMapBuilder(TurbineRolePermissionMapBuilder.CLASS_NAME); 61 } 62 63 64 public static final String ROLE_ID; 65 66 public static final String PERMISSION_ID; 67 68 static 69 { 70 ROLE_ID = "TURBINE_ROLE_PERMISSION.ROLE_ID"; 71 PERMISSION_ID = "TURBINE_ROLE_PERMISSION.PERMISSION_ID"; 72 if (Torque.isInit()) 73 { 74 try 75 { 76 getMapBuilder(); 77 } 78 catch (Exception e) 79 { 80 log.error("Could not initialize Peer", e); 81 } 82 } 83 else 84 { 85 Torque.registerMapBuilder(TurbineRolePermissionMapBuilder.CLASS_NAME); 86 } 87 } 88 89 90 public static final int numColumns = 2; 91 92 93 protected static final String CLASSNAME_DEFAULT = 94 "org.apache.jetspeed.om.security.turbine.TurbineRolePermission"; 95 96 97 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 98 99 105 private static Class initClass(String className) 106 { 107 Class c = null; 108 try 109 { 110 c = Class.forName(className); 111 } 112 catch (Throwable t) 113 { 114 log.error("A FATAL ERROR has occurred which should not " 115 + "have happened under any circumstance. Please notify " 116 + "the Torque developers <turbine-torque-dev@jakarta.apache.org> " 117 + "and give as many details as possible (including the error " 118 + "stack trace).", t); 119 120 if (t instanceof Error ) 122 { 123 throw (Error ) t.fillInStackTrace(); 124 } 125 } 126 return c; 127 } 128 129 139 public static List resultSet2Objects(java.sql.ResultSet results) 140 throws TorqueException 141 { 142 try 143 { 144 QueryDataSet qds = null; 145 List rows = null; 146 try 147 { 148 qds = new QueryDataSet(results); 149 rows = getSelectResults(qds); 150 } 151 finally 152 { 153 if (qds != null) 154 { 155 qds.close(); 156 } 157 } 158 159 return populateObjects(rows); 160 } 161 catch (SQLException e) 162 { 163 throw new TorqueException(e); 164 } 165 catch (DataSetException e) 166 { 167 throw new TorqueException(e); 168 } 169 } 170 171 172 173 180 public static ObjectKey doInsert(Criteria criteria) 181 throws TorqueException 182 { 183 return BaseTurbineRolePermissionPeer 184 .doInsert(criteria, (Connection ) null); 185 } 186 187 197 public static ObjectKey doInsert(Criteria criteria, Connection con) 198 throws TorqueException 199 { 200 201 if (criteria.getDbName() == Torque.getDefaultDB()) 205 { 206 criteria.setDbName(DATABASE_NAME); 207 } 208 if (con == null) 209 { 210 return BasePeer.doInsert(criteria); 211 } 212 else 213 { 214 return BasePeer.doInsert(criteria, con); 215 } 216 } 217 218 225 public static void addSelectColumns(Criteria criteria) 226 throws TorqueException 227 { 228 criteria.addSelectColumn(ROLE_ID); 229 criteria.addSelectColumn(PERMISSION_ID); 230 } 231 232 241 public static TurbineRolePermission row2Object(Record row, 242 int offset, 243 Class cls) 244 throws TorqueException 245 { 246 try 247 { 248 TurbineRolePermission obj = (TurbineRolePermission) cls.newInstance(); 249 TurbineRolePermissionPeer.populateObject(row, offset, obj); 250 obj.setModified(false); 251 obj.setNew(false); 252 253 return obj; 254 } 255 catch (InstantiationException e) 256 { 257 throw new TorqueException(e); 258 } 259 catch (IllegalAccessException e) 260 { 261 throw new TorqueException(e); 262 } 263 } 264 265 274 public static void populateObject(Record row, 275 int offset, 276 TurbineRolePermission obj) 277 throws TorqueException 278 { 279 try 280 { 281 obj.setRoleId(row.getValue(offset + 0).asInt()); 282 obj.setPermissionId(row.getValue(offset + 1).asInt()); 283 } 284 catch (DataSetException e) 285 { 286 throw new TorqueException(e); 287 } 288 } 289 290 298 public static List doSelect(Criteria criteria) throws TorqueException 299 { 300 return populateObjects(doSelectVillageRecords(criteria)); 301 } 302 303 312 public static List doSelect(Criteria criteria, Connection con) 313 throws TorqueException 314 { 315 return populateObjects(doSelectVillageRecords(criteria, con)); 316 } 317 318 328 public static List doSelectVillageRecords(Criteria criteria) 329 throws TorqueException 330 { 331 return BaseTurbineRolePermissionPeer 332 .doSelectVillageRecords(criteria, (Connection ) null); 333 } 334 335 343 public static List doSelectVillageRecords(Criteria criteria, Connection con) 344 throws TorqueException 345 { 346 if (criteria.getSelectColumns().size() == 0) 347 { 348 addSelectColumns(criteria); 349 } 350 351 352 if (criteria.getDbName() == Torque.getDefaultDB()) 356 { 357 criteria.setDbName(DATABASE_NAME); 358 } 359 if (con == null) 362 { 363 return BasePeer.doSelect(criteria); 364 } 365 else 366 { 367 return BasePeer.doSelect(criteria, con); 368 } 369 } 370 371 378 public static List populateObjects(List records) 379 throws TorqueException 380 { 381 List results = new ArrayList (records.size()); 382 383 for (int i = 0; i < records.size(); i++) 385 { 386 Record row = (Record) records.get(i); 387 results.add(TurbineRolePermissionPeer.row2Object(row, 1, 388 TurbineRolePermissionPeer.getOMClass())); 389 } 390 return results; 391 } 392 393 394 402 public static Class getOMClass() 403 throws TorqueException 404 { 405 return CLASS_DEFAULT; 406 } 407 408 416 public static void doUpdate(Criteria criteria) throws TorqueException 417 { 418 BaseTurbineRolePermissionPeer 419 .doUpdate(criteria, (Connection ) null); 420 } 421 422 433 public static void doUpdate(Criteria criteria, Connection con) 434 throws TorqueException 435 { 436 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 437 selectCriteria.put(ROLE_ID, criteria.remove(ROLE_ID)); 438 selectCriteria.put(PERMISSION_ID, criteria.remove(PERMISSION_ID)); 439 440 if (criteria.getDbName() == Torque.getDefaultDB()) 444 { 445 criteria.setDbName(DATABASE_NAME); 446 } 447 if (con == null) 448 { 449 BasePeer.doUpdate(selectCriteria, criteria); 450 } 451 else 452 { 453 BasePeer.doUpdate(selectCriteria, criteria, con); 454 } 455 } 456 457 464 public static void doDelete(Criteria criteria) throws TorqueException 465 { 466 BaseTurbineRolePermissionPeer 467 .doDelete(criteria, (Connection ) null); 468 } 469 470 480 public static void doDelete(Criteria criteria, Connection con) 481 throws TorqueException 482 { 483 484 if (criteria.getDbName() == Torque.getDefaultDB()) 488 { 489 criteria.setDbName(DATABASE_NAME); 490 } 491 if (con == null) 492 { 493 BasePeer.doDelete(criteria); 494 } 495 else 496 { 497 BasePeer.doDelete(criteria, con); 498 } 499 } 500 501 507 public static List doSelect(TurbineRolePermission obj) throws TorqueException 508 { 509 return doSelect(buildCriteria(obj)); 510 } 511 512 518 public static void doInsert(TurbineRolePermission obj) throws TorqueException 519 { 520 doInsert(buildCriteria(obj)); 521 obj.setNew(false); 522 obj.setModified(false); 523 } 524 525 530 public static void doUpdate(TurbineRolePermission obj) throws TorqueException 531 { 532 doUpdate(buildCriteria(obj)); 533 obj.setModified(false); 534 } 535 536 541 public static void doDelete(TurbineRolePermission obj) throws TorqueException 542 { 543 doDelete(buildCriteria(obj)); 544 } 545 546 556 public static void doInsert(TurbineRolePermission obj, Connection con) 557 throws TorqueException 558 { 559 doInsert(buildCriteria(obj), con); 560 obj.setNew(false); 561 obj.setModified(false); 562 } 563 564 574 public static void doUpdate(TurbineRolePermission obj, Connection con) 575 throws TorqueException 576 { 577 doUpdate(buildCriteria(obj), con); 578 obj.setModified(false); 579 } 580 581 591 public static void doDelete(TurbineRolePermission obj, Connection con) 592 throws TorqueException 593 { 594 doDelete(buildCriteria(obj), con); 595 } 596 597 604 public static void doDelete(ObjectKey pk) throws TorqueException 605 { 606 BaseTurbineRolePermissionPeer 607 .doDelete(pk, (Connection ) null); 608 } 609 610 620 public static void doDelete(ObjectKey pk, Connection con) 621 throws TorqueException 622 { 623 doDelete(buildCriteria(pk), con); 624 } 625 626 627 public static Criteria buildCriteria( ObjectKey pk ) 628 { 629 Criteria criteria = new Criteria(); 630 SimpleKey[] keys = (SimpleKey[])pk.getValue(); 631 criteria.add(ROLE_ID, keys[0]); 632 criteria.add(PERMISSION_ID, keys[1]); 633 return criteria; 634 } 635 636 637 public static Criteria buildCriteria( TurbineRolePermission obj ) 638 { 639 Criteria criteria = new Criteria(DATABASE_NAME); 640 criteria.add(ROLE_ID, obj.getRoleId()); 641 criteria.add(PERMISSION_ID, obj.getPermissionId()); 642 return criteria; 643 } 644 645 646 647 656 public static TurbineRolePermission retrieveByPK(ObjectKey pk) 657 throws TorqueException, NoRowsException, TooManyRowsException 658 { 659 Connection db = null; 660 TurbineRolePermission 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 TurbineRolePermission 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 (TurbineRolePermission)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 ROLE_ID, keys[0], Criteria.EQUAL); 752 Criteria.Criterion c1 = criteria.getNewCriterion( 753 PERMISSION_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 TurbineRolePermission retrieveByPK( 770 int role_id 771 , int permission_id 772 ) throws TorqueException 773 { 774 Connection db = null; 775 TurbineRolePermission retVal = null; 776 try 777 { 778 db = Torque.getConnection(DATABASE_NAME); 779 retVal = retrieveByPK( 780 role_id 781 , permission_id 782 , db); 783 } 784 finally 785 { 786 Torque.closeConnection(db); 787 } 788 return(retVal); 789 } 790 791 798 public static TurbineRolePermission retrieveByPK( 799 int role_id 800 , int permission_id 801 ,Connection con) throws TorqueException 802 { 803 804 Criteria criteria = new Criteria(5); 805 criteria.add(ROLE_ID, role_id); 806 criteria.add(PERMISSION_ID, permission_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 (TurbineRolePermission) v.get(0); 815 } 816 } 817 818 819 820 821 822 823 824 825 836 protected static List doSelectJoinTurbineRole(Criteria c) 837 throws TorqueException 838 { 839 if (c.getDbName() == Torque.getDefaultDB()) 843 { 844 c.setDbName(DATABASE_NAME); 845 } 846 847 TurbineRolePermissionPeer.addSelectColumns(c); 848 int offset = numColumns + 1; 849 TurbineRolePeer.addSelectColumns(c); 850 851 852 c.addJoin(TurbineRolePermissionPeer.ROLE_ID, 853 TurbineRolePeer.ROLE_ID); 854 855 856 857 List rows = BasePeer.doSelect(c); 858 List results = new ArrayList (); 859 860 for (int i = 0; i < rows.size(); i++) 861 { 862 Record row = (Record) rows.get(i); 863 864 Class omClass = TurbineRolePermissionPeer.getOMClass(); 865 TurbineRolePermission obj1 = (TurbineRolePermission) TurbineRolePermissionPeer 866 .row2Object(row, 1, omClass); 867 omClass = TurbineRolePeer.getOMClass(); 868 TurbineRole obj2 = (TurbineRole)TurbineRolePeer 869 .row2Object(row, offset, omClass); 870 871 boolean newObject = true; 872 for (int j = 0; j < results.size(); j++) 873 { 874 TurbineRolePermission temp_obj1 = (TurbineRolePermission)results.get(j); 875 TurbineRole temp_obj2 = (TurbineRole)temp_obj1.getTurbineRole(); 876 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 877 { 878 newObject = false; 879 temp_obj2.addTurbineRolePermission(obj1); 880 break; 881 } 882 } 883 if (newObject) 884 { 885 obj2.initTurbineRolePermissions(); 886 obj2.addTurbineRolePermission(obj1); 887 } 888 results.add(obj1); 889 } 890 return results; 891 } 892 893 894 895 896 907 protected static List doSelectJoinTurbinePermission(Criteria c) 908 throws TorqueException 909 { 910 if (c.getDbName() == Torque.getDefaultDB()) 914 { 915 c.setDbName(DATABASE_NAME); 916 } 917 918 TurbineRolePermissionPeer.addSelectColumns(c); 919 int offset = numColumns + 1; 920 TurbinePermissionPeer.addSelectColumns(c); 921 922 923 c.addJoin(TurbineRolePermissionPeer.PERMISSION_ID, 924 TurbinePermissionPeer.PERMISSION_ID); 925 926 927 928 List rows = BasePeer.doSelect(c); 929 List results = new ArrayList (); 930 931 for (int i = 0; i < rows.size(); i++) 932 { 933 Record row = (Record) rows.get(i); 934 935 Class omClass = TurbineRolePermissionPeer.getOMClass(); 936 TurbineRolePermission obj1 = (TurbineRolePermission) TurbineRolePermissionPeer 937 .row2Object(row, 1, omClass); 938 omClass = TurbinePermissionPeer.getOMClass(); 939 TurbinePermission obj2 = (TurbinePermission)TurbinePermissionPeer 940 .row2Object(row, offset, omClass); 941 942 boolean newObject = true; 943 for (int j = 0; j < results.size(); j++) 944 { 945 TurbineRolePermission temp_obj1 = (TurbineRolePermission)results.get(j); 946 TurbinePermission temp_obj2 = (TurbinePermission)temp_obj1.getTurbinePermission(); 947 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 948 { 949 newObject = false; 950 temp_obj2.addTurbineRolePermission(obj1); 951 break; 952 } 953 } 954 if (newObject) 955 { 956 obj2.initTurbineRolePermissions(); 957 obj2.addTurbineRolePermission(obj1); 958 } 959 results.add(obj1); 960 } 961 return results; 962 } 963 964 965 966 967 974 protected static TableMap getTableMap() 975 throws TorqueException 976 { 977 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 978 } 979 } 980 | Popular Tags |