KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ebank > ejb > tx > TxBean


1 /*
2  * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2004 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

27
28
29 package com.sun.ebank.ejb.tx;
30
31 import java.sql.*;
32 import javax.sql.*;
33 import java.util.*;
34 import java.math.*;
35 import javax.ejb.*;
36 import javax.naming.*;
37 import com.sun.ebank.ejb.exception.MissingPrimaryKeyException;
38 import com.sun.ebank.util.Debug;
39 import com.sun.ebank.util.TxDetails;
40 import com.sun.ebank.util.CodedNames;
41 import com.sun.ebank.util.DBHelper;
42
43
44 public class TxBean implements EntityBean {
45     private String JavaDoc txId;
46     private String JavaDoc accountId;
47     private java.util.Date JavaDoc timeStamp;
48     private BigDecimal amount;
49     private BigDecimal balance;
50     private String JavaDoc description;
51     private EntityContext context;
52     private Connection con;
53
54     // business methods
55
public TxDetails getDetails() {
56         Debug.print("TxBean getDetails");
57
58         return new TxDetails(txId, accountId, timeStamp, amount, balance,
59             description);
60     }
61
62     // ejb methods
63
public String JavaDoc ejbCreate(String JavaDoc txId, String JavaDoc accountId,
64         java.util.Date JavaDoc timeStamp, BigDecimal amount, BigDecimal balance,
65         String JavaDoc description) throws CreateException, MissingPrimaryKeyException {
66         Debug.print("TxBean ejbCreate");
67
68         if ((txId == null) || (txId.trim()
69                                        .length() == 0)) {
70             throw new MissingPrimaryKeyException(
71                 "ejbCreate: txId arg is null or empty");
72         }
73
74         this.txId = txId;
75         this.accountId = accountId;
76         this.timeStamp = timeStamp;
77         this.amount = amount;
78         this.balance = balance;
79         this.description = description;
80
81         try {
82             insertRow();
83         } catch (Exception JavaDoc ex) {
84             throw new EJBException("ejbCreate: " + ex.getMessage());
85         }
86
87         return txId;
88     }
89
90     public String JavaDoc ejbFindByPrimaryKey(String JavaDoc primaryKey)
91         throws FinderException {
92         Debug.print("TxBean ejbFindByPrimaryKey");
93
94         boolean result;
95
96         try {
97             result = selectByPrimaryKey(primaryKey);
98         } catch (Exception JavaDoc ex) {
99             throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage());
100         }
101
102         if (result) {
103             return primaryKey;
104         } else {
105             throw new ObjectNotFoundException("Row for id " + primaryKey +
106                 " not found.");
107         }
108     }
109
110     public Collection ejbFindByAccountId(java.util.Date JavaDoc startDate,
111         java.util.Date JavaDoc endDate, String JavaDoc accountId) throws FinderException {
112         Debug.print("TxBean ejbFindByAccountId");
113
114         Collection result;
115
116         try {
117             result = selectByAccountId(accountId, startDate, endDate);
118         } catch (Exception JavaDoc ex) {
119             throw new EJBException("ejbFindByAccountId " + ex.getMessage());
120         }
121
122         return result;
123     }
124
125     public void ejbRemove() {
126         Debug.print("TxBean ejbRemove");
127
128         try {
129             deleteRow(txId);
130         } catch (Exception JavaDoc ex) {
131             throw new EJBException("ejbRemove: " + ex.getMessage());
132         }
133     }
134
135     public void setEntityContext(EntityContext context) {
136         Debug.print("TxBean setEntityContext");
137         this.context = context;
138     }
139
140     public void unsetEntityContext() {
141         Debug.print("TxBean unsetEntityContext");
142     }
143
144     public void ejbLoad() {
145         Debug.print("TxBean ejbLoad");
146
147         try {
148             loadTx();
149         } catch (Exception JavaDoc ex) {
150             throw new EJBException("ejbLoad: " + ex.getMessage());
151         }
152     }
153
154     public void ejbStore() {
155         Debug.print("TxBean ejbStore");
156
157         try {
158             storeTx();
159         } catch (Exception JavaDoc ex) {
160             throw new EJBException("ejbStore: " + ex.getMessage());
161         }
162     }
163
164     public void ejbActivate() {
165         Debug.print("TxBean ejbActivate");
166         txId = (String JavaDoc) context.getPrimaryKey();
167     }
168
169     public void ejbPassivate() {
170         Debug.print("TxBean ejbPassivate");
171         txId = null;
172     }
173
174     public void ejbPostCreate(String JavaDoc txId, String JavaDoc accountId,
175         java.util.Date JavaDoc timeStamp, BigDecimal amount, BigDecimal balance,
176         String JavaDoc description) {
177     }
178
179     /*********************** Database Routines *************************/
180     private void makeConnection() {
181         Debug.print("TxBean makeConnection");
182
183         try {
184             InitialContext ic = new InitialContext();
185             DataSource ds = (DataSource) ic.lookup(CodedNames.BANK_DATABASE);
186             con = ds.getConnection();
187         } catch (Exception JavaDoc ex) {
188             throw new EJBException("Unable to connect to database. " +
189                 ex.getMessage());
190         }
191     }
192      // makeConnection
193

