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 BasePortletCategoryPeer 42 extends BasePeer 43 { 44 45 46 public static final String DATABASE_NAME = "default"; 47 48 49 public static final String TABLE_NAME = "CATEGORY"; 50 51 56 public static MapBuilder getMapBuilder() 57 throws TorqueException 58 { 59 return getMapBuilder(PortletCategoryMapBuilder.CLASS_NAME); 60 } 61 62 63 public static final String ID; 64 65 public static final String NAME; 66 67 public static final String GROUPE; 68 69 public static final String OWNER; 70 71 static 72 { 73 ID = "CATEGORY.ID"; 74 NAME = "CATEGORY.NAME"; 75 GROUPE = "CATEGORY.GROUPE"; 76 OWNER = "CATEGORY.OWNER"; 77 if (Torque.isInit()) 78 { 79 try 80 { 81 getMapBuilder(); 82 } 83 catch (Exception e) 84 { 85 log.error("Could not initialize Peer", e); 86 } 87 } 88 else 89 { 90 Torque.registerMapBuilder(PortletCategoryMapBuilder.CLASS_NAME); 91 } 92 } 93 94 95 public static final int numColumns = 4; 96 97 98 protected static final String CLASSNAME_DEFAULT = 99 "org.apache.jetspeed.om.dbregistry.PortletCategory"; 100 101 102 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 103 104 110 private static Class initClass(String className) 111 { 112 Class c = null; 113 try 114 { 115 c = Class.forName(className); 116 } 117 catch (Throwable t) 118 { 119 log.error("A FATAL ERROR has occurred which should not " 120 + "have happened under any circumstance. Please notify " 121 + "the Torque developers <turbine-torque-dev@jakarta.apache.org> " 122 + "and give as many details as possible (including the error " 123 + "stack trace).", t); 124 125 if (t instanceof Error ) 127 { 128 throw (Error ) t.fillInStackTrace(); 129 } 130 } 131 return c; 132 } 133 134 144 public static List resultSet2Objects(java.sql.ResultSet results) 145 throws TorqueException 146 { 147 try 148 { 149 QueryDataSet qds = null; 150 List rows = null; 151 try 152 { 153 qds = new QueryDataSet(results); 154 rows = getSelectResults(qds); 155 } 156 finally 157 { 158 if (qds != null) 159 { 160 qds.close(); 161 } 162 } 163 164 return populateObjects(rows); 165 } 166 catch (SQLException e) 167 { 168 throw new TorqueException(e); 169 } 170 catch (DataSetException e) 171 { 172 throw new TorqueException(e); 173 } 174 } 175 176 177 178 185 public static ObjectKey doInsert(Criteria criteria) 186 throws TorqueException 187 { 188 return BasePortletCategoryPeer 189 .doInsert(criteria, (Connection ) null); 190 } 191 192 202 public static ObjectKey doInsert(Criteria criteria, Connection con) 203 throws TorqueException 204 { 205 206 if (criteria.getDbName() == Torque.getDefaultDB()) 210 { 211 criteria.setDbName(DATABASE_NAME); 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(ID); 234 criteria.addSelectColumn(NAME); 235 criteria.addSelectColumn(GROUPE); 236 criteria.addSelectColumn(OWNER); 237 } 238 239 248 public static PortletCategory row2Object(Record row, 249 int offset, 250 Class cls) 251 throws TorqueException 252 { 253 try 254 { 255 PortletCategory obj = (PortletCategory) cls.newInstance(); 256 PortletCategoryPeer.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 PortletCategory obj) 284 throws TorqueException 285 { 286 try 287 { 288 obj.setId(row.getValue(offset + 0).asLong()); 289 obj.setName(row.getValue(offset + 1).asString()); 290 obj.setGroup(row.getValue(offset + 2).asString()); 291 obj.setOwner(row.getValue(offset + 3).asLong()); 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 BasePortletCategoryPeer 341 .doSelectVillageRecords(criteria, (Connection ) null); 342 } 343 344 352 public static List doSelectVillageRecords(Criteria criteria, Connection con) 353 throws TorqueException 354 { 355 if (criteria.getSelectColumns().size() == 0) 356 { 357 addSelectColumns(criteria); 358 } 359 360 361 if (criteria.getDbName() == Torque.getDefaultDB()) 365 { 366 criteria.setDbName(DATABASE_NAME); 367 } 368 if (con == null) 371 { 372 return BasePeer.doSelect(criteria); 373 } 374 else 375 { 376 return BasePeer.doSelect(criteria, con); 377 } 378 } 379 380 387 public static List populateObjects(List records) 388 throws TorqueException 389 { 390 List results = new ArrayList (records.size()); 391 392 for (int i = 0; i < records.size(); i++) 394 { 395 Record row = (Record) records.get(i); 396 results.add(PortletCategoryPeer.row2Object(row, 1, 397 PortletCategoryPeer.getOMClass())); 398 } 399 return results; 400 } 401 402 403 411 public static Class getOMClass() 412 throws TorqueException 413 { 414 return CLASS_DEFAULT; 415 } 416 417 425 public static void doUpdate(Criteria criteria) throws TorqueException 426 { 427 BasePortletCategoryPeer 428 .doUpdate(criteria, (Connection ) null); 429 } 430 431 442 public static void doUpdate(Criteria criteria, Connection con) 443 throws TorqueException 444 { 445 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 446 selectCriteria.put(ID, criteria.remove(ID)); 447 448 if (criteria.getDbName() == Torque.getDefaultDB()) 452 { 453 criteria.setDbName(DATABASE_NAME); 454 } 455 if (con == null) 456 { 457 BasePeer.doUpdate(selectCriteria, criteria); 458 } 459 else 460 { 461 BasePeer.doUpdate(selectCriteria, criteria, con); 462 } 463 } 464 465 472 public static void doDelete(Criteria criteria) throws TorqueException 473 { 474 BasePortletCategoryPeer 475 .doDelete(criteria, (Connection ) null); 476 } 477 478 488 public static void doDelete(Criteria criteria, Connection con) 489 throws TorqueException 490 { 491 492 if (criteria.getDbName() == Torque.getDefaultDB()) 496 { 497 criteria.setDbName(DATABASE_NAME); 498 } 499 if (con == null) 500 { 501 BasePeer.doDelete(criteria); 502 } 503 else 504 { 505 BasePeer.doDelete(criteria, con); 506 } 507 } 508 509 515 public static List doSelect(PortletCategory obj) throws TorqueException 516 { 517 return doSelect(buildCriteria(obj)); 518 } 519 520 526 public static void doInsert(PortletCategory obj) throws TorqueException 527 { 528 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 529 obj.setNew(false); 530 obj.setModified(false); 531 } 532 533 538 public static void doUpdate(PortletCategory obj) throws TorqueException 539 { 540 doUpdate(buildCriteria(obj)); 541 obj.setModified(false); 542 } 543 544 549 public static void doDelete(PortletCategory obj) throws TorqueException 550 { 551 doDelete(buildCriteria(obj)); 552 } 553 554 564 public static void doInsert(PortletCategory obj, Connection con) 565 throws TorqueException 566 { 567 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 568 obj.setNew(false); 569 obj.setModified(false); 570 } 571 572 582 public static void doUpdate(PortletCategory obj, Connection con) 583 throws TorqueException 584 { 585 doUpdate(buildCriteria(obj), con); 586 obj.setModified(false); 587 } 588 589 599 public static void doDelete(PortletCategory obj, Connection con) 600 throws TorqueException 601 { 602 doDelete(buildCriteria(obj), con); 603 } 604 605 612 public static void doDelete(ObjectKey pk) throws TorqueException 613 { 614 BasePortletCategoryPeer 615 .doDelete(pk, (Connection ) null); 616 } 617 618 628 public static void doDelete(ObjectKey pk, Connection con) 629 throws TorqueException 630 { 631 doDelete(buildCriteria(pk), con); 632 } 633 634 635 public static Criteria buildCriteria( ObjectKey pk ) 636 { 637 Criteria criteria = new Criteria(); 638 criteria.add(ID, pk); 639 return criteria; 640 } 641 642 643 public static Criteria buildCriteria( PortletCategory obj ) 644 { 645 Criteria criteria = new Criteria(DATABASE_NAME); 646 if (!obj.isNew()) 647 criteria.add(ID, obj.getId()); 648 criteria.add(NAME, obj.getName()); 649 criteria.add(GROUPE, obj.getGroup()); 650 criteria.add(OWNER, obj.getOwner()); 651 return criteria; 652 } 653 654 655 664 public static PortletCategory retrieveByPK(long pk) 665 throws TorqueException, NoRowsException, TooManyRowsException 666 { 667 return retrieveByPK(SimpleKey.keyFor(pk)); 668 } 669 670 679 public static PortletCategory retrieveByPK(ObjectKey pk) 680 throws TorqueException, NoRowsException, TooManyRowsException 681 { 682 Connection db = null; 683 PortletCategory retVal = null; 684 try 685 { 686 db = Torque.getConnection(DATABASE_NAME); 687 retVal = retrieveByPK(pk, db); 688 } 689 finally 690 { 691 Torque.closeConnection(db); 692 } 693 return(retVal); 694 } 695 696 706 public static PortletCategory retrieveByPK(ObjectKey pk, Connection con) 707 throws TorqueException, NoRowsException, TooManyRowsException 708 { 709 Criteria criteria = buildCriteria(pk); 710 List v = doSelect(criteria, con); 711 if (v.size() == 0) 712 { 713 throw new NoRowsException("Failed to select a row."); 714 } 715 else if (v.size() > 1) 716 { 717 throw new TooManyRowsException("Failed to select only one row."); 718 } 719 else 720 { 721 return (PortletCategory)v.get(0); 722 } 723 } 724 725 732 public static List retrieveByPKs(List pks) 733 throws TorqueException 734 { 735 Connection db = null; 736 List retVal = null; 737 try 738 { 739 db = Torque.getConnection(DATABASE_NAME); 740 retVal = retrieveByPKs(pks, db); 741 } 742 finally 743 { 744 Torque.closeConnection(db); 745 } 746 return(retVal); 747 } 748 749 757 public static List retrieveByPKs( List pks, Connection dbcon ) 758 throws TorqueException 759 { 760 List objs = null; 761 if (pks == null || pks.size() == 0) 762 { 763 objs = new LinkedList (); 764 } 765 else 766 { 767 Criteria criteria = new Criteria(); 768 criteria.addIn( ID, pks ); 769 objs = doSelect(criteria, dbcon); 770 } 771 return objs; 772 } 773 774 775 776 777 778 779 780 781 782 783 794 protected static List doSelectJoinPortletDbEntry(Criteria c) 795 throws TorqueException 796 { 797 if (c.getDbName() == Torque.getDefaultDB()) 801 { 802 c.setDbName(DATABASE_NAME); 803 } 804 805 PortletCategoryPeer.addSelectColumns(c); 806 int offset = numColumns + 1; 807 PortletDbEntryPeer.addSelectColumns(c); 808 809 810 c.addJoin(PortletCategoryPeer.OWNER, 811 PortletDbEntryPeer.ID); 812 813 814 815 List rows = BasePeer.doSelect(c); 816 List results = new ArrayList (); 817 818 for (int i = 0; i < rows.size(); i++) 819 { 820 Record row = (Record) rows.get(i); 821 822 Class omClass = PortletCategoryPeer.getOMClass(); 823 PortletCategory obj1 = (PortletCategory) PortletCategoryPeer 824 .row2Object(row, 1, omClass); 825 omClass = PortletDbEntryPeer.getOMClass(); 826 PortletDbEntry obj2 = (PortletDbEntry)PortletDbEntryPeer 827 .row2Object(row, offset, omClass); 828 829 boolean newObject = true; 830 for (int j = 0; j < results.size(); j++) 831 { 832 PortletCategory temp_obj1 = (PortletCategory)results.get(j); 833 PortletDbEntry temp_obj2 = (PortletDbEntry)temp_obj1.getPortletDbEntry(); 834 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 835 { 836 newObject = false; 837 temp_obj2.addPortletCategory(obj1); 838 break; 839 } 840 } 841 if (newObject) 842 { 843 obj2.initPortletCategorys(); 844 obj2.addPortletCategory(obj1); 845 } 846 results.add(obj1); 847 } 848 return results; 849 } 850 851 852 853 854 861 protected static TableMap getTableMap() 862 throws TorqueException 863 { 864 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 865 } 866 } 867 | Popular Tags |