KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > beanexc > AccountEB


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: AccountEB.java,v 1.5 2004/03/15 16:02:58 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.beanexc;
27
28 import java.sql.Connection JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Vector JavaDoc;
34
35 import javax.ejb.EntityBean JavaDoc;
36 import javax.ejb.FinderException JavaDoc;
37 import javax.ejb.ObjectNotFoundException JavaDoc;
38 import javax.ejb.RemoveException JavaDoc;
39 import javax.naming.Context JavaDoc;
40 import javax.naming.InitialContext JavaDoc;
41 import javax.sql.DataSource JavaDoc;
42
43 import org.objectweb.util.monolog.api.BasicLevel;
44
45
46 /**
47  * This is an entity bean with "bean managed persistence".
48  * The state of an instance is stored into a relational database.
49  * The table must exist.
50  * @author Philippe Durieux, Philippe Coq
51  */

52 public class AccountEB extends AccountEC implements EntityBean JavaDoc {
53
54     static final String JavaDoc tableName = "beanexcAccountEB";
55
56     // Database related information
57
private DataSource JavaDoc dataSource = null;
58     private static String JavaDoc dataSourceName;
59
60
61     public void ejbLoad() {
62         logger.log(BasicLevel.DEBUG, "");
63         AccountPK pk = (AccountPK) entityContext.getPrimaryKey();
64         PreparedStatement JavaDoc loadPstmt = null;
65         Connection JavaDoc conn = null;
66         try {
67             conn = getConnection();
68             loadPstmt = conn.prepareStatement("select c_number, c_customer, c_balance from " + tableName + " where c_number=?");
69
70             // PK fields
71
loadPstmt.setInt(1, pk.number);
72             ResultSet JavaDoc rs = loadPstmt.executeQuery();
73             if (!rs.next()) {
74                 System.err.println("Fails to load bean from database");
75                 throw new javax.ejb.EJBException JavaDoc("Failed to load bean from database");
76             }
77             number = rs.getInt("c_number");
78             customer = rs.getString("c_customer");
79             balance = rs.getLong("c_balance");
80         } catch (SQLException JavaDoc e) {
81             System.err.println("Fails to load bean from database");
82             throw new javax.ejb.EJBException JavaDoc("Failed to load bean from database" + e);
83         } finally {
84             try {
85                 loadPstmt.close();
86                 conn.close();
87             } catch (Exception JavaDoc e) {
88                 System.err.println("ejbLoad: Ignore exception:" + e);
89             }
90         }
91
92     }
93
94     public void ejbStore() {
95         AccountPK pk = (AccountPK) entityContext.getPrimaryKey();
96         logger.log(BasicLevel.DEBUG, "pk=" + pk.number);
97         PreparedStatement JavaDoc storePstmt = null;
98         Connection JavaDoc conn = null;
99         if (forceToFailEjbStore) {
100             forceToFailEjbStore = false;
101             throw new javax.ejb.EJBException JavaDoc("Failed to store bean to database");
102         }
103
104         try {
105             conn = getConnection();
106             storePstmt = conn.prepareStatement("update " + tableName + " set c_customer=?,c_balance=? where c_number=?");
107
108             // non PK fields
109
if (customer == null) {
110                 storePstmt.setNull(1, java.sql.Types.VARCHAR);
111             } else {
112                 storePstmt.setString(1, customer);
113             }
114             storePstmt.setLong(2, balance);
115             // PK fields
116
storePstmt.setInt(3, number);
117             storePstmt.executeUpdate();
118         } catch (SQLException JavaDoc e) {
119             throw new javax.ejb.EJBException JavaDoc("Failed to store bean to database");
120         } finally {
121             try {
122                 storePstmt.close();
123                 conn.close();
124             } catch (Exception JavaDoc e) {
125                 System.err.println("ejbStore: Ignore exception:" + e);
126             }
127         }
128     }
129
130     public void ejbRemove() throws RemoveException JavaDoc {
131         logger.log(BasicLevel.DEBUG, "");
132
133         // A RemoveException may be throwned depending on the PK value
134
super.ejbRemove();
135
136         AccountPK pk = (AccountPK) entityContext.getPrimaryKey();
137         PreparedStatement JavaDoc removePstmt = null;
138         Connection JavaDoc conn = null;
139         try {
140             conn = getConnection();
141             removePstmt = conn.prepareStatement("delete from " + tableName + " where c_number=?");
142             // PK fields
143
removePstmt.setInt(1, pk.number);
144             removePstmt.executeUpdate();
145         } catch (SQLException JavaDoc e) {
146             throw new javax.ejb.EJBException JavaDoc("Failed to delete bean from database");
147         } finally {
148             try {
149                 removePstmt.close();
150                 conn.close();
151             } catch (Exception JavaDoc e) {
152                 System.err.println("ejbRemove: Ignore exception:" + e);
153             }
154         }
155     }
156
157
158     public AccountPK ejbCreate(int val_number, String JavaDoc val_customer, long val_balance) throws javax.ejb.CreateException JavaDoc {
159         logger.log(BasicLevel.DEBUG, "");
160
161         super.ejbCreate(val_number, val_customer, val_balance);
162
163         PreparedStatement JavaDoc createPstmt = null;
164         Connection JavaDoc conn = null;
165         AccountPK pk = new AccountPK(val_number);
166
167         // persistence management
168
try {
169             conn = getConnection();
170             createPstmt = conn.prepareStatement("insert into " + tableName + " values (?, ?, ?)");
171             // all fields
172
createPstmt.setInt(1, pk.number);
173             if (customer == null) {
174                 createPstmt.setNull(2, java.sql.Types.VARCHAR);
175             } else {
176                 createPstmt.setString(2, customer);
177             }
178             createPstmt.setLong(3, balance);
179             createPstmt.executeUpdate();
180         } catch (SQLException JavaDoc e) {
181             throw new javax.ejb.EJBException JavaDoc("Failed to create bean in database" + e);
182         } finally {
183             try {
184                 createPstmt.close();
185                 conn.close();
186             } catch (Exception JavaDoc e) {
187                 logger.log(BasicLevel.ERROR, "ejbCreate: Ignore exception:" + e);
188             }
189         }
190         return pk;
191     }
192
193     /**
194      * creer une instance bidon le flag sert pour savoir quelle exception lever
195      */

196     public AccountPK ejbCreate(int flag) throws javax.ejb.CreateException JavaDoc, AppException {
197         logger.log(BasicLevel.DEBUG, "");
198         AccountPK pk = super.ejbCreate(flag);
199         return pk;
200     }
201
202     public AccountPK ejbCreate(boolean flag) throws javax.ejb.CreateException JavaDoc, AppException {
203         logger.log(BasicLevel.DEBUG, "");
204         AccountPK pk = super.ejbCreate(flag);
205         return pk;
206     }
207
208     public void ejbPostCreate(int val_number, String JavaDoc val_customer, long val_balance) {
209         logger.log(BasicLevel.DEBUG, "");
210     }
211
212     public AccountPK ejbFindByPrimaryKey(AccountPK pk) throws ObjectNotFoundException JavaDoc, FinderException JavaDoc {
213         logger.log(BasicLevel.DEBUG, "");
214         PreparedStatement JavaDoc loadPstmt = null;
215         Connection JavaDoc conn = null;
216         try {
217             conn = getConnection();
218             loadPstmt = conn.prepareStatement("select c_customer, c_balance from " + tableName + " where c_number=?");
219             loadPstmt.setInt(1, pk.number);
220             ResultSet JavaDoc rs = loadPstmt.executeQuery();
221             if (!rs.next()) {
222                 throw new javax.ejb.ObjectNotFoundException JavaDoc("primary key");
223             }
224         } catch (SQLException JavaDoc e) {
225             System.err.println("Fails to executeQuery " + e);
226             throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " + e);
227         } finally {
228             try {
229                 loadPstmt.close();
230                 conn.close();
231             } catch (Exception JavaDoc e) {
232                 logger.log(BasicLevel.ERROR, "ejbFindByPrimaryKey: Ignore exception:" + e);
233             }
234         }
235         return pk;
236     }
237
238     public AccountPK ejbFindByNoAccount(int number)
239         throws ObjectNotFoundException JavaDoc, FinderException JavaDoc {
240         logger.log(BasicLevel.DEBUG, "");
241         PreparedStatement JavaDoc loadPstmt = null;
242         Connection JavaDoc conn = null;
243         AccountPK pk = new AccountPK(number);
244         try {
245             conn = getConnection();
246             loadPstmt = conn.prepareStatement("select c_customer,c_balance from " + tableName + " where c_number=?");
247             loadPstmt.setInt(1, pk.number);
248             ResultSet JavaDoc rs = loadPstmt.executeQuery();
249             if (!rs.next()) {
250                 logger.log(BasicLevel.ERROR, "Object Not Found primary key : " + pk.number);
251                 throw new javax.ejb.ObjectNotFoundException JavaDoc("primary key : " + pk.number);
252             }
253         } catch (SQLException JavaDoc e) {
254             System.err.println("Fails to executeQuery " + e);
255             throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " + e);
256         } finally {
257             try {
258                 loadPstmt.close();
259                 conn.close();
260             } catch (Exception JavaDoc e) {
261                 logger.log(BasicLevel.ERROR, "ejbFindByNoAccount: Ignore exception:" + e);
262             }
263         }
264         return pk;
265     }
266
267     public Enumeration JavaDoc ejbFindCustomerAccounts(String JavaDoc name) throws FinderException JavaDoc {
268         logger.log(BasicLevel.DEBUG, "");
269         PreparedStatement JavaDoc loadPstmt = null;
270         Connection JavaDoc conn = null;
271         Vector JavaDoc pkV = new Vector JavaDoc();
272         try {
273             conn = getConnection();
274             loadPstmt = conn.prepareStatement("select c_number from " + tableName + " where c_customer=?");
275             loadPstmt.setString(1, name);
276             ResultSet JavaDoc rs = loadPstmt.executeQuery();
277             while (rs.next()) {
278                 int b = rs.getInt("c_number");
279                 AccountPK pk = new AccountPK(b);
280                 pkV.addElement((Object JavaDoc) pk);
281             }
282
283         } catch (SQLException JavaDoc e) {
284             System.err.println("Fails to executeQuery " + e);
285             throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " + e);
286         } finally {
287             try {
288                 loadPstmt.close();
289                 conn.close();
290             } catch (Exception JavaDoc e) {
291                 logger.log(BasicLevel.ERROR, "ejbFindCustomerAccounts: Ignore exception:" + e);
292             }
293         }
294         return (pkV.elements());
295     }
296
297     public Enumeration JavaDoc ejbFindBigAccounts(long minBalance) throws FinderException JavaDoc {
298         logger.log(BasicLevel.DEBUG, "");
299         PreparedStatement JavaDoc loadPstmt = null;
300         Connection JavaDoc conn = null;
301         Vector JavaDoc pkV = new Vector JavaDoc();
302         try {
303             conn = getConnection();
304             loadPstmt = conn.prepareStatement("select c_number from " + tableName + " where c_balance>?");
305             loadPstmt.setLong(1, minBalance);
306             ResultSet JavaDoc rs = loadPstmt.executeQuery();
307             while (rs.next()) {
308                 int b = rs.getInt("c_number");
309                 AccountPK pk = new AccountPK(b);
310                 pkV.addElement((Object JavaDoc) pk);
311             }
312         } catch (SQLException JavaDoc e) {
313             System.err.println("Fails to executeQuery " + e);
314             throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " + e);
315         } finally {
316             try {
317                 loadPstmt.close();
318                 conn.close();
319             } catch (Exception JavaDoc e) {
320                 logger.log(BasicLevel.ERROR, "ejbFindBigAccounts: Ignore exception:" + e);
321             }
322         }
323         return (pkV.elements());
324     }
325
326     public Enumeration JavaDoc ejbFindAllAccounts() throws FinderException JavaDoc {
327         logger.log(BasicLevel.DEBUG, "");
328         PreparedStatement JavaDoc loadPstmt = null;
329         Connection JavaDoc conn = null;
330         Vector JavaDoc pkV = new Vector JavaDoc();
331         try {
332             conn = getConnection();
333             loadPstmt = conn.prepareStatement("select c_number from " + tableName);
334             ResultSet JavaDoc rs = loadPstmt.executeQuery();
335             while (rs.next()) {
336                 int b = rs.getInt("c_number");
337                 AccountPK pk = new AccountPK(b);
338                 pkV.addElement((Object JavaDoc) pk);
339             }
340         } catch (SQLException JavaDoc e) {
341             System.err.println("Fails to executeQuery " + e);
342             throw new javax.ejb.FinderException JavaDoc("Failed to executeQuery " + e);
343         } finally {
344             try {
345                 loadPstmt.close();
346                 conn.close();
347             } catch (Exception JavaDoc e) {
348                 logger.log(BasicLevel.ERROR, "ejbFindAllAccounts: Ignore exception:" + e);
349             }
350         }
351         return (pkV.elements());
352     }
353
354     private Connection JavaDoc getConnection() throws java.sql.SQLException JavaDoc {
355         logger.log(BasicLevel.DEBUG, "");
356         if (dataSource == null) {
357             // Finds DataSource from JNDI
358
Context JavaDoc initialContext = null;
359             try {
360                 initialContext = new InitialContext JavaDoc();
361                 dataSource = (DataSource JavaDoc) initialContext.lookup("java:comp/env/jdbc/AccountEB");
362             } catch (Exception JavaDoc e) {
363                 logger.log(BasicLevel.ERROR, "Pb with naming context");
364                 throw new javax.ejb.EJBException JavaDoc("Pb with naming context ");
365             }
366         }
367         Connection JavaDoc ret = dataSource.getConnection();
368         if (ret == null) {
369             throw new javax.ejb.EJBException JavaDoc("dataSource.getConnection() returned null");
370         }
371         return ret;
372     }
373 }
374
Popular Tags