1 package org.campware.cream.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.campware.cream.om.map.*; 32 33 34 35 41 public abstract class BaseNotificationPeer 42 extends BasePeer 43 { 44 45 46 public static final String DATABASE_NAME = "cream"; 47 48 49 public static final String TABLE_NAME = "NOTIFICATION"; 50 51 56 public static MapBuilder getMapBuilder() 57 throws TorqueException 58 { 59 return getMapBuilder(NotificationMapBuilder.CLASS_NAME); 60 } 61 62 63 public static final String NOTIFICATION_ID; 64 65 public static final String NOTIFICATION_TYPE; 66 67 public static final String LANGUAGE_ID; 68 69 public static final String EMAIL_FORMAT; 70 71 public static final String SUBJECT; 72 73 public static final String BODY; 74 75 static 76 { 77 NOTIFICATION_ID = "NOTIFICATION.NOTIFICATION_ID"; 78 NOTIFICATION_TYPE = "NOTIFICATION.NOTIFICATION_TYPE"; 79 LANGUAGE_ID = "NOTIFICATION.LANGUAGE_ID"; 80 EMAIL_FORMAT = "NOTIFICATION.EMAIL_FORMAT"; 81 SUBJECT = "NOTIFICATION.SUBJECT"; 82 BODY = "NOTIFICATION.BODY"; 83 if (Torque.isInit()) 84 { 85 try 86 { 87 getMapBuilder(NotificationMapBuilder.CLASS_NAME); 88 } 89 catch (Exception e) 90 { 91 log.error("Could not initialize Peer", e); 92 } 93 } 94 else 95 { 96 Torque.registerMapBuilder(NotificationMapBuilder.CLASS_NAME); 97 } 98 } 99 100 101 public static final int numColumns = 6; 102 103 104 protected static final String CLASSNAME_DEFAULT = 105 "org.campware.cream.om.Notification"; 106 107 108 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 109 110 116 private static Class initClass(String className) 117 { 118 Class c = null; 119 try 120 { 121 c = Class.forName(className); 122 } 123 catch (Throwable t) 124 { 125 log.error("A FATAL ERROR has occurred which should not " 126 + "have happened under any circumstance. Please notify " 127 + "the Torque developers <torque-dev@db.apache.org> " 128 + "and give as many details as possible (including the error " 129 + "stack trace).", t); 130 131 if (t instanceof Error ) 133 { 134 throw (Error ) t.fillInStackTrace(); 135 } 136 } 137 return c; 138 } 139 140 150 public static List resultSet2Objects(java.sql.ResultSet results) 151 throws TorqueException 152 { 153 try 154 { 155 QueryDataSet qds = null; 156 List rows = null; 157 try 158 { 159 qds = new QueryDataSet(results); 160 rows = getSelectResults(qds); 161 } 162 finally 163 { 164 if (qds != null) 165 { 166 qds.close(); 167 } 168 } 169 170 return populateObjects(rows); 171 } 172 catch (SQLException e) 173 { 174 throw new TorqueException(e); 175 } 176 catch (DataSetException e) 177 { 178 throw new TorqueException(e); 179 } 180 } 181 182 183 184 191 public static ObjectKey doInsert(Criteria criteria) 192 throws TorqueException 193 { 194 return BaseNotificationPeer 195 .doInsert(criteria, (Connection ) null); 196 } 197 198 208 public static ObjectKey doInsert(Criteria criteria, Connection con) 209 throws TorqueException 210 { 211 212 setDbName(criteria); 213 214 if (con == null) 215 { 216 return BasePeer.doInsert(criteria); 217 } 218 else 219 { 220 return BasePeer.doInsert(criteria, con); 221 } 222 } 223 224 231 public static void addSelectColumns(Criteria criteria) 232 throws TorqueException 233 { 234 criteria.addSelectColumn(NOTIFICATION_ID); 235 criteria.addSelectColumn(NOTIFICATION_TYPE); 236 criteria.addSelectColumn(LANGUAGE_ID); 237 criteria.addSelectColumn(EMAIL_FORMAT); 238 criteria.addSelectColumn(SUBJECT); 239 criteria.addSelectColumn(BODY); 240 } 241 242 251 public static Notification row2Object(Record row, 252 int offset, 253 Class cls) 254 throws TorqueException 255 { 256 try 257 { 258 Notification obj = (Notification) cls.newInstance(); 259 NotificationPeer.populateObject(row, offset, obj); 260 obj.setModified(false); 261 obj.setNew(false); 262 263 return obj; 264 } 265 catch (InstantiationException e) 266 { 267 throw new TorqueException(e); 268 } 269 catch (IllegalAccessException e) 270 { 271 throw new TorqueException(e); 272 } 273 } 274 275 284 public static void populateObject(Record row, 285 int offset, 286 Notification obj) 287 throws TorqueException 288 { 289 try 290 { 291 obj.setNotificationId(row.getValue(offset + 0).asInt()); 292 obj.setNotificationType(row.getValue(offset + 1).asInt()); 293 obj.setLanguageId(row.getValue(offset + 2).asInt()); 294 obj.setEmailFormat(row.getValue(offset + 3).asInt()); 295 obj.setSubject(row.getValue(offset + 4).asString()); 296 obj.setBody(row.getValue(offset + 5).asString()); 297 } 298 catch (DataSetException e) 299 { 300 throw new TorqueException(e); 301 } 302 } 303 304 312 public static List doSelect(Criteria criteria) throws TorqueException 313 { 314 return populateObjects(doSelectVillageRecords(criteria)); 315 } 316 317 326 public static List doSelect(Criteria criteria, Connection con) 327 throws TorqueException 328 { 329 return populateObjects(doSelectVillageRecords(criteria, con)); 330 } 331 332 342 public static List doSelectVillageRecords(Criteria criteria) 343 throws TorqueException 344 { 345 return BaseNotificationPeer 346 .doSelectVillageRecords(criteria, (Connection ) null); 347 } 348 349 358 public static List doSelectVillageRecords(Criteria criteria, Connection con) 359 throws TorqueException 360 { 361 if (criteria.getSelectColumns().size() == 0) 362 { 363 addSelectColumns(criteria); 364 } 365 366 367 setDbName(criteria); 368 369 if (con == null) 372 { 373 return BasePeer.doSelect(criteria); 374 } 375 else 376 { 377 return BasePeer.doSelect(criteria, con); 378 } 379 } 380 381 388 public static List populateObjects(List records) 389 throws TorqueException 390 { 391 List results = new ArrayList (records.size()); 392 393 for (int i = 0; i < records.size(); i++) 395 { 396 Record row = (Record) records.get(i); 397 results.add(NotificationPeer.row2Object(row, 1, 398 NotificationPeer.getOMClass())); 399 } 400 return results; 401 } 402 403 404 412 public static Class getOMClass() 413 throws TorqueException 414 { 415 return CLASS_DEFAULT; 416 } 417 418 426 public static void doUpdate(Criteria criteria) throws TorqueException 427 { 428 BaseNotificationPeer 429 .doUpdate(criteria, (Connection ) null); 430 } 431 432 443 public static void doUpdate(Criteria criteria, Connection con) 444 throws TorqueException 445 { 446 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 447 selectCriteria.put(NOTIFICATION_ID, criteria.remove(NOTIFICATION_ID)); 448 449 setDbName(criteria); 450 451 if (con == null) 452 { 453 BasePeer.doUpdate(selectCriteria, criteria); 454 } 455 else 456 { 457 BasePeer.doUpdate(selectCriteria, criteria, con); 458 } 459 } 460 461 468 public static void doDelete(Criteria criteria) throws TorqueException 469 { 470 NotificationPeer 471 .doDelete(criteria, (Connection ) null); 472 } 473 474 484 public static void doDelete(Criteria criteria, Connection con) 485 throws TorqueException 486 { 487 488 setDbName(criteria); 489 490 if (con == null) 491 { 492 BasePeer.doDelete(criteria); 493 } 494 else 495 { 496 BasePeer.doDelete(criteria, con); 497 } 498 } 499 500 506 public static List doSelect(Notification obj) throws TorqueException 507 { 508 return doSelect(buildSelectCriteria(obj)); 509 } 510 511 517 public static void doInsert(Notification obj) throws TorqueException 518 { 519 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 520 obj.setNew(false); 521 obj.setModified(false); 522 } 523 524 529 public static void doUpdate(Notification obj) throws TorqueException 530 { 531 doUpdate(buildCriteria(obj)); 532 obj.setModified(false); 533 } 534 535 540 public static void doDelete(Notification obj) throws TorqueException 541 { 542 doDelete(buildSelectCriteria(obj)); 543 } 544 545 555 public static void doInsert(Notification obj, Connection con) 556 throws TorqueException 557 { 558 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 559 obj.setNew(false); 560 obj.setModified(false); 561 } 562 563 573 public static void doUpdate(Notification obj, Connection con) 574 throws TorqueException 575 { 576 doUpdate(buildCriteria(obj), con); 577 obj.setModified(false); 578 } 579 580 590 public static void doDelete(Notification obj, Connection con) 591 throws TorqueException 592 { 593 doDelete(buildSelectCriteria(obj), con); 594 } 595 596 603 public static void doDelete(ObjectKey pk) throws TorqueException 604 { 605 BaseNotificationPeer 606 .doDelete(pk, (Connection ) null); 607 } 608 609 619 public static void doDelete(ObjectKey pk, Connection con) 620 throws TorqueException 621 { 622 doDelete(buildCriteria(pk), con); 623 } 624 625 626 public static Criteria buildCriteria( ObjectKey pk ) 627 { 628 Criteria criteria = new Criteria(); 629 criteria.add(NOTIFICATION_ID, pk); 630 return criteria; 631 } 632 633 634 public static Criteria buildCriteria( Notification obj ) 635 { 636 Criteria criteria = new Criteria(DATABASE_NAME); 637 if (!obj.isNew()) 638 criteria.add(NOTIFICATION_ID, obj.getNotificationId()); 639 criteria.add(NOTIFICATION_TYPE, obj.getNotificationType()); 640 criteria.add(LANGUAGE_ID, obj.getLanguageId()); 641 criteria.add(EMAIL_FORMAT, obj.getEmailFormat()); 642 criteria.add(SUBJECT, obj.getSubject()); 643 criteria.add(BODY, obj.getBody()); 644 return criteria; 645 } 646 647 648 public static Criteria buildSelectCriteria( Notification obj ) 649 { 650 Criteria criteria = new Criteria(DATABASE_NAME); 651 if (!obj.isNew()) 652 criteria.add(NOTIFICATION_ID, obj.getNotificationId()); 653 criteria.add(NOTIFICATION_TYPE, obj.getNotificationType()); 654 criteria.add(LANGUAGE_ID, obj.getLanguageId()); 655 criteria.add(EMAIL_FORMAT, obj.getEmailFormat()); 656 criteria.add(SUBJECT, obj.getSubject()); 657 criteria.add(BODY, obj.getBody()); 658 return criteria; 659 } 660 661 662 671 public static Notification retrieveByPK(int pk) 672 throws TorqueException, NoRowsException, TooManyRowsException 673 { 674 return retrieveByPK(SimpleKey.keyFor(pk)); 675 } 676 677 687 public static Notification retrieveByPK(int pk, Connection con) 688 throws TorqueException, NoRowsException, TooManyRowsException 689 { 690 return retrieveByPK(SimpleKey.keyFor(pk), con); 691 } 692 693 702 public static Notification retrieveByPK(ObjectKey pk) 703 throws TorqueException, NoRowsException, TooManyRowsException 704 { 705 Connection db = null; 706 Notification retVal = null; 707 try 708 { 709 db = Torque.getConnection(DATABASE_NAME); 710 retVal = retrieveByPK(pk, db); 711 } 712 finally 713 { 714 Torque.closeConnection(db); 715 } 716 return(retVal); 717 } 718 719 729 public static Notification retrieveByPK(ObjectKey pk, Connection con) 730 throws TorqueException, NoRowsException, TooManyRowsException 731 { 732 Criteria criteria = buildCriteria(pk); 733 List v = doSelect(criteria, con); 734 if (v.size() == 0) 735 { 736 throw new NoRowsException("Failed to select a row."); 737 } 738 else if (v.size() > 1) 739 { 740 throw new TooManyRowsException("Failed to select only one row."); 741 } 742 else 743 { 744 return (Notification)v.get(0); 745 } 746 } 747 748 755 public static List retrieveByPKs(List pks) 756 throws TorqueException 757 { 758 Connection db = null; 759 List retVal = null; 760 try 761 { 762 db = Torque.getConnection(DATABASE_NAME); 763 retVal = retrieveByPKs(pks, db); 764 } 765 finally 766 { 767 Torque.closeConnection(db); 768 } 769 return(retVal); 770 } 771 772 780 public static List retrieveByPKs( List pks, Connection dbcon ) 781 throws TorqueException 782 { 783 List objs = null; 784 if (pks == null || pks.size() == 0) 785 { 786 objs = new LinkedList (); 787 } 788 else 789 { 790 Criteria criteria = new Criteria(); 791 criteria.addIn( NOTIFICATION_ID, pks ); 792 objs = doSelect(criteria, dbcon); 793 } 794 return objs; 795 } 796 797 798 799 800 801 802 803 804 805 806 817 protected static List doSelectJoinLanguage(Criteria criteria) 818 throws TorqueException 819 { 820 setDbName(criteria); 821 822 NotificationPeer.addSelectColumns(criteria); 823 int offset = numColumns + 1; 824 LanguagePeer.addSelectColumns(criteria); 825 826 827 criteria.addJoin(NotificationPeer.LANGUAGE_ID, 828 LanguagePeer.LANGUAGE_ID); 829 830 831 832 List rows = BasePeer.doSelect(criteria); 833 List results = new ArrayList (); 834 835 for (int i = 0; i < rows.size(); i++) 836 { 837 Record row = (Record) rows.get(i); 838 839 Class omClass = NotificationPeer.getOMClass(); 840 Notification obj1 = (Notification) NotificationPeer 841 .row2Object(row, 1, omClass); 842 omClass = LanguagePeer.getOMClass(); 843 Language obj2 = (Language)LanguagePeer 844 .row2Object(row, offset, omClass); 845 846 boolean newObject = true; 847 for (int j = 0; j < results.size(); j++) 848 { 849 Notification temp_obj1 = (Notification)results.get(j); 850 Language temp_obj2 = (Language)temp_obj1.getLanguage(); 851 if (temp_obj2.getPrimaryKey().equals(obj2.getPrimaryKey())) 852 { 853 newObject = false; 854 temp_obj2.addNotification(obj1); 855 break; 856 } 857 } 858 if (newObject) 859 { 860 obj2.initNotifications(); 861 obj2.addNotification(obj1); 862 } 863 results.add(obj1); 864 } 865 return results; 866 } 867 868 869 870 871 878 protected static TableMap getTableMap() 879 throws TorqueException 880 { 881 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 882 } 883 884 private static void setDbName(Criteria crit) 885 { 886 if (crit.getDbName() == Torque.getDefaultDB()) 890 { 891 crit.setDbName(DATABASE_NAME); 892 } 893 } 894 } 895
| Popular Tags
|