1 6 7 package org.contineo.communication.dao; 8 9 import java.util.ArrayList ; 10 import java.util.Collection ; 11 import java.util.Date ; 12 import java.util.Iterator ; 13 import org.apache.log4j.Level; 14 import org.apache.log4j.Logger; 15 import org.apache.ojb.broker.PBKey; 16 import org.apache.ojb.broker.PersistenceBroker; 17 import org.apache.ojb.broker.PersistenceBrokerFactory; 18 import org.apache.ojb.broker.query.Criteria; 19 import org.apache.ojb.broker.query.Query; 20 import org.apache.ojb.broker.query.QueryByCriteria; 21 import org.contineo.communication.SystemMessage; 22 import org.contineo.core.LoggingManager; 23 28 public class SystemMessageDAO { 29 30 34 private PersistenceBroker broker = null; 35 36 40 PBKey pbkey; 41 42 46 private Logger logger; 47 48 49 50 51 public SystemMessageDAO() { 52 logger = LoggingManager.getLogger(this.getClass()); 53 pbkey = new PBKey("contineo"); 54 } 55 56 61 public boolean store(SystemMessage sysmess) { 62 boolean result = true; 63 try { 64 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 65 broker.beginTransaction(); 66 broker.store(sysmess); 67 broker.commitTransaction(); 68 } catch (Exception e) { 69 broker.abortTransaction(); 70 if (logger.isEnabledFor(Level.ERROR)) 71 logger.error(e.getMessage()); 72 result = false; 73 } finally { 74 if (broker != null) broker.close(); 75 } 76 return result; 77 } 78 79 83 public boolean delete(int messageid) { 84 boolean result = true; 85 try { 86 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 87 Criteria crit = new Criteria(); 88 crit.addEqualTo("co_messageid", new Integer (messageid)); 89 Query query = new QueryByCriteria(org.contineo.communication.SystemMessage.class, crit); 90 broker.beginTransaction(); 91 broker.deleteByQuery(query); 92 broker.commitTransaction(); 93 } catch (Exception e) { 94 broker.abortTransaction(); 95 if (logger.isEnabledFor(Level.ERROR)) 96 logger.error(e.getMessage()); 97 result = false; 98 } finally { 99 if (broker != null) broker.close(); 100 } 101 return result; 102 } 103 104 107 public SystemMessage findByPrimaryKey(int messageid) { 108 SystemMessage sysmess = new SystemMessage(); 109 try { 110 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 111 Criteria crit = new Criteria(); 112 crit.addEqualTo("co_messageid", new Integer (messageid)); 113 Query query = new QueryByCriteria(org.contineo.communication.SystemMessage.class, crit); 114 sysmess = (SystemMessage)broker.getObjectByQuery(query); 115 } catch (Exception e) { 116 if (logger.isEnabledFor(Level.ERROR)) 117 logger.error(e.getMessage()); 118 } finally { 119 if (broker != null) broker.close(); 120 } 121 return sysmess; 122 } 123 124 127 public Collection findByRecipient(String recipient) { 128 Collection coll = new ArrayList (); 129 try { 130 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 131 Criteria crit = new Criteria(); 132 crit.addEqualTo("co_recipient", recipient); 133 QueryByCriteria query = new QueryByCriteria(org.contineo.communication.SystemMessage.class, crit); 134 query.addOrderBy("co_sentdate", false); 135 coll = broker.getCollectionByQuery(query); 136 coll = collectGarbage(coll); 137 } catch (Exception e) { 138 if (logger.isEnabledFor(Level.ERROR)) 139 logger.error(e.getMessage()); 140 } finally { 141 if (broker != null) broker.close(); 142 } 143 return coll; 144 } 145 146 149 public int getCount(String recipient) { 150 int count = 0; 151 try { 152 broker = PersistenceBrokerFactory.createPersistenceBroker(pbkey); 153 Criteria crit = new Criteria(); 154 crit.addEqualTo("co_recipient", recipient); 155 crit.addEqualTo("co_red", new Integer (0)); 156 Query query = new QueryByCriteria(org.contineo.communication.SystemMessage.class, crit); 157 count = broker.getCount(query); 158 } catch (Exception e) { 159 if (logger.isEnabledFor(Level.ERROR)) 160 logger.error(e.getMessage()); 161 } finally { 162 if (broker != null) broker.close(); 163 } 164 return count; 165 } 166 167 protected Collection collectGarbage(Collection coll) { 168 try { 169 Iterator iter = coll.iterator(); 170 Date date = new Date (); 171 long time = date.getTime(); 172 while (iter.hasNext()) { 173 SystemMessage sm = (SystemMessage)iter.next(); 174 long sentdate = Long.parseLong(sm.getSentDate()); 175 long timespan = sm.getDateScope(); 176 timespan = timespan * 86400000; 177 sentdate += timespan; 178 if (time >= sentdate) { 179 coll.remove(sm); 180 delete(sm.getMessageId()); 181 } 182 } 183 } catch (Exception e) { 184 if (logger.isEnabledFor(Level.ERROR)) 185 logger.error(e.getMessage()); 186 } 187 return coll; 188 } 189 } 190 | Popular Tags |