KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > bank > ejb > AccountBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.jca.bank.ejb;
23
24
25
26 import java.rmi.RemoteException JavaDoc;
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.util.Collection JavaDoc;
31 import javax.ejb.CreateException JavaDoc;
32 import javax.ejb.EJBException JavaDoc;
33 import javax.ejb.EntityBean JavaDoc;
34 import javax.ejb.EntityContext JavaDoc;
35 import javax.ejb.FinderException JavaDoc;
36 import javax.ejb.NoSuchEntityException JavaDoc;
37 import javax.ejb.ObjectNotFoundException JavaDoc;
38 import javax.sql.DataSource JavaDoc;
39
40 import javax.naming.InitialContext JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.sql.SQLException JavaDoc;
43
44 import org.jboss.logging.Logger;
45
46
47 /**
48  * Describe class <code>AccountBean</code> here.
49  *
50  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
51  * @version 1.0
52  * @ejb:bean name="Account"
53  * jndi-name="Account"
54  * local-jndi-name="LocalAccount"
55  * view-type="both"
56  * type="BMP"
57  * primkey-field="id"
58  * @ejb:pk class="java.lang.Integer"
59  */

60 public class AccountBean
61    implements EntityBean JavaDoc
62 {
63
64    private Connection JavaDoc c;
65
66    private Integer JavaDoc id;
67    private int balance;
68    private Integer JavaDoc customerId;
69
70    private EntityContext JavaDoc ctx;
71    
72    /**
73     * Abstract cmp2 field get-set pair for field id
74     * Get the value of id
75     * @return value of id
76     *
77     * @ejb:interface-method
78     */

79    public Integer JavaDoc getId()
80    {
81       return id;
82    }
83    
84    /**
85     * Set the value of id
86     * @param id Value to assign to id
87     *
88     * @ejb:interface-method
89     */

90    public void setId(final Integer JavaDoc id)
91    {
92       this.id = id;
93    }
94    
95    
96    
97    /**
98     * field get-set pair for field balance
99     * Get the value of balance
100     * @return value of balance
101     *
102     * @ejb:interface-method
103     */

104    public int getBalance()
105    {
106       return balance;
107    }
108    
109    /**
110     * Set the value of balance
111     * @param balance Value to assign to balance
112     *
113     * @ejb:interface-method
114     */

115    public void setBalance(final int balance)
116    {
117       this.balance = balance;
118    }
119    
120    
121    
122    /**
123     * field get-set pair for field customerId
124     * Get the value of customerId
125     * @return value of customerId
126     *
127     * @ejb:interface-method
128     */

129    public Integer JavaDoc getCustomerId()
130    {
131       return customerId;
132    }
133    
134    /**
135     * Set the value of customerId
136     * @param customerId Value to assign to customerId
137     *
138     * @ejb:interface-method
139     */

140    public void setCustomerId(final Integer JavaDoc customerId)
141    {
142       this.customerId = customerId;
143    }
144    
145
146
147
148    /**
149     * Describe <code>deposit</code> method here.
150     *
151     * @param amount an <code>int</code> value
152     * @ejb:interface-method
153     */

154    public void deposit(int amount)
155    {
156       setBalance(getBalance()+amount);
157    }
158    
159    /**
160     * Describe <code>withdraw</code> method here.
161     *
162     * @param amount an <code>int</code> value
163     * @ejb:interface-method
164     */

165    public void withdraw(int amount)
166    {
167       setBalance(getBalance()-amount);
168    }
169
170
171    //ENTITY methods
172
/**
173     * Describe <code>ejbCreate</code> method here.
174     *
175     * @param id an <code>Integer</code> value
176     * @param balance an <code>int</code> value
177     * @param customerId an <code>Integer</code> value
178     * @return an <code>Integer</code> value
179     * @ejb:create-method
180     */

181    public Integer JavaDoc ejbCreate(final Integer JavaDoc id, final int balance, final Integer JavaDoc customerId)
182       throws CreateException JavaDoc
183    {
184       setId(id);
185       setBalance(balance);
186       setCustomerId(customerId);
187       PreparedStatement JavaDoc ps = null;
188       try
189       {
190       
191          ps = getConnection().prepareStatement("INSERT INTO CCBMPACCOUNT (ID, BALANCE, CUSTOMERID) VALUES (?, ?, ?)");
192          ps.setInt(1, id.intValue());
193          ps.setInt(2, balance);
194          ps.setObject(3, customerId);
195       }
196       catch (Exception JavaDoc e)
197       {
198          Logger.getLogger(getClass().getName()).info("Exception in ejbCreate", e);
199          throw new CreateException JavaDoc("Can't insert: " + e);
200       } // end of try-catch
201
finally
202       {
203          try
204          {
205             if (ps != null) {ps.close();}
206          }
207          catch (Exception JavaDoc e)
208          {
209             Logger.getLogger(getClass().getName()).info("Exception closing ps: " + e);
210          } // end of try-catch
211

212       } // end of finally
213
return id;
214    }
215    
216    public void ejbPostCreate(final Integer JavaDoc id, final int balance, final Integer JavaDoc customerId)
217    {
218    }
219
220    /**
221     * Describe <code>ejbFindByPrimaryKey</code> method here.
222     *
223     * @param id an <code>Integer</code> value
224     * @return an <code>Integer</code> value
225     * @exception FinderException if an error occurs
226     * @ejb:finder
227     */

228    public Integer JavaDoc ejbFindByPrimaryKey(final Integer JavaDoc id)
229       throws FinderException JavaDoc
230    {
231       PreparedStatement JavaDoc ps = null;
232       try
233       {
234          ps = getConnection().prepareStatement("SELECT ID FROM CCBMPACCOUNT WHERE ID = ?");
235          ps.setInt(1, id.intValue());
236          ResultSet JavaDoc rs = ps.executeQuery();
237          if (!rs.next())
238          {
239             throw new ObjectNotFoundException JavaDoc("No such account: " + id);
240          } // end of if ()
241
rs.close();
242
243       }
244       catch (Exception JavaDoc e)
245       {
246          Logger.getLogger(getClass().getName()).info("Exception in findByPK", e);
247          throw new EJBException JavaDoc("Problem in findByPrimaryKey: " + e);
248       } // end of try-catch
249
finally
250       {
251          try
252          {
253             if (ps != null) {ps.close();}
254          }
255          catch (Exception JavaDoc e)
256          {
257             Logger.getLogger(getClass().getName()).info("Exception closing ps: " + e);
258          } // end of try-catch
259
} // end of finally
260
return id;
261    }
262
263    /**
264     * Describe <code>ejbFindByCustomerId</code> method here.
265     *
266     * @param customerId an <code>Integer</code> value
267     * @return a <code>Collection</code> value
268     * @ejb:finder-method
269     */

270    public Collection JavaDoc ejbFindByCustomerId(final Integer JavaDoc customerId)
271    {
272       PreparedStatement JavaDoc ps = null;
273       try
274       {
275          ps = getConnection().prepareStatement("SELECT ID FROM CCBMPACCOUNT WHERE CUSTOMERID = ?");
276          ps.setInt(1, customerId.intValue());
277          ResultSet JavaDoc rs = ps.executeQuery();
278          Collection JavaDoc result = new ArrayList JavaDoc();
279          while (rs.next())
280          {
281             result.add(new Integer JavaDoc(rs.getInt(1)));
282          } // end of if ()
283
rs.close();
284             return result;
285       }
286       catch (Exception JavaDoc e)
287       {
288          Logger.getLogger(getClass().getName()).info("Exception in findbyCustomerID", e);
289          throw new EJBException JavaDoc(e);
290       } // end of try-catch
291
finally
292       {
293          try
294          {
295             if (ps != null) {ps.close();}
296          }
297          catch (Exception JavaDoc e)
298          {
299             Logger.getLogger(getClass().getName()).info("Exception closing ps: " + e);
300          } // end of try-catch
301
} // end of finally
302
}
303
304    public void ejbActivate()
305    {
306    }
307    
308    public void ejbPassivate()
309    {
310       if (c != null)
311       {
312          try
313          {
314             c.close();
315          }
316          catch (Exception JavaDoc e)
317          {
318             Logger.getLogger(getClass().getName()).info("Exception closing c: " + e);
319          } // end of try-catch
320
c = null;
321       } // end of if ()
322
}
323    
324    public void ejbLoad()
325    {
326       id = (Integer JavaDoc) ctx.getPrimaryKey();
327       if (id == null)
328       {
329          Logger.getLogger(getClass().getName()).info("null id!");
330       } // end of if ()
331

332       PreparedStatement JavaDoc ps = null;
333       try
334       {
335       
336          ps = getConnection().prepareStatement("SELECT BALANCE, CUSTOMERID FROM CCBMPACCOUNT WHERE ID = ?");
337          if (ps == null)
338          {
339             Logger.getLogger(getClass().getName()).info("WFT? null ps!");
340          } // end of if ()
341

342          ps.setInt(1, id.intValue());
343          ResultSet JavaDoc rs = ps.executeQuery();
344          if (rs.next() == false)
345             throw new NoSuchEntityException JavaDoc("Account does not exist " + id.toString());
346          this.balance = rs.getInt(1);
347          this.customerId = (Integer JavaDoc)rs.getObject(2);
348       }
349       catch (Exception JavaDoc e)
350       {
351          Logger.getLogger(getClass().getName()).info("Exception in ejbLoad", e);
352          throw new EJBException JavaDoc(e);
353       } // end of try-catch
354
finally
355       {
356          try
357          {
358             if (ps != null) {ps.close();}
359          }
360          catch (Exception JavaDoc e)
361          {
362             Logger.getLogger(getClass().getName()).info("Exception closing ps: " + e);
363          } // end of try-catch
364
} // end of finally
365
}
366    
367    public void ejbStore()
368    {
369       PreparedStatement JavaDoc ps = null;
370       try
371       {
372          ps = getConnection().prepareStatement("UPDATE CCBMPACCOUNT SET BALANCE = ?, CUSTOMERID = ? WHERE ID = ?");
373          ps.setInt(1, balance);
374          ps.setObject(2, customerId);
375          ps.setInt(3, id.intValue());
376          ps.execute();
377       }
378       catch (Exception JavaDoc e)
379       {
380          Logger.getLogger(getClass().getName()).info("Exception in ejbStore", e);
381          throw new EJBException JavaDoc(e);
382       } // end of try-catch
383
finally
384       {
385          try
386          {
387             if (ps != null) {ps.close();}
388          }
389          catch (Exception JavaDoc e)
390          {
391             Logger.getLogger(getClass().getName()).info("Exception closing ps: " + e);
392          } // end of try-catch
393
} // end of finally
394

395    }
396    
397    public void ejbRemove()
398    {
399       PreparedStatement JavaDoc ps = null;
400       try
401       {
402          ps = getConnection().prepareStatement("DELETE FROM CCBMPACCOUNT WHERE ID = ?");
403          ps.setInt(1, id.intValue());
404          ps.execute();
405       }
406       catch (Exception JavaDoc e)
407       {
408          Logger.getLogger(getClass().getName()).info("Exception in ejbRemove", e);
409          throw new EJBException JavaDoc(e);
410       } // end of try-catch
411
finally
412       {
413          try
414          {
415             if (ps != null) {ps.close();}
416          }
417          catch (Exception JavaDoc e)
418          {
419             Logger.getLogger(getClass().getName()).info("Exception closing ps: " + e);
420          } // end of try-catch
421
} // end of finally
422
}
423    
424    public void setEntityContext(EntityContext JavaDoc ctx)
425    {
426       this.ctx = ctx;
427    }
428    
429    public void unsetEntityContext()
430    {
431       ctx = null;
432    }
433
434    private Connection JavaDoc getConnection() throws Exception JavaDoc
435    {
436       if (c == null)
437       {
438          DataSource JavaDoc ds = (DataSource JavaDoc)new InitialContext JavaDoc().lookup("java:/DefaultDS");
439          c = ds.getConnection();
440          
441       } // end of if ()
442
return c;
443    }
444 }
445
446
Popular Tags