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 BaseAttributeOptionPeer 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_OPTION"; 46 47 52 public static MapBuilder getMapBuilder() 53 throws TorqueException 54 { 55 return getMapBuilder(AttributeOptionMapBuilder.CLASS_NAME); 56 } 57 58 59 public static final String OPTION_ID; 60 61 public static final String ATTRIBUTE_ID; 62 63 public static final String OPTION_NAME; 64 65 public static final String DELETED; 66 67 static 68 { 69 OPTION_ID = "SCARAB_ATTRIBUTE_OPTION.OPTION_ID"; 70 ATTRIBUTE_ID = "SCARAB_ATTRIBUTE_OPTION.ATTRIBUTE_ID"; 71 OPTION_NAME = "SCARAB_ATTRIBUTE_OPTION.OPTION_NAME"; 72 DELETED = "SCARAB_ATTRIBUTE_OPTION.DELETED"; 73 if (Torque.isInit()) 74 { 75 try 76 { 77 getMapBuilder(AttributeOptionMapBuilder.CLASS_NAME); 78 } 79 catch (Exception e) 80 { 81 log.error("Could not initialize Peer", e); 82 } 83 } 84 else 85 { 86 Torque.registerMapBuilder(AttributeOptionMapBuilder.CLASS_NAME); 87 } 88 } 89 90 91 public static final int numColumns = 4; 92 93 94 protected static final String CLASSNAME_DEFAULT = 95 "org.tigris.scarab.om.AttributeOption"; 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 <torque-dev@db.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 BaseAttributeOptionPeer 185 .doInsert(criteria, (Connection ) null); 186 } 187 188 198 public static ObjectKey doInsert(Criteria criteria, Connection con) 199 throws TorqueException 200 { 201 if (criteria.containsKey(DELETED)) 203 { 204 Object possibleBoolean = criteria.get(DELETED); 205 if (possibleBoolean instanceof Boolean ) 206 { 207 criteria.add(DELETED, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 208 } 209 } 210 211 setDbName(criteria); 212 213 if (con == null) 214 { 215 return BasePeer.doInsert(criteria); 216 } 217 else 218 { 219 return BasePeer.doInsert(criteria, con); 220 } 221 } 222 223 230 public static void addSelectColumns(Criteria criteria) 231 throws TorqueException 232 { 233 criteria.addSelectColumn(OPTION_ID); 234 criteria.addSelectColumn(ATTRIBUTE_ID); 235 criteria.addSelectColumn(OPTION_NAME); 236 criteria.addSelectColumn(DELETED); 237 } 238 239 248 public static AttributeOption row2Object(Record row, 249 int offset, 250 Class cls) 251 throws TorqueException 252 { 253 try 254 { 255 AttributeOption obj = (AttributeOption) cls.newInstance(); 256 AttributeOptionPeer.populateObject(row, offset, obj); 257 obj.setModified(false); 258 obj.setNew(false); 259 260 return obj; 261 } 262 catch (InstantiationException e) 263 { 264 throw new TorqueException(e); 265 } 266 catch (IllegalAccessException e) 267 { 268 throw new TorqueException(e); 269 } 270 } 271 272 281 public static void populateObject(Record row, 282 int offset, 283 AttributeOption obj) 284 throws TorqueException 285 { 286 try 287 { 288 obj.setOptionId(row.getValue(offset + 0).asIntegerObj()); 289 obj.setAttributeId(row.getValue(offset + 1).asIntegerObj()); 290 obj.setName(row.getValue(offset + 2).asString()); 291 obj.setDeleted(row.getValue(offset + 3).asBoolean()); 292 } 293 catch (DataSetException e) 294 { 295 throw new TorqueException(e); 296 } 297 } 298 299 307 public static List doSelect(Criteria criteria) throws TorqueException 308 { 309 return populateObjects(doSelectVillageRecords(criteria)); 310 } 311 312 321 public static List doSelect(Criteria criteria, Connection con) 322 throws TorqueException 323 { 324 return populateObjects(doSelectVillageRecords(criteria, con)); 325 } 326 327 337 public static List doSelectVillageRecords(Criteria criteria) 338 throws TorqueException 339 { 340 return BaseAttributeOptionPeer 341 .doSelectVillageRecords(criteria, (Connection ) null); 342 } 343 344 353 public static List doSelectVillageRecords(Criteria criteria, Connection con) 354 throws TorqueException 355 { 356 if (criteria.getSelectColumns().size() == 0) 357 { 358 addSelectColumns(criteria); 359 } 360 361 if (criteria.containsKey(DELETED)) 363 { 364 Object possibleBoolean = criteria.get(DELETED); 365 if (possibleBoolean instanceof Boolean ) 366 { 367 criteria.add(DELETED, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 368 } 369 } 370 371 setDbName(criteria); 372 373 if (con == null) 376 { 377 return BasePeer.doSelect(criteria); 378 } 379 else 380 { 381 return BasePeer.doSelect(criteria, con); 382 } 383 } 384 385 392 public static List populateObjects(List records) 393 throws TorqueException 394 { 395 List results = new ArrayList (records.size()); 396 397 for (int i = 0; i < records.size(); i++) 399 { 400 Record row = (Record) records.get(i); 401 results.add(AttributeOptionPeer.row2Object(row, 1, 402 AttributeOptionPeer.getOMClass())); 403 } 404 return results; 405 } 406 407 408 416 public static Class getOMClass() 417 throws TorqueException 418 { 419 return CLASS_DEFAULT; 420 } 421 422 430 public static void doUpdate(Criteria criteria) throws TorqueException 431 { 432 BaseAttributeOptionPeer 433 .doUpdate(criteria, (Connection ) null); 434 } 435 436 447 public static void doUpdate(Criteria criteria, Connection con) 448 throws TorqueException 449 { 450 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 451 selectCriteria.put(OPTION_ID, criteria.remove(OPTION_ID)); 452 if (criteria.containsKey(DELETED)) 454 { 455 Object possibleBoolean = criteria.get(DELETED); 456 if (possibleBoolean instanceof Boolean ) 457 { 458 criteria.add(DELETED, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 459 } 460 } 461 462 setDbName(criteria); 463 464 if (con == null) 465 { 466 BasePeer.doUpdate(selectCriteria, criteria); 467 } 468 else 469 { 470 BasePeer.doUpdate(selectCriteria, criteria, con); 471 } 472 } 473 474 481 public static void doDelete(Criteria criteria) throws TorqueException 482 { 483 AttributeOptionPeer 484 .doDelete(criteria, (Connection ) null); 485 } 486 487 497 public static void doDelete(Criteria criteria, Connection con) 498 throws TorqueException 499 { 500 if (criteria.containsKey(DELETED)) 502 { 503 Object possibleBoolean = criteria.get(DELETED); 504 if (possibleBoolean instanceof Boolean ) 505 { 506 criteria.add(DELETED, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 507 } 508 } 509 510 setDbName(criteria); 511 512 if (con == null) 513 { 514 BasePeer.doDelete(criteria); 515 } 516 else 517 { 518 BasePeer.doDelete(criteria, con); 519 } 520 } 521 522 528 public static List doSelect(AttributeOption obj) throws TorqueException 529 { 530 return doSelect(buildSelectCriteria(obj)); 531 } 532 533 539 public static void doInsert(AttributeOption obj) throws TorqueException 540 { 541 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 542 obj.setNew(false); 543 obj.setModified(false); 544 } 545 546 551 public static void doUpdate(AttributeOption obj) throws TorqueException 552 { 553 doUpdate(buildCriteria(obj)); 554 obj.setModified(false); 555 } 556 557 562 public static void doDelete(AttributeOption obj) throws TorqueException 563 { 564 doDelete(buildSelectCriteria(obj)); 565 } 566 567 577 public static void doInsert(AttributeOption obj, Connection con) 578 throws TorqueException 579 { 580 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 581 obj.setNew(false); 582 obj.setModified(false); 583 } 584 585 595 public static void doUpdate(AttributeOption obj, Connection con) 596 throws TorqueException 597 { 598 doUpdate(buildCriteria(obj), con); 599 obj.setModified(false); 600 } 601 602 612 public static void doDelete(AttributeOption obj, Connection con) 613 throws TorqueException 614 { 615 doDelete(buildSelectCriteria(obj), con); 616 } 617 618 625 public static void doDelete(ObjectKey pk) throws TorqueException 626 { 627 BaseAttributeOptionPeer 628 .doDelete(pk, (Connection ) null); 629 } 630 631 641 public static void doDelete(ObjectKey pk, Connection con) 642 throws TorqueException 643 { 644 doDelete(buildCriteria(pk), con); 645 } 646 647 648 public static Criteria buildCriteria( ObjectKey pk ) 649 { 650 Criteria criteria = new Criteria(); 651 criteria.add(OPTION_ID, pk); 652 return criteria; 653 } 654 655 656 public static Criteria buildCriteria( AttributeOption obj ) 657 { 658 Criteria criteria = new Criteria(DATABASE_NAME); 659 if (!obj.isNew()) 660 criteria.add(OPTION_ID, obj.getOptionId()); 661 criteria.add(ATTRIBUTE_ID, obj.getAttributeId()); 662 criteria.add(OPTION_NAME, obj.getName()); 663 criteria.add(DELETED, obj.getDeleted()); 664 return criteria; 665 } 666 667 668 public static Criteria buildSelectCriteria( AttributeOption obj ) 669 { 670 Criteria criteria = new Criteria(DATABASE_NAME); 671 if (!obj.isNew()) 672 criteria.add(OPTION_ID, obj.getOptionId()); 673 criteria.add(ATTRIBUTE_ID, obj.getAttributeId()); 674 criteria.add(OPTION_NAME, obj.getName()); 675 criteria.add(DELETED, obj.getDeleted()); 676 return criteria; 677 } 678 679 680 689 public static AttributeOption retrieveByPK(Integer pk) 690 throws TorqueException, NoRowsException, TooManyRowsException 691 { 692 return retrieveByPK(SimpleKey.keyFor(pk)); 693 } 694 695 705 public static AttributeOption retrieveByPK(Integer pk, Connection con) 706 throws TorqueException, NoRowsException, TooManyRowsException 707 { 708 return retrieveByPK(SimpleKey.keyFor(pk), con); 709 } 710 711 720 public static AttributeOption retrieveByPK(ObjectKey pk) 721 throws TorqueException, NoRowsException, TooManyRowsException 722 { 723 Connection db = null; 724 AttributeOption retVal = null; 725 try 726 { 727 db = Torque.getConnection(DATABASE_NAME); 728 retVal = retrieveByPK(pk, db); 729 } 730 finally 731 { 732 Torque.closeConnection(db); 733 } 734 return(retVal); 735 } 736 737 747 public static AttributeOption retrieveByPK(ObjectKey pk, Connection con) 748 throws TorqueException, NoRowsException, TooManyRowsException 749 { 750 Criteria criteria = buildCriteria(pk); 751 List v = doSelect(criteria, con); 752 if (v.size() == 0) 753 { 754 throw new NoRowsException("Failed to select a row."); 755 } 756 else if (v.size() > 1) 757 { 758 throw new TooManyRowsException("Failed to select only one row."); 759 } 760 else 761 { 762 return (AttributeOption)v.get(0); 763 } 764 } 765 766 773 public static List retrieveByPKs(List pks) 774 throws TorqueException 775 { 776 Connection db = null; 777 List retVal = null; 778 try 779 { 780 db = Torque.getConnection(DATABASE_NAME); 781 retVal = retrieveByPKs(pks, db); 782 } 783 finally 784 { 785 Torque.closeConnection(db); 786 } 787 return(retVal); 788 } 789 790 798 public static List retrieveByPKs( List pks, Connection dbcon ) 799 throws TorqueException 800 { 801 List objs = null; 802 if (pks == null || pks.size() == 0) 803 { 804 objs = new LinkedList (); 805 } 806 else 807 { 808 Criteria criteria = new Criteria(); 809 criteria.addIn( OPTION_ID, pks ); 810 objs = doSelect(criteria, dbcon); 811 } 812 return objs; 813 } 814 815 816 817 818 819 820 821 822 823 824 835 protected static List doSelectJoinAttribute(Criteria criteria) 836 throws TorqueException 837 { 838 setDbName(criteria); 839 840 AttributeOptionPeer.addSelectColumns(criteria); 841 int offset = numColumns + 1; 842 AttributePeer.addSelectColumns(criteria); 843 844 845 criteria.addJoin(AttributeOptionPeer.ATTRIBUTE_ID, 846 AttributePeer.ATTRIBUTE_ID); 847 848 849 if (criteria.containsKey(DELETED)) 851 { 852 Object possibleBoolean = criteria.get(DELETED); 853 if (possibleBoolean instanceof Boolean ) 854 { 855 criteria.add(DELETED, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 856 } 857 } 858 859 List rows = BasePeer.doSelect(criteria); 860 List results = new ArrayList (); 861 862 for (int i = 0; i < rows.size(); i++) 863 { 864 Record row = (Record) rows.get(i); 865 866 Class omClass = AttributeOptionPeer.getOMClass(); 867 AttributeOption obj1 = (AttributeOption) AttributeOptionPeer 868 .row2Object(row, 1, omClass); 869 omClass = AttributePeer.getOMClass(); 870 Attribute obj2 = (Attribute)AttributePeer 871 .row2Object(row, offset, omClass); 872 873 boolean newObject = true; 874 for (int j = 0; j < results.size(); j++) 875 { 876 AttributeOption temp_obj1 = (AttributeOption)results.get(j); 877 Attribute temp_obj2 = (Attribute)temp_obj1.getAttribute(); 878 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 879 { 880 newObject = false; 881 temp_obj2.addAttributeOption(obj1); 882 break; 883 } 884 } 885 if (newObject) 886 { 887 obj2.initAttributeOptions(); 888 obj2.addAttributeOption(obj1); 889 } 890 results.add(obj1); 891 } 892 return results; 893 } 894 895 896 897 898 905 protected static TableMap getTableMap() 906 throws TorqueException 907 { 908 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 909 } 910 911 private static void setDbName(Criteria crit) 912 { 913 if (crit.getDbName() == Torque.getDefaultDB()) 917 { 918 crit.setDbName(DATABASE_NAME); 919 } 920 } 921 } 922 | Popular Tags |