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 35 41 public abstract class BasePortletParameterPeer 42 extends BasePeer 43 { 44 45 46 public static final String DATABASE_NAME = "default"; 47 48 49 public static final String TABLE_NAME = "PORTLET_PARAMETER"; 50 51 56 public static MapBuilder getMapBuilder() 57 throws TorqueException 58 { 59 return getMapBuilder(PortletParameterMapBuilder.CLASS_NAME); 60 } 61 62 63 public static final String ID; 64 65 public static final String NAME; 66 67 public static final String VALUE; 68 69 public static final String TYPE; 70 71 public static final String HIDDEN; 72 73 public static final String ROLE; 74 75 public static final String CACHEDONVALUE; 76 77 public static final String CACHEDONNAME; 78 79 public static final String TITLE; 80 81 public static final String DESCRIPTION; 82 83 public static final String IMAGE; 84 85 public static final String PORTLET_ID; 86 87 public static final String SECURITY; 88 89 static 90 { 91 ID = "PORTLET_PARAMETER.ID"; 92 NAME = "PORTLET_PARAMETER.NAME"; 93 VALUE = "PORTLET_PARAMETER.VALUE"; 94 TYPE = "PORTLET_PARAMETER.TYPE"; 95 HIDDEN = "PORTLET_PARAMETER.HIDDEN"; 96 ROLE = "PORTLET_PARAMETER.ROLE"; 97 CACHEDONVALUE = "PORTLET_PARAMETER.CACHEDONVALUE"; 98 CACHEDONNAME = "PORTLET_PARAMETER.CACHEDONNAME"; 99 TITLE = "PORTLET_PARAMETER.TITLE"; 100 DESCRIPTION = "PORTLET_PARAMETER.DESCRIPTION"; 101 IMAGE = "PORTLET_PARAMETER.IMAGE"; 102 PORTLET_ID = "PORTLET_PARAMETER.PORTLET_ID"; 103 SECURITY = "PORTLET_PARAMETER.SECURITY"; 104 if (Torque.isInit()) 105 { 106 try 107 { 108 getMapBuilder(); 109 } 110 catch (Exception e) 111 { 112 log.error("Could not initialize Peer", e); 113 } 114 } 115 else 116 { 117 Torque.registerMapBuilder(PortletParameterMapBuilder.CLASS_NAME); 118 } 119 } 120 121 122 public static final int numColumns = 13; 123 124 125 protected static final String CLASSNAME_DEFAULT = 126 "org.apache.jetspeed.om.dbregistry.PortletParameter"; 127 128 129 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 130 131 137 private static Class initClass(String className) 138 { 139 Class c = null; 140 try 141 { 142 c = Class.forName(className); 143 } 144 catch (Throwable t) 145 { 146 log.error("A FATAL ERROR has occurred which should not " 147 + "have happened under any circumstance. Please notify " 148 + "the Torque developers <turbine-torque-dev@jakarta.apache.org> " 149 + "and give as many details as possible (including the error " 150 + "stack trace).", t); 151 152 if (t instanceof Error ) 154 { 155 throw (Error ) t.fillInStackTrace(); 156 } 157 } 158 return c; 159 } 160 161 171 public static List resultSet2Objects(java.sql.ResultSet results) 172 throws TorqueException 173 { 174 try 175 { 176 QueryDataSet qds = null; 177 List rows = null; 178 try 179 { 180 qds = new QueryDataSet(results); 181 rows = getSelectResults(qds); 182 } 183 finally 184 { 185 if (qds != null) 186 { 187 qds.close(); 188 } 189 } 190 191 return populateObjects(rows); 192 } 193 catch (SQLException e) 194 { 195 throw new TorqueException(e); 196 } 197 catch (DataSetException e) 198 { 199 throw new TorqueException(e); 200 } 201 } 202 203 204 205 212 public static ObjectKey doInsert(Criteria criteria) 213 throws TorqueException 214 { 215 return BasePortletParameterPeer 216 .doInsert(criteria, (Connection ) null); 217 } 218 219 229 public static ObjectKey doInsert(Criteria criteria, Connection con) 230 throws TorqueException 231 { 232 if (criteria.containsKey(HIDDEN)) 234 { 235 Object possibleBoolean = criteria.get(HIDDEN); 236 if (possibleBoolean instanceof Boolean ) 237 { 238 if (((Boolean ) possibleBoolean).booleanValue()) 239 { 240 criteria.add(HIDDEN, 1); 241 } 242 else 243 { 244 criteria.add(HIDDEN, 0); 245 } 246 } 247 } 248 if (criteria.containsKey(CACHEDONVALUE)) 250 { 251 Object possibleBoolean = criteria.get(CACHEDONVALUE); 252 if (possibleBoolean instanceof Boolean ) 253 { 254 if (((Boolean ) possibleBoolean).booleanValue()) 255 { 256 criteria.add(CACHEDONVALUE, 1); 257 } 258 else 259 { 260 criteria.add(CACHEDONVALUE, 0); 261 } 262 } 263 } 264 if (criteria.containsKey(CACHEDONNAME)) 266 { 267 Object possibleBoolean = criteria.get(CACHEDONNAME); 268 if (possibleBoolean instanceof Boolean ) 269 { 270 if (((Boolean ) possibleBoolean).booleanValue()) 271 { 272 criteria.add(CACHEDONNAME, 1); 273 } 274 else 275 { 276 criteria.add(CACHEDONNAME, 0); 277 } 278 } 279 } 280 281 if (criteria.getDbName() == Torque.getDefaultDB()) 285 { 286 criteria.setDbName(DATABASE_NAME); 287 } 288 if (con == null) 289 { 290 return BasePeer.doInsert(criteria); 291 } 292 else 293 { 294 return BasePeer.doInsert(criteria, con); 295 } 296 } 297 298 305 public static void addSelectColumns(Criteria criteria) 306 throws TorqueException 307 { 308 criteria.addSelectColumn(ID); 309 criteria.addSelectColumn(NAME); 310 criteria.addSelectColumn(VALUE); 311 criteria.addSelectColumn(TYPE); 312 criteria.addSelectColumn(HIDDEN); 313 criteria.addSelectColumn(ROLE); 314 criteria.addSelectColumn(CACHEDONVALUE); 315 criteria.addSelectColumn(CACHEDONNAME); 316 criteria.addSelectColumn(TITLE); 317 criteria.addSelectColumn(DESCRIPTION); 318 criteria.addSelectColumn(IMAGE); 319 criteria.addSelectColumn(PORTLET_ID); 320 criteria.addSelectColumn(SECURITY); 321 } 322 323 332 public static PortletParameter row2Object(Record row, 333 int offset, 334 Class cls) 335 throws TorqueException 336 { 337 try 338 { 339 PortletParameter obj = (PortletParameter) cls.newInstance(); 340 PortletParameterPeer.populateObject(row, offset, obj); 341 obj.setModified(false); 342 obj.setNew(false); 343 344 return obj; 345 } 346 catch (InstantiationException e) 347 { 348 throw new TorqueException(e); 349 } 350 catch (IllegalAccessException e) 351 { 352 throw new TorqueException(e); 353 } 354 } 355 356 365 public static void populateObject(Record row, 366 int offset, 367 PortletParameter obj) 368 throws TorqueException 369 { 370 try 371 { 372 obj.setId(row.getValue(offset + 0).asLong()); 373 obj.setName(row.getValue(offset + 1).asString()); 374 obj.setValue(row.getValue(offset + 2).asString()); 375 obj.setType(row.getValue(offset + 3).asString()); 376 obj.setHidden(row.getValue(offset + 4).asBoolean()); 377 obj.setRole(row.getValue(offset + 5).asString()); 378 obj.setCachedonvalue(row.getValue(offset + 6).asBoolean()); 379 obj.setCachedonname(row.getValue(offset + 7).asBoolean()); 380 obj.setTitle(row.getValue(offset + 8).asString()); 381 obj.setDescription(row.getValue(offset + 9).asString()); 382 obj.setImage(row.getValue(offset + 10).asString()); 383 obj.setPortletId(row.getValue(offset + 11).asLong()); 384 obj.setSecurityRef(row.getValue(offset + 12).asString()); 385 } 386 catch (DataSetException e) 387 { 388 throw new TorqueException(e); 389 } 390 } 391 392 400 public static List doSelect(Criteria criteria) throws TorqueException 401 { 402 return populateObjects(doSelectVillageRecords(criteria)); 403 } 404 405 414 public static List doSelect(Criteria criteria, Connection con) 415 throws TorqueException 416 { 417 return populateObjects(doSelectVillageRecords(criteria, con)); 418 } 419 420 430 public static List doSelectVillageRecords(Criteria criteria) 431 throws TorqueException 432 { 433 return BasePortletParameterPeer 434 .doSelectVillageRecords(criteria, (Connection ) null); 435 } 436 437 445 public static List doSelectVillageRecords(Criteria criteria, Connection con) 446 throws TorqueException 447 { 448 if (criteria.getSelectColumns().size() == 0) 449 { 450 addSelectColumns(criteria); 451 } 452 453 if (criteria.containsKey(HIDDEN)) 455 { 456 Object possibleBoolean = criteria.get(HIDDEN); 457 if (possibleBoolean instanceof Boolean ) 458 { 459 if (((Boolean ) possibleBoolean).booleanValue()) 460 { 461 criteria.add(HIDDEN, 1); 462 } 463 else 464 { 465 criteria.add(HIDDEN, 0); 466 } 467 } 468 } 469 if (criteria.containsKey(CACHEDONVALUE)) 471 { 472 Object possibleBoolean = criteria.get(CACHEDONVALUE); 473 if (possibleBoolean instanceof Boolean ) 474 { 475 if (((Boolean ) possibleBoolean).booleanValue()) 476 { 477 criteria.add(CACHEDONVALUE, 1); 478 } 479 else 480 { 481 criteria.add(CACHEDONVALUE, 0); 482 } 483 } 484 } 485 if (criteria.containsKey(CACHEDONNAME)) 487 { 488 Object possibleBoolean = criteria.get(CACHEDONNAME); 489 if (possibleBoolean instanceof Boolean ) 490 { 491 if (((Boolean ) possibleBoolean).booleanValue()) 492 { 493 criteria.add(CACHEDONNAME, 1); 494 } 495 else 496 { 497 criteria.add(CACHEDONNAME, 0); 498 } 499 } 500 } 501 502 if (criteria.getDbName() == Torque.getDefaultDB()) 506 { 507 criteria.setDbName(DATABASE_NAME); 508 } 509 if (con == null) 512 { 513 return BasePeer.doSelect(criteria); 514 } 515 else 516 { 517 return BasePeer.doSelect(criteria, con); 518 } 519 } 520 521 528 public static List populateObjects(List records) 529 throws TorqueException 530 { 531 List results = new ArrayList (records.size()); 532 533 for (int i = 0; i < records.size(); i++) 535 { 536 Record row = (Record) records.get(i); 537 results.add(PortletParameterPeer.row2Object(row, 1, 538 PortletParameterPeer.getOMClass())); 539 } 540 return results; 541 } 542 543 544 552 public static Class getOMClass() 553 throws TorqueException 554 { 555 return CLASS_DEFAULT; 556 } 557 558 566 public static void doUpdate(Criteria criteria) throws TorqueException 567 { 568 BasePortletParameterPeer 569 .doUpdate(criteria, (Connection ) null); 570 } 571 572 583 public static void doUpdate(Criteria criteria, Connection con) 584 throws TorqueException 585 { 586 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 587 selectCriteria.put(ID, criteria.remove(ID)); 588 if (criteria.containsKey(HIDDEN)) 590 { 591 Object possibleBoolean = criteria.get(HIDDEN); 592 if (possibleBoolean instanceof Boolean ) 593 { 594 if (((Boolean ) possibleBoolean).booleanValue()) 595 { 596 criteria.add(HIDDEN, 1); 597 } 598 else 599 { 600 criteria.add(HIDDEN, 0); 601 } 602 } 603 } 604 if (criteria.containsKey(CACHEDONVALUE)) 606 { 607 Object possibleBoolean = criteria.get(CACHEDONVALUE); 608 if (possibleBoolean instanceof Boolean ) 609 { 610 if (((Boolean ) possibleBoolean).booleanValue()) 611 { 612 criteria.add(CACHEDONVALUE, 1); 613 } 614 else 615 { 616 criteria.add(CACHEDONVALUE, 0); 617 } 618 } 619 } 620 if (criteria.containsKey(CACHEDONNAME)) 622 { 623 Object possibleBoolean = criteria.get(CACHEDONNAME); 624 if (possibleBoolean instanceof Boolean ) 625 { 626 if (((Boolean ) possibleBoolean).booleanValue()) 627 { 628 criteria.add(CACHEDONNAME, 1); 629 } 630 else 631 { 632 criteria.add(CACHEDONNAME, 0); 633 } 634 } 635 } 636 637 if (criteria.getDbName() == Torque.getDefaultDB()) 641 { 642 criteria.setDbName(DATABASE_NAME); 643 } 644 if (con == null) 645 { 646 BasePeer.doUpdate(selectCriteria, criteria); 647 } 648 else 649 { 650 BasePeer.doUpdate(selectCriteria, criteria, con); 651 } 652 } 653 654 661 public static void doDelete(Criteria criteria) throws TorqueException 662 { 663 BasePortletParameterPeer 664 .doDelete(criteria, (Connection ) null); 665 } 666 667 677 public static void doDelete(Criteria criteria, Connection con) 678 throws TorqueException 679 { 680 if (criteria.containsKey(HIDDEN)) 682 { 683 Object possibleBoolean = criteria.get(HIDDEN); 684 if (possibleBoolean instanceof Boolean ) 685 { 686 if (((Boolean ) possibleBoolean).booleanValue()) 687 { 688 criteria.add(HIDDEN, 1); 689 } 690 else 691 { 692 criteria.add(HIDDEN, 0); 693 } 694 } 695 } 696 if (criteria.containsKey(CACHEDONVALUE)) 698 { 699 Object possibleBoolean = criteria.get(CACHEDONVALUE); 700 if (possibleBoolean instanceof Boolean ) 701 { 702 if (((Boolean ) possibleBoolean).booleanValue()) 703 { 704 criteria.add(CACHEDONVALUE, 1); 705 } 706 else 707 { 708 criteria.add(CACHEDONVALUE, 0); 709 } 710 } 711 } 712 if (criteria.containsKey(CACHEDONNAME)) 714 { 715 Object possibleBoolean = criteria.get(CACHEDONNAME); 716 if (possibleBoolean instanceof Boolean ) 717 { 718 if (((Boolean ) possibleBoolean).booleanValue()) 719 { 720 criteria.add(CACHEDONNAME, 1); 721 } 722 else 723 { 724 criteria.add(CACHEDONNAME, 0); 725 } 726 } 727 } 728 729 if (criteria.getDbName() == Torque.getDefaultDB()) 733 { 734 criteria.setDbName(DATABASE_NAME); 735 } 736 if (con == null) 737 { 738 BasePeer.doDelete(criteria); 739 } 740 else 741 { 742 BasePeer.doDelete(criteria, con); 743 } 744 } 745 746 752 public static List doSelect(PortletParameter obj) throws TorqueException 753 { 754 return doSelect(buildCriteria(obj)); 755 } 756 757 763 public static void doInsert(PortletParameter obj) throws TorqueException 764 { 765 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 766 obj.setNew(false); 767 obj.setModified(false); 768 } 769 770 775 public static void doUpdate(PortletParameter obj) throws TorqueException 776 { 777 doUpdate(buildCriteria(obj)); 778 obj.setModified(false); 779 } 780 781 786 public static void doDelete(PortletParameter obj) throws TorqueException 787 { 788 doDelete(buildCriteria(obj)); 789 } 790 791 801 public static void doInsert(PortletParameter obj, Connection con) 802 throws TorqueException 803 { 804 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 805 obj.setNew(false); 806 obj.setModified(false); 807 } 808 809 819 public static void doUpdate(PortletParameter obj, Connection con) 820 throws TorqueException 821 { 822 doUpdate(buildCriteria(obj), con); 823 obj.setModified(false); 824 } 825 826 836 public static void doDelete(PortletParameter obj, Connection con) 837 throws TorqueException 838 { 839 doDelete(buildCriteria(obj), con); 840 } 841 842 849 public static void doDelete(ObjectKey pk) throws TorqueException 850 { 851 BasePortletParameterPeer 852 .doDelete(pk, (Connection ) null); 853 } 854 855 865 public static void doDelete(ObjectKey pk, Connection con) 866 throws TorqueException 867 { 868 doDelete(buildCriteria(pk), con); 869 } 870 871 872 public static Criteria buildCriteria( ObjectKey pk ) 873 { 874 Criteria criteria = new Criteria(); 875 criteria.add(ID, pk); 876 return criteria; 877 } 878 879 880 public static Criteria buildCriteria( PortletParameter obj ) 881 { 882 Criteria criteria = new Criteria(DATABASE_NAME); 883 if (!obj.isNew()) 884 criteria.add(ID, obj.getId()); 885 criteria.add(NAME, obj.getName()); 886 criteria.add(VALUE, obj.getValue()); 887 criteria.add(TYPE, obj.getType()); 888 criteria.add(HIDDEN, obj.getHidden()); 889 criteria.add(ROLE, obj.getRole()); 890 criteria.add(CACHEDONVALUE, obj.getCachedonvalue()); 891 criteria.add(CACHEDONNAME, obj.getCachedonname()); 892 criteria.add(TITLE, obj.getTitle()); 893 criteria.add(DESCRIPTION, obj.getDescription()); 894 criteria.add(IMAGE, obj.getImage()); 895 criteria.add(PORTLET_ID, obj.getPortletId()); 896 criteria.add(SECURITY, obj.getSecurityRef()); 897 return criteria; 898 } 899 900 901 910 public static PortletParameter retrieveByPK(long pk) 911 throws TorqueException, NoRowsException, TooManyRowsException 912 { 913 return retrieveByPK(SimpleKey.keyFor(pk)); 914 } 915 916 925 public static PortletParameter retrieveByPK(ObjectKey pk) 926 throws TorqueException, NoRowsException, TooManyRowsException 927 { 928 Connection db = null; 929 PortletParameter retVal = null; 930 try 931 { 932 db = Torque.getConnection(DATABASE_NAME); 933 retVal = retrieveByPK(pk, db); 934 } 935 finally 936 { 937 Torque.closeConnection(db); 938 } 939 return(retVal); 940 } 941 942 952 public static PortletParameter retrieveByPK(ObjectKey pk, Connection con) 953 throws TorqueException, NoRowsException, TooManyRowsException 954 { 955 Criteria criteria = buildCriteria(pk); 956 List v = doSelect(criteria, con); 957 if (v.size() == 0) 958 { 959 throw new NoRowsException("Failed to select a row."); 960 } 961 else if (v.size() > 1) 962 { 963 throw new TooManyRowsException("Failed to select only one row."); 964 } 965 else 966 { 967 return (PortletParameter)v.get(0); 968 } 969 } 970 971 978 public static List retrieveByPKs(List pks) 979 throws TorqueException 980 { 981 Connection db = null; 982 List retVal = null; 983 try 984 { 985 db = Torque.getConnection(DATABASE_NAME); 986 retVal = retrieveByPKs(pks, db); 987 } 988 finally 989 { 990 Torque.closeConnection(db); 991 } 992 return(retVal); 993 } 994 995 1003 public static List retrieveByPKs( List pks, Connection dbcon ) 1004 throws TorqueException 1005 { 1006 List objs = null; 1007 if (pks == null || pks.size() == 0) 1008 { 1009 objs = new LinkedList (); 1010 } 1011 else 1012 { 1013 Criteria criteria = new Criteria(); 1014 criteria.addIn( ID, pks ); 1015 objs = doSelect(criteria, dbcon); 1016 } 1017 return objs; 1018 } 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1040 protected static List doSelectJoinPortletDbEntry(Criteria c) 1041 throws TorqueException 1042 { 1043 if (c.getDbName() == Torque.getDefaultDB()) 1047 { 1048 c.setDbName(DATABASE_NAME); 1049 } 1050 1051 PortletParameterPeer.addSelectColumns(c); 1052 int offset = numColumns + 1; 1053 PortletDbEntryPeer.addSelectColumns(c); 1054 1055 1056 c.addJoin(PortletParameterPeer.PORTLET_ID, 1057 PortletDbEntryPeer.ID); 1058 1059 1060 if (c.containsKey(HIDDEN)) 1062 { 1063 Object possibleBoolean = c.get(HIDDEN); 1064 if (possibleBoolean instanceof Boolean ) 1065 { 1066 if (((Boolean ) possibleBoolean).booleanValue()) 1067 { 1068 c.add(HIDDEN, 1); 1069 } 1070 else 1071 { 1072 c.add(HIDDEN, 0); 1073 } 1074 } 1075 } 1076 if (c.containsKey(CACHEDONVALUE)) 1078 { 1079 Object possibleBoolean = c.get(CACHEDONVALUE); 1080 if (possibleBoolean instanceof Boolean ) 1081 { 1082 if (((Boolean ) possibleBoolean).booleanValue()) 1083 { 1084 c.add(CACHEDONVALUE, 1); 1085 } 1086 else 1087 { 1088 c.add(CACHEDONVALUE, 0); 1089 } 1090 } 1091 } 1092 if (c.containsKey(CACHEDONNAME)) 1094 { 1095 Object possibleBoolean = c.get(CACHEDONNAME); 1096 if (possibleBoolean instanceof Boolean ) 1097 { 1098 if (((Boolean ) possibleBoolean).booleanValue()) 1099 { 1100 c.add(CACHEDONNAME, 1); 1101 } 1102 else 1103 { 1104 c.add(CACHEDONNAME, 0); 1105 } 1106 } 1107 } 1108 1109 List rows = BasePeer.doSelect(c); 1110 List results = new ArrayList (); 1111 1112 for (int i = 0; i < rows.size(); i++) 1113 { 1114 Record row = (Record) rows.get(i); 1115 1116 Class omClass = PortletParameterPeer.getOMClass(); 1117 PortletParameter obj1 = (PortletParameter) PortletParameterPeer 1118 .row2Object(row, 1, omClass); 1119 omClass = PortletDbEntryPeer.getOMClass(); 1120 PortletDbEntry obj2 = (PortletDbEntry)PortletDbEntryPeer 1121 .row2Object(row, offset, omClass); 1122 1123 boolean newObject = true; 1124 for (int j = 0; j < results.size(); j++) 1125 { 1126 PortletParameter temp_obj1 = (PortletParameter)results.get(j); 1127 PortletDbEntry temp_obj2 = (PortletDbEntry)temp_obj1.getPortletDbEntry(); 1128 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 1129 { 1130 newObject = false; 1131 temp_obj2.addPortletParameter(obj1); 1132 break; 1133 } 1134 } 1135 if (newObject) 1136 { 1137 obj2.initPortletParameters(); 1138 obj2.addPortletParameter(obj1); 1139 } 1140 results.add(obj1); 1141 } 1142 return results; 1143 } 1144 1145 1146 1147 1148 1155 protected static TableMap getTableMap() 1156 throws TorqueException 1157 { 1158 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 1159 } 1160 } 1161 | Popular Tags |