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 36 42 public abstract class BasePortletMediatypePeer 43 extends BasePeer 44 { 45 46 47 public static final String DATABASE_NAME = "default"; 48 49 50 public static final String TABLE_NAME = "PORTLET_MEDIATYPE"; 51 52 57 public static MapBuilder getMapBuilder() 58 throws TorqueException 59 { 60 return getMapBuilder(PortletMediatypeMapBuilder.CLASS_NAME); 61 } 62 63 64 public static final String ID; 65 66 public static final String MEDIA_ID; 67 68 static 69 { 70 ID = "PORTLET_MEDIATYPE.ID"; 71 MEDIA_ID = "PORTLET_MEDIATYPE.MEDIA_ID"; 72 if (Torque.isInit()) 73 { 74 try 75 { 76 getMapBuilder(); 77 } 78 catch (Exception e) 79 { 80 log.error("Could not initialize Peer", e); 81 } 82 } 83 else 84 { 85 Torque.registerMapBuilder(PortletMediatypeMapBuilder.CLASS_NAME); 86 } 87 } 88 89 90 public static final int numColumns = 2; 91 92 93 protected static final String CLASSNAME_DEFAULT = 94 "org.apache.jetspeed.om.dbregistry.PortletMediatype"; 95 96 97 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 98 99 105 private static Class initClass(String className) 106 { 107 Class c = null; 108 try 109 { 110 c = Class.forName(className); 111 } 112 catch (Throwable t) 113 { 114 log.error("A FATAL ERROR has occurred which should not " 115 + "have happened under any circumstance. Please notify " 116 + "the Torque developers <turbine-torque-dev@jakarta.apache.org> " 117 + "and give as many details as possible (including the error " 118 + "stack trace).", t); 119 120 if (t instanceof Error ) 122 { 123 throw (Error ) t.fillInStackTrace(); 124 } 125 } 126 return c; 127 } 128 129 139 public static List resultSet2Objects(java.sql.ResultSet results) 140 throws TorqueException 141 { 142 try 143 { 144 QueryDataSet qds = null; 145 List rows = null; 146 try 147 { 148 qds = new QueryDataSet(results); 149 rows = getSelectResults(qds); 150 } 151 finally 152 { 153 if (qds != null) 154 { 155 qds.close(); 156 } 157 } 158 159 return populateObjects(rows); 160 } 161 catch (SQLException e) 162 { 163 throw new TorqueException(e); 164 } 165 catch (DataSetException e) 166 { 167 throw new TorqueException(e); 168 } 169 } 170 171 172 173 180 public static ObjectKey doInsert(Criteria criteria) 181 throws TorqueException 182 { 183 return BasePortletMediatypePeer 184 .doInsert(criteria, (Connection ) null); 185 } 186 187 197 public static ObjectKey doInsert(Criteria criteria, Connection con) 198 throws TorqueException 199 { 200 201 if (criteria.getDbName() == Torque.getDefaultDB()) 205 { 206 criteria.setDbName(DATABASE_NAME); 207 } 208 if (con == null) 209 { 210 return BasePeer.doInsert(criteria); 211 } 212 else 213 { 214 return BasePeer.doInsert(criteria, con); 215 } 216 } 217 218 225 public static void addSelectColumns(Criteria criteria) 226 throws TorqueException 227 { 228 criteria.addSelectColumn(ID); 229 criteria.addSelectColumn(MEDIA_ID); 230 } 231 232 241 public static PortletMediatype row2Object(Record row, 242 int offset, 243 Class cls) 244 throws TorqueException 245 { 246 try 247 { 248 PortletMediatype obj = (PortletMediatype) cls.newInstance(); 249 PortletMediatypePeer.populateObject(row, offset, obj); 250 obj.setModified(false); 251 obj.setNew(false); 252 253 return obj; 254 } 255 catch (InstantiationException e) 256 { 257 throw new TorqueException(e); 258 } 259 catch (IllegalAccessException e) 260 { 261 throw new TorqueException(e); 262 } 263 } 264 265 274 public static void populateObject(Record row, 275 int offset, 276 PortletMediatype obj) 277 throws TorqueException 278 { 279 try 280 { 281 obj.setId(row.getValue(offset + 0).asLong()); 282 obj.setMediaId(row.getValue(offset + 1).asLong()); 283 } 284 catch (DataSetException e) 285 { 286 throw new TorqueException(e); 287 } 288 } 289 290 298 public static List doSelect(Criteria criteria) throws TorqueException 299 { 300 return populateObjects(doSelectVillageRecords(criteria)); 301 } 302 303 312 public static List doSelect(Criteria criteria, Connection con) 313 throws TorqueException 314 { 315 return populateObjects(doSelectVillageRecords(criteria, con)); 316 } 317 318 328 public static List doSelectVillageRecords(Criteria criteria) 329 throws TorqueException 330 { 331 return BasePortletMediatypePeer 332 .doSelectVillageRecords(criteria, (Connection ) null); 333 } 334 335 343 public static List doSelectVillageRecords(Criteria criteria, Connection con) 344 throws TorqueException 345 { 346 if (criteria.getSelectColumns().size() == 0) 347 { 348 addSelectColumns(criteria); 349 } 350 351 352 if (criteria.getDbName() == Torque.getDefaultDB()) 356 { 357 criteria.setDbName(DATABASE_NAME); 358 } 359 if (con == null) 362 { 363 return BasePeer.doSelect(criteria); 364 } 365 else 366 { 367 return BasePeer.doSelect(criteria, con); 368 } 369 } 370 371 378 public static List populateObjects(List records) 379 throws TorqueException 380 { 381 List results = new ArrayList (records.size()); 382 383 for (int i = 0; i < records.size(); i++) 385 { 386 Record row = (Record) records.get(i); 387 results.add(PortletMediatypePeer.row2Object(row, 1, 388 PortletMediatypePeer.getOMClass())); 389 } 390 return results; 391 } 392 393 394 402 public static Class getOMClass() 403 throws TorqueException 404 { 405 return CLASS_DEFAULT; 406 } 407 408 416 public static void doUpdate(Criteria criteria) throws TorqueException 417 { 418 BasePortletMediatypePeer 419 .doUpdate(criteria, (Connection ) null); 420 } 421 422 433 public static void doUpdate(Criteria criteria, Connection con) 434 throws TorqueException 435 { 436 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 437 438 if (criteria.getDbName() == Torque.getDefaultDB()) 442 { 443 criteria.setDbName(DATABASE_NAME); 444 } 445 if (con == null) 446 { 447 BasePeer.doUpdate(selectCriteria, criteria); 448 } 449 else 450 { 451 BasePeer.doUpdate(selectCriteria, criteria, con); 452 } 453 } 454 455 462 public static void doDelete(Criteria criteria) throws TorqueException 463 { 464 BasePortletMediatypePeer 465 .doDelete(criteria, (Connection ) null); 466 } 467 468 478 public static void doDelete(Criteria criteria, Connection con) 479 throws TorqueException 480 { 481 482 if (criteria.getDbName() == Torque.getDefaultDB()) 486 { 487 criteria.setDbName(DATABASE_NAME); 488 } 489 if (con == null) 490 { 491 BasePeer.doDelete(criteria); 492 } 493 else 494 { 495 BasePeer.doDelete(criteria, con); 496 } 497 } 498 499 505 public static List doSelect(PortletMediatype obj) throws TorqueException 506 { 507 return doSelect(buildCriteria(obj)); 508 } 509 510 516 public static void doInsert(PortletMediatype obj) throws TorqueException 517 { 518 doInsert(buildCriteria(obj)); 519 obj.setNew(false); 520 obj.setModified(false); 521 } 522 523 528 public static void doUpdate(PortletMediatype obj) throws TorqueException 529 { 530 doUpdate(buildCriteria(obj)); 531 obj.setModified(false); 532 } 533 534 539 public static void doDelete(PortletMediatype obj) throws TorqueException 540 { 541 doDelete(buildCriteria(obj)); 542 } 543 544 554 public static void doInsert(PortletMediatype obj, Connection con) 555 throws TorqueException 556 { 557 doInsert(buildCriteria(obj), con); 558 obj.setNew(false); 559 obj.setModified(false); 560 } 561 562 572 public static void doUpdate(PortletMediatype obj, Connection con) 573 throws TorqueException 574 { 575 doUpdate(buildCriteria(obj), con); 576 obj.setModified(false); 577 } 578 579 589 public static void doDelete(PortletMediatype obj, Connection con) 590 throws TorqueException 591 { 592 doDelete(buildCriteria(obj), con); 593 } 594 595 602 public static void doDelete(ObjectKey pk) throws TorqueException 603 { 604 BasePortletMediatypePeer 605 .doDelete(pk, (Connection ) null); 606 } 607 608 618 public static void doDelete(ObjectKey pk, Connection con) 619 throws TorqueException 620 { 621 doDelete(buildCriteria(pk), con); 622 } 623 624 625 public static Criteria buildCriteria( ObjectKey pk ) 626 { 627 Criteria criteria = new Criteria(); 628 SimpleKey[] keys = (SimpleKey[])pk.getValue(); 629 return criteria; 630 } 631 632 633 public static Criteria buildCriteria( PortletMediatype obj ) 634 { 635 Criteria criteria = new Criteria(DATABASE_NAME); 636 criteria.add(ID, obj.getId()); 637 criteria.add(MEDIA_ID, obj.getMediaId()); 638 return criteria; 639 } 640 641 642 643 644 645 646 647 648 649 650 661 protected static List doSelectJoinPortletDbEntry(Criteria c) 662 throws TorqueException 663 { 664 if (c.getDbName() == Torque.getDefaultDB()) 668 { 669 c.setDbName(DATABASE_NAME); 670 } 671 672 PortletMediatypePeer.addSelectColumns(c); 673 int offset = numColumns + 1; 674 PortletDbEntryPeer.addSelectColumns(c); 675 676 677 c.addJoin(PortletMediatypePeer.ID, 678 PortletDbEntryPeer.ID); 679 680 681 682 List rows = BasePeer.doSelect(c); 683 List results = new ArrayList (); 684 685 for (int i = 0; i < rows.size(); i++) 686 { 687 Record row = (Record) rows.get(i); 688 689 Class omClass = PortletMediatypePeer.getOMClass(); 690 PortletMediatype obj1 = (PortletMediatype) PortletMediatypePeer 691 .row2Object(row, 1, omClass); 692 omClass = PortletDbEntryPeer.getOMClass(); 693 PortletDbEntry obj2 = (PortletDbEntry)PortletDbEntryPeer 694 .row2Object(row, offset, omClass); 695 696 boolean newObject = true; 697 for (int j = 0; j < results.size(); j++) 698 { 699 PortletMediatype temp_obj1 = (PortletMediatype)results.get(j); 700 PortletDbEntry temp_obj2 = (PortletDbEntry)temp_obj1.getPortletDbEntry(); 701 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 702 { 703 newObject = false; 704 temp_obj2.addPortletMediatype(obj1); 705 break; 706 } 707 } 708 if (newObject) 709 { 710 obj2.initPortletMediatypes(); 711 obj2.addPortletMediatype(obj1); 712 } 713 results.add(obj1); 714 } 715 return results; 716 } 717 718 719 720 721 732 protected static List doSelectJoinMediatype(Criteria c) 733 throws TorqueException 734 { 735 if (c.getDbName() == Torque.getDefaultDB()) 739 { 740 c.setDbName(DATABASE_NAME); 741 } 742 743 PortletMediatypePeer.addSelectColumns(c); 744 int offset = numColumns + 1; 745 MediatypePeer.addSelectColumns(c); 746 747 748 c.addJoin(PortletMediatypePeer.MEDIA_ID, 749 MediatypePeer.ID); 750 751 752 753 List rows = BasePeer.doSelect(c); 754 List results = new ArrayList (); 755 756 for (int i = 0; i < rows.size(); i++) 757 { 758 Record row = (Record) rows.get(i); 759 760 Class omClass = PortletMediatypePeer.getOMClass(); 761 PortletMediatype obj1 = (PortletMediatype) PortletMediatypePeer 762 .row2Object(row, 1, omClass); 763 omClass = MediatypePeer.getOMClass(); 764 Mediatype obj2 = (Mediatype)MediatypePeer 765 .row2Object(row, offset, omClass); 766 767 boolean newObject = true; 768 for (int j = 0; j < results.size(); j++) 769 { 770 PortletMediatype temp_obj1 = (PortletMediatype)results.get(j); 771 Mediatype temp_obj2 = (Mediatype)temp_obj1.getMediatype(); 772 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 773 { 774 newObject = false; 775 temp_obj2.addPortletMediatype(obj1); 776 break; 777 } 778 } 779 if (newObject) 780 { 781 obj2.initPortletMediatypes(); 782 obj2.addPortletMediatype(obj1); 783 } 784 results.add(obj1); 785 } 786 return results; 787 } 788 789 790 791 792 799 protected static TableMap getTableMap() 800 throws TorqueException 801 { 802 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 803 } 804 } 805 | Popular Tags |