1 package org.apache.jetspeed.om.apps.email; 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.apps.email.map.*; 32 33 34 40 public abstract class BaseEmailInboxPeer 41 extends BasePeer 42 { 43 44 45 public static final String DATABASE_NAME = "default"; 46 47 48 public static final String TABLE_NAME = "EMAIL_INBOX"; 49 50 55 public static MapBuilder getMapBuilder() 56 throws TorqueException 57 { 58 return getMapBuilder(EmailInboxMapBuilder.CLASS_NAME); 59 } 60 61 62 public static final String EMAIL_INBOX_ID; 63 64 public static final String MESSAGE_ID; 65 66 public static final String FILENAME; 67 68 public static final String ATTACHMENT; 69 70 public static final String READFLAG; 71 72 static 73 { 74 EMAIL_INBOX_ID = "EMAIL_INBOX.EMAIL_INBOX_ID"; 75 MESSAGE_ID = "EMAIL_INBOX.MESSAGE_ID"; 76 FILENAME = "EMAIL_INBOX.FILENAME"; 77 ATTACHMENT = "EMAIL_INBOX.ATTACHMENT"; 78 READFLAG = "EMAIL_INBOX.READFLAG"; 79 if (Torque.isInit()) 80 { 81 try 82 { 83 getMapBuilder(); 84 } 85 catch (Exception e) 86 { 87 log.error("Could not initialize Peer", e); 88 } 89 } 90 else 91 { 92 Torque.registerMapBuilder(EmailInboxMapBuilder.CLASS_NAME); 93 } 94 } 95 96 97 public static final int numColumns = 5; 98 99 100 protected static final String CLASSNAME_DEFAULT = 101 "org.apache.jetspeed.om.apps.email.EmailInbox"; 102 103 104 protected static final Class CLASS_DEFAULT = initClass(CLASSNAME_DEFAULT); 105 106 112 private static Class initClass(String className) 113 { 114 Class c = null; 115 try 116 { 117 c = Class.forName(className); 118 } 119 catch (Throwable t) 120 { 121 log.error("A FATAL ERROR has occurred which should not " 122 + "have happened under any circumstance. Please notify " 123 + "the Torque developers <turbine-torque-dev@jakarta.apache.org> " 124 + "and give as many details as possible (including the error " 125 + "stack trace).", t); 126 127 if (t instanceof Error ) 129 { 130 throw (Error ) t.fillInStackTrace(); 131 } 132 } 133 return c; 134 } 135 136 146 public static List resultSet2Objects(java.sql.ResultSet results) 147 throws TorqueException 148 { 149 try 150 { 151 QueryDataSet qds = null; 152 List rows = null; 153 try 154 { 155 qds = new QueryDataSet(results); 156 rows = getSelectResults(qds); 157 } 158 finally 159 { 160 if (qds != null) 161 { 162 qds.close(); 163 } 164 } 165 166 return populateObjects(rows); 167 } 168 catch (SQLException e) 169 { 170 throw new TorqueException(e); 171 } 172 catch (DataSetException e) 173 { 174 throw new TorqueException(e); 175 } 176 } 177 178 179 180 187 public static ObjectKey doInsert(Criteria criteria) 188 throws TorqueException 189 { 190 return BaseEmailInboxPeer 191 .doInsert(criteria, (Connection ) null); 192 } 193 194 204 public static ObjectKey doInsert(Criteria criteria, Connection con) 205 throws TorqueException 206 { 207 208 if (criteria.getDbName() == Torque.getDefaultDB()) 212 { 213 criteria.setDbName(DATABASE_NAME); 214 } 215 if (con == null) 216 { 217 return BasePeer.doInsert(criteria); 218 } 219 else 220 { 221 return BasePeer.doInsert(criteria, con); 222 } 223 } 224 225 232 public static void addSelectColumns(Criteria criteria) 233 throws TorqueException 234 { 235 criteria.addSelectColumn(EMAIL_INBOX_ID); 236 criteria.addSelectColumn(MESSAGE_ID); 237 criteria.addSelectColumn(FILENAME); 238 criteria.addSelectColumn(ATTACHMENT); 239 criteria.addSelectColumn(READFLAG); 240 } 241 242 251 public static EmailInbox row2Object(Record row, 252 int offset, 253 Class cls) 254 throws TorqueException 255 { 256 try 257 { 258 EmailInbox obj = (EmailInbox) cls.newInstance(); 259 EmailInboxPeer.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 EmailInbox obj) 287 throws TorqueException 288 { 289 try 290 { 291 obj.setEmailInboxId(row.getValue(offset + 0).asInt()); 292 obj.setMessageId(row.getValue(offset + 1).asString()); 293 obj.setFilename(row.getValue(offset + 2).asString()); 294 obj.setAttachment(row.getValue(offset + 3).asBytes()); 295 obj.setReadflag(row.getValue(offset + 4).asInt()); 296 } 297 catch (DataSetException e) 298 { 299 throw new TorqueException(e); 300 } 301 } 302 303 311 public static List doSelect(Criteria criteria) throws TorqueException 312 { 313 return populateObjects(doSelectVillageRecords(criteria)); 314 } 315 316 325 public static List doSelect(Criteria criteria, Connection con) 326 throws TorqueException 327 { 328 return populateObjects(doSelectVillageRecords(criteria, con)); 329 } 330 331 341 public static List doSelectVillageRecords(Criteria criteria) 342 throws TorqueException 343 { 344 return BaseEmailInboxPeer 345 .doSelectVillageRecords(criteria, (Connection ) null); 346 } 347 348 356 public static List doSelectVillageRecords(Criteria criteria, Connection con) 357 throws TorqueException 358 { 359 if (criteria.getSelectColumns().size() == 0) 360 { 361 addSelectColumns(criteria); 362 } 363 364 365 if (criteria.getDbName() == Torque.getDefaultDB()) 369 { 370 criteria.setDbName(DATABASE_NAME); 371 } 372 if (con == null) 375 { 376 return BasePeer.doSelect(criteria); 377 } 378 else 379 { 380 return BasePeer.doSelect(criteria, con); 381 } 382 } 383 384 391 public static List populateObjects(List records) 392 throws TorqueException 393 { 394 List results = new ArrayList (records.size()); 395 396 for (int i = 0; i < records.size(); i++) 398 { 399 Record row = (Record) records.get(i); 400 results.add(EmailInboxPeer.row2Object(row, 1, 401 EmailInboxPeer.getOMClass())); 402 } 403 return results; 404 } 405 406 407 415 public static Class getOMClass() 416 throws TorqueException 417 { 418 return CLASS_DEFAULT; 419 } 420 421 429 public static void doUpdate(Criteria criteria) throws TorqueException 430 { 431 BaseEmailInboxPeer 432 .doUpdate(criteria, (Connection ) null); 433 } 434 435 446 public static void doUpdate(Criteria criteria, Connection con) 447 throws TorqueException 448 { 449 Criteria selectCriteria = new Criteria(DATABASE_NAME, 2); 450 selectCriteria.put(EMAIL_INBOX_ID, criteria.remove(EMAIL_INBOX_ID)); 451 452 if (criteria.getDbName() == Torque.getDefaultDB()) 456 { 457 criteria.setDbName(DATABASE_NAME); 458 } 459 if (con == null) 460 { 461 BasePeer.doUpdate(selectCriteria, criteria); 462 } 463 else 464 { 465 BasePeer.doUpdate(selectCriteria, criteria, con); 466 } 467 } 468 469 476 public static void doDelete(Criteria criteria) throws TorqueException 477 { 478 BaseEmailInboxPeer 479 .doDelete(criteria, (Connection ) null); 480 } 481 482 492 public static void doDelete(Criteria criteria, Connection con) 493 throws TorqueException 494 { 495 496 if (criteria.getDbName() == Torque.getDefaultDB()) 500 { 501 criteria.setDbName(DATABASE_NAME); 502 } 503 if (con == null) 504 { 505 BasePeer.doDelete(criteria); 506 } 507 else 508 { 509 BasePeer.doDelete(criteria, con); 510 } 511 } 512 513 519 public static List doSelect(EmailInbox obj) throws TorqueException 520 { 521 return doSelect(buildCriteria(obj)); 522 } 523 524 530 public static void doInsert(EmailInbox obj) throws TorqueException 531 { 532 obj.setPrimaryKey(doInsert(buildCriteria(obj))); 533 obj.setNew(false); 534 obj.setModified(false); 535 } 536 537 542 public static void doUpdate(EmailInbox obj) throws TorqueException 543 { 544 doUpdate(buildCriteria(obj)); 545 obj.setModified(false); 546 } 547 548 553 public static void doDelete(EmailInbox obj) throws TorqueException 554 { 555 doDelete(buildCriteria(obj)); 556 } 557 558 568 public static void doInsert(EmailInbox obj, Connection con) 569 throws TorqueException 570 { 571 obj.setPrimaryKey(doInsert(buildCriteria(obj), con)); 572 obj.setNew(false); 573 obj.setModified(false); 574 } 575 576 586 public static void doUpdate(EmailInbox obj, Connection con) 587 throws TorqueException 588 { 589 doUpdate(buildCriteria(obj), con); 590 obj.setModified(false); 591 } 592 593 603 public static void doDelete(EmailInbox obj, Connection con) 604 throws TorqueException 605 { 606 doDelete(buildCriteria(obj), con); 607 } 608 609 616 public static void doDelete(ObjectKey pk) throws TorqueException 617 { 618 BaseEmailInboxPeer 619 .doDelete(pk, (Connection ) null); 620 } 621 622 632 public static void doDelete(ObjectKey pk, Connection con) 633 throws TorqueException 634 { 635 doDelete(buildCriteria(pk), con); 636 } 637 638 639 public static Criteria buildCriteria( ObjectKey pk ) 640 { 641 Criteria criteria = new Criteria(); 642 criteria.add(EMAIL_INBOX_ID, pk); 643 return criteria; 644 } 645 646 647 public static Criteria buildCriteria( EmailInbox obj ) 648 { 649 Criteria criteria = new Criteria(DATABASE_NAME); 650 if (!obj.isNew()) 651 criteria.add(EMAIL_INBOX_ID, obj.getEmailInboxId()); 652 criteria.add(MESSAGE_ID, obj.getMessageId()); 653 criteria.add(FILENAME, obj.getFilename()); 654 criteria.add(ATTACHMENT, obj.getAttachment()); 655 criteria.add(READFLAG, obj.getReadflag()); 656 return criteria; 657 } 658 659 660 669 public static EmailInbox retrieveByPK(int pk) 670 throws TorqueException, NoRowsException, TooManyRowsException 671 { 672 return retrieveByPK(SimpleKey.keyFor(pk)); 673 } 674 675 684 public static EmailInbox retrieveByPK(ObjectKey pk) 685 throws TorqueException, NoRowsException, TooManyRowsException 686 { 687 Connection db = null; 688 EmailInbox retVal = null; 689 try 690 { 691 db = Torque.getConnection(DATABASE_NAME); 692 retVal = retrieveByPK(pk, db); 693 } 694 finally 695 { 696 Torque.closeConnection(db); 697 } 698 return(retVal); 699 } 700 701 711 public static EmailInbox retrieveByPK(ObjectKey pk, Connection con) 712 throws TorqueException, NoRowsException, TooManyRowsException 713 { 714 Criteria criteria = buildCriteria(pk); 715 List v = doSelect(criteria, con); 716 if (v.size() == 0) 717 { 718 throw new NoRowsException("Failed to select a row."); 719 } 720 else if (v.size() > 1) 721 { 722 throw new TooManyRowsException("Failed to select only one row."); 723 } 724 else 725 { 726 return (EmailInbox)v.get(0); 727 } 728 } 729 730 737 public static List retrieveByPKs(List pks) 738 throws TorqueException 739 { 740 Connection db = null; 741 List retVal = null; 742 try 743 { 744 db = Torque.getConnection(DATABASE_NAME); 745 retVal = retrieveByPKs(pks, db); 746 } 747 finally 748 { 749 Torque.closeConnection(db); 750 } 751 return(retVal); 752 } 753 754 762 public static List retrieveByPKs( List pks, Connection dbcon ) 763 throws TorqueException 764 { 765 List objs = null; 766 if (pks == null || pks.size() == 0) 767 { 768 objs = new LinkedList (); 769 } 770 else 771 { 772 Criteria criteria = new Criteria(); 773 criteria.addIn( EMAIL_INBOX_ID, pks ); 774 objs = doSelect(criteria, dbcon); 775 } 776 return objs; 777 } 778 779 780 781 782 783 784 785 786 787 788 795 protected static TableMap getTableMap() 796 throws TorqueException 797 { 798 return Torque.getDatabaseMap(DATABASE_NAME).getTable(TABLE_NAME); 799 } 800 } 801 | Popular Tags |