KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > bmp > beans > SimpleBMPBean


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.bmp.beans;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Vector JavaDoc;
31
32 import javax.ejb.CreateException JavaDoc;
33 import javax.ejb.DuplicateKeyException JavaDoc;
34 import javax.ejb.EJBException JavaDoc;
35 import javax.ejb.EntityBean JavaDoc;
36 import javax.ejb.EntityContext JavaDoc;
37 import javax.ejb.FinderException JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40 import javax.sql.DataSource JavaDoc;
41
42 import org.jboss.logging.Logger;
43
44 public class SimpleBMPBean implements EntityBean JavaDoc
45 {
46    /** The serialVersionUID */
47    private static final long serialVersionUID = 1L;
48
49    Logger log = Logger.getLogger(getClass());
50    EntityContext JavaDoc ctx = null;
51    DataSource JavaDoc ds = null;
52
53    // bmp fields
54
Integer JavaDoc id;
55    String JavaDoc name;
56    
57    public Integer JavaDoc ejbCreate (int _id, String JavaDoc _name)
58       throws CreateException JavaDoc, RemoteException JavaDoc
59    {
60       log.debug ("ejbCreate (int, String) called");
61       
62       id = new Integer JavaDoc (_id);
63       
64       boolean dublicate = false;
65       
66       Connection JavaDoc con = null;
67       try
68       {
69          con = ds.getConnection ();
70          Statement JavaDoc s = con.createStatement ();
71          ResultSet JavaDoc rs = s.executeQuery ("SELECT id FROM simplebean WHERE id=" + id.toString ());
72          dublicate = rs.next ();
73          rs.close ();
74          s.close ();
75          
76          if (!dublicate)
77          {
78             PreparedStatement JavaDoc ps = con.prepareStatement ("INSERT INTO simplebean VALUES (?,?)");
79             ps.setInt (1, _id);
80             ps.setString (2, _name);
81             ps.execute ();
82             ps.close ();
83
84             name = _name;
85          }
86       }
87       catch (Exception JavaDoc _e)
88       {
89          throw new EJBException JavaDoc ("couldnt create: "+_e.getMessage ());
90       }
91       finally
92       {
93          try
94          {
95             if (con != null)
96                con.close ();
97          }
98          catch (Exception JavaDoc _sqle)
99          {
100          }
101       }
102       
103       if (dublicate)
104          throw new DuplicateKeyException JavaDoc ("Bean with id="+_id+" already exists.");
105
106       return id;
107    }
108    
109    public Integer JavaDoc ejbCreateMETHOD (int _id, String JavaDoc _name)
110       throws CreateException JavaDoc, RemoteException JavaDoc
111    {
112       log.debug ("ejbCreateMETHOD (int, String) called");
113       
114       id = new Integer JavaDoc (_id);
115       
116       boolean dublicate = false;
117       
118       Connection JavaDoc con = null;
119       try
120       {
121          con = ds.getConnection ();
122          Statement JavaDoc s = con.createStatement ();
123          ResultSet JavaDoc rs = s.executeQuery ("SELECT id FROM simplebean WHERE id=" + id.toString ());
124          dublicate = rs.next ();
125          rs.close ();
126          s.close ();
127          
128          if (!dublicate)
129          {
130             PreparedStatement JavaDoc ps = con.prepareStatement ("INSERT INTO simplebean VALUES (?,?)");
131             ps.setInt (1, _id);
132             ps.setString (2, _name);
133             ps.execute ();
134             ps.close ();
135
136             name = _name;
137          }
138       }
139       catch (Exception JavaDoc _e)
140       {
141          throw new EJBException JavaDoc ("couldnt create: "+_e.getMessage ());
142       }
143       finally
144       {
145          try
146          {
147             if (con != null)
148                con.close ();
149          }
150          catch (Exception JavaDoc _sqle)
151          {
152          }
153       }
154       
155       if (dublicate)
156          throw new DuplicateKeyException JavaDoc ("Bean with id="+_id+" already exists.");
157
158       return id;
159    }
160    
161    public void ejbPostCreate (int _id, String JavaDoc _name)
162       throws CreateException JavaDoc, RemoteException JavaDoc
163    {
164       log.debug ("ejbPostCreate (int, String) called");
165    }
166    
167    public void ejbPostCreateMETHOD (int _id, String JavaDoc _name)
168       throws CreateException JavaDoc, RemoteException JavaDoc
169    {
170       log.debug ("ejbPostCreateMETHOD (int, String) called");
171    }
172    
173    public void ejbLoad ()
174    {
175       log.debug ("ejbLoad () called " + this);
176       
177       Connection JavaDoc con = null;
178       try
179       {
180          con = ds.getConnection ();
181          PreparedStatement JavaDoc ps = con.prepareStatement ("SELECT id,name FROM simplebean WHERE id=?");
182          ps.setInt (1, ((Integer JavaDoc)ctx.getPrimaryKey ()).intValue ());
183          ResultSet JavaDoc rs = ps.executeQuery ();
184          if (rs.next ())
185          {
186             id = new Integer JavaDoc (rs.getInt ("id"));
187             name = rs.getString ("name");
188          }
189          rs.close ();
190          ps.close ();
191       }
192       catch (Exception JavaDoc _e)
193       {
194          throw new EJBException JavaDoc ("couldnt load: "+_e.getMessage ());
195       }
196       finally
197       {
198          try
199          {
200             if (con != null)
201                con.close ();
202          }
203          catch (Exception JavaDoc _sqle)
204          {
205          }
206       }
207    }
208    
209    public void ejbStore ()
210    {
211       log.debug ("ejbStore () called " + this);
212
213       Connection JavaDoc con = null;
214       try
215       {
216          con = ds.getConnection ();
217          PreparedStatement JavaDoc ps = con.prepareStatement ("UPDATE simplebean SET name=? WHERE id=?");
218          ps.setString (1, name);
219          ps.setInt (2, id.intValue ());
220          ps.execute ();
221          ps.close ();
222       }
223       catch (Exception JavaDoc _e)
224       {
225          throw new EJBException JavaDoc ("couldnt store: "+_e.getMessage ());
226       }
227       finally
228       {
229          try
230          {
231             if (con != null)
232                con.close ();
233          }
234          catch (Exception JavaDoc _sqle)
235          {
236          }
237       }
238    }
239    
240    public void ejbRemove ()
241    {
242       log.debug ("ejbRemove () called " + this);
243
244       Connection JavaDoc con = null;
245       try
246       {
247          con = ds.getConnection ();
248          PreparedStatement JavaDoc ps = con.prepareStatement ("DELETE FROM simplebean WHERE id=?");
249          ps.setInt (1, id.intValue ());
250          ps.execute ();
251          ps.close ();
252       }
253       catch (Exception JavaDoc _e)
254       {
255          throw new EJBException JavaDoc ("couldnt remove: "+_e.getMessage ());
256       }
257       finally
258       {
259          try
260          {
261             if (con != null)
262                con.close ();
263          }
264          catch (Exception JavaDoc _sqle)
265          {
266          }
267       }
268    }
269
270    
271    public Integer JavaDoc ejbFindByPrimaryKey (Integer JavaDoc _key) throws FinderException JavaDoc
272    {
273       log.debug ("ejbFindByPrimaryKey (Integer) called " + this);
274
275       Connection JavaDoc con = null;
276       boolean found = false;
277       try
278       {
279          con = ds.getConnection ();
280          PreparedStatement JavaDoc ps = con.prepareStatement ("SELECT id FROM simplebean WHERE id=?");
281          ps.setInt (1, _key.intValue ());
282          ResultSet JavaDoc rs = ps.executeQuery ();
283          found = rs.next ();
284          rs.close ();
285          ps.close ();
286       }
287       catch (Exception JavaDoc _e)
288       {
289          throw new EJBException JavaDoc ("couldnt seek: "+_e.getMessage ());
290       }
291       finally
292       {
293          
294          try
295          {
296             if (con != null)
297                con.close ();
298          }
299          catch (Exception JavaDoc _sqle)
300          {
301          }
302       }
303       if (!found)
304          throw new FinderException JavaDoc ("No bean with id="+_key+" found.");
305       
306       return _key;
307    }
308
309    public Collection JavaDoc ejbFindAll () throws FinderException JavaDoc
310    {
311       log.debug ("ejbFindAll () called");
312
313       Connection JavaDoc con = null;
314       Vector JavaDoc result = new Vector JavaDoc ();
315       try
316       {
317          con = ds.getConnection ();
318          Statement JavaDoc s = con.createStatement ();
319          ResultSet JavaDoc rs = s.executeQuery ("SELECT id FROM simplebean");
320          while (rs.next ())
321          {
322             result.add (new Integer JavaDoc (rs.getInt ("id")));
323          }
324          rs.close ();
325          s.close ();
326       }
327       catch (Exception JavaDoc _e)
328       {
329          throw new EJBException JavaDoc ("couldnt seek: "+_e.getMessage ());
330       }
331       finally
332       {
333          
334          try
335          {
336             if (con != null)
337                con.close ();
338          }
339          catch (Exception JavaDoc _sqle)
340          {
341          }
342       }
343       
344       return result;
345    }
346
347
348    
349    public void ejbActivate ()
350    {
351       log.debug ("ejbActivate () called " + this);
352    }
353    
354    public void ejbPassivate ()
355    {
356       log.debug ("ejbPassivate () called " + this, new Exception JavaDoc("ST"));
357    }
358    
359    public void setEntityContext (EntityContext JavaDoc _ctx)
360    {
361       log.debug ("setEntityContext() called " + this);
362
363       ctx = _ctx;
364       // lookup the datasource
365
try
366       {
367          ds = (DataSource JavaDoc)new InitialContext JavaDoc ().lookup ("java:comp/env/datasource");
368       }
369       catch (NamingException JavaDoc _ne)
370       {
371          throw new EJBException JavaDoc ("Datasource not found: "+_ne.getMessage ());
372       }
373    }
374    
375    public void unsetEntityContext ()
376    {
377       log.debug ("unsetEntityContext () called");
378
379       ctx = null;
380    }
381    
382    // business methods ---------------------------------------------------------------
383

384    public Integer JavaDoc getIdViaEJBObject()
385    {
386       try
387       {
388          Integer JavaDoc result = (Integer JavaDoc) ctx.getEJBObject().getPrimaryKey();
389          log.debug(result + " " + ctx.getPrimaryKey());
390          return result;
391       }
392       catch (RemoteException JavaDoc e)
393       {
394          throw new EJBException JavaDoc(e);
395       }
396    }
397    
398    public void setName (String JavaDoc _name)
399    {
400       name = _name;
401    }
402    
403    public String JavaDoc getName ()
404    {
405       return name;
406    }
407    
408    
409 }
410
Popular Tags