194     private void releaseConnection() {
195         Debug.print("TxBean releaseConnection");
196
197         try {
198             con.close();
199         } catch (SQLException ex) {
200             throw new EJBException("releaseConnection: " + ex.getMessage());
201         }
202     }
203      // releaseConnection
204

205     private void insertRow() throws SQLException {
206         Debug.print("TxBean insertRow");
207
208         makeConnection();
209
210         String JavaDoc insertStatement =
211             "insert into tx values ( ? , ? , ? , ? , ? , ? )";
212         PreparedStatement prepStmt = con.prepareStatement(insertStatement);
213
214         prepStmt.setString(1, txId);
215         prepStmt.setString(2, accountId);
216         prepStmt.setDate(3, DBHelper.toSQLDate(timeStamp));
217         prepStmt.setBigDecimal(4, amount);
218         prepStmt.setBigDecimal(5, balance);
219         prepStmt.setString(6, description);
220
221         prepStmt.executeUpdate();
222         prepStmt.close();
223         releaseConnection();
224     }
225
226     private void deleteRow(String JavaDoc id) throws SQLException {
227         Debug.print("TxBean deleteRow");
228
229         makeConnection();
230
231         String JavaDoc deleteStatement = "delete from tx where tx_id = ? ";
232         PreparedStatement prepStmt = con.prepareStatement(deleteStatement);
233
234         prepStmt.setString(1, id);
235         prepStmt.executeUpdate();
236         prepStmt.close();
237         releaseConnection();
238     }
239
240     private boolean selectByPrimaryKey(String JavaDoc primaryKey)
241         throws SQLException {
242         Debug.print("TxBean selectByPrimaryKey");
243
244         makeConnection();
245
246         String JavaDoc selectStatement = "select tx_id " + "from tx where tx_id = ? ";
247         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
248         prepStmt.setString(1, primaryKey);
249
250         ResultSet rs = prepStmt.executeQuery();
251         boolean result = rs.next();
252         prepStmt.close();
253         releaseConnection();
254
255         return result;
256     }
257
258     private Collection selectByAccountId(String JavaDoc accountId,
259         java.util.Date JavaDoc startDate, java.util.Date JavaDoc endDate)
260         throws SQLException {
261         Debug.print("TxBean selectByAccountId");
262
263         makeConnection();
264
265         String JavaDoc selectStatement =
266             "select tx_id " + "from tx " + "where (account_id = ?) " +
267             "and ((time_stamp >= ?) and (time_stamp <= ?)) ";
268         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
269
270         prepStmt.setString(1, accountId);
271         prepStmt.setDate(2, DBHelper.toSQLDate(startDate));
272         prepStmt.setDate(3, DBHelper.toSQLDate(endDate));
273
274         ResultSet rs = prepStmt.executeQuery();
275         ArrayList a = new ArrayList();
276
277         while (rs.next()) {
278             a.add(rs.getString(1));
279         }
280
281         prepStmt.close();
282         releaseConnection();
283
284         return a;
285     }
286
287     private void loadTx() throws SQLException {
288         Debug.print("TxBean loadTx");
289
290         makeConnection();
291
292         String JavaDoc selectStatement =
293             "select account_id, time_stamp, amount, balance, description " +
294             "from tx where tx_id = ? ";
295         PreparedStatement prepStmt = con.prepareStatement(selectStatement);
296
297         prepStmt.setString(1, txId);
298
299         ResultSet rs = prepStmt.executeQuery();
300
301         if (rs.next()) {
302             accountId = rs.getString(1);
303             timeStamp = rs.getDate(2);
304             amount = rs.getBigDecimal(3);
305             balance = rs.getBigDecimal(4);
306             description = rs.getString(5);
307             prepStmt.close();
308             releaseConnection();
309         } else {
310             prepStmt.close();
311             releaseConnection();
312             throw new NoSuchEntityException("Row for id " + txId +
313                 " not found in database.");
314         }
315     }
316
317     private void storeTx() throws SQLException {
318         Debug.print("TxBean storeTx");
319
320         makeConnection();
321
322         String JavaDoc updateStatement =
323             "update tx " + "set account_id = ? , time_stamp = ? , " +
324             "amount = ? , balance = ? , description = ? " +
325             "where tx_id = ? ";
326         PreparedStatement prepStmt = con.prepareStatement(updateStatement);
327
328         prepStmt.setString(1, accountId);
329         prepStmt.setDate(2, DBHelper.toSQLDate(timeStamp));
330         prepStmt.setBigDecimal(3, amount);
331         prepStmt.setBigDecimal(4, balance);
332         prepStmt.setString(5, description);
333         prepStmt.setString(6, txId);
334
335         int rowCount = prepStmt.executeUpdate();
336         prepStmt.close();
337         releaseConnection();
338
339         if (rowCount == 0) {
340             throw new EJBException("Storing row for id " + txId + " failed.");
341         }
342     }
343 }
344  // TxBean
345
Popular Tags