KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * EMailDAO.java
3  *
4  * Created on 28. Februar 2004, 18:11
5  */

6
7 package org.contineo.communication.dao;
8
9 import java.util.ArrayList JavaDoc;
10 import java.util.Collection JavaDoc;
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 /**
22  *
23  * @author Michael Scholz
24  */

25 public class EMailDAO {
26
27     /**
28      * @uml.property name="broker"
29      * @uml.associationEnd
30      */

31     private PersistenceBroker broker = null;
32
33     /**
34      * @uml.property name="pbkey"
35      * @uml.associationEnd
36      */

37     PBKey pbkey;
38
39     /**
40      * @uml.property name="logger"
41      * @uml.associationEnd
42      */

43     private Logger logger;
44
45         
46     
47     /** Creates a new instance of EMailAccountDAO */
48     public EMailDAO() {
49         logger = LoggingManager.getLogger(this.getClass());
50         pbkey = new PBKey("contineo");
51     }
52     
53     /**
54      * This method persists an emailaccount object.
55      * @param account EMailAccount which should be store.
56      * @return True if successfully stored in a database.
57      */

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 JavaDoc 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     /**
77      * This method deletes an emailaccount.
78      * @param accountid AccountId of the emailaccount which should be delete.
79      */

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 JavaDoc(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 JavaDoc 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     /**
106      * This method finds an email by its id.
107      */

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 JavaDoc(emailid));
114             Query query = new QueryByCriteria(org.contineo.communication.EMail.class, crit);
115             email = (EMail)broker.getObjectByQuery(query);
116         } catch (Exception JavaDoc 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 JavaDoc findByUserName(String JavaDoc username) {
126         Collection JavaDoc result = new ArrayList JavaDoc();
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 JavaDoc 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 JavaDoc findByUserName(String JavaDoc username, String JavaDoc folder) {
143         Collection JavaDoc result = new ArrayList JavaDoc();
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 JavaDoc 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