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 40 public abstract class BasePortletDbEntryPeer 41 extends BasePeer 42 { 43 44 45 public static final String DATABASE_NAME = "default"; 46 47 48 public static final String TABLE_NAME = "PORTLET"; 49 50 55 public static MapBuilder getMapBuilder() 56 throws TorqueException 57 { 58 return getMapBuilder(PortletDbEntryMapBuilder.CLASS_NAME); 59 } 60 61 62 public static final String ID; 63 64 public static final String NAME; 65 66 public static final String HIDDEN; 67 68 public static final String CLASSNAME; 69 70 public static final String TYPE; 71 72 public static final String APPLICATION; 73 74 public static final String PARENT; 75 76 public static final String URL; 77 78 public static final String CACHEDONURL; 79 80 public static final String ROLE; 81 82 public static final String TITLE; 83 84 public static final String DESCRIPTION; 85 86 public static final String IMAGE; 87 88 public static final String SECURITY; 89 90 static 91 { 92 ID = "PORTLET.ID"; 93 NAME = "PORTLET.NAME"; 94 HIDDEN = "PORTLET.HIDDEN"; 95 CLASSNAME = "PORTLET.CLASSNAME"; 96 TYPE = "PORTLET.TYPE"; 97 APPLICATION = "PORTLET.APPLICATION"; 98 PARENT = "PORTLET.PARENT"; 99 URL = "PORTLET.URL"; 100 CACHEDONURL = "PORTLET.CACHEDONURL"; 101 ROLE = "PORTLET.ROLE"; 102 TITLE = "PORTLET.TITLE"; 103 DESCRIPTION = "PORTLET.DESCRIPTION"; 104 IMAGE = "PORTLET.IMAGE"; 105 SECURITY = "PORTLET.SECURITY"; 106 if (Torque.isInit()) 107 { 108 try 109 { 110 getMapBuilder(); 111 } 112 catch (Exception e) 113 { 114 log.error("Could not initialize Peer", e); 115 } 116 } 117 else 118 { 119 Torque.registerMapBuilder(PortletDbEntryMapBuilder.CLASS_NAME); 120 } 121 } 122 123 124 public static final int numColumns = 14; 125 126 127 protected static final String CLASSNAME_DEFAULT = 128 "org.apache.jetspeed.om.dbregistry.PortletDbEntry"; 129 130 131 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 132 133 139 private static Class initClass(String className) 140 { 141 Class c = null; 142 try 143 { 144 c = Class.forName(className); 145 } 146 catch (Throwable t) 147 { 148 log.error("A FATAL ERROR has occurred which should not " 149 + "have happened under any circumstance. Please notify " 150 + "the Torque developers <turbine-torque-dev@jakarta.apache.org> " 151 + "and give as many details as possible (including the error " 152 + "stack trace).", t); 153 154 if (t instanceof Error ) 156 { 157 throw (Error ) t.fillInStackTrace(); 158 } 159 } 160 return c; 161 } 162 163 173 public static List resultSet2Objects(java.sql.ResultSet results) 174 throws TorqueException 175 { 176 try 177 { 178 QueryDataSet qds = null; 179 List rows = null; 180 try 181 { 182 qds = new QueryDataSet(results); 183 rows = getSelectResults(qds); 184 } 185 finally 186 { 187 if (qds != null) 188 { 189 qds.close(); 190 } 191 } 192 193 return populateObjects(rows); 194 } 195 catch (SQLException e) 196 { 197 throw new TorqueException(e); 198 } 199 catch (DataSetException e) 200 { 201 throw new TorqueException(e); 202 } 203 } 204 205 206 207 214 public static ObjectKey doInsert(Criteria criteria) 215 throws TorqueException 216 { 217 return BasePortletDbEntryPeer 218 .doInsert(criteria, (Connection ) null); 219 } 220 221 231 public static ObjectKey doInsert(Criteria criteria, Connection con) 232 throws TorqueException 233 { 234 if (criteria.containsKey(HIDDEN)) 236 { 237 Object possibleBoolean = criteria.get(HIDDEN); 238 if (possibleBoolean instanceof Boolean ) 239 { 240 if (((Boolean ) possibleBoolean).booleanValue()) 241 { 242 criteria.add(HIDDEN, 1); 243 } 244 else 245 { 246 criteria.add(HIDDEN, 0); 247 } 248 } 249 } 250 if (criteria.containsKey(APPLICATION)) 252 { 253 Object possibleBoolean = criteria.get(APPLICATION); 254 if (possibleBoolean instanceof Boolean ) 255 { 256 if (((Boolean ) possibleBoolean).booleanValue()) 257 { 258 criteria.add(APPLICATION, 1); 259 } 260 else 261 { 262 criteria.add(APPLICATION, 0); 263 } 264 } 265 } 266 if (criteria.containsKey(CACHEDONURL)) 268 { 269 Object possibleBoolean = criteria.get(CACHEDONURL); 270 if (possibleBoolean instanceof Boolean ) 271 { 272 if (((Boolean ) possibleBoolean).booleanValue()) 273 { 274 criteria.add(CACHEDONURL, 1); 275 } 276 else 277 { 278 criteria.add(CACHEDONURL, 0); 279 } 280 } 281 } 282 283 if (criteria.getDbName() == Torque.getDefaultDB()) 287 { 288 criteria.setDbName(DATABASE_NAME); 289 } 290 if (con == null) 291 { 292 return BasePeer.doInsert(criteria); 293 } 294 else 295 { 296 return BasePeer.doInsert(criteria, con); 297 } 298 } 299 300 307 public static void addSelectColumns(Criteria criteria) 308 throws TorqueException 309 { 310 criteria.addSelectColumn(ID); 311 criteria.addSelectColumn(NAME); 312 criteria.addSelectColumn(HIDDEN); 313 criteria.addSelectColumn(CLASSNAME); 314 criteria.addSelectColumn(TYPE); 315 criteria.addSelectColumn(APPLICATION); 316 criteria.addSelectColumn(PARENT); 317 criteria.addSelectColumn(URL); 318 criteria.addSelectColumn(CACHEDONURL); 319 criteria.addSelectColumn(ROLE); 320 criteria.addSelectColumn(TITLE); 321 criteria.addSelectColumn(DESCRIPTION); 322 criteria.addSelectColumn(IMAGE); 323 criteria.addSelectColumn(SECURITY); 324 } 325 326 335 public static PortletDbEntry row2Object(Record row, 336 int offset, 337 Class cls) 338 throws TorqueException 339 { 340 try 341 { 342 PortletDbEntry obj = (PortletDbEntry) cls.newInstance(); 343 PortletDbEntryPeer.populateObject(row, offset, obj); 344 obj.setModified(false); 345 obj.setNew(false); 346 347 return obj; 348 } 349 catch (InstantiationException e) 350 { 351 throw new TorqueException(e); 352 } 353 catch (IllegalAccessException e) 354 { 355 throw new TorqueException(e); 356 } 357 } 358 359 368 public static void populateObject(Record row, 369 int offset, 370 PortletDbEntry obj) 371 throws TorqueException 372 { 373 try 374 { 375 obj.setId(row.getValue(offset + 0).asLong()); 376 obj.setName(row.getValue(offset + 1).asString()); 377 obj.setHidden(row.getValue(offset + 2).asBoolean()); 378 obj.setClassname(row.getValue(offset + 3).asString()); 379 obj.setType(row.getValue(offset + 4).asString()); 380 obj.setApplication(row.getValue(offset + 5).asBoolean()); 381 obj.setParentRef(row.getValue(offset + 6).asLong()); 382 obj.setURL(row.getValue(offset + 7).asString()); 383 obj.setCachedonurl(row.getValue(offset + 8).asBoolean()); 384 obj.setRole(row.getValue(offset + 9).asString()); 385 obj.setTitle(row.getValue(offset + 10).asString()); 386 obj.setDescription(row.getValue(offset + 11).asString()); 387 obj.setImage(row.getValue(offset + 12).asString()); 388 obj.setSecurityRef(row.getValue(offset + 13).asString()); 389 } 390 catch (DataSetException e) 391 { 392 throw new TorqueException(e); 393 } 394 } 395 396 404 public static List doSelect(Criteria criteria) throws TorqueException 405 { 406 return populateObjects(doSelectVillageRecords(criteria)); 407 } 408 409 418 public static List doSelect(Criteria criteria, Connection con) 419 throws TorqueException 420 { 421 return populateObjects(doSelectVillageRecords(criteria, con)); 422 } 423 424 434 public static List doSelectVillageRecords(Criteria criteria) 435 throws TorqueException 436 { 437 return BasePortletDbEntryPeer 438 .doSelectVillageRecords(criteria, (Connection ) null); 439 } 440 441 449 public static List doSelectVillageRecords(Criteria criteria, Connection con) 450 throws TorqueException 451 { 452 if (criteria.getSelectColumns().size() == 0) 453 { 454 addSelectColumns(criteria); 455 } 456 457 if (criteria.containsKey(HIDDEN)) 459 { 460 Object possibleBoolean = criteria.get(HIDDEN); 461 if (possibleBoolean instanceof Boolean ) 462 { 463 if (((Boolean ) possibleBoolean).booleanValue()) 464 { 465 criteria.add(HIDDEN, 1); 466 } 467 else 468 { 469 criteria.add(HIDDEN, 0); 470 } 471 } 472 } 473 if (criteria.containsKey(APPLICATION)) 475 { 476 Object possibleBoolean = criteria.get(APPLICATION); 477 if (possibleBoolean instanceof Boolean ) 478 { 479 if (((Boolean ) possibleBoolean).booleanValue()) 480 { 481 criteria.add(APPLICATION, 1); 482 } 483 else 484 { 485 criteria.add(APPLICATION, 0); 486 } 487 } 488 } 489 if (criteria.containsKey(CACHEDONURL)) 491 { 492 Object possibleBoolean = criteria.get(CACHEDONURL); 493 if (possibleBoolean instanceof Boolean ) 494 { 495 if (((Boolean ) possibleBoolean).booleanValue()) 496 { 497 criteria.add(CACHEDONURL, 1); 498 } 499 else 500 { 501 criteria.add(CACHEDONURL, 0); 502 } 503 } 504 } 505 506 if (criteria.getDbName() == Torque.getDefaultDB()) 510 { 511 criteria.setDbName(DATABASE_NAME); 512 } 513 if (con == null) 516 { 517 return BasePeer.doSelect(criteria); 518 } 519 else 520 { 521 return BasePeer.doSelect(criteria, con); 522 } 523 } 524 525 532 public static List populateObjects(List records) 533 throws TorqueException 534 { 535 List results = new ArrayList (records.size()); 536 537 for (int i = 0; i < records.size(); i++) 539 { 540 Record row = (Record) records.get(i); 541 results.add(PortletDbEntryPeer.row2Object(row, 1, 542 PortletDbEntryPeer.getOMClass())); 543 } 544 return results; 545 } 546 547 548 556 public static Class getOMClass() 557 throws TorqueException 558 { 559 return CLASS_DEFAULT; 560 } 561 562 570 public static void doUpdate(Criteria criteria) throws TorqueException 571 { 572 BasePortletDbEntryPeer 573 .doUpdate(criteria, (Connection ) null); 574 } 575 576 587 public static void doUpdate(Criteria criteria, Connection con) 588 throws TorqueException 589 { 590 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 591 selectCriteria.put(ID, criteria.remove(ID)); 592 if (criteria.containsKey(HIDDEN)) 594 { 595 Object possibleBoolean = criteria.get(HIDDEN); 596 if (possibleBoolean instanceof Boolean ) 597 { 598 if (((Boolean ) possibleBoolean).booleanValue()) 599 { 600 criteria.add(HIDDEN, 1); 601 } 602 else 603 { 604 criteria.add(HIDDEN, 0); 605 } 606 } 607 } 608 if (criteria.containsKey(APPLICATION)) 610 { 611 Object possibleBoolean = criteria.get(APPLICATION); 612 if (possibleBoolean instanceof Boolean ) 613 { 614 if (((Boolean ) possibleBoolean).booleanValue()) 615 { 616 criteria.add(APPLICATION, 1); 617 } 618 else 619 { 620 criteria.add(APPLICATION, 0); 621 } 622 } 623 } 624 if (criteria.containsKey(CACHEDONURL)) 626 { 627 Object possibleBoolean = criteria.get(CACHEDONURL); 628 if (possibleBoolean instanceof Boolean ) 629 { 630 if (((Boolean ) possibleBoolean).booleanValue()) 631 { 632 criteria.add(CACHEDONURL, 1); 633 } 634 else 635 { 636 criteria.add(CACHEDONURL, 0); 637 } 638 } 639 } 640 641 if (criteria.getDbName() == Torque.getDefaultDB()) 645 { 646 criteria.setDbName(DATABASE_NAME); 647 } 648 if (con == null) 649 { 650 BasePeer.doUpdate(selectCriteria, criteria); 651 } 652 else 653 { 654 BasePeer.doUpdate(selectCriteria, criteria, con); 655 } 656 } 657 658 665 public static void doDelete(Criteria criteria) throws TorqueException 666 { 667 BasePortletDbEntryPeer 668 .doDelete(criteria, (Connection ) null); 669 } 670 671 681 public static void doDelete(Criteria criteria, Connection con) 682 throws TorqueException 683 { 684 if (criteria.containsKey(HIDDEN)) 686 { 687 Object possibleBoolean = criteria.get(HIDDEN); 688 if (possibleBoolean instanceof Boolean ) 689 { 690 if (((Boolean ) possibleBoolean).booleanValue()) 691 { 692 criteria.add(HIDDEN, 1); 693 } 694 else 695 { 696 criteria.add(HIDDEN, 0); 697 } 698 } 699 } 700 if (criteria.containsKey(APPLICATION)) 702 { 703 Object possibleBoolean = criteria.get(APPLICATION); 704 if (possibleBoolean instanceof Boolean ) 705 { 706 if (((Boolean ) possibleBoolean).booleanValue()) 707 { 708 criteria.add(APPLICATION, 1); 709 } 710 else 711 { 712 criteria.add(APPLICATION, 0); 713 } 714 } 715 } 716 if (criteria.containsKey(CACHEDONURL)) 718 { 719 Object possibleBoolean = criteria.get(CACHEDONURL); 720 if (possibleBoolean instanceof Boolean ) 721 { 722 if (((Boolean ) possibleBoolean).booleanValue()) 723 { 724 criteria.add(CACHEDONURL, 1); 725 } 726 else 727 { 728 criteria.add(CACHEDONURL, 0); 729 } 730 } 731 } 732 733 if (criteria.getDbName() == Torque.getDefaultDB()) 737 { 738 criteria.setDbName(DATABASE_NAME); 739 } 740 if (con == null) 741 { 742 BasePeer.doDelete(criteria); 743 } 744 else 745 { 746 BasePeer.doDelete(criteria, con); 747 } 748 } 749 750 756 public static List doSelect(PortletDbEntry obj) throws TorqueException 757 { 758 return doSelect(buildCriteria(obj)); 759 } 760 761 767 public static void doInsert(PortletDbEntry obj) throws TorqueException 768 { 769 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 770 obj.setNew(false); 771 obj.setModified(false); 772 } 773 774 779 public static void doUpdate(PortletDbEntry obj) throws TorqueException 780 { 781 doUpdate(buildCriteria(obj)); 782 obj.setModified(false); 783 } 784 785 790 public static void doDelete(PortletDbEntry obj) throws TorqueException 791 { 792 doDelete(buildCriteria(obj)); 793 } 794 795 805 public static void doInsert(PortletDbEntry obj, Connection con) 806 throws TorqueException 807 { 808 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 809 obj.setNew(false); 810 obj.setModified(false); 811 } 812 813 823 public static void doUpdate(PortletDbEntry obj, Connection con) 824 throws TorqueException 825 { 826 doUpdate(buildCriteria(obj), con); 827 obj.setModified(false); 828 } 829 830 840 public static void doDelete(PortletDbEntry obj, Connection con) 841 throws TorqueException 842 { 843 doDelete(buildCriteria(obj), con); 844 } 845 846 853 public static void doDelete(ObjectKey pk) throws TorqueException 854 { 855 BasePortletDbEntryPeer 856 .doDelete(pk, (Connection ) null); 857 } 858 859 869 public static void doDelete(ObjectKey pk, Connection con) 870 throws TorqueException 871 { 872 doDelete(buildCriteria(pk), con); 873 } 874 875 876 public static Criteria buildCriteria( ObjectKey pk ) 877 { 878 Criteria criteria = new Criteria(); 879 criteria.add(ID, pk); 880 return criteria; 881 } 882 883 884 public static Criteria buildCriteria( PortletDbEntry obj ) 885 { 886 Criteria criteria = new Criteria(DATABASE_NAME); 887 if (!obj.isNew()) 888 criteria.add(ID, obj.getId()); 889 criteria.add(NAME, obj.getName()); 890 criteria.add(HIDDEN, obj.getHidden()); 891 criteria.add(CLASSNAME, obj.getClassname()); 892 criteria.add(TYPE, obj.getType()); 893 criteria.add(APPLICATION, obj.getApplication()); 894 criteria.add(PARENT, obj.getParentRef()); 895 criteria.add(URL, obj.getURL()); 896 criteria.add(CACHEDONURL, obj.getCachedonurl()); 897 criteria.add(ROLE, obj.getRole()); 898 criteria.add(TITLE, obj.getTitle()); 899 criteria.add(DESCRIPTION, obj.getDescription()); 900 criteria.add(IMAGE, obj.getImage()); 901 criteria.add(SECURITY, obj.getSecurityRef()); 902 return criteria; 903 } 904 905 906 915 public static PortletDbEntry retrieveByPK(long pk) 916 throws TorqueException, NoRowsException, TooManyRowsException 917 { 918 return retrieveByPK(SimpleKey.keyFor(pk)); 919 } 920 921 930 public static PortletDbEntry retrieveByPK(ObjectKey pk) 931 throws TorqueException, NoRowsException, TooManyRowsException 932 { 933 Connection db = null; 934 PortletDbEntry retVal = null; 935 try 936 { 937 db = Torque.getConnection(DATABASE_NAME); 938 retVal = retrieveByPK(pk, db); 939 } 940 finally 941 { 942 Torque.closeConnection(db); 943 } 944 return(retVal); 945 } 946 947 957 public static PortletDbEntry retrieveByPK(ObjectKey pk, Connection con) 958 throws TorqueException, NoRowsException, TooManyRowsException 959 { 960 Criteria criteria = buildCriteria(pk); 961 List v = doSelect(criteria, con); 962 if (v.size() == 0) 963 { 964 throw new NoRowsException("Failed to select a row."); 965 } 966 else if (v.size() > 1) 967 { 968 throw new TooManyRowsException("Failed to select only one row."); 969 } 970 else 971 { 972 return (PortletDbEntry)v.get(0); 973 } 974 } 975 976 983 public static List retrieveByPKs(List pks) 984 throws TorqueException 985 { 986 Connection db = null; 987 List retVal = null; 988 try 989 { 990 db = Torque.getConnection(DATABASE_NAME); 991 retVal = retrieveByPKs(pks, db); 992 } 993 finally 994 { 995 Torque.closeConnection(db); 996 } 997 return(retVal); 998 } 999 1000 1008 public static List retrieveByPKs( List pks, Connection dbcon ) 1009 throws TorqueException 1010 { 1011 List objs = null; 1012 if (pks == null || pks.size() == 0) 1013 { 1014 objs = new LinkedList (); 1015 } 1016 else 1017 { 1018 Criteria criteria = new Criteria(); 1019 criteria.addIn( ID, pks ); 1020 objs = doSelect(criteria, dbcon); 1021 } 1022 return objs; 1023 } 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1041 protected static TableMap getTableMap() 1042 throws TorqueException 1043 { 1044 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 1045 } 1046 } 1047 | Popular Tags |