001: /*
002: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
003: * Government Rights - Commercial software. Government users are subject
004: * to the Sun Microsystems, Inc. standard license agreement and
005: * applicable provisions of the FAR and its supplements. Use is subject
006: * to license terms.
007: *
008: * This distribution may include materials developed by third parties.
009: * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
010: * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
011: * other countries.
012: *
013: * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
014: *
015: * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
016: * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
017: * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
018: * en vigueur de la FAR (Federal Acquisition Regulations) et des
019: * supplements a celles-ci. Distribue par des licences qui en
020: * restreignent l'utilisation.
021: *
022: * Cette distribution peut comprendre des composants developpes par des
023: * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
024: * sont des marques de fabrique ou des marques deposees de Sun
025: * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
026: */
027:
028: package bank;
029:
030: import java.math.BigDecimal;
031: import java.sql.Connection;
032: import java.sql.PreparedStatement;
033: import java.sql.ResultSet;
034: import java.sql.SQLException;
035: import java.util.ArrayList;
036: import java.util.Collection;
037: import java.util.Iterator;
038: import javax.ejb.*;
039:
040: /**
041: * This is the bean class for the SavingsAccountBean enterprise bean.
042: * Created Mar 23, 2005 12:58:37 PM
043: * @author blaha
044: */
045: public class SavingsAccountBean implements EntityBean,
046: SavingsAccountRemoteBusiness {
047: private EntityContext context;
048: private Connection con;
049: private String id;
050: private String firstName;
051: private String lastName;
052: private BigDecimal balance;
053:
054: // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">
055: // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise beans, Web services)
056: // TODO Add business methods
057: // TODO Add create methods
058: /**
059: * @see javax.ejb.EntityBean#setEntityContext(javax.ejb.EntityContext)
060: */
061: public void setEntityContext(EntityContext aContext) {
062: context = aContext;
063: }
064:
065: /**
066: * @see javax.ejb.EntityBean#ejbActivate()
067: */
068: public void ejbActivate() {
069: id = (String) context.getPrimaryKey();
070: }
071:
072: /**
073: * @see javax.ejb.EntityBean#ejbPassivate()
074: */
075: public void ejbPassivate() {
076: id = null;
077: }
078:
079: /**
080: * @see javax.ejb.EntityBean#ejbRemove()
081: */
082: public void ejbRemove() {
083: try {
084: deleteRow(id);
085: } catch (SQLException ex) {
086: throw new EJBException("ejbRemove: " + ex.getMessage());
087: }
088: }
089:
090: /**
091: * @see javax.ejb.EntityBean#unsetEntityContext()
092: */
093: public void unsetEntityContext() {
094: context = null;
095: }
096:
097: /**
098: * @see javax.ejb.EntityBean#ejbLoad()
099: */
100: public void ejbLoad() {
101: // TODO add code to retrieve data
102: try {
103: loadRow();
104: } catch (SQLException ex) {
105: throw new EJBException("ejbLoad() " + ex.getMessage());
106: }
107:
108: }
109:
110: /**
111: * @see javax.ejb.EntityBean#ejbStore()
112: */
113: public void ejbStore() {
114: // TODO add code to persist data
115: try {
116: storeRow();
117: } catch (SQLException ex) {
118: throw new EJBException("ejbStore " + ex.getMessage());
119: }
120: }
121:
122: // </editor-fold>
123:
124: /**
125: * See EJB 2.0 and EJB 2.1 section 12.2.5
126: */
127: public java.lang.String ejbFindByPrimaryKey(java.lang.String aKey)
128: throws FinderException {
129: // TODO add code to locate aKey from persistent storage
130: // throw javax.ejb.ObjectNotFoundException if aKey is not in
131: // persistent storage.
132: boolean result;
133: try {
134: result = selectByPrimaryKey(aKey);
135: } catch (SQLException ex) {
136: throw new EJBException("ejbFindByPrimaryKey: "
137: + ex.getMessage());
138: }
139: if (result) {
140: return aKey;
141: } else {
142: throw new ObjectNotFoundException("Account for id " + aKey
143: + " not found.");
144: }
145: }
146:
147: public java.lang.String ejbCreate(java.lang.String id,
148: java.lang.String firstName, java.lang.String lastName,
149: java.math.BigDecimal balance) throws CreateException {
150: //TODO implement ejbCreate
151: if (balance.signum() == -1) {
152: throw new CreateException(
153: "A negative initial balance is not allowed.");
154: }
155: try {
156: insertRow(id, firstName, lastName, balance);
157: } catch (SQLException ex) {
158: throw new EJBException("ejbCreate: " + ex.getMessage());
159: }
160: this .id = id;
161: this .firstName = firstName;
162: this .lastName = lastName;
163: this .balance = balance;
164: return id;
165: }
166:
167: public void ejbPostCreate(java.lang.String id,
168: java.lang.String firstName, java.lang.String lastName,
169: java.math.BigDecimal balance) throws CreateException {
170: //TODO implement ejbPostCreate
171: }
172:
173: private javax.sql.DataSource getSavingsAccountDB()
174: throws javax.naming.NamingException {
175: javax.naming.Context c = new javax.naming.InitialContext();
176: return (javax.sql.DataSource) c
177: .lookup("java:comp/env/jdbc/pointbase");
178: }
179:
180: public String getId() {
181: return id;
182: }
183:
184: public void setId(String id) {
185: this .id = id;
186: }
187:
188: public String getFirstName() {
189: return firstName;
190: }
191:
192: public void setFirstName(String firstName) {
193: this .firstName = firstName;
194: }
195:
196: public String getLastName() {
197: return lastName;
198: }
199:
200: public void setLastName(String lastName) {
201: this .lastName = lastName;
202: }
203:
204: public BigDecimal getBalance() {
205: return balance;
206: }
207:
208: public void setBalance(BigDecimal balance) {
209: this .balance = balance;
210: }
211:
212: public void credit(BigDecimal credit) {
213: balance = balance.add(credit);
214: }
215:
216: // business method
217: public void debit(BigDecimal amount)
218: throws InsufficientBalanceException {
219: if (balance.compareTo(amount) == -1) {
220: throw new InsufficientBalanceException();
221: }
222:
223: balance = balance.subtract(amount);
224: }
225:
226: private void makeConnection() {
227: try {
228: con = getSavingsAccountDB().getConnection();
229: } catch (Exception ex) {
230: throw new EJBException("Unable to connect to database. "
231: + ex.getMessage());
232: }
233: }
234:
235: private void releaseConnection() {
236: try {
237: con.close();
238: } catch (SQLException ex) {
239: throw new EJBException("Unable to connect to database. "
240: + ex.getMessage());
241: }
242: }
243:
244: // finder methods
245:
246: public java.util.Collection ejbFindInRange(BigDecimal low,
247: BigDecimal high) throws FinderException {
248: //TODO implement ejbFindByInRange
249: Collection result;
250: try {
251: result = selectInRange(low, high);
252: } catch (SQLException ex) {
253: throw new EJBException("ejbFindByInRange" + ex.getMessage());
254: }
255: return result;
256: }
257:
258: public java.util.Collection ejbFindByLastName(
259: java.lang.String lastName) throws FinderException {
260: //TODO implement ejbFindByLastName
261: Collection result;
262: try {
263: result = selectByLastName(lastName);
264: } catch (SQLException ex) {
265: throw new EJBException("ejbFindByLastName"
266: + ex.getMessage());
267: }
268: return result;
269: }
270:
271: // home method
272: public void ejbHomeChargeForLowBalance(BigDecimal minimumBalance,
273: BigDecimal charge) throws InsufficientBalanceException {
274: SavingsAccountRemoteHome home = (SavingsAccountRemoteHome) context
275: .getEJBHome();
276: try {
277: Collection c = home.findInRange(new BigDecimal(0.00),
278: minimumBalance.subtract(new BigDecimal(0.01)));
279: Iterator it = c.iterator();
280: while (it.hasNext()) {
281: SavingsAccountRemote account = (SavingsAccountRemote) it
282: .next();
283: if (account.getBalance().compareTo(charge) == 1) {
284: account.debit(charge);
285: }
286: }
287: } catch (Exception ex) {
288: throw new EJBException("ejbHomeChargeForLowBalance"
289: + ex.getMessage());
290: }
291: }
292:
293: // database methods
294:
295: private void insertRow(String id, String firstName,
296: String lastName, BigDecimal balance) throws SQLException {
297: makeConnection();
298: String insertStatement = "insert into savingsaccount values ( ? , ? , ? , ? )";
299: PreparedStatement prepStmt = con
300: .prepareStatement(insertStatement);
301:
302: prepStmt.setString(1, id);
303: prepStmt.setString(2, firstName);
304: prepStmt.setString(3, lastName);
305: prepStmt.setBigDecimal(4, balance);
306:
307: prepStmt.executeUpdate();
308: prepStmt.close();
309: releaseConnection();
310: }
311:
312: private void deleteRow(String id) throws SQLException {
313: makeConnection();
314: String deleteStatement = "delete from savingsaccount where id = ?";
315: PreparedStatement prepStmt = con
316: .prepareStatement(deleteStatement);
317: prepStmt.setString(1, id);
318: prepStmt.executeUpdate();
319: prepStmt.close();
320: releaseConnection();
321: }
322:
323: private boolean selectByPrimaryKey(String id) throws SQLException {
324: makeConnection();
325: String selectStatement = "select id from savingsaccount where id = ?";
326: PreparedStatement prepStmt = con
327: .prepareStatement(selectStatement);
328: prepStmt.setString(1, id);
329:
330: ResultSet rs = prepStmt.executeQuery();
331: boolean result = rs.next();
332:
333: releaseConnection();
334: return result;
335: }
336:
337: private void loadRow() throws SQLException {
338: makeConnection();
339: String selectString = "select firstname, lastname, balance from savingsaccount where id = ?";
340: PreparedStatement prepStmt = con.prepareStatement(selectString);
341: prepStmt.setString(1, this .id);
342: ResultSet rs = prepStmt.executeQuery();
343:
344: if (rs.next()) {
345: this .firstName = rs.getString(1);
346: this .lastName = rs.getString(2);
347: this .balance = rs.getBigDecimal(3);
348: prepStmt.close();
349: } else {
350: prepStmt.close();
351: throw new NoSuchEntityException("Row for id " + id
352: + " not found in database");
353: }
354:
355: releaseConnection();
356: }
357:
358: private void storeRow() throws SQLException {
359: makeConnection();
360: String updateString = "update savingsaccount set firstname = ? , lastname = ? , "
361: + "balance = ? where id = ?";
362: PreparedStatement prepStmt = con.prepareStatement(updateString);
363: prepStmt.setString(1, firstName);
364: prepStmt.setString(2, lastName);
365: prepStmt.setBigDecimal(3, balance);
366: prepStmt.setString(4, id);
367:
368: int rows = prepStmt.executeUpdate();
369: prepStmt.close();
370: releaseConnection();
371: if (rows == 0) {
372: throw new EJBException("Storing row id " + id + " failed.");
373: }
374: }
375:
376: private Collection selectByLastName(String lastName)
377: throws SQLException {
378: makeConnection();
379: String selectStatement = "select id from savingsaccount where lastname = ?";
380: PreparedStatement prepStmt = con
381: .prepareStatement(selectStatement);
382: prepStmt.setString(1, lastName);
383: ResultSet rs = prepStmt.executeQuery();
384: ArrayList accounts = new ArrayList();
385:
386: while (rs.next()) {
387: accounts.add(rs.getString(1));
388: }
389: prepStmt.close();
390: releaseConnection();
391: return accounts;
392: }
393:
394: private Collection selectInRange(BigDecimal low, BigDecimal high)
395: throws SQLException {
396: makeConnection();
397: String selectStatement = "select id from savingsaccount where balance between ? and ?";
398: PreparedStatement prepStmt = con
399: .prepareStatement(selectStatement);
400: prepStmt.setBigDecimal(1, low);
401: prepStmt.setBigDecimal(2, high);
402:
403: ResultSet rs = prepStmt.executeQuery();
404: ArrayList a = new ArrayList();
405:
406: while (rs.next()) {
407: a.add(rs.getString(1));
408: }
409: prepStmt.close();
410: releaseConnection();
411: return a;
412: }
413: }
|