KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > contineo > communication > dao > SystemMessageDAO


1 /*
2  * SystemMessageDAO.java
3  *
4  * Created on 9. Dezember 2003, 23:29
5  */

6
7 package org.contineo.communication.dao;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Date JavaDoc;
12 import java.util.Iterator JavaDoc;
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 /**
24  * This is a DAO service for SystemMessages.
25  * @author Michael Scholz
26  * @version 1.0
27  */

28 public class SystemMessageDAO {
29
30     /**
31      * @uml.property name="broker"
32      * @uml.associationEnd
33      */

34     private PersistenceBroker broker = null;
35
36     /**
37      * @uml.property name="pbkey"
38      * @uml.associationEnd
39      */

40     PBKey pbkey;
41
42     /**
43      * @uml.property name="logger"
44      * @uml.associationEnd
45      */

46     private Logger logger;
47
48     
49     
50     /** Creates a new instance of SystemMessageDAO */
51     public SystemMessageDAO() {
52         logger = LoggingManager.getLogger(this.getClass());
53         pbkey = new PBKey("contineo");
54     }
55
56     /**
57      * This method persists a systemmessage object.
58      * @param sysmess SystemMessage which should be store.
59      * @return True if successfully stored in a database.
60      */

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 JavaDoc 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     /**
80      * This method deletes a systemmessage.
81      * @param messageid MessageId of the systemmessage which should be delete.
82      */

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 JavaDoc(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 JavaDoc 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     /**
105      * This method finds a systemmessage by its messageid.
106      */

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 JavaDoc(messageid));
113             Query query = new QueryByCriteria(org.contineo.communication.SystemMessage.class, crit);
114             sysmess = (SystemMessage)broker.getObjectByQuery(query);
115         } catch (Exception JavaDoc 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     /**
125      * This method finds all systemmessages for a recipient.
126      */

127     public Collection JavaDoc findByRecipient(String JavaDoc recipient) {
128         Collection JavaDoc coll = new ArrayList JavaDoc();
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 JavaDoc 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     /**
147      * This method returns the count of unread systemmessages for a recipient.
148      */

149     public int getCount(String JavaDoc 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 JavaDoc(0));
156             Query query = new QueryByCriteria(org.contineo.communication.SystemMessage.class, crit);
157             count = broker.getCount(query);
158         } catch (Exception JavaDoc 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 JavaDoc collectGarbage(Collection JavaDoc coll) {
168         try {
169             Iterator JavaDoc iter = coll.iterator();
170             Date JavaDoc date = new Date JavaDoc();
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 JavaDoc e) {
184             if (logger.isEnabledFor(Level.ERROR))
185                 logger.error(e.getMessage());
186         }
187         return coll;
188     }
189 }
190
Popular Tags