1 6 7 package org.contineo.communication.dao; 8 9 import java.util.ArrayList ; 10 import java.util.Collection ; 11 import org.apache.log4j.Level; 12 import org.apache.log4j.Logger; 13 import org.apache.ojb.broker.PBKey; 14 import org.apache.ojb.broker.PersistenceBroker; 15 import org.apache.ojb.broker.PersistenceBrokerFactory; 16 import org.apache.ojb.broker.query.Criteria; 17 import org.apache.ojb.broker.query.Query; 18 import org.apache.ojb.broker.query.QueryByCriteria; 19 import org.contineo.communication.EMail; 20 import org.contineo.core.LoggingManager; 21 25 public class EMailDAO { 26 27 31 private PersistenceBroker broker = null; 32 33 37 PBKey pbkey; 38 39 43 private Logger logger; 44 45 46 47 48 public EMailDAO() { 49 logger = LoggingManager.getLogger(this.getClass()); 50 pbkey = new PBKey("contineo"); 51 } 52 53 58 public boolean store(EMail email) { 59 boolean result = true; 60 try { 61 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 62 broker.beginTransaction(); 63 broker.store(email); 64 broker.commitTransaction(); 65 } catch (Exception e) { 66 broker.abortTransaction(); 67 if (logger.isEnabledFor(Level.ERROR)) 68 logger.error(e.getMessage()); 69 result = false; 70 } finally { 71 if (broker != null) broker.close(); 72 } 73 return result; 74 } 75 76 80 public boolean delete(int emailid) { 81 boolean result = true; 82 try { 83 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 84 Criteria crit = new Criteria(); 85 crit.addEqualTo("co_messageid", new Integer (emailid)); 86 Query query = new QueryByCriteria(org.contineo.communication.EMail.class, crit); 87 Query query_at = new QueryByCriteria(org.contineo.communication.Attachment.class, crit); 88 Query query_re = new QueryByCriteria(org.contineo.communication.Recipient.class, crit); 89 broker.beginTransaction(); 90 broker.deleteByQuery(query_at); 91 broker.deleteByQuery(query_re); 92 broker.deleteByQuery(query); 93 broker.commitTransaction(); 94 } catch (Exception e) { 95 broker.abortTransaction(); 96 if (logger.isEnabledFor(Level.ERROR)) 97 logger.error(e.getMessage()); 98 result = false; 99 } finally { 100 if (broker != null) broker.close(); 101 } 102 return result; 103 } 104 105 108 public EMail findByPrimaryKey(int emailid) { 109 EMail email = new EMail(); 110 try { 111 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 112 Criteria crit = new Criteria(); 113 crit.addEqualTo("co_messageid", new Integer (emailid)); 114 Query query = new QueryByCriteria(org.contineo.communication.EMail.class, crit); 115 email = (EMail)broker.getObjectByQuery(query); 116 } catch (Exception e) { 117 if (logger.isEnabledFor(Level.ERROR)) 118 logger.error(e.getMessage()); 119 } finally { 120 if (broker != null) broker.close(); 121 } 122 return email; 123 } 124 125 public Collection findByUserName(String username) { 126 Collection result = new ArrayList (); 127 try { 128 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 129 Criteria crit = new Criteria(); 130 crit.addEqualTo("co_username", username); 131 Query query = new QueryByCriteria(org.contineo.communication.EMail.class, crit); 132 result = broker.getCollectionByQuery(query); 133 } catch (Exception e) { 134 if (logger.isEnabledFor(Level.ERROR)) 135 logger.error(e.getMessage()); 136 } finally { 137 if (broker != null) broker.close(); 138 } 139 return result; 140 } 141 142 public Collection findByUserName(String username, String folder) { 143 Collection result = new ArrayList (); 144 try { 145 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 146 Criteria crit = new Criteria(); 147 crit.addEqualTo("co_username", username); 148 crit.addEqualTo("co_folder", folder); 149 QueryByCriteria query = new QueryByCriteria(org.contineo.communication.EMail.class, crit); 150 query.addOrderBy("co_sentdate",true); 151 result = broker.getCollectionByQuery(query); 152 } catch (Exception e) { 153 if (logger.isEnabledFor(Level.ERROR)) 154 logger.error(e.getMessage()); 155 } finally { 156 if (broker != null) broker.close(); 157 } 158 return result; 159 } 160 } 161 | Popular Tags |