KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * EMailAccountDAO.java
3  *
4  * Created on 28. Februar 2004, 17:08
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.EMailAccount;
20 import org.contineo.core.LoggingManager;
21 /**
22  * This is a DAO service for EMailAccounts.
23  * @author Michael Scholz
24  * @version 1.0
25  */

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

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

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

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

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

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 JavaDoc(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 JavaDoc 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     /**
103      * This method deletes an emailaccount.
104      * @param username Username of the emailaccount which should be delete.
105      */

106     public boolean deleteByUsername(String JavaDoc 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 JavaDoc 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     /**
128      * This method finds an emailaccount by its accountid.
129      */

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 JavaDoc(accountid));
136             Query query = new QueryByCriteria(org.contineo.communication.EMailAccount.class, crit);
137             account = (EMailAccount)broker.getObjectByQuery(query);
138         } catch (Exception JavaDoc 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 JavaDoc findByUserName(String JavaDoc username) {
148         Collection JavaDoc result = new ArrayList JavaDoc();
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 JavaDoc 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