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 BaseTurbinePermissionPeer 41 extends BasePeer 42 { 43 44 45 public static final String DATABASE_NAME = "default"; 46 47 48 public static final String TABLE_NAME = "TURBINE_PERMISSION"; 49 50 55 public static MapBuilder getMapBuilder() 56 throws TorqueException 57 { 58 return getMapBuilder(TurbinePermissionMapBuilder.CLASS_NAME); 59 } 60 61 62 public static final String PERMISSION_ID; 63 64 public static final String PERMISSION_NAME; 65 66 public static final String OBJECTDATA; 67 68 static 69 { 70 PERMISSION_ID = "TURBINE_PERMISSION.PERMISSION_ID"; 71 PERMISSION_NAME = "TURBINE_PERMISSION.PERMISSION_NAME"; 72 OBJECTDATA = "TURBINE_PERMISSION.OBJECTDATA"; 73 if (Torque.isInit()) 74 { 75 try 76 { 77 getMapBuilder(); 78 } 79 catch (Exception e) 80 { 81 log.error("Could not initialize Peer", e); 82 } 83 } 84 else 85 { 86 Torque.registerMapBuilder(TurbinePermissionMapBuilder.CLASS_NAME); 87 } 88 } 89 90 91 public static final int numColumns = 3; 92 93 94 protected static final String CLASSNAME_DEFAULT = 95 "org.apache.jetspeed.om.security.turbine.TurbinePermission"; 96 97 98 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 99 100 106 private static Class initClass(String className) 107 { 108 Class c = null; 109 try 110 { 111 c = Class.forName(className); 112 } 113 catch (Throwable t) 114 { 115 log.error("A FATAL ERROR has occurred which should not " 116 + "have happened under any circumstance. Please notify " 117 + "the Torque developers <turbine-torque-dev@jakarta.apache.org> " 118 + "and give as many details as possible (including the error " 119 + "stack trace).", t); 120 121 if (t instanceof Error ) 123 { 124 throw (Error ) t.fillInStackTrace(); 125 } 126 } 127 return c; 128 } 129 130 140 public static List resultSet2Objects(java.sql.ResultSet results) 141 throws TorqueException 142 { 143 try 144 { 145 QueryDataSet qds = null; 146 List rows = null; 147 try 148 { 149 qds = new QueryDataSet(results); 150 rows = getSelectResults(qds); 151 } 152 finally 153 { 154 if (qds != null) 155 { 156 qds.close(); 157 } 158 } 159 160 return populateObjects(rows); 161 } 162 catch (SQLException e) 163 { 164 throw new TorqueException(e); 165 } 166 catch (DataSetException e) 167 { 168 throw new TorqueException(e); 169 } 170 } 171 172 173 174 181 public static ObjectKey doInsert(Criteria criteria) 182 throws TorqueException 183 { 184 return BaseTurbinePermissionPeer 185 .doInsert(criteria, (Connection ) null); 186 } 187 188 198 public static ObjectKey doInsert(Criteria criteria, Connection con) 199 throws TorqueException 200 { 201 202 if (criteria.getDbName() == Torque.getDefaultDB()) 206 { 207 criteria.setDbName(DATABASE_NAME); 208 } 209 if (con == null) 210 { 211 return BasePeer.doInsert(criteria); 212 } 213 else 214 { 215 return BasePeer.doInsert(criteria, con); 216 } 217 } 218 219 226 public static void addSelectColumns(Criteria criteria) 227 throws TorqueException 228 { 229 criteria.addSelectColumn(PERMISSION_ID); 230 criteria.addSelectColumn(PERMISSION_NAME); 231 criteria.addSelectColumn(OBJECTDATA); 232 } 233 234 243 public static TurbinePermission row2Object(Record row, 244 int offset, 245 Class cls) 246 throws TorqueException 247 { 248 try 249 { 250 TurbinePermission obj = (TurbinePermission) cls.newInstance(); 251 TurbinePermissionPeer.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 TurbinePermission obj) 279 throws TorqueException 280 { 281 try 282 { 283 obj.setPermissionId(row.getValue(offset + 0).asInt()); 284 obj.setPermissionName(row.getValue(offset + 1).asString()); 285 obj.setObjectdata(row.getValue(offset + 2).asBytes()); 286 } 287 catch (DataSetException e) 288 { 289 throw new TorqueException(e); 290 } 291 } 292 293 301 public static List doSelect(Criteria criteria) throws TorqueException 302 { 303 return populateObjects(doSelectVillageRecords(criteria)); 304 } 305 306 315 public static List doSelect(Criteria criteria, Connection con) 316 throws TorqueException 317 { 318 return populateObjects(doSelectVillageRecords(criteria, con)); 319 } 320 321 331 public static List doSelectVillageRecords(Criteria criteria) 332 throws TorqueException 333 { 334 return BaseTurbinePermissionPeer 335 .doSelectVillageRecords(criteria, (Connection ) null); 336 } 337 338 346 public static List doSelectVillageRecords(Criteria criteria, Connection con) 347 throws TorqueException 348 { 349 if (criteria.getSelectColumns().size() == 0) 350 { 351 addSelectColumns(criteria); 352 } 353 354 355 if (criteria.getDbName() == Torque.getDefaultDB()) 359 { 360 criteria.setDbName(DATABASE_NAME); 361 } 362 if (con == null) 365 { 366 return BasePeer.doSelect(criteria); 367 } 368 else 369 { 370 return BasePeer.doSelect(criteria, con); 371 } 372 } 373 374 381 public static List populateObjects(List records) 382 throws TorqueException 383 { 384 List results = new ArrayList (records.size()); 385 386 for (int i = 0; i < records.size(); i++) 388 { 389 Record row = (Record) records.get(i); 390 results.add(TurbinePermissionPeer.row2Object(row, 1, 391 TurbinePermissionPeer.getOMClass())); 392 } 393 return results; 394 } 395 396 397 405 public static Class getOMClass() 406 throws TorqueException 407 { 408 return CLASS_DEFAULT; 409 } 410 411 419 public static void doUpdate(Criteria criteria) throws TorqueException 420 { 421 BaseTurbinePermissionPeer 422 .doUpdate(criteria, (Connection ) null); 423 } 424 425 436 public static void doUpdate(Criteria criteria, Connection con) 437 throws TorqueException 438 { 439 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 440 selectCriteria.put(PERMISSION_ID, criteria.remove(PERMISSION_ID)); 441 442 if (criteria.getDbName() == Torque.getDefaultDB()) 446 { 447 criteria.setDbName(DATABASE_NAME); 448 } 449 if (con == null) 450 { 451 BasePeer.doUpdate(selectCriteria, criteria); 452 } 453 else 454 { 455 BasePeer.doUpdate(selectCriteria, criteria, con); 456 } 457 } 458 459 466 public static void doDelete(Criteria criteria) throws TorqueException 467 { 468 BaseTurbinePermissionPeer 469 .doDelete(criteria, (Connection ) null); 470 } 471 472 482 public static void doDelete(Criteria criteria, Connection con) 483 throws TorqueException 484 { 485 486 if (criteria.getDbName() == Torque.getDefaultDB()) 490 { 491 criteria.setDbName(DATABASE_NAME); 492 } 493 if (con == null) 494 { 495 BasePeer.doDelete(criteria); 496 } 497 else 498 { 499 BasePeer.doDelete(criteria, con); 500 } 501 } 502 503 509 public static List doSelect(TurbinePermission obj) throws TorqueException 510 { 511 return doSelect(buildCriteria(obj)); 512 } 513 514 520 public static void doInsert(TurbinePermission obj) throws TorqueException 521 { 522 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 523 obj.setNew(false); 524 obj.setModified(false); 525 } 526 527 532 public static void doUpdate(TurbinePermission obj) throws TorqueException 533 { 534 doUpdate(buildCriteria(obj)); 535 obj.setModified(false); 536 } 537 538 543 public static void doDelete(TurbinePermission obj) throws TorqueException 544 { 545 doDelete(buildCriteria(obj)); 546 } 547 548 558 public static void doInsert(TurbinePermission obj, Connection con) 559 throws TorqueException 560 { 561 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 562 obj.setNew(false); 563 obj.setModified(false); 564 } 565 566 576 public static void doUpdate(TurbinePermission obj, Connection con) 577 throws TorqueException 578 { 579 doUpdate(buildCriteria(obj), con); 580 obj.setModified(false); 581 } 582 583 593 public static void doDelete(TurbinePermission obj, Connection con) 594 throws TorqueException 595 { 596 doDelete(buildCriteria(obj), con); 597 } 598 599 606 public static void doDelete(ObjectKey pk) throws TorqueException 607 { 608 BaseTurbinePermissionPeer 609 .doDelete(pk, (Connection ) null); 610 } 611 612 622 public static void doDelete(ObjectKey pk, Connection con) 623 throws TorqueException 624 { 625 doDelete(buildCriteria(pk), con); 626 } 627 628 629 public static Criteria buildCriteria( ObjectKey pk ) 630 { 631 Criteria criteria = new Criteria(); 632 criteria.add(PERMISSION_ID, pk); 633 return criteria; 634 } 635 636 637 public static Criteria buildCriteria( TurbinePermission obj ) 638 { 639 Criteria criteria = new Criteria(DATABASE_NAME); 640 if (!obj.isNew()) 641 criteria.add(PERMISSION_ID, obj.getPermissionId()); 642 criteria.add(PERMISSION_NAME, obj.getPermissionName()); 643 criteria.add(OBJECTDATA, obj.getObjectdata()); 644 return criteria; 645 } 646 647 648 657 public static TurbinePermission retrieveByPK(int pk) 658 throws TorqueException, NoRowsException, TooManyRowsException 659 { 660 return retrieveByPK(SimpleKey.keyFor(pk)); 661 } 662 663 672 public static TurbinePermission retrieveByPK(ObjectKey pk) 673 throws TorqueException, NoRowsException, TooManyRowsException 674 { 675 Connection db = null; 676 TurbinePermission retVal = null; 677 try 678 { 679 db = Torque.getConnection(DATABASE_NAME); 680 retVal = retrieveByPK(pk, db); 681 } 682 finally 683 { 684 Torque.closeConnection(db); 685 } 686 return(retVal); 687 } 688 689 699 public static TurbinePermission retrieveByPK(ObjectKey pk, Connection con) 700 throws TorqueException, NoRowsException, TooManyRowsException 701 { 702 Criteria criteria = buildCriteria(pk); 703 List v = doSelect(criteria, con); 704 if (v.size() == 0) 705 { 706 throw new NoRowsException("Failed to select a row."); 707 } 708 else if (v.size() > 1) 709 { 710 throw new TooManyRowsException("Failed to select only one row."); 711 } 712 else 713 { 714 return (TurbinePermission)v.get(0); 715 } 716 } 717 718 725 public static List retrieveByPKs(List pks) 726 throws TorqueException 727 { 728 Connection db = null; 729 List retVal = null; 730 try 731 { 732 db = Torque.getConnection(DATABASE_NAME); 733 retVal = retrieveByPKs(pks, db); 734 } 735 finally 736 { 737 Torque.closeConnection(db); 738 } 739 return(retVal); 740 } 741 742 750 public static List retrieveByPKs( List pks, Connection dbcon ) 751 throws TorqueException 752 { 753 List objs = null; 754 if (pks == null || pks.size() == 0) 755 { 756 objs = new LinkedList (); 757 } 758 else 759 { 760 Criteria criteria = new Criteria(); 761 criteria.addIn( PERMISSION_ID, pks ); 762 objs = doSelect(criteria, dbcon); 763 } 764 return objs; 765 } 766 767 768 769 770 771 772 773 774 775 776 783 protected static TableMap getTableMap() 784 throws TorqueException 785 { 786 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 787 } 788 } 789 | Popular Tags |