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 35 37 public abstract class BaseAttributeTypePeer 38 extends BasePeer 39 { 40 41 42 public static final String DATABASE_NAME = "scarab"; 43 44 45 public static final String TABLE_NAME = "SCARAB_ATTRIBUTE_TYPE"; 46 47 52 public static MapBuilder getMapBuilder() 53 throws TorqueException 54 { 55 return getMapBuilder(AttributeTypeMapBuilder.CLASS_NAME); 56 } 57 58 59 public static final String ATTRIBUTE_TYPE_ID; 60 61 public static final String ATTRIBUTE_CLASS_ID; 62 63 public static final String ATTRIBUTE_TYPE_NAME; 64 65 public static final String JAVA_CLASS_NAME; 66 67 public static final String VALIDATION_KEY; 68 69 static 70 { 71 ATTRIBUTE_TYPE_ID = "SCARAB_ATTRIBUTE_TYPE.ATTRIBUTE_TYPE_ID"; 72 ATTRIBUTE_CLASS_ID = "SCARAB_ATTRIBUTE_TYPE.ATTRIBUTE_CLASS_ID"; 73 ATTRIBUTE_TYPE_NAME = "SCARAB_ATTRIBUTE_TYPE.ATTRIBUTE_TYPE_NAME"; 74 JAVA_CLASS_NAME = "SCARAB_ATTRIBUTE_TYPE.JAVA_CLASS_NAME"; 75 VALIDATION_KEY = "SCARAB_ATTRIBUTE_TYPE.VALIDATION_KEY"; 76 if (Torque.isInit()) 77 { 78 try 79 { 80 getMapBuilder(AttributeTypeMapBuilder.CLASS_NAME); 81 } 82 catch (Exception e) 83 { 84 log.error("Could not initialize Peer", e); 85 } 86 } 87 else 88 { 89 Torque.registerMapBuilder(AttributeTypeMapBuilder.CLASS_NAME); 90 } 91 } 92 93 94 public static final int numColumns = 5; 95 96 97 protected static final String CLASSNAME_DEFAULT = 98 "org.tigris.scarab.om.AttributeType"; 99 100 101 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 102 103 109 private static Class initClass(String className) 110 { 111 Class c = null; 112 try 113 { 114 c = Class.forName(className); 115 } 116 catch (Throwable t) 117 { 118 log.error("A FATAL ERROR has occurred which should not " 119 + "have happened under any circumstance. Please notify " 120 + "the Torque developers <torque-dev@db.apache.org> " 121 + "and give as many details as possible (including the error " 122 + "stack trace).", t); 123 124 if (t instanceof Error ) 126 { 127 throw (Error ) t.fillInStackTrace(); 128 } 129 } 130 return c; 131 } 132 133 143 public static List resultSet2Objects(java.sql.ResultSet results) 144 throws TorqueException 145 { 146 try 147 { 148 QueryDataSet qds = null; 149 List rows = null; 150 try 151 { 152 qds = new QueryDataSet(results); 153 rows = getSelectResults(qds); 154 } 155 finally 156 { 157 if (qds != null) 158 { 159 qds.close(); 160 } 161 } 162 163 return populateObjects(rows); 164 } 165 catch (SQLException e) 166 { 167 throw new TorqueException(e); 168 } 169 catch (DataSetException e) 170 { 171 throw new TorqueException(e); 172 } 173 } 174 175 176 177 184 public static ObjectKey doInsert(Criteria criteria) 185 throws TorqueException 186 { 187 return BaseAttributeTypePeer 188 .doInsert(criteria, (Connection ) null); 189 } 190 191 201 public static ObjectKey doInsert(Criteria criteria, Connection con) 202 throws TorqueException 203 { 204 205 setDbName(criteria); 206 207 if (con == null) 208 { 209 return BasePeer.doInsert(criteria); 210 } 211 else 212 { 213 return BasePeer.doInsert(criteria, con); 214 } 215 } 216 217 224 public static void addSelectColumns(Criteria criteria) 225 throws TorqueException 226 { 227 criteria.addSelectColumn(ATTRIBUTE_TYPE_ID); 228 criteria.addSelectColumn(ATTRIBUTE_CLASS_ID); 229 criteria.addSelectColumn(ATTRIBUTE_TYPE_NAME); 230 criteria.addSelectColumn(JAVA_CLASS_NAME); 231 criteria.addSelectColumn(VALIDATION_KEY); 232 } 233 234 243 public static AttributeType row2Object(Record row, 244 int offset, 245 Class cls) 246 throws TorqueException 247 { 248 try 249 { 250 AttributeType obj = (AttributeType) cls.newInstance(); 251 AttributeTypePeer.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 AttributeType obj) 279 throws TorqueException 280 { 281 try 282 { 283 obj.setAttributeTypeId(row.getValue(offset + 0).asIntegerObj()); 284 obj.setClassId(row.getValue(offset + 1).asIntegerObj()); 285 obj.setName(row.getValue(offset + 2).asString()); 286 obj.setJavaClassName(row.getValue(offset + 3).asString()); 287 obj.setValidationKey(row.getValue(offset + 4).asString()); 288 } 289 catch (DataSetException e) 290 { 291 throw new TorqueException(e); 292 } 293 } 294 295 303 public static List doSelect(Criteria criteria) throws TorqueException 304 { 305 return populateObjects(doSelectVillageRecords(criteria)); 306 } 307 308 317 public static List doSelect(Criteria criteria, Connection con) 318 throws TorqueException 319 { 320 return populateObjects(doSelectVillageRecords(criteria, con)); 321 } 322 323 333 public static List doSelectVillageRecords(Criteria criteria) 334 throws TorqueException 335 { 336 return BaseAttributeTypePeer 337 .doSelectVillageRecords(criteria, (Connection ) null); 338 } 339 340 349 public static List doSelectVillageRecords(Criteria criteria, Connection con) 350 throws TorqueException 351 { 352 if (criteria.getSelectColumns().size() == 0) 353 { 354 addSelectColumns(criteria); 355 } 356 357 358 setDbName(criteria); 359 360 if (con == null) 363 { 364 return BasePeer.doSelect(criteria); 365 } 366 else 367 { 368 return BasePeer.doSelect(criteria, con); 369 } 370 } 371 372 379 public static List populateObjects(List records) 380 throws TorqueException 381 { 382 List results = new ArrayList (records.size()); 383 384 for (int i = 0; i < records.size(); i++) 386 { 387 Record row = (Record) records.get(i); 388 results.add(AttributeTypePeer.row2Object(row, 1, 389 AttributeTypePeer.getOMClass())); 390 } 391 return results; 392 } 393 394 395 403 public static Class getOMClass() 404 throws TorqueException 405 { 406 return CLASS_DEFAULT; 407 } 408 409 417 public static void doUpdate(Criteria criteria) throws TorqueException 418 { 419 BaseAttributeTypePeer 420 .doUpdate(criteria, (Connection ) null); 421 } 422 423 434 public static void doUpdate(Criteria criteria, Connection con) 435 throws TorqueException 436 { 437 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 438 selectCriteria.put(ATTRIBUTE_TYPE_ID, criteria.remove(ATTRIBUTE_TYPE_ID)); 439 440 setDbName(criteria); 441 442 if (con == null) 443 { 444 BasePeer.doUpdate(selectCriteria, criteria); 445 } 446 else 447 { 448 BasePeer.doUpdate(selectCriteria, criteria, con); 449 } 450 } 451 452 459 public static void doDelete(Criteria criteria) throws TorqueException 460 { 461 AttributeTypePeer 462 .doDelete(criteria, (Connection ) null); 463 } 464 465 475 public static void doDelete(Criteria criteria, Connection con) 476 throws TorqueException 477 { 478 479 setDbName(criteria); 480 481 if (con == null) 482 { 483 BasePeer.doDelete(criteria); 484 } 485 else 486 { 487 BasePeer.doDelete(criteria, con); 488 } 489 } 490 491 497 public static List doSelect(AttributeType obj) throws TorqueException 498 { 499 return doSelect(buildSelectCriteria(obj)); 500 } 501 502 508 public static void doInsert(AttributeType obj) throws TorqueException 509 { 510 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 511 obj.setNew(false); 512 obj.setModified(false); 513 } 514 515 520 public static void doUpdate(AttributeType obj) throws TorqueException 521 { 522 doUpdate(buildCriteria(obj)); 523 obj.setModified(false); 524 } 525 526 531 public static void doDelete(AttributeType obj) throws TorqueException 532 { 533 doDelete(buildSelectCriteria(obj)); 534 } 535 536 546 public static void doInsert(AttributeType obj, Connection con) 547 throws TorqueException 548 { 549 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 550 obj.setNew(false); 551 obj.setModified(false); 552 } 553 554 564 public static void doUpdate(AttributeType obj, Connection con) 565 throws TorqueException 566 { 567 doUpdate(buildCriteria(obj), con); 568 obj.setModified(false); 569 } 570 571 581 public static void doDelete(AttributeType obj, Connection con) 582 throws TorqueException 583 { 584 doDelete(buildSelectCriteria(obj), con); 585 } 586 587 594 public static void doDelete(ObjectKey pk) throws TorqueException 595 { 596 BaseAttributeTypePeer 597 .doDelete(pk, (Connection ) null); 598 } 599 600 610 public static void doDelete(ObjectKey pk, Connection con) 611 throws TorqueException 612 { 613 doDelete(buildCriteria(pk), con); 614 } 615 616 617 public static Criteria buildCriteria( ObjectKey pk ) 618 { 619 Criteria criteria = new Criteria(); 620 criteria.add(ATTRIBUTE_TYPE_ID, pk); 621 return criteria; 622 } 623 624 625 public static Criteria buildCriteria( AttributeType obj ) 626 { 627 Criteria criteria = new Criteria(DATABASE_NAME); 628 if (!obj.isNew()) 629 criteria.add(ATTRIBUTE_TYPE_ID, obj.getAttributeTypeId()); 630 criteria.add(ATTRIBUTE_CLASS_ID, obj.getClassId()); 631 criteria.add(ATTRIBUTE_TYPE_NAME, obj.getName()); 632 criteria.add(JAVA_CLASS_NAME, obj.getJavaClassName()); 633 criteria.add(VALIDATION_KEY, obj.getValidationKey()); 634 return criteria; 635 } 636 637 638 public static Criteria buildSelectCriteria( AttributeType obj ) 639 { 640 Criteria criteria = new Criteria(DATABASE_NAME); 641 if (!obj.isNew()) 642 criteria.add(ATTRIBUTE_TYPE_ID, obj.getAttributeTypeId()); 643 criteria.add(ATTRIBUTE_CLASS_ID, obj.getClassId()); 644 criteria.add(ATTRIBUTE_TYPE_NAME, obj.getName()); 645 criteria.add(JAVA_CLASS_NAME, obj.getJavaClassName()); 646 criteria.add(VALIDATION_KEY, obj.getValidationKey()); 647 return criteria; 648 } 649 650 651 660 public static AttributeType retrieveByPK(Integer pk) 661 throws TorqueException, NoRowsException, TooManyRowsException 662 { 663 return retrieveByPK(SimpleKey.keyFor(pk)); 664 } 665 666 676 public static AttributeType retrieveByPK(Integer pk, Connection con) 677 throws TorqueException, NoRowsException, TooManyRowsException 678 { 679 return retrieveByPK(SimpleKey.keyFor(pk), con); 680 } 681 682 691 public static AttributeType retrieveByPK(ObjectKey pk) 692 throws TorqueException, NoRowsException, TooManyRowsException 693 { 694 Connection db = null; 695 AttributeType retVal = null; 696 try 697 { 698 db = Torque.getConnection(DATABASE_NAME); 699 retVal = retrieveByPK(pk, db); 700 } 701 finally 702 { 703 Torque.closeConnection(db); 704 } 705 return(retVal); 706 } 707 708 718 public static AttributeType retrieveByPK(ObjectKey pk, Connection con) 719 throws TorqueException, NoRowsException, TooManyRowsException 720 { 721 Criteria criteria = buildCriteria(pk); 722 List v = doSelect(criteria, con); 723 if (v.size() == 0) 724 { 725 throw new NoRowsException("Failed to select a row."); 726 } 727 else if (v.size() > 1) 728 { 729 throw new TooManyRowsException("Failed to select only one row."); 730 } 731 else 732 { 733 return (AttributeType)v.get(0); 734 } 735 } 736 737 744 public static List retrieveByPKs(List pks) 745 throws TorqueException 746 { 747 Connection db = null; 748 List retVal = null; 749 try 750 { 751 db = Torque.getConnection(DATABASE_NAME); 752 retVal = retrieveByPKs(pks, db); 753 } 754 finally 755 { 756 Torque.closeConnection(db); 757 } 758 return(retVal); 759 } 760 761 769 public static List retrieveByPKs( List pks, Connection dbcon ) 770 throws TorqueException 771 { 772 List objs = null; 773 if (pks == null || pks.size() == 0) 774 { 775 objs = new LinkedList (); 776 } 777 else 778 { 779 Criteria criteria = new Criteria(); 780 criteria.addIn( ATTRIBUTE_TYPE_ID, pks ); 781 objs = doSelect(criteria, dbcon); 782 } 783 return objs; 784 } 785 786 787 788 789 790 791 792 793 794 795 806 protected static List doSelectJoinAttributeClass(Criteria criteria) 807 throws TorqueException 808 { 809 setDbName(criteria); 810 811 AttributeTypePeer.addSelectColumns(criteria); 812 int offset = numColumns + 1; 813 AttributeClassPeer.addSelectColumns(criteria); 814 815 816 criteria.addJoin(AttributeTypePeer.ATTRIBUTE_CLASS_ID, 817 AttributeClassPeer.ATTRIBUTE_CLASS_ID); 818 819 820 821 List rows = BasePeer.doSelect(criteria); 822 List results = new ArrayList (); 823 824 for (int i = 0; i < rows.size(); i++) 825 { 826 Record row = (Record) rows.get(i); 827 828 Class omClass = AttributeTypePeer.getOMClass(); 829 AttributeType obj1 = (AttributeType) AttributeTypePeer 830 .row2Object(row, 1, omClass); 831 omClass = AttributeClassPeer.getOMClass(); 832 AttributeClass obj2 = (AttributeClass)AttributeClassPeer 833 .row2Object(row, offset, omClass); 834 835 boolean newObject = true; 836 for (int j = 0; j < results.size(); j++) 837 { 838 AttributeType temp_obj1 = (AttributeType)results.get(j); 839 AttributeClass temp_obj2 = (AttributeClass)temp_obj1.getAttributeClass(); 840 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 841 { 842 newObject = false; 843 temp_obj2.addAttributeType(obj1); 844 break; 845 } 846 } 847 if (newObject) 848 { 849 obj2.initAttributeTypes(); 850 obj2.addAttributeType(obj1); 851 } 852 results.add(obj1); 853 } 854 return results; 855 } 856 857 858 859 860 867 protected static TableMap getTableMap() 868 throws TorqueException 869 { 870 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 871 } 872 873 private static void setDbName(Criteria crit) 874 { 875 if (crit.getDbName() == Torque.getDefaultDB()) 879 { 880 crit.setDbName(DATABASE_NAME); 881 } 882 } 883 } 884 | Popular Tags |