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.EMailAccount; 20 import org.contineo.core.LoggingManager; 21 26 public class EMailAccountDAO { 27 28 32 private PersistenceBroker broker = null; 33 34 38 PBKey pbkey; 39 40 44 private Logger logger; 45 46 47 48 49 public EMailAccountDAO() { 50 logger = LoggingManager.getLogger(this.getClass()); 51 pbkey = new PBKey("contineo"); 52 } 53 54 59 public boolean store(EMailAccount account) { 60 boolean result = true; 61 try { 62 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 63 broker.beginTransaction(); 64 broker.store(account); 65 broker.commitTransaction(); 66 } catch (Exception e) { 67 broker.abortTransaction(); 68 if (logger.isEnabledFor(Level.ERROR)) 69 logger.error(e.getMessage()); 70 result = false; 71 } finally { 72 if (broker != null) broker.close(); 73 } 74 return result; 75 } 76 77 81 public boolean delete(int accountid) { 82 boolean result = true; 83 try { 84 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 85 Criteria crit = new Criteria(); 86 crit.addEqualTo("co_accountid", new Integer (accountid)); 87 Query query = new QueryByCriteria(org.contineo.communication.EMailAccount.class, crit); 88 broker.beginTransaction(); 89 broker.deleteByQuery(query); 90 broker.commitTransaction(); 91 } catch (Exception e) { 92 broker.abortTransaction(); 93 if (logger.isEnabledFor(Level.ERROR)) 94 logger.error(e.getMessage()); 95 result = false; 96 } finally { 97 if (broker != null) broker.close(); 98 } 99 return result; 100 } 101 102 106 public boolean deleteByUsername(String username) { 107 boolean result = true; 108 try { 109 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 110 Criteria crit = new Criteria(); 111 crit.addEqualTo("co_username", username); 112 Query query = new QueryByCriteria(org.contineo.communication.EMailAccount.class, crit); 113 broker.beginTransaction(); 114 broker.deleteByQuery(query); 115 broker.commitTransaction(); 116 } catch (Exception e) { 117 broker.abortTransaction(); 118 if (logger.isEnabledFor(Level.ERROR)) 119 logger.error(e.getMessage()); 120 result = false; 121 } finally { 122 if (broker != null) broker.close(); 123 } 124 return result; 125 } 126 127 130 public EMailAccount findByPrimaryKey(int accountid) { 131 EMailAccount account = new EMailAccount(); 132 try { 133 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 134 Criteria crit = new Criteria(); 135 crit.addEqualTo("co_accountid", new Integer (accountid)); 136 Query query = new QueryByCriteria(org.contineo.communication.EMailAccount.class, crit); 137 account = (EMailAccount)broker.getObjectByQuery(query); 138 } catch (Exception e) { 139 if (logger.isEnabledFor(Level.ERROR)) 140 logger.error(e.getMessage()); 141 } finally { 142 if (broker != null) broker.close(); 143 } 144 return account; 145 } 146 147 public Collection findByUserName(String username) { 148 Collection result = new ArrayList (); 149 try { 150 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 151 Criteria crit = new Criteria(); 152 crit.addEqualTo("co_username", username); 153 Query query = new QueryByCriteria(org.contineo.communication.EMailAccount.class, crit); 154 result = broker.getCollectionByQuery(query); 155 } catch (Exception e) { 156 if (logger.isEnabledFor(Level.ERROR)) 157 logger.error(e.getMessage()); 158 } finally { 159 if (broker != null) broker.close(); 160 } 161 return result; 162 } 163 } 164 | Popular Tags |