001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.jca.bank.ejb;
023:
024: import java.rmi.RemoteException;
025: import java.sql.Connection;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import java.util.Collection;
029: import javax.ejb.CreateException;
030: import javax.ejb.EJBException;
031: import javax.ejb.EntityBean;
032: import javax.ejb.EntityContext;
033: import javax.ejb.FinderException;
034: import javax.ejb.NoSuchEntityException;
035: import javax.ejb.ObjectNotFoundException;
036: import javax.sql.DataSource;
037:
038: import javax.naming.InitialContext;
039: import java.util.ArrayList;
040: import java.sql.SQLException;
041:
042: import org.apache.log4j.Category;
043:
044: /**
045: * Describe class <code>AccountBean</code> here.
046: *
047: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
048: * @version 1.0
049: * @ejb:bean name="Account"
050: * jndi-name="Account"
051: * local-jndi-name="LocalAccount"
052: * view-type="both"
053: * type="BMP"
054: * primkey-field="id"
055: * @ejb:pk class="java.lang.Integer"
056: */
057: public class AccountBean implements EntityBean {
058:
059: private Connection c;
060:
061: private Integer id;
062: private int balance;
063: private Integer customerId;
064:
065: private EntityContext ctx;
066:
067: /**
068: * Abstract cmp2 field get-set pair for field id
069: * Get the value of id
070: * @return value of id
071: *
072: * @ejb:interface-method
073: */
074: public Integer getId() {
075: return id;
076: }
077:
078: /**
079: * Set the value of id
080: * @param id Value to assign to id
081: *
082: * @ejb:interface-method
083: */
084: public void setId(final Integer id) {
085: this .id = id;
086: }
087:
088: /**
089: * field get-set pair for field balance
090: * Get the value of balance
091: * @return value of balance
092: *
093: * @ejb:interface-method
094: */
095: public int getBalance() {
096: return balance;
097: }
098:
099: /**
100: * Set the value of balance
101: * @param balance Value to assign to balance
102: *
103: * @ejb:interface-method
104: */
105: public void setBalance(final int balance) {
106: this .balance = balance;
107: }
108:
109: /**
110: * field get-set pair for field customerId
111: * Get the value of customerId
112: * @return value of customerId
113: *
114: * @ejb:interface-method
115: */
116: public Integer getCustomerId() {
117: return customerId;
118: }
119:
120: /**
121: * Set the value of customerId
122: * @param customerId Value to assign to customerId
123: *
124: * @ejb:interface-method
125: */
126: public void setCustomerId(final Integer customerId) {
127: this .customerId = customerId;
128: }
129:
130: /**
131: * Describe <code>deposit</code> method here.
132: *
133: * @param amount an <code>int</code> value
134: * @ejb:interface-method
135: */
136: public void deposit(int amount) {
137: setBalance(getBalance() + amount);
138: }
139:
140: /**
141: * Describe <code>withdraw</code> method here.
142: *
143: * @param amount an <code>int</code> value
144: * @ejb:interface-method
145: */
146: public void withdraw(int amount) {
147: setBalance(getBalance() - amount);
148: }
149:
150: //ENTITY methods
151: /**
152: * Describe <code>ejbCreate</code> method here.
153: *
154: * @param id an <code>Integer</code> value
155: * @param balance an <code>int</code> value
156: * @param customerId an <code>Integer</code> value
157: * @return an <code>Integer</code> value
158: * @ejb:create-method
159: */
160: public Integer ejbCreate(final Integer id, final int balance,
161: final Integer customerId) throws CreateException {
162: setId(id);
163: setBalance(balance);
164: setCustomerId(customerId);
165: PreparedStatement ps = null;
166: try {
167:
168: ps = getConnection()
169: .prepareStatement(
170: "INSERT INTO CCBMPACCOUNT (ID, BALANCE, CUSTOMERID) VALUES (?, ?, ?)");
171: ps.setInt(1, id.intValue());
172: ps.setInt(2, balance);
173: ps.setObject(3, customerId);
174: } catch (Exception e) {
175: Category.getInstance(getClass().getName()).info(
176: "Exception in ejbCreate", e);
177: throw new CreateException("Can't insert: " + e);
178: } // end of try-catch
179: finally {
180: try {
181: if (ps != null) {
182: ps.close();
183: }
184: } catch (Exception e) {
185: Category.getInstance(getClass().getName()).info(
186: "Exception closing ps: " + e);
187: } // end of try-catch
188:
189: } // end of finally
190: return id;
191: }
192:
193: public void ejbPostCreate(final Integer id, final int balance,
194: final Integer customerId) {
195: }
196:
197: /**
198: * Describe <code>ejbFindByPrimaryKey</code> method here.
199: *
200: * @param id an <code>Integer</code> value
201: * @return an <code>Integer</code> value
202: * @exception FinderException if an error occurs
203: * @ejb:finder
204: */
205: public Integer ejbFindByPrimaryKey(final Integer id)
206: throws FinderException {
207: PreparedStatement ps = null;
208: try {
209: ps = getConnection().prepareStatement(
210: "SELECT ID FROM CCBMPACCOUNT WHERE ID = ?");
211: ps.setInt(1, id.intValue());
212: ResultSet rs = ps.executeQuery();
213: if (!rs.next()) {
214: throw new ObjectNotFoundException("No such account: "
215: + id);
216: } // end of if ()
217: rs.close();
218:
219: } catch (Exception e) {
220: Category.getInstance(getClass().getName()).info(
221: "Exception in findByPK", e);
222: throw new EJBException("Problem in findByPrimaryKey: " + e);
223: } // end of try-catch
224: finally {
225: try {
226: if (ps != null) {
227: ps.close();
228: }
229: } catch (Exception e) {
230: Category.getInstance(getClass().getName()).info(
231: "Exception closing ps: " + e);
232: } // end of try-catch
233: } // end of finally
234: return id;
235: }
236:
237: /**
238: * Describe <code>ejbFindByCustomerId</code> method here.
239: *
240: * @param customerId an <code>Integer</code> value
241: * @return a <code>Collection</code> value
242: * @ejb:finder-method
243: */
244: public Collection ejbFindByCustomerId(final Integer customerId) {
245: PreparedStatement ps = null;
246: try {
247: ps = getConnection().prepareStatement(
248: "SELECT ID FROM CCBMPACCOUNT WHERE CUSTOMERID = ?");
249: ps.setInt(1, customerId.intValue());
250: ResultSet rs = ps.executeQuery();
251: Collection result = new ArrayList();
252: while (rs.next()) {
253: result.add(new Integer(rs.getInt(1)));
254: } // end of if ()
255: rs.close();
256: return result;
257: } catch (Exception e) {
258: Category.getInstance(getClass().getName()).info(
259: "Exception in findbyCustomerID", e);
260: throw new EJBException(e);
261: } // end of try-catch
262: finally {
263: try {
264: if (ps != null) {
265: ps.close();
266: }
267: } catch (Exception e) {
268: Category.getInstance(getClass().getName()).info(
269: "Exception closing ps: " + e);
270: } // end of try-catch
271: } // end of finally
272: }
273:
274: public void ejbActivate() {
275: }
276:
277: public void ejbPassivate() {
278: if (c != null) {
279: try {
280: c.close();
281: } catch (Exception e) {
282: Category.getInstance(getClass().getName()).info(
283: "Exception closing c: " + e);
284: } // end of try-catch
285: c = null;
286: } // end of if ()
287: }
288:
289: public void ejbLoad() {
290: id = (Integer) ctx.getPrimaryKey();
291: if (id == null) {
292: Category.getInstance(getClass().getName()).info("null id!");
293: } // end of if ()
294:
295: PreparedStatement ps = null;
296: try {
297:
298: ps = getConnection()
299: .prepareStatement(
300: "SELECT BALANCE, CUSTOMERID FROM CCBMPACCOUNT WHERE ID = ?");
301: if (ps == null) {
302: Category.getInstance(getClass().getName()).info(
303: "WFT? null ps!");
304: } // end of if ()
305:
306: ps.setInt(1, id.intValue());
307: ResultSet rs = ps.executeQuery();
308: if (rs.next() == false)
309: throw new NoSuchEntityException(
310: "Account does not exist " + id.toString());
311: this .balance = rs.getInt(1);
312: this .customerId = (Integer) rs.getObject(2);
313: } catch (Exception e) {
314: Category.getInstance(getClass().getName()).info(
315: "Exception in ejbLoad", e);
316: throw new EJBException(e);
317: } // end of try-catch
318: finally {
319: try {
320: if (ps != null) {
321: ps.close();
322: }
323: } catch (Exception e) {
324: Category.getInstance(getClass().getName()).info(
325: "Exception closing ps: " + e);
326: } // end of try-catch
327: } // end of finally
328: }
329:
330: public void ejbStore() {
331: PreparedStatement ps = null;
332: try {
333: ps = getConnection()
334: .prepareStatement(
335: "UPDATE CCBMPACCOUNT SET BALANCE = ?, CUSTOMERID = ? WHERE ID = ?");
336: ps.setInt(1, balance);
337: ps.setObject(2, customerId);
338: ps.setInt(3, id.intValue());
339: ps.execute();
340: } catch (Exception e) {
341: Category.getInstance(getClass().getName()).info(
342: "Exception in ejbStore", e);
343: throw new EJBException(e);
344: } // end of try-catch
345: finally {
346: try {
347: if (ps != null) {
348: ps.close();
349: }
350: } catch (Exception e) {
351: Category.getInstance(getClass().getName()).info(
352: "Exception closing ps: " + e);
353: } // end of try-catch
354: } // end of finally
355:
356: }
357:
358: public void ejbRemove() {
359: PreparedStatement ps = null;
360: try {
361: ps = getConnection().prepareStatement(
362: "DELETE FROM CCBMPACCOUNT WHERE ID = ?");
363: ps.setInt(1, id.intValue());
364: ps.execute();
365: } catch (Exception e) {
366: Category.getInstance(getClass().getName()).info(
367: "Exception in ejbRemove", e);
368: throw new EJBException(e);
369: } // end of try-catch
370: finally {
371: try {
372: if (ps != null) {
373: ps.close();
374: }
375: } catch (Exception e) {
376: Category.getInstance(getClass().getName()).info(
377: "Exception closing ps: " + e);
378: } // end of try-catch
379: } // end of finally
380: }
381:
382: public void setEntityContext(EntityContext ctx) {
383: this .ctx = ctx;
384: }
385:
386: public void unsetEntityContext() {
387: ctx = null;
388: }
389:
390: private Connection getConnection() throws Exception {
391: if (c == null) {
392: DataSource ds = (DataSource) new InitialContext()
393: .lookup("java:/DefaultDS");
394: c = ds.getConnection();
395:
396: } // end of if ()
397: return c;
398: }
399: }
|