1 27 28 package bank; 29 30 import java.math.BigDecimal ; 31 import java.sql.Connection ; 32 import java.sql.PreparedStatement ; 33 import java.sql.ResultSet ; 34 import java.sql.SQLException ; 35 import java.util.ArrayList ; 36 import java.util.Collection ; 37 import java.util.Iterator ; 38 import javax.ejb.*; 39 40 45 public class SavingsAccountBean implements EntityBean, SavingsAccountRemoteBusiness { 46 private EntityContext context; 47 private Connection con; 48 private String id; 49 private String firstName; 50 private String lastName; 51 private BigDecimal balance; 52 53 60 public void setEntityContext(EntityContext aContext) { 61 context = aContext; 62 } 63 64 67 public void ejbActivate() { 68 id = (String )context.getPrimaryKey(); 69 } 70 71 74 public void ejbPassivate() { 75 id = null; 76 } 77 78 81 public void ejbRemove() { 82 try{ 83 deleteRow(id); 84 }catch(SQLException ex){ 85 throw new EJBException("ejbRemove: " + ex.getMessage()); 86 } 87 } 88 89 92 public void unsetEntityContext() { 93 context = null; 94 } 95 96 99 public void ejbLoad() { 100 try{ 102 loadRow(); 103 }catch(SQLException ex){ 104 throw new EJBException("ejbLoad() " + ex.getMessage()); 105 } 106 107 } 108 109 112 public void ejbStore() { 113 try{ 115 storeRow(); 116 }catch(SQLException ex){ 117 throw new EJBException("ejbStore " + ex.getMessage()); 118 } 119 } 120 121 123 126 public java.lang.String ejbFindByPrimaryKey(java.lang.String aKey) throws FinderException { 127 boolean result; 131 try{ 132 result = selectByPrimaryKey(aKey); 133 }catch(SQLException ex){ 134 throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); 135 } 136 if(result){ 137 return aKey; 138 }else{ 139 throw new ObjectNotFoundException("Account for id " + aKey + " not found."); 140 } 141 } 142 143 public java.lang.String ejbCreate(java.lang.String id, java.lang.String firstName, java.lang.String lastName, java.math.BigDecimal balance) throws CreateException { 144 if(balance.signum() == -1){ 146 throw new CreateException("A negative initial balance is not allowed."); 147 } 148 try{ 149 insertRow(id, firstName, lastName, balance); 150 }catch(SQLException ex){ 151 throw new EJBException("ejbCreate: " + ex.getMessage()); 152 } 153 this.id = id; 154 this.firstName = firstName; 155 this.lastName = lastName; 156 this.balance = balance; 157 return id; 158 } 159 160 public void ejbPostCreate(java.lang.String id, java.lang.String firstName, java.lang.String lastName, java.math.BigDecimal balance) throws CreateException { 161 } 163 164 private javax.sql.DataSource getSavingsAccountDB() throws javax.naming.NamingException { 165 javax.naming.Context c = new javax.naming.InitialContext (); 166 return (javax.sql.DataSource ) c.lookup("java:comp/env/jdbc/pointbase"); 167 } 168 169 public String getId() { 170 return id; 171 } 172 173 public void setId(String id) { 174 this.id = id; 175 } 176 177 public String getFirstName() { 178 return firstName; 179 } 180 181 public void setFirstName(String firstName) { 182 this.firstName = firstName; 183 } 184 185 public String getLastName() { 186 return lastName; 187 } 188 189 public void setLastName(String lastName) { 190 this.lastName = lastName; 191 } 192 193 public BigDecimal getBalance() { 194 return balance; 195 } 196 197 public void setBalance(BigDecimal balance) { 198 this.balance = balance; 199 } 200 201 public void credit(BigDecimal credit){ 202 balance = balance.add(credit); 203 } 204 205 public void debit(BigDecimal amount) throws InsufficientBalanceException { 207 if (balance.compareTo(amount) == -1) { 208 throw new InsufficientBalanceException(); 209 } 210 211 balance = balance.subtract(amount); 212 } 213 214 private void makeConnection(){ 215 try{ 216 con = getSavingsAccountDB().getConnection(); 217 }catch(Exception ex){ 218 throw new EJBException("Unable to connect to database. " + 219 ex.getMessage()); 220 } 221 } 222 223 private void releaseConnection(){ 224 try{ 225 con.close(); 226 }catch(SQLException ex){ 227 throw new EJBException("Unable to connect to database. " + 228 ex.getMessage()); 229 } 230 } 231 232 234 public java.util.Collection ejbFindInRange(BigDecimal low, BigDecimal high) throws FinderException { 235 Collection result; 237 try{ 238 result = selectInRange(low, high); 239 }catch(SQLException ex){ 240 throw new EJBException("ejbFindByInRange" + ex.getMessage()); 241 } 242 return result; 243 } 244 245 public java.util.Collection ejbFindByLastName(java.lang.String lastName) throws FinderException { 246 Collection result; 248 try{ 249 result = selectByLastName(lastName); 250 }catch(SQLException ex){ 251 throw new EJBException("ejbFindByLastName" + ex.getMessage()); 252 } 253 return result; 254 } 255 256 public void ejbHomeChargeForLowBalance(BigDecimal minimumBalance, 258 BigDecimal charge) throws InsufficientBalanceException { 259 SavingsAccountRemoteHome home = (SavingsAccountRemoteHome)context.getEJBHome(); 260 try{ 261 Collection c = 262 home.findInRange(new BigDecimal (0.00), 263 minimumBalance.subtract(new BigDecimal (0.01))); 264 Iterator it = c.iterator(); 265 while(it.hasNext()){ 266 SavingsAccountRemote account = (SavingsAccountRemote)it.next(); 267 if(account.getBalance().compareTo(charge) == 1){ 268 account.debit(charge); 269 } 270 } 271 }catch(Exception ex){ 272 throw new EJBException("ejbHomeChargeForLowBalance" + ex.getMessage()); 273 } 274 } 275 276 278 private void insertRow(String id, String firstName, 279 String lastName, BigDecimal balance) throws SQLException { 280 makeConnection(); 281 String insertStatement = "insert into savingsaccount values ( ? , ? , ? , ? )"; 282 PreparedStatement prepStmt = con.prepareStatement(insertStatement); 283 284 prepStmt.setString(1, id); 285 prepStmt.setString(2, firstName); 286 prepStmt.setString(3, lastName); 287 prepStmt.setBigDecimal(4, balance); 288 289 prepStmt.executeUpdate(); 290 prepStmt.close(); 291 releaseConnection(); 292 } 293 294 private void deleteRow(String id) throws SQLException { 295 makeConnection(); 296 String deleteStatement = "delete from savingsaccount where id = ?"; 297 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 298 prepStmt.setString(1, id); 299 prepStmt.executeUpdate(); 300 prepStmt.close(); 301 releaseConnection(); 302 } 303 304 private boolean selectByPrimaryKey(String id) throws SQLException { 305 makeConnection(); 306 String selectStatement = "select id from savingsaccount where id = ?"; 307 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 308 prepStmt.setString(1, id); 309 310 ResultSet rs = prepStmt.executeQuery(); 311 boolean result = rs.next(); 312 313 releaseConnection(); 314 return result; 315 } 316 317 private void loadRow() throws SQLException { 318 makeConnection(); 319 String selectString = 320 "select firstname, lastname, balance from savingsaccount where id = ?"; 321 PreparedStatement prepStmt = con.prepareStatement(selectString); 322 prepStmt.setString(1, this.id); 323 ResultSet rs = prepStmt.executeQuery(); 324 325 if(rs.next()){ 326 this.firstName = rs.getString(1); 327 this.lastName = rs.getString(2); 328 this.balance = rs.getBigDecimal(3); 329 prepStmt.close(); 330 }else { 331 prepStmt.close(); 332 throw new NoSuchEntityException("Row for id " + id + 333 " not found in database"); 334 } 335 336 releaseConnection(); 337 } 338 339 private void storeRow() throws SQLException { 340 makeConnection(); 341 String updateString = 342 "update savingsaccount set firstname = ? , lastname = ? , " + 343 "balance = ? where id = ?"; 344 PreparedStatement prepStmt = con.prepareStatement(updateString); 345 prepStmt.setString(1, firstName); 346 prepStmt.setString(2, lastName); 347 prepStmt.setBigDecimal(3, balance); 348 prepStmt.setString(4, id); 349 350 int rows = prepStmt.executeUpdate(); 351 prepStmt.close(); 352 releaseConnection(); 353 if(rows == 0){ 354 throw new EJBException("Storing row id " + id + " failed."); 355 } 356 } 357 358 private Collection selectByLastName(String lastName) throws SQLException { 359 makeConnection(); 360 String selectStatement = "select id from savingsaccount where lastname = ?"; 361 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 362 prepStmt.setString(1, lastName); 363 ResultSet rs = prepStmt.executeQuery(); 364 ArrayList accounts = new ArrayList (); 365 366 while(rs.next()){ 367 accounts.add(rs.getString(1)); 368 } 369 prepStmt.close(); 370 releaseConnection(); 371 return accounts; 372 } 373 374 private Collection selectInRange(BigDecimal low, BigDecimal high) throws SQLException { 375 makeConnection(); 376 String selectStatement = "select id from savingsaccount where balance between ? and ?"; 377 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 378 prepStmt.setBigDecimal(1, low); 379 prepStmt.setBigDecimal(2, high); 380 381 ResultSet rs = prepStmt.executeQuery(); 382 ArrayList a = new ArrayList (); 383 384 while(rs.next()){ 385 a.add(rs.getString(1)); 386 } 387 prepStmt.close(); 388 releaseConnection(); 389 return a; 390 } 391 } 392 | Popular Tags |