KickJava   Java API By Example, From Geeks To Geeks.

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


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

61 public class CustomerBean
62    implements EntityBean JavaDoc
63 {
64
65    private Connection JavaDoc c;
66
67    public Integer JavaDoc id;
68    public String JavaDoc name;
69    public Collection JavaDoc accounts;
70
71    private EntityContext JavaDoc ctx;
72    
73    /**
74     * field get-set pair for field id
75     * Get the value of id
76     * @return value of id
77     *
78     * @ejb:interface-method
79     */

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

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

104    public String JavaDoc getName()
105    {
106       return name;
107    }
108    
109    /**
110     * Set the value of name
111     * @param name Value to assign to name
112     *
113     * @ejb:interface-method view-type="local"
114     */

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

129    public Collection JavaDoc getAccounts()
130    {
131       return accounts;
132    }
133    
134    
135
136    
137    /**
138     * Describe <code>addAccountLocal</code> method here.
139     *
140     * @param account an <code>AccountLocal</code> value
141     * @ejb:interface-method
142     */

143    public void addAccount(AccountLocal account)
144    {
145       try
146       {
147          account.setCustomerId(id);
148          accounts.add(account);
149       }
150       catch (Exception JavaDoc e)
151       {
152          throw new EJBException JavaDoc("Problem in addAccount: " + e);
153       }
154    }
155    
156    /**
157     * Describe <code>removeAccount</code> method here.
158     *
159     * @param acct an <code>AccountLocal</code> value
160     * @ejb:interface-method
161     */

162    public void removeAccount(AccountLocal account)
163    {
164       try
165       {
166          accounts.remove(account);
167          account.remove();
168       }
169       catch (Exception JavaDoc e)
170       {
171          throw new EJBException JavaDoc("Problem in removeAccount: " + e);
172       }
173
174    }
175    
176    // EntityHome implementation -------------------------------------
177
/**
178     * Describe <code>ejbCreate</code> method here.
179     *
180     * @param id an <code>Integer</code> value
181     * @param name a <code>String</code> value
182     * @return a <code>Integer</code> value
183     * @ejb:create-method
184     */

185    public Integer JavaDoc ejbCreate(Integer JavaDoc id, String JavaDoc name)
186       throws CreateException JavaDoc
187    {
188       setId(id);
189       setName(name);
190       PreparedStatement JavaDoc ps = null;
191       try
192       {
193       
194          ps = getConnection().prepareStatement("INSERT INTO CCBMPCUSTOMER (ID, NAME) VALUES (?, ?)");
195          ps.setInt(1, id.intValue());
196          ps.setString(2, name);
197          accounts = new ArrayList JavaDoc();
198       }
199       catch (Exception JavaDoc e)
200       {
201          throw new CreateException JavaDoc("Problem in ejbCreate: " + e);
202       } // end of try-catch
203
finally
204       {
205          try
206          {
207             ps.close();
208          }
209          catch (SQLException JavaDoc e)
210          {
211             Logger.getLogger(getClass().getName()).info("SQLException closing ps: " + e);
212          } // end of try-catch
213
} // end of finally
214
return id;
215    }
216
217    public void ejbPostCreate(Integer JavaDoc id, String JavaDoc name)
218    {
219    }
220
221    public Integer JavaDoc ejbFindByPrimaryKey(final Integer JavaDoc id)
222       throws FinderException JavaDoc
223    {
224       PreparedStatement JavaDoc ps = null;
225       try
226       {
227          ps = getConnection().prepareStatement("SELECT ID FROM CCBMPCUSTOMER WHERE ID = ?");
228          ps.setInt(1, id.intValue());
229          ResultSet JavaDoc rs = ps.executeQuery();
230          if (!rs.next())
231          {
232             throw new ObjectNotFoundException JavaDoc("No such customer: " + id);
233          } // end of if ()
234
rs.close();
235
236       }
237       catch (Exception JavaDoc e)
238       {
239          throw new EJBException JavaDoc("problem in findByPK: " + e);
240       } // end of try-catch
241
finally
242       {
243          try
244          {
245             ps.close();
246          }
247          catch (SQLException JavaDoc e)
248          {
249             Logger.getLogger(getClass().getName()).info("SQLException closing ps: " + e);
250          } // end of try-catch
251
} // end of finally
252
return id;
253    }
254    
255    public void ejbPostCreate(String JavaDoc id, String JavaDoc name)
256    {
257    }
258
259    public void ejbActivate()
260    {
261    }
262    
263    public void ejbPassivate()
264    {
265       if (c != null)
266       {
267          try
268          {
269             c.close();
270          }
271          catch (SQLException JavaDoc e)
272          {
273             Logger.getLogger(getClass().getName()).info("SQLException closing c: " + e);
274          } // end of try-catch
275
c = null;
276       } // end of if ()
277
}
278    
279    public void ejbLoad()
280    {
281       id = (Integer JavaDoc) ctx.getPrimaryKey();
282       if (id == null)
283          throw new EJBException JavaDoc("Null id!");
284
285       PreparedStatement JavaDoc ps = null;
286       try
287       {
288       
289          ps = getConnection().prepareStatement("SELECT NAME FROM CCBMPCUSTOMER WHERE ID = ?");
290          ps.setInt(1, id.intValue());
291          ResultSet JavaDoc rs = ps.executeQuery();
292          if (rs.next() == false)
293             throw new NoSuchEntityException JavaDoc("Customer does not exist " + id.toString());
294          this.name = rs.getString(1);
295          AccountLocalHome ah = (AccountLocalHome)new InitialContext JavaDoc().lookup("AccountLocal");
296          accounts = ah.findByCustomerId(id);
297
298       }
299       catch (Exception JavaDoc e)
300       {
301          throw new EJBException JavaDoc("Problem in ejbLoad: " + e);
302       } // end of try-catch
303
finally
304       {
305          try
306          {
307             ps.close();
308          }
309          catch (SQLException JavaDoc e)
310          {
311             Logger.getLogger(getClass().getName()).info("SQLException closing ps: " + e);
312          } // end of try-catch
313
} // end of finally
314
}
315    
316    public void ejbStore()
317    {
318       PreparedStatement JavaDoc ps = null;
319       try
320       {
321          ps = getConnection().prepareStatement("UPDATE CCBMPCUSTOMER SET NAME = ? WHERE ID = ?");
322          ps.setString(1, name);
323          ps.setInt(2, id.intValue());
324          ps.execute();
325       }
326       catch (Exception JavaDoc e)
327       {
328          throw new EJBException JavaDoc("Problem in ejbStore: " + e);
329       } // end of try-catch
330
finally
331       {
332          try
333          {
334             ps.close();
335          }
336          catch (SQLException JavaDoc e)
337          {
338             Logger.getLogger(getClass().getName()).info("SQLException closing ps: " + e);
339          } // end of try-catch
340
} // end of finally
341

342    }
343    
344    public void ejbRemove()
345    {
346       PreparedStatement JavaDoc ps = null;
347       try
348       {
349          ps = getConnection().prepareStatement("DELETE FROM CCBMPCUSTOMER WHERE ID = ?");
350          ps.setInt(1, id.intValue());
351          ps.execute();
352       }
353       catch (Exception JavaDoc e)
354       {
355          throw new EJBException JavaDoc("Problem in ejbRemove: " + e);
356       } // end of try-catch
357
finally
358       {
359          try
360          {
361             ps.close();
362          }
363          catch (SQLException JavaDoc e)
364          {
365             Logger.getLogger(getClass().getName()).info("SQLException closing ps: " + e);
366          } // end of try-catch
367
} // end of finally
368

369    }
370    
371    public void setEntityContext(EntityContext JavaDoc ctx)
372    {
373       this.ctx = ctx;
374    }
375    
376    public void unsetEntityContext()
377    {
378       ctx = null;
379    }
380
381    private Connection JavaDoc getConnection() throws Exception JavaDoc
382    {
383       if (c == null)
384       {
385          DataSource JavaDoc ds = (DataSource JavaDoc)new InitialContext JavaDoc().lookup("java:/DefaultDS");
386          c = ds.getConnection();
387          
388       } // end of if ()
389

390       return c;
391    }
392 }
393
394
Popular Tags