1 22 package org.jboss.test.bmp.beans; 23 24 import java.rmi.RemoteException ; 25 import java.sql.Connection ; 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.Statement ; 29 import java.util.Collection ; 30 import java.util.Vector ; 31 32 import javax.ejb.CreateException ; 33 import javax.ejb.DuplicateKeyException ; 34 import javax.ejb.EJBException ; 35 import javax.ejb.EntityBean ; 36 import javax.ejb.EntityContext ; 37 import javax.ejb.FinderException ; 38 import javax.naming.InitialContext ; 39 import javax.naming.NamingException ; 40 import javax.sql.DataSource ; 41 42 import org.jboss.logging.Logger; 43 44 public class SimpleBMPBean implements EntityBean 45 { 46 47 private static final long serialVersionUID = 1L; 48 49 Logger log = Logger.getLogger(getClass()); 50 EntityContext ctx = null; 51 DataSource ds = null; 52 53 Integer id; 55 String name; 56 57 public Integer ejbCreate (int _id, String _name) 58 throws CreateException , RemoteException 59 { 60 log.debug ("ejbCreate (int, String) called"); 61 62 id = new Integer (_id); 63 64 boolean dublicate = false; 65 66 Connection con = null; 67 try 68 { 69 con = ds.getConnection (); 70 Statement s = con.createStatement (); 71 ResultSet 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 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 _e) 88 { 89 throw new EJBException ("couldnt create: "+_e.getMessage ()); 90 } 91 finally 92 { 93 try 94 { 95 if (con != null) 96 con.close (); 97 } 98 catch (Exception _sqle) 99 { 100 } 101 } 102 103 if (dublicate) 104 throw new DuplicateKeyException ("Bean with id="+_id+" already exists."); 105 106 return id; 107 } 108 109 public Integer ejbCreateMETHOD (int _id, String _name) 110 throws CreateException , RemoteException 111 { 112 log.debug ("ejbCreateMETHOD (int, String) called"); 113 114 id = new Integer (_id); 115 116 boolean dublicate = false; 117 118 Connection con = null; 119 try 120 { 121 con = ds.getConnection (); 122 Statement s = con.createStatement (); 123 ResultSet 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 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 _e) 140 { 141 throw new EJBException ("couldnt create: "+_e.getMessage ()); 142 } 143 finally 144 { 145 try 146 { 147 if (con != null) 148 con.close (); 149 } 150 catch (Exception _sqle) 151 { 152 } 153 } 154 155 if (dublicate) 156 throw new DuplicateKeyException ("Bean with id="+_id+" already exists."); 157 158 return id; 159 } 160 161 public void ejbPostCreate (int _id, String _name) 162 throws CreateException , RemoteException 163 { 164 log.debug ("ejbPostCreate (int, String) called"); 165 } 166 167 public void ejbPostCreateMETHOD (int _id, String _name) 168 throws CreateException , RemoteException 169 { 170 log.debug ("ejbPostCreateMETHOD (int, String) called"); 171 } 172 173 public void ejbLoad () 174 { 175 log.debug ("ejbLoad () called " + this); 176 177 Connection con = null; 178 try 179 { 180 con = ds.getConnection (); 181 PreparedStatement ps = con.prepareStatement ("SELECT id,name FROM simplebean WHERE id=?"); 182 ps.setInt (1, ((Integer )ctx.getPrimaryKey ()).intValue ()); 183 ResultSet rs = ps.executeQuery (); 184 if (rs.next ()) 185 { 186 id = new Integer (rs.getInt ("id")); 187 name = rs.getString ("name"); 188 } 189 rs.close (); 190 ps.close (); 191 } 192 catch (Exception _e) 193 { 194 throw new EJBException ("couldnt load: "+_e.getMessage ()); 195 } 196 finally 197 { 198 try 199 { 200 if (con != null) 201 con.close (); 202 } 203 catch (Exception _sqle) 204 { 205 } 206 } 207 } 208 209 public void ejbStore () 210 { 211 log.debug ("ejbStore () called " + this); 212 213 Connection con = null; 214 try 215 { 216 con = ds.getConnection (); 217 PreparedStatement 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 _e) 224 { 225 throw new EJBException ("couldnt store: "+_e.getMessage ()); 226 } 227 finally 228 { 229 try 230 { 231 if (con != null) 232 con.close (); 233 } 234 catch (Exception _sqle) 235 { 236 } 237 } 238 } 239 240 public void ejbRemove () 241 { 242 log.debug ("ejbRemove () called " + this); 243 244 Connection con = null; 245 try 246 { 247 con = ds.getConnection (); 248 PreparedStatement ps = con.prepareStatement ("DELETE FROM simplebean WHERE id=?"); 249 ps.setInt (1, id.intValue ()); 250 ps.execute (); 251 ps.close (); 252 } 253 catch (Exception _e) 254 { 255 throw new EJBException ("couldnt remove: "+_e.getMessage ()); 256 } 257 finally 258 { 259 try 260 { 261 if (con != null) 262 con.close (); 263 } 264 catch (Exception _sqle) 265 { 266 } 267 } 268 } 269 270 271 public Integer ejbFindByPrimaryKey (Integer _key) throws FinderException 272 { 273 log.debug ("ejbFindByPrimaryKey (Integer) called " + this); 274 275 Connection con = null; 276 boolean found = false; 277 try 278 { 279 con = ds.getConnection (); 280 PreparedStatement ps = con.prepareStatement ("SELECT id FROM simplebean WHERE id=?"); 281 ps.setInt (1, _key.intValue ()); 282 ResultSet rs = ps.executeQuery (); 283 found = rs.next (); 284 rs.close (); 285 ps.close (); 286 } 287 catch (Exception _e) 288 { 289 throw new EJBException ("couldnt seek: "+_e.getMessage ()); 290 } 291 finally 292 { 293 294 try 295 { 296 if (con != null) 297 con.close (); 298 } 299 catch (Exception _sqle) 300 { 301 } 302 } 303 if (!found) 304 throw new FinderException ("No bean with id="+_key+" found."); 305 306 return _key; 307 } 308 309 public Collection ejbFindAll () throws FinderException 310 { 311 log.debug ("ejbFindAll () called"); 312 313 Connection con = null; 314 Vector result = new Vector (); 315 try 316 { 317 con = ds.getConnection (); 318 Statement s = con.createStatement (); 319 ResultSet rs = s.executeQuery ("SELECT id FROM simplebean"); 320 while (rs.next ()) 321 { 322 result.add (new Integer (rs.getInt ("id"))); 323 } 324 rs.close (); 325 s.close (); 326 } 327 catch (Exception _e) 328 { 329 throw new EJBException ("couldnt seek: "+_e.getMessage ()); 330 } 331 finally 332 { 333 334 try 335 { 336 if (con != null) 337 con.close (); 338 } 339 catch (Exception _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 ("ST")); 357 } 358 359 public void setEntityContext (EntityContext _ctx) 360 { 361 log.debug ("setEntityContext() called " + this); 362 363 ctx = _ctx; 364 try 366 { 367 ds = (DataSource )new InitialContext ().lookup ("java:comp/env/datasource"); 368 } 369 catch (NamingException _ne) 370 { 371 throw new EJBException ("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 384 public Integer getIdViaEJBObject() 385 { 386 try 387 { 388 Integer result = (Integer ) ctx.getEJBObject().getPrimaryKey(); 389 log.debug(result + " " + ctx.getPrimaryKey()); 390 return result; 391 } 392 catch (RemoteException e) 393 { 394 throw new EJBException (e); 395 } 396 } 397 398 public void setName (String _name) 399 { 400 name = _name; 401 } 402 403 public String getName () 404 { 405 return name; 406 } 407 408 409 } 410 | Popular Tags |