1 package org.tigris.scarab.om; 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.tigris.scarab.om.map.*; 32 33 34 36 public abstract class BaseAttachmentTypePeer 37 extends BasePeer 38 { 39 40 41 public static final String DATABASE_NAME = "scarab"; 42 43 44 public static final String TABLE_NAME = "SCARAB_ATTACHMENT_TYPE"; 45 46 51 public static MapBuilder getMapBuilder() 52 throws TorqueException 53 { 54 return getMapBuilder(AttachmentTypeMapBuilder.CLASS_NAME); 55 } 56 57 58 public static final String ATTACHMENT_TYPE_ID; 59 60 public static final String ATTACHMENT_TYPE_NAME; 61 62 public static final String SEARCHABLE; 63 64 static 65 { 66 ATTACHMENT_TYPE_ID = "SCARAB_ATTACHMENT_TYPE.ATTACHMENT_TYPE_ID"; 67 ATTACHMENT_TYPE_NAME = "SCARAB_ATTACHMENT_TYPE.ATTACHMENT_TYPE_NAME"; 68 SEARCHABLE = "SCARAB_ATTACHMENT_TYPE.SEARCHABLE"; 69 if (Torque.isInit()) 70 { 71 try 72 { 73 getMapBuilder(AttachmentTypeMapBuilder.CLASS_NAME); 74 } 75 catch (Exception e) 76 { 77 log.error("Could not initialize Peer", e); 78 } 79 } 80 else 81 { 82 Torque.registerMapBuilder(AttachmentTypeMapBuilder.CLASS_NAME); 83 } 84 } 85 86 87 public static final int numColumns = 3; 88 89 90 protected static final String CLASSNAME_DEFAULT = 91 "org.tigris.scarab.om.AttachmentType"; 92 93 94 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 95 96 102 private static Class initClass(String className) 103 { 104 Class c = null; 105 try 106 { 107 c = Class.forName(className); 108 } 109 catch (Throwable t) 110 { 111 log.error("A FATAL ERROR has occurred which should not " 112 + "have happened under any circumstance. Please notify " 113 + "the Torque developers <torque-dev@db.apache.org> " 114 + "and give as many details as possible (including the error " 115 + "stack trace).", t); 116 117 if (t instanceof Error ) 119 { 120 throw (Error ) t.fillInStackTrace(); 121 } 122 } 123 return c; 124 } 125 126 136 public static List resultSet2Objects(java.sql.ResultSet results) 137 throws TorqueException 138 { 139 try 140 { 141 QueryDataSet qds = null; 142 List rows = null; 143 try 144 { 145 qds = new QueryDataSet(results); 146 rows = getSelectResults(qds); 147 } 148 finally 149 { 150 if (qds != null) 151 { 152 qds.close(); 153 } 154 } 155 156 return populateObjects(rows); 157 } 158 catch (SQLException e) 159 { 160 throw new TorqueException(e); 161 } 162 catch (DataSetException e) 163 { 164 throw new TorqueException(e); 165 } 166 } 167 168 169 170 177 public static ObjectKey doInsert(Criteria criteria) 178 throws TorqueException 179 { 180 return BaseAttachmentTypePeer 181 .doInsert(criteria, (Connection ) null); 182 } 183 184 194 public static ObjectKey doInsert(Criteria criteria, Connection con) 195 throws TorqueException 196 { 197 if (criteria.containsKey(SEARCHABLE)) 199 { 200 Object possibleBoolean = criteria.get(SEARCHABLE); 201 if (possibleBoolean instanceof Boolean ) 202 { 203 criteria.add(SEARCHABLE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 204 } 205 } 206 207 setDbName(criteria); 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(ATTACHMENT_TYPE_ID); 230 criteria.addSelectColumn(ATTACHMENT_TYPE_NAME); 231 criteria.addSelectColumn(SEARCHABLE); 232 } 233 234 243 public static AttachmentType row2Object(Record row, 244 int offset, 245 Class cls) 246 throws TorqueException 247 { 248 try 249 { 250 AttachmentType obj = (AttachmentType) cls.newInstance(); 251 AttachmentTypePeer.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 AttachmentType obj) 279 throws TorqueException 280 { 281 try 282 { 283 obj.setAttachmentTypeId(row.getValue(offset + 0).asIntegerObj()); 284 obj.setName(row.getValue(offset + 1).asString()); 285 obj.setSearchable(row.getValue(offset + 2).asBoolean()); 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 BaseAttachmentTypePeer 335 .doSelectVillageRecords(criteria, (Connection ) null); 336 } 337 338 347 public static List doSelectVillageRecords(Criteria criteria, Connection con) 348 throws TorqueException 349 { 350 if (criteria.getSelectColumns().size() == 0) 351 { 352 addSelectColumns(criteria); 353 } 354 355 if (criteria.containsKey(SEARCHABLE)) 357 { 358 Object possibleBoolean = criteria.get(SEARCHABLE); 359 if (possibleBoolean instanceof Boolean ) 360 { 361 criteria.add(SEARCHABLE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 362 } 363 } 364 365 setDbName(criteria); 366 367 if (con == null) 370 { 371 return BasePeer.doSelect(criteria); 372 } 373 else 374 { 375 return BasePeer.doSelect(criteria, con); 376 } 377 } 378 379 386 public static List populateObjects(List records) 387 throws TorqueException 388 { 389 List results = new ArrayList (records.size()); 390 391 for (int i = 0; i < records.size(); i++) 393 { 394 Record row = (Record) records.get(i); 395 results.add(AttachmentTypePeer.row2Object(row, 1, 396 AttachmentTypePeer.getOMClass())); 397 } 398 return results; 399 } 400 401 402 410 public static Class getOMClass() 411 throws TorqueException 412 { 413 return CLASS_DEFAULT; 414 } 415 416 424 public static void doUpdate(Criteria criteria) throws TorqueException 425 { 426 BaseAttachmentTypePeer 427 .doUpdate(criteria, (Connection ) null); 428 } 429 430 441 public static void doUpdate(Criteria criteria, Connection con) 442 throws TorqueException 443 { 444 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 445 selectCriteria.put(ATTACHMENT_TYPE_ID, criteria.remove(ATTACHMENT_TYPE_ID)); 446 if (criteria.containsKey(SEARCHABLE)) 448 { 449 Object possibleBoolean = criteria.get(SEARCHABLE); 450 if (possibleBoolean instanceof Boolean ) 451 { 452 criteria.add(SEARCHABLE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 453 } 454 } 455 456 setDbName(criteria); 457 458 if (con == null) 459 { 460 BasePeer.doUpdate(selectCriteria, criteria); 461 } 462 else 463 { 464 BasePeer.doUpdate(selectCriteria, criteria, con); 465 } 466 } 467 468 475 public static void doDelete(Criteria criteria) throws TorqueException 476 { 477 AttachmentTypePeer 478 .doDelete(criteria, (Connection ) null); 479 } 480 481 491 public static void doDelete(Criteria criteria, Connection con) 492 throws TorqueException 493 { 494 if (criteria.containsKey(SEARCHABLE)) 496 { 497 Object possibleBoolean = criteria.get(SEARCHABLE); 498 if (possibleBoolean instanceof Boolean ) 499 { 500 criteria.add(SEARCHABLE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 501 } 502 } 503 504 setDbName(criteria); 505 506 if (con == null) 507 { 508 BasePeer.doDelete(criteria); 509 } 510 else 511 { 512 BasePeer.doDelete(criteria, con); 513 } 514 } 515 516 522 public static List doSelect(AttachmentType obj) throws TorqueException 523 { 524 return doSelect(buildSelectCriteria(obj)); 525 } 526 527 533 public static void doInsert(AttachmentType obj) throws TorqueException 534 { 535 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 536 obj.setNew(false); 537 obj.setModified(false); 538 } 539 540 545 public static void doUpdate(AttachmentType obj) throws TorqueException 546 { 547 doUpdate(buildCriteria(obj)); 548 obj.setModified(false); 549 } 550 551 556 public static void doDelete(AttachmentType obj) throws TorqueException 557 { 558 doDelete(buildSelectCriteria(obj)); 559 } 560 561 571 public static void doInsert(AttachmentType obj, Connection con) 572 throws TorqueException 573 { 574 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 575 obj.setNew(false); 576 obj.setModified(false); 577 } 578 579 589 public static void doUpdate(AttachmentType obj, Connection con) 590 throws TorqueException 591 { 592 doUpdate(buildCriteria(obj), con); 593 obj.setModified(false); 594 } 595 596 606 public static void doDelete(AttachmentType obj, Connection con) 607 throws TorqueException 608 { 609 doDelete(buildSelectCriteria(obj), con); 610 } 611 612 619 public static void doDelete(ObjectKey pk) throws TorqueException 620 { 621 BaseAttachmentTypePeer 622 .doDelete(pk, (Connection ) null); 623 } 624 625 635 public static void doDelete(ObjectKey pk, Connection con) 636 throws TorqueException 637 { 638 doDelete(buildCriteria(pk), con); 639 } 640 641 642 public static Criteria buildCriteria( ObjectKey pk ) 643 { 644 Criteria criteria = new Criteria(); 645 criteria.add(ATTACHMENT_TYPE_ID, pk); 646 return criteria; 647 } 648 649 650 public static Criteria buildCriteria( AttachmentType obj ) 651 { 652 Criteria criteria = new Criteria(DATABASE_NAME); 653 if (!obj.isNew()) 654 criteria.add(ATTACHMENT_TYPE_ID, obj.getAttachmentTypeId()); 655 criteria.add(ATTACHMENT_TYPE_NAME, obj.getName()); 656 criteria.add(SEARCHABLE, obj.getSearchable()); 657 return criteria; 658 } 659 660 661 public static Criteria buildSelectCriteria( AttachmentType obj ) 662 { 663 Criteria criteria = new Criteria(DATABASE_NAME); 664 if (!obj.isNew()) 665 criteria.add(ATTACHMENT_TYPE_ID, obj.getAttachmentTypeId()); 666 criteria.add(ATTACHMENT_TYPE_NAME, obj.getName()); 667 criteria.add(SEARCHABLE, obj.getSearchable()); 668 return criteria; 669 } 670 671 672 681 public static AttachmentType retrieveByPK(Integer pk) 682 throws TorqueException, NoRowsException, TooManyRowsException 683 { 684 return retrieveByPK(SimpleKey.keyFor(pk)); 685 } 686 687 697 public static AttachmentType retrieveByPK(Integer pk, Connection con) 698 throws TorqueException, NoRowsException, TooManyRowsException 699 { 700 return retrieveByPK(SimpleKey.keyFor(pk), con); 701 } 702 703 712 public static AttachmentType retrieveByPK(ObjectKey pk) 713 throws TorqueException, NoRowsException, TooManyRowsException 714 { 715 Connection db = null; 716 AttachmentType retVal = null; 717 try 718 { 719 db = Torque.getConnection(DATABASE_NAME); 720 retVal = retrieveByPK(pk, db); 721 } 722 finally 723 { 724 Torque.closeConnection(db); 725 } 726 return(retVal); 727 } 728 729 739 public static AttachmentType retrieveByPK(ObjectKey pk, Connection con) 740 throws TorqueException, NoRowsException, TooManyRowsException 741 { 742 Criteria criteria = buildCriteria(pk); 743 List v = doSelect(criteria, con); 744 if (v.size() == 0) 745 { 746 throw new NoRowsException("Failed to select a row."); 747 } 748 else if (v.size() > 1) 749 { 750 throw new TooManyRowsException("Failed to select only one row."); 751 } 752 else 753 { 754 return (AttachmentType)v.get(0); 755 } 756 } 757 758 765 public static List retrieveByPKs(List pks) 766 throws TorqueException 767 { 768 Connection db = null; 769 List retVal = null; 770 try 771 { 772 db = Torque.getConnection(DATABASE_NAME); 773 retVal = retrieveByPKs(pks, db); 774 } 775 finally 776 { 777 Torque.closeConnection(db); 778 } 779 return(retVal); 780 } 781 782 790 public static List retrieveByPKs( List pks, Connection dbcon ) 791 throws TorqueException 792 { 793 List objs = null; 794 if (pks == null || pks.size() == 0) 795 { 796 objs = new LinkedList (); 797 } 798 else 799 { 800 Criteria criteria = new Criteria(); 801 criteria.addIn( ATTACHMENT_TYPE_ID, pks ); 802 objs = doSelect(criteria, dbcon); 803 } 804 return objs; 805 } 806 807 808 809 810 811 812 813 814 815 816 823 protected static TableMap getTableMap() 824 throws TorqueException 825 { 826 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 827 } 828 829 private static void setDbName(Criteria crit) 830 { 831 if (crit.getDbName() == Torque.getDefaultDB()) 835 { 836 crit.setDbName(DATABASE_NAME); 837 } 838 } 839 } 840 | Popular Tags |