1 package org.apache.jetspeed.om.dbregistry; 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.dbregistry.map.*; 32 33 34 40 public abstract class BaseMediatypePeer 41 extends BasePeer 42 { 43 44 45 public static final String DATABASE_NAME = "default"; 46 47 48 public static final String TABLE_NAME = "MEDIATYPE"; 49 50 55 public static MapBuilder getMapBuilder() 56 throws TorqueException 57 { 58 return getMapBuilder(MediatypeMapBuilder.CLASS_NAME); 59 } 60 61 62 public static final String ID; 63 64 public static final String NAME; 65 66 public static final String HIDDEN; 67 68 public static final String MIMETYPE; 69 70 public static final String ROLE; 71 72 public static final String TITLE; 73 74 public static final String DESCRIPTION; 75 76 public static final String IMAGE; 77 78 static 79 { 80 ID = "MEDIATYPE.ID"; 81 NAME = "MEDIATYPE.NAME"; 82 HIDDEN = "MEDIATYPE.HIDDEN"; 83 MIMETYPE = "MEDIATYPE.MIMETYPE"; 84 ROLE = "MEDIATYPE.ROLE"; 85 TITLE = "MEDIATYPE.TITLE"; 86 DESCRIPTION = "MEDIATYPE.DESCRIPTION"; 87 IMAGE = "MEDIATYPE.IMAGE"; 88 if (Torque.isInit()) 89 { 90 try 91 { 92 getMapBuilder(); 93 } 94 catch (Exception e) 95 { 96 log.error("Could not initialize Peer", e); 97 } 98 } 99 else 100 { 101 Torque.registerMapBuilder(MediatypeMapBuilder.CLASS_NAME); 102 } 103 } 104 105 106 public static final int numColumns = 8; 107 108 109 protected static final String CLASSNAME_DEFAULT = 110 "org.apache.jetspeed.om.dbregistry.Mediatype"; 111 112 113 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 114 115 121 private static Class initClass(String className) 122 { 123 Class c = null; 124 try 125 { 126 c = Class.forName(className); 127 } 128 catch (Throwable t) 129 { 130 log.error("A FATAL ERROR has occurred which should not " 131 + "have happened under any circumstance. Please notify " 132 + "the Torque developers <turbine-torque-dev@jakarta.apache.org> " 133 + "and give as many details as possible (including the error " 134 + "stack trace).", t); 135 136 if (t instanceof Error ) 138 { 139 throw (Error ) t.fillInStackTrace(); 140 } 141 } 142 return c; 143 } 144 145 155 public static List resultSet2Objects(java.sql.ResultSet results) 156 throws TorqueException 157 { 158 try 159 { 160 QueryDataSet qds = null; 161 List rows = null; 162 try 163 { 164 qds = new QueryDataSet(results); 165 rows = getSelectResults(qds); 166 } 167 finally 168 { 169 if (qds != null) 170 { 171 qds.close(); 172 } 173 } 174 175 return populateObjects(rows); 176 } 177 catch (SQLException e) 178 { 179 throw new TorqueException(e); 180 } 181 catch (DataSetException e) 182 { 183 throw new TorqueException(e); 184 } 185 } 186 187 188 189 196 public static ObjectKey doInsert(Criteria criteria) 197 throws TorqueException 198 { 199 return BaseMediatypePeer 200 .doInsert(criteria, (Connection ) null); 201 } 202 203 213 public static ObjectKey doInsert(Criteria criteria, Connection con) 214 throws TorqueException 215 { 216 if (criteria.containsKey(HIDDEN)) 218 { 219 Object possibleBoolean = criteria.get(HIDDEN); 220 if (possibleBoolean instanceof Boolean ) 221 { 222 if (((Boolean ) possibleBoolean).booleanValue()) 223 { 224 criteria.add(HIDDEN, 1); 225 } 226 else 227 { 228 criteria.add(HIDDEN, 0); 229 } 230 } 231 } 232 233 if (criteria.getDbName() == Torque.getDefaultDB()) 237 { 238 criteria.setDbName(DATABASE_NAME); 239 } 240 if (con == null) 241 { 242 return BasePeer.doInsert(criteria); 243 } 244 else 245 { 246 return BasePeer.doInsert(criteria, con); 247 } 248 } 249 250 257 public static void addSelectColumns(Criteria criteria) 258 throws TorqueException 259 { 260 criteria.addSelectColumn(ID); 261 criteria.addSelectColumn(NAME); 262 criteria.addSelectColumn(HIDDEN); 263 criteria.addSelectColumn(MIMETYPE); 264 criteria.addSelectColumn(ROLE); 265 criteria.addSelectColumn(TITLE); 266 criteria.addSelectColumn(DESCRIPTION); 267 criteria.addSelectColumn(IMAGE); 268 } 269 270 279 public static Mediatype row2Object(Record row, 280 int offset, 281 Class cls) 282 throws TorqueException 283 { 284 try 285 { 286 Mediatype obj = (Mediatype) cls.newInstance(); 287 MediatypePeer.populateObject(row, offset, obj); 288 obj.setModified(false); 289 obj.setNew(false); 290 291 return obj; 292 } 293 catch (InstantiationException e) 294 { 295 throw new TorqueException(e); 296 } 297 catch (IllegalAccessException e) 298 { 299 throw new TorqueException(e); 300 } 301 } 302 303 312 public static void populateObject(Record row, 313 int offset, 314 Mediatype obj) 315 throws TorqueException 316 { 317 try 318 { 319 obj.setId(row.getValue(offset + 0).asLong()); 320 obj.setName(row.getValue(offset + 1).asString()); 321 obj.setHidden(row.getValue(offset + 2).asBoolean()); 322 obj.setMimetype(row.getValue(offset + 3).asString()); 323 obj.setRole(row.getValue(offset + 4).asString()); 324 obj.setTitle(row.getValue(offset + 5).asString()); 325 obj.setDescription(row.getValue(offset + 6).asString()); 326 obj.setImage(row.getValue(offset + 7).asString()); 327 } 328 catch (DataSetException e) 329 { 330 throw new TorqueException(e); 331 } 332 } 333 334 342 public static List doSelect(Criteria criteria) throws TorqueException 343 { 344 return populateObjects(doSelectVillageRecords(criteria)); 345 } 346 347 356 public static List doSelect(Criteria criteria, Connection con) 357 throws TorqueException 358 { 359 return populateObjects(doSelectVillageRecords(criteria, con)); 360 } 361 362 372 public static List doSelectVillageRecords(Criteria criteria) 373 throws TorqueException 374 { 375 return BaseMediatypePeer 376 .doSelectVillageRecords(criteria, (Connection ) null); 377 } 378 379 387 public static List doSelectVillageRecords(Criteria criteria, Connection con) 388 throws TorqueException 389 { 390 if (criteria.getSelectColumns().size() == 0) 391 { 392 addSelectColumns(criteria); 393 } 394 395 if (criteria.containsKey(HIDDEN)) 397 { 398 Object possibleBoolean = criteria.get(HIDDEN); 399 if (possibleBoolean instanceof Boolean ) 400 { 401 if (((Boolean ) possibleBoolean).booleanValue()) 402 { 403 criteria.add(HIDDEN, 1); 404 } 405 else 406 { 407 criteria.add(HIDDEN, 0); 408 } 409 } 410 } 411 412 if (criteria.getDbName() == Torque.getDefaultDB()) 416 { 417 criteria.setDbName(DATABASE_NAME); 418 } 419 if (con == null) 422 { 423 return BasePeer.doSelect(criteria); 424 } 425 else 426 { 427 return BasePeer.doSelect(criteria, con); 428 } 429 } 430 431 438 public static List populateObjects(List records) 439 throws TorqueException 440 { 441 List results = new ArrayList (records.size()); 442 443 for (int i = 0; i < records.size(); i++) 445 { 446 Record row = (Record) records.get(i); 447 results.add(MediatypePeer.row2Object(row, 1, 448 MediatypePeer.getOMClass())); 449 } 450 return results; 451 } 452 453 454 462 public static Class getOMClass() 463 throws TorqueException 464 { 465 return CLASS_DEFAULT; 466 } 467 468 476 public static void doUpdate(Criteria criteria) throws TorqueException 477 { 478 BaseMediatypePeer 479 .doUpdate(criteria, (Connection ) null); 480 } 481 482 493 public static void doUpdate(Criteria criteria, Connection con) 494 throws TorqueException 495 { 496 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 497 selectCriteria.put(ID, criteria.remove(ID)); 498 if (criteria.containsKey(HIDDEN)) 500 { 501 Object possibleBoolean = criteria.get(HIDDEN); 502 if (possibleBoolean instanceof Boolean ) 503 { 504 if (((Boolean ) possibleBoolean).booleanValue()) 505 { 506 criteria.add(HIDDEN, 1); 507 } 508 else 509 { 510 criteria.add(HIDDEN, 0); 511 } 512 } 513 } 514 515 if (criteria.getDbName() == Torque.getDefaultDB()) 519 { 520 criteria.setDbName(DATABASE_NAME); 521 } 522 if (con == null) 523 { 524 BasePeer.doUpdate(selectCriteria, criteria); 525 } 526 else 527 { 528 BasePeer.doUpdate(selectCriteria, criteria, con); 529 } 530 } 531 532 539 public static void doDelete(Criteria criteria) throws TorqueException 540 { 541 BaseMediatypePeer 542 .doDelete(criteria, (Connection ) null); 543 } 544 545 555 public static void doDelete(Criteria criteria, Connection con) 556 throws TorqueException 557 { 558 if (criteria.containsKey(HIDDEN)) 560 { 561 Object possibleBoolean = criteria.get(HIDDEN); 562 if (possibleBoolean instanceof Boolean ) 563 { 564 if (((Boolean ) possibleBoolean).booleanValue()) 565 { 566 criteria.add(HIDDEN, 1); 567 } 568 else 569 { 570 criteria.add(HIDDEN, 0); 571 } 572 } 573 } 574 575 if (criteria.getDbName() == Torque.getDefaultDB()) 579 { 580 criteria.setDbName(DATABASE_NAME); 581 } 582 if (con == null) 583 { 584 BasePeer.doDelete(criteria); 585 } 586 else 587 { 588 BasePeer.doDelete(criteria, con); 589 } 590 } 591 592 598 public static List doSelect(Mediatype obj) throws TorqueException 599 { 600 return doSelect(buildCriteria(obj)); 601 } 602 603 609 public static void doInsert(Mediatype obj) throws TorqueException 610 { 611 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 612 obj.setNew(false); 613 obj.setModified(false); 614 } 615 616 621 public static void doUpdate(Mediatype obj) throws TorqueException 622 { 623 doUpdate(buildCriteria(obj)); 624 obj.setModified(false); 625 } 626 627 632 public static void doDelete(Mediatype obj) throws TorqueException 633 { 634 doDelete(buildCriteria(obj)); 635 } 636 637 647 public static void doInsert(Mediatype obj, Connection con) 648 throws TorqueException 649 { 650 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 651 obj.setNew(false); 652 obj.setModified(false); 653 } 654 655 665 public static void doUpdate(Mediatype obj, Connection con) 666 throws TorqueException 667 { 668 doUpdate(buildCriteria(obj), con); 669 obj.setModified(false); 670 } 671 672 682 public static void doDelete(Mediatype obj, Connection con) 683 throws TorqueException 684 { 685 doDelete(buildCriteria(obj), con); 686 } 687 688 695 public static void doDelete(ObjectKey pk) throws TorqueException 696 { 697 BaseMediatypePeer 698 .doDelete(pk, (Connection ) null); 699 } 700 701 711 public static void doDelete(ObjectKey pk, Connection con) 712 throws TorqueException 713 { 714 doDelete(buildCriteria(pk), con); 715 } 716 717 718 public static Criteria buildCriteria( ObjectKey pk ) 719 { 720 Criteria criteria = new Criteria(); 721 criteria.add(ID, pk); 722 return criteria; 723 } 724 725 726 public static Criteria buildCriteria( Mediatype obj ) 727 { 728 Criteria criteria = new Criteria(DATABASE_NAME); 729 if (!obj.isNew()) 730 criteria.add(ID, obj.getId()); 731 criteria.add(NAME, obj.getName()); 732 criteria.add(HIDDEN, obj.getHidden()); 733 criteria.add(MIMETYPE, obj.getMimetype()); 734 criteria.add(ROLE, obj.getRole()); 735 criteria.add(TITLE, obj.getTitle()); 736 criteria.add(DESCRIPTION, obj.getDescription()); 737 criteria.add(IMAGE, obj.getImage()); 738 return criteria; 739 } 740 741 742 751 public static Mediatype retrieveByPK(long pk) 752 throws TorqueException, NoRowsException, TooManyRowsException 753 { 754 return retrieveByPK(SimpleKey.keyFor(pk)); 755 } 756 757 766 public static Mediatype retrieveByPK(ObjectKey pk) 767 throws TorqueException, NoRowsException, TooManyRowsException 768 { 769 Connection db = null; 770 Mediatype retVal = null; 771 try 772 { 773 db = Torque.getConnection(DATABASE_NAME); 774 retVal = retrieveByPK(pk, db); 775 } 776 finally 777 { 778 Torque.closeConnection(db); 779 } 780 return(retVal); 781 } 782 783 793 public static Mediatype retrieveByPK(ObjectKey pk, Connection con) 794 throws TorqueException, NoRowsException, TooManyRowsException 795 { 796 Criteria criteria = buildCriteria(pk); 797 List v = doSelect(criteria, con); 798 if (v.size() == 0) 799 { 800 throw new NoRowsException("Failed to select a row."); 801 } 802 else if (v.size() > 1) 803 { 804 throw new TooManyRowsException("Failed to select only one row."); 805 } 806 else 807 { 808 return (Mediatype)v.get(0); 809 } 810 } 811 812 819 public static List retrieveByPKs(List pks) 820 throws TorqueException 821 { 822 Connection db = null; 823 List retVal = null; 824 try 825 { 826 db = Torque.getConnection(DATABASE_NAME); 827 retVal = retrieveByPKs(pks, db); 828 } 829 finally 830 { 831 Torque.closeConnection(db); 832 } 833 return(retVal); 834 } 835 836 844 public static List retrieveByPKs( List pks, Connection dbcon ) 845 throws TorqueException 846 { 847 List objs = null; 848 if (pks == null || pks.size() == 0) 849 { 850 objs = new LinkedList (); 851 } 852 else 853 { 854 Criteria criteria = new Criteria(); 855 criteria.addIn( ID, pks ); 856 objs = doSelect(criteria, dbcon); 857 } 858 return objs; 859 } 860 861 862 863 864 865 866 867 868 869 870 877 protected static TableMap getTableMap() 878 throws TorqueException 879 { 880 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 881 } 882 } 883 | Popular Tags |