1 22 package org.jboss.test.jca.bank.ejb; 23 24 25 import java.util.Collection ; 26 import java.sql.Connection ; 27 import java.sql.PreparedStatement ; 28 import java.sql.ResultSet ; 29 import javax.sql.DataSource ; 30 import java.sql.SQLException ; 31 32 import javax.ejb.ObjectNotFoundException ; 33 import javax.ejb.CreateException ; 34 import javax.ejb.EJBException ; 35 import javax.ejb.EntityBean ; 36 import javax.ejb.EntityContext ; 37 import javax.ejb.EntityContext ; 38 import javax.ejb.FinderException ; 39 import javax.ejb.NoSuchEntityException ; 40 import javax.naming.InitialContext ; 41 import java.util.ArrayList ; 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 61 public class CustomerBean 62 implements EntityBean 63 { 64 65 private Connection c; 66 67 public Integer id; 68 public String name; 69 public Collection accounts; 70 71 private EntityContext ctx; 72 73 80 public Integer getId() 81 { 82 return id; 83 } 84 85 91 public void setId(final Integer id) 92 { 93 this.id = id; 94 } 95 96 97 104 public String getName() 105 { 106 return name; 107 } 108 109 115 public void setName(final String name) 116 { 117 this.name = name; 118 } 119 120 121 122 129 public Collection getAccounts() 130 { 131 return accounts; 132 } 133 134 135 136 137 143 public void addAccount(AccountLocal account) 144 { 145 try 146 { 147 account.setCustomerId(id); 148 accounts.add(account); 149 } 150 catch (Exception e) 151 { 152 throw new EJBException ("Problem in addAccount: " + e); 153 } 154 } 155 156 162 public void removeAccount(AccountLocal account) 163 { 164 try 165 { 166 accounts.remove(account); 167 account.remove(); 168 } 169 catch (Exception e) 170 { 171 throw new EJBException ("Problem in removeAccount: " + e); 172 } 173 174 } 175 176 185 public Integer ejbCreate(Integer id, String name) 186 throws CreateException 187 { 188 setId(id); 189 setName(name); 190 PreparedStatement 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 (); 198 } 199 catch (Exception e) 200 { 201 throw new CreateException ("Problem in ejbCreate: " + e); 202 } finally 204 { 205 try 206 { 207 ps.close(); 208 } 209 catch (SQLException e) 210 { 211 Logger.getLogger(getClass().getName()).info("SQLException closing ps: " + e); 212 } } return id; 215 } 216 217 public void ejbPostCreate(Integer id, String name) 218 { 219 } 220 221 public Integer ejbFindByPrimaryKey(final Integer id) 222 throws FinderException 223 { 224 PreparedStatement ps = null; 225 try 226 { 227 ps = getConnection().prepareStatement("SELECT ID FROM CCBMPCUSTOMER WHERE ID = ?"); 228 ps.setInt(1, id.intValue()); 229 ResultSet rs = ps.executeQuery(); 230 if (!rs.next()) 231 { 232 throw new ObjectNotFoundException ("No such customer: " + id); 233 } rs.close(); 235 236 } 237 catch (Exception e) 238 { 239 throw new EJBException ("problem in findByPK: " + e); 240 } finally 242 { 243 try 244 { 245 ps.close(); 246 } 247 catch (SQLException e) 248 { 249 Logger.getLogger(getClass().getName()).info("SQLException closing ps: " + e); 250 } } return id; 253 } 254 255 public void ejbPostCreate(String id, String 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 e) 272 { 273 Logger.getLogger(getClass().getName()).info("SQLException closing c: " + e); 274 } c = null; 276 } } 278 279 public void ejbLoad() 280 { 281 id = (Integer ) ctx.getPrimaryKey(); 282 if (id == null) 283 throw new EJBException ("Null id!"); 284 285 PreparedStatement ps = null; 286 try 287 { 288 289 ps = getConnection().prepareStatement("SELECT NAME FROM CCBMPCUSTOMER WHERE ID = ?"); 290 ps.setInt(1, id.intValue()); 291 ResultSet rs = ps.executeQuery(); 292 if (rs.next() == false) 293 throw new NoSuchEntityException ("Customer does not exist " + id.toString()); 294 this.name = rs.getString(1); 295 AccountLocalHome ah = (AccountLocalHome)new InitialContext ().lookup("AccountLocal"); 296 accounts = ah.findByCustomerId(id); 297 298 } 299 catch (Exception e) 300 { 301 throw new EJBException ("Problem in ejbLoad: " + e); 302 } finally 304 { 305 try 306 { 307 ps.close(); 308 } 309 catch (SQLException e) 310 { 311 Logger.getLogger(getClass().getName()).info("SQLException closing ps: " + e); 312 } } } 315 316 public void ejbStore() 317 { 318 PreparedStatement 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 e) 327 { 328 throw new EJBException ("Problem in ejbStore: " + e); 329 } finally 331 { 332 try 333 { 334 ps.close(); 335 } 336 catch (SQLException e) 337 { 338 Logger.getLogger(getClass().getName()).info("SQLException closing ps: " + e); 339 } } 342 } 343 344 public void ejbRemove() 345 { 346 PreparedStatement 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 e) 354 { 355 throw new EJBException ("Problem in ejbRemove: " + e); 356 } finally 358 { 359 try 360 { 361 ps.close(); 362 } 363 catch (SQLException e) 364 { 365 Logger.getLogger(getClass().getName()).info("SQLException closing ps: " + e); 366 } } 369 } 370 371 public void setEntityContext(EntityContext ctx) 372 { 373 this.ctx = ctx; 374 } 375 376 public void unsetEntityContext() 377 { 378 ctx = null; 379 } 380 381 private Connection getConnection() throws Exception 382 { 383 if (c == null) 384 { 385 DataSource ds = (DataSource )new InitialContext ().lookup("java:/DefaultDS"); 386 c = ds.getConnection(); 387 388 } 390 return c; 391 } 392 } 393 394 | Popular Tags |