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 36 38 public abstract class BaseAttributeGroupPeer 39 extends BasePeer 40 { 41 42 43 public static final String DATABASE_NAME = "scarab"; 44 45 46 public static final String TABLE_NAME = "SCARAB_ATTRIBUTE_GROUP"; 47 48 53 public static MapBuilder getMapBuilder() 54 throws TorqueException 55 { 56 return getMapBuilder(AttributeGroupMapBuilder.CLASS_NAME); 57 } 58 59 60 public static final String ATTRIBUTE_GROUP_ID; 61 62 public static final String NAME; 63 64 public static final String DESCRIPTION; 65 66 public static final String MODULE_ID; 67 68 public static final String ISSUE_TYPE_ID; 69 70 public static final String ACTIVE; 71 72 public static final String DEDUPE; 73 74 public static final String PREFERRED_ORDER; 75 76 static 77 { 78 ATTRIBUTE_GROUP_ID = "SCARAB_ATTRIBUTE_GROUP.ATTRIBUTE_GROUP_ID"; 79 NAME = "SCARAB_ATTRIBUTE_GROUP.NAME"; 80 DESCRIPTION = "SCARAB_ATTRIBUTE_GROUP.DESCRIPTION"; 81 MODULE_ID = "SCARAB_ATTRIBUTE_GROUP.MODULE_ID"; 82 ISSUE_TYPE_ID = "SCARAB_ATTRIBUTE_GROUP.ISSUE_TYPE_ID"; 83 ACTIVE = "SCARAB_ATTRIBUTE_GROUP.ACTIVE"; 84 DEDUPE = "SCARAB_ATTRIBUTE_GROUP.DEDUPE"; 85 PREFERRED_ORDER = "SCARAB_ATTRIBUTE_GROUP.PREFERRED_ORDER"; 86 if (Torque.isInit()) 87 { 88 try 89 { 90 getMapBuilder(AttributeGroupMapBuilder.CLASS_NAME); 91 } 92 catch (Exception e) 93 { 94 log.error("Could not initialize Peer", e); 95 } 96 } 97 else 98 { 99 Torque.registerMapBuilder(AttributeGroupMapBuilder.CLASS_NAME); 100 } 101 } 102 103 104 public static final int numColumns = 8; 105 106 107 protected static final String CLASSNAME_DEFAULT = 108 "org.tigris.scarab.om.AttributeGroup"; 109 110 111 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 112 113 119 private static Class initClass(String className) 120 { 121 Class c = null; 122 try 123 { 124 c = Class.forName(className); 125 } 126 catch (Throwable t) 127 { 128 log.error("A FATAL ERROR has occurred which should not " 129 + "have happened under any circumstance. Please notify " 130 + "the Torque developers <torque-dev@db.apache.org> " 131 + "and give as many details as possible (including the error " 132 + "stack trace).", t); 133 134 if (t instanceof Error ) 136 { 137 throw (Error ) t.fillInStackTrace(); 138 } 139 } 140 return c; 141 } 142 143 153 public static List resultSet2Objects(java.sql.ResultSet results) 154 throws TorqueException 155 { 156 try 157 { 158 QueryDataSet qds = null; 159 List rows = null; 160 try 161 { 162 qds = new QueryDataSet(results); 163 rows = getSelectResults(qds); 164 } 165 finally 166 { 167 if (qds != null) 168 { 169 qds.close(); 170 } 171 } 172 173 return populateObjects(rows); 174 } 175 catch (SQLException e) 176 { 177 throw new TorqueException(e); 178 } 179 catch (DataSetException e) 180 { 181 throw new TorqueException(e); 182 } 183 } 184 185 186 187 194 public static ObjectKey doInsert(Criteria criteria) 195 throws TorqueException 196 { 197 return BaseAttributeGroupPeer 198 .doInsert(criteria, (Connection ) null); 199 } 200 201 211 public static ObjectKey doInsert(Criteria criteria, Connection con) 212 throws TorqueException 213 { 214 if (criteria.containsKey(ACTIVE)) 216 { 217 Object possibleBoolean = criteria.get(ACTIVE); 218 if (possibleBoolean instanceof Boolean ) 219 { 220 criteria.add(ACTIVE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 221 } 222 } 223 if (criteria.containsKey(DEDUPE)) 225 { 226 Object possibleBoolean = criteria.get(DEDUPE); 227 if (possibleBoolean instanceof Boolean ) 228 { 229 criteria.add(DEDUPE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 230 } 231 } 232 233 setDbName(criteria); 234 235 if (con == null) 236 { 237 return BasePeer.doInsert(criteria); 238 } 239 else 240 { 241 return BasePeer.doInsert(criteria, con); 242 } 243 } 244 245 252 public static void addSelectColumns(Criteria criteria) 253 throws TorqueException 254 { 255 criteria.addSelectColumn(ATTRIBUTE_GROUP_ID); 256 criteria.addSelectColumn(NAME); 257 criteria.addSelectColumn(DESCRIPTION); 258 criteria.addSelectColumn(MODULE_ID); 259 criteria.addSelectColumn(ISSUE_TYPE_ID); 260 criteria.addSelectColumn(ACTIVE); 261 criteria.addSelectColumn(DEDUPE); 262 criteria.addSelectColumn(PREFERRED_ORDER); 263 } 264 265 274 public static AttributeGroup row2Object(Record row, 275 int offset, 276 Class cls) 277 throws TorqueException 278 { 279 try 280 { 281 AttributeGroup obj = (AttributeGroup) cls.newInstance(); 282 AttributeGroupPeer.populateObject(row, offset, obj); 283 obj.setModified(false); 284 obj.setNew(false); 285 286 return obj; 287 } 288 catch (InstantiationException e) 289 { 290 throw new TorqueException(e); 291 } 292 catch (IllegalAccessException e) 293 { 294 throw new TorqueException(e); 295 } 296 } 297 298 307 public static void populateObject(Record row, 308 int offset, 309 AttributeGroup obj) 310 throws TorqueException 311 { 312 try 313 { 314 obj.setAttributeGroupId(row.getValue(offset + 0).asIntegerObj()); 315 obj.setName(row.getValue(offset + 1).asString()); 316 obj.setDescription(row.getValue(offset + 2).asString()); 317 obj.setModuleId(row.getValue(offset + 3).asIntegerObj()); 318 obj.setIssueTypeId(row.getValue(offset + 4).asIntegerObj()); 319 obj.setActive(row.getValue(offset + 5).asBoolean()); 320 obj.setDedupe(row.getValue(offset + 6).asBoolean()); 321 obj.setOrder(row.getValue(offset + 7).asInt()); 322 } 323 catch (DataSetException e) 324 { 325 throw new TorqueException(e); 326 } 327 } 328 329 337 public static List doSelect(Criteria criteria) throws TorqueException 338 { 339 return populateObjects(doSelectVillageRecords(criteria)); 340 } 341 342 351 public static List doSelect(Criteria criteria, Connection con) 352 throws TorqueException 353 { 354 return populateObjects(doSelectVillageRecords(criteria, con)); 355 } 356 357 367 public static List doSelectVillageRecords(Criteria criteria) 368 throws TorqueException 369 { 370 return BaseAttributeGroupPeer 371 .doSelectVillageRecords(criteria, (Connection ) null); 372 } 373 374 383 public static List doSelectVillageRecords(Criteria criteria, Connection con) 384 throws TorqueException 385 { 386 if (criteria.getSelectColumns().size() == 0) 387 { 388 addSelectColumns(criteria); 389 } 390 391 if (criteria.containsKey(ACTIVE)) 393 { 394 Object possibleBoolean = criteria.get(ACTIVE); 395 if (possibleBoolean instanceof Boolean ) 396 { 397 criteria.add(ACTIVE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 398 } 399 } 400 if (criteria.containsKey(DEDUPE)) 402 { 403 Object possibleBoolean = criteria.get(DEDUPE); 404 if (possibleBoolean instanceof Boolean ) 405 { 406 criteria.add(DEDUPE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 407 } 408 } 409 410 setDbName(criteria); 411 412 if (con == null) 415 { 416 return BasePeer.doSelect(criteria); 417 } 418 else 419 { 420 return BasePeer.doSelect(criteria, con); 421 } 422 } 423 424 431 public static List populateObjects(List records) 432 throws TorqueException 433 { 434 List results = new ArrayList (records.size()); 435 436 for (int i = 0; i < records.size(); i++) 438 { 439 Record row = (Record) records.get(i); 440 results.add(AttributeGroupPeer.row2Object(row, 1, 441 AttributeGroupPeer.getOMClass())); 442 } 443 return results; 444 } 445 446 447 455 public static Class getOMClass() 456 throws TorqueException 457 { 458 return CLASS_DEFAULT; 459 } 460 461 469 public static void doUpdate(Criteria criteria) throws TorqueException 470 { 471 BaseAttributeGroupPeer 472 .doUpdate(criteria, (Connection ) null); 473 } 474 475 486 public static void doUpdate(Criteria criteria, Connection con) 487 throws TorqueException 488 { 489 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 490 selectCriteria.put(ATTRIBUTE_GROUP_ID, criteria.remove(ATTRIBUTE_GROUP_ID)); 491 if (criteria.containsKey(ACTIVE)) 493 { 494 Object possibleBoolean = criteria.get(ACTIVE); 495 if (possibleBoolean instanceof Boolean ) 496 { 497 criteria.add(ACTIVE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 498 } 499 } 500 if (criteria.containsKey(DEDUPE)) 502 { 503 Object possibleBoolean = criteria.get(DEDUPE); 504 if (possibleBoolean instanceof Boolean ) 505 { 506 criteria.add(DEDUPE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 507 } 508 } 509 510 setDbName(criteria); 511 512 if (con == null) 513 { 514 BasePeer.doUpdate(selectCriteria, criteria); 515 } 516 else 517 { 518 BasePeer.doUpdate(selectCriteria, criteria, con); 519 } 520 } 521 522 529 public static void doDelete(Criteria criteria) throws TorqueException 530 { 531 AttributeGroupPeer 532 .doDelete(criteria, (Connection ) null); 533 } 534 535 545 public static void doDelete(Criteria criteria, Connection con) 546 throws TorqueException 547 { 548 if (criteria.containsKey(ACTIVE)) 550 { 551 Object possibleBoolean = criteria.get(ACTIVE); 552 if (possibleBoolean instanceof Boolean ) 553 { 554 criteria.add(ACTIVE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 555 } 556 } 557 if (criteria.containsKey(DEDUPE)) 559 { 560 Object possibleBoolean = criteria.get(DEDUPE); 561 if (possibleBoolean instanceof Boolean ) 562 { 563 criteria.add(DEDUPE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 564 } 565 } 566 567 setDbName(criteria); 568 569 if (con == null) 570 { 571 BasePeer.doDelete(criteria); 572 } 573 else 574 { 575 BasePeer.doDelete(criteria, con); 576 } 577 } 578 579 585 public static List doSelect(AttributeGroup obj) throws TorqueException 586 { 587 return doSelect(buildSelectCriteria(obj)); 588 } 589 590 596 public static void doInsert(AttributeGroup obj) throws TorqueException 597 { 598 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 599 obj.setNew(false); 600 obj.setModified(false); 601 } 602 603 608 public static void doUpdate(AttributeGroup obj) throws TorqueException 609 { 610 doUpdate(buildCriteria(obj)); 611 obj.setModified(false); 612 } 613 614 619 public static void doDelete(AttributeGroup obj) throws TorqueException 620 { 621 doDelete(buildSelectCriteria(obj)); 622 } 623 624 634 public static void doInsert(AttributeGroup obj, Connection con) 635 throws TorqueException 636 { 637 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 638 obj.setNew(false); 639 obj.setModified(false); 640 } 641 642 652 public static void doUpdate(AttributeGroup obj, Connection con) 653 throws TorqueException 654 { 655 doUpdate(buildCriteria(obj), con); 656 obj.setModified(false); 657 } 658 659 669 public static void doDelete(AttributeGroup obj, Connection con) 670 throws TorqueException 671 { 672 doDelete(buildSelectCriteria(obj), con); 673 } 674 675 682 public static void doDelete(ObjectKey pk) throws TorqueException 683 { 684 BaseAttributeGroupPeer 685 .doDelete(pk, (Connection ) null); 686 } 687 688 698 public static void doDelete(ObjectKey pk, Connection con) 699 throws TorqueException 700 { 701 doDelete(buildCriteria(pk), con); 702 } 703 704 705 public static Criteria buildCriteria( ObjectKey pk ) 706 { 707 Criteria criteria = new Criteria(); 708 criteria.add(ATTRIBUTE_GROUP_ID, pk); 709 return criteria; 710 } 711 712 713 public static Criteria buildCriteria( AttributeGroup obj ) 714 { 715 Criteria criteria = new Criteria(DATABASE_NAME); 716 if (!obj.isNew()) 717 criteria.add(ATTRIBUTE_GROUP_ID, obj.getAttributeGroupId()); 718 criteria.add(NAME, obj.getName()); 719 criteria.add(DESCRIPTION, obj.getDescription()); 720 criteria.add(MODULE_ID, obj.getModuleId()); 721 criteria.add(ISSUE_TYPE_ID, obj.getIssueTypeId()); 722 criteria.add(ACTIVE, obj.getActive()); 723 criteria.add(DEDUPE, obj.getDedupe()); 724 criteria.add(PREFERRED_ORDER, obj.getOrder()); 725 return criteria; 726 } 727 728 729 public static Criteria buildSelectCriteria( AttributeGroup obj ) 730 { 731 Criteria criteria = new Criteria(DATABASE_NAME); 732 if (!obj.isNew()) 733 criteria.add(ATTRIBUTE_GROUP_ID, obj.getAttributeGroupId()); 734 criteria.add(NAME, obj.getName()); 735 criteria.add(DESCRIPTION, obj.getDescription()); 736 criteria.add(MODULE_ID, obj.getModuleId()); 737 criteria.add(ISSUE_TYPE_ID, obj.getIssueTypeId()); 738 criteria.add(ACTIVE, obj.getActive()); 739 criteria.add(DEDUPE, obj.getDedupe()); 740 criteria.add(PREFERRED_ORDER, obj.getOrder()); 741 return criteria; 742 } 743 744 745 754 public static AttributeGroup retrieveByPK(Integer pk) 755 throws TorqueException, NoRowsException, TooManyRowsException 756 { 757 return retrieveByPK(SimpleKey.keyFor(pk)); 758 } 759 760 770 public static AttributeGroup retrieveByPK(Integer pk, Connection con) 771 throws TorqueException, NoRowsException, TooManyRowsException 772 { 773 return retrieveByPK(SimpleKey.keyFor(pk), con); 774 } 775 776 785 public static AttributeGroup retrieveByPK(ObjectKey pk) 786 throws TorqueException, NoRowsException, TooManyRowsException 787 { 788 Connection db = null; 789 AttributeGroup retVal = null; 790 try 791 { 792 db = Torque.getConnection(DATABASE_NAME); 793 retVal = retrieveByPK(pk, db); 794 } 795 finally 796 { 797 Torque.closeConnection(db); 798 } 799 return(retVal); 800 } 801 802 812 public static AttributeGroup retrieveByPK(ObjectKey pk, Connection con) 813 throws TorqueException, NoRowsException, TooManyRowsException 814 { 815 Criteria criteria = buildCriteria(pk); 816 List v = doSelect(criteria, con); 817 if (v.size() == 0) 818 { 819 throw new NoRowsException("Failed to select a row."); 820 } 821 else if (v.size() > 1) 822 { 823 throw new TooManyRowsException("Failed to select only one row."); 824 } 825 else 826 { 827 return (AttributeGroup)v.get(0); 828 } 829 } 830 831 838 public static List retrieveByPKs(List pks) 839 throws TorqueException 840 { 841 Connection db = null; 842 List retVal = null; 843 try 844 { 845 db = Torque.getConnection(DATABASE_NAME); 846 retVal = retrieveByPKs(pks, db); 847 } 848 finally 849 { 850 Torque.closeConnection(db); 851 } 852 return(retVal); 853 } 854 855 863 public static List retrieveByPKs( List pks, Connection dbcon ) 864 throws TorqueException 865 { 866 List objs = null; 867 if (pks == null || pks.size() == 0) 868 { 869 objs = new LinkedList (); 870 } 871 else 872 { 873 Criteria criteria = new Criteria(); 874 criteria.addIn( ATTRIBUTE_GROUP_ID, pks ); 875 objs = doSelect(criteria, dbcon); 876 } 877 return objs; 878 } 879 880 881 882 883 884 885 886 887 888 889 900 protected static List doSelectJoinScarabModule(Criteria criteria) 901 throws TorqueException 902 { 903 setDbName(criteria); 904 905 AttributeGroupPeer.addSelectColumns(criteria); 906 int offset = numColumns + 1; 907 ScarabModulePeer.addSelectColumns(criteria); 908 909 910 criteria.addJoin(AttributeGroupPeer.MODULE_ID, 911 ScarabModulePeer.MODULE_ID); 912 913 914 if (criteria.containsKey(ACTIVE)) 916 { 917 Object possibleBoolean = criteria.get(ACTIVE); 918 if (possibleBoolean instanceof Boolean ) 919 { 920 criteria.add(ACTIVE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 921 } 922 } 923 if (criteria.containsKey(DEDUPE)) 925 { 926 Object possibleBoolean = criteria.get(DEDUPE); 927 if (possibleBoolean instanceof Boolean ) 928 { 929 criteria.add(DEDUPE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 930 } 931 } 932 933 List rows = BasePeer.doSelect(criteria); 934 List results = new ArrayList (); 935 936 for (int i = 0; i < rows.size(); i++) 937 { 938 Record row = (Record) rows.get(i); 939 940 Class omClass = AttributeGroupPeer.getOMClass(); 941 AttributeGroup obj1 = (AttributeGroup) AttributeGroupPeer 942 .row2Object(row, 1, omClass); 943 omClass = ScarabModulePeer.getOMClass(row, offset); 944 ScarabModule obj2 = (ScarabModule)ScarabModulePeer 945 .row2Object(row, offset, omClass); 946 947 boolean newObject = true; 948 for (int j = 0; j < results.size(); j++) 949 { 950 AttributeGroup temp_obj1 = (AttributeGroup)results.get(j); 951 ScarabModule temp_obj2 = (ScarabModule)temp_obj1.getModule(); 952 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 953 { 954 newObject = false; 955 temp_obj2.addAttributeGroup(obj1); 956 break; 957 } 958 } 959 if (newObject) 960 { 961 obj2.initAttributeGroups(); 962 obj2.addAttributeGroup(obj1); 963 } 964 results.add(obj1); 965 } 966 return results; 967 } 968 969 970 971 972 983 protected static List doSelectJoinIssueType(Criteria criteria) 984 throws TorqueException 985 { 986 setDbName(criteria); 987 988 AttributeGroupPeer.addSelectColumns(criteria); 989 int offset = numColumns + 1; 990 IssueTypePeer.addSelectColumns(criteria); 991 992 993 criteria.addJoin(AttributeGroupPeer.ISSUE_TYPE_ID, 994 IssueTypePeer.ISSUE_TYPE_ID); 995 996 997 if (criteria.containsKey(ACTIVE)) 999 { 1000 Object possibleBoolean = criteria.get(ACTIVE); 1001 if (possibleBoolean instanceof Boolean ) 1002 { 1003 criteria.add(ACTIVE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 1004 } 1005 } 1006 if (criteria.containsKey(DEDUPE)) 1008 { 1009 Object possibleBoolean = criteria.get(DEDUPE); 1010 if (possibleBoolean instanceof Boolean ) 1011 { 1012 criteria.add(DEDUPE, ((Boolean ) possibleBoolean).booleanValue() ? 1 : 0); 1013 } 1014 } 1015 1016 List rows = BasePeer.doSelect(criteria); 1017 List results = new ArrayList (); 1018 1019 for (int i = 0; i < rows.size(); i++) 1020 { 1021 Record row = (Record) rows.get(i); 1022 1023 Class omClass = AttributeGroupPeer.getOMClass(); 1024 AttributeGroup obj1 = (AttributeGroup) AttributeGroupPeer 1025 .row2Object(row, 1, omClass); 1026 omClass = IssueTypePeer.getOMClass(); 1027 IssueType obj2 = (IssueType)IssueTypePeer 1028 .row2Object(row, offset, omClass); 1029 1030 boolean newObject = true; 1031 for (int j = 0; j < results.size(); j++) 1032 { 1033 AttributeGroup temp_obj1 = (AttributeGroup)results.get(j); 1034 IssueType temp_obj2 = (IssueType)temp_obj1.getIssueType(); 1035 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 1036 { 1037 newObject = false; 1038 temp_obj2.addAttributeGroup(obj1); 1039 break; 1040 } 1041 } 1042 if (newObject) 1043 { 1044 obj2.initAttributeGroups(); 1045 obj2.addAttributeGroup(obj1); 1046 } 1047 results.add(obj1); 1048 } 1049 return results; 1050 } 1051 1052 1053 1054 1055 1062 protected static TableMap getTableMap() 1063 throws TorqueException 1064 { 1065 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 1066 } 1067 1068 private static void setDbName(Criteria crit) 1069 { 1070 if (crit.getDbName() == Torque.getDefaultDB()) 1074 { 1075 crit.setDbName(DATABASE_NAME); 1076 } 1077 } 1078} 1079 | Popular Tags |