1 27 28 29 package com.sun.ebank.ejb.account; 30 31 import java.sql.*; 32 import javax.sql.*; 33 import java.util.*; 34 import java.math.*; 35 import javax.ejb.*; 36 import javax.naming.*; 37 import java.rmi.RemoteException ; 38 import com.sun.ebank.ejb.customer.CustomerHome; 39 import com.sun.ebank.ejb.customer.Customer; 40 import com.sun.ebank.ejb.exception.MissingPrimaryKeyException; 41 import com.sun.ebank.ejb.exception.AccountNotFoundException; 42 import com.sun.ebank.ejb.exception.CustomerInAccountException; 43 import com.sun.ebank.ejb.exception.IllegalAccountTypeException; 44 import com.sun.ebank.ejb.exception.CustomerRequiredException; 45 import com.sun.ebank.ejb.exception.CustomerNotInAccountException; 46 import com.sun.ebank.ejb.exception.CustomerNotFoundException; 47 import com.sun.ebank.ejb.exception.InvalidParameterException; 48 import com.sun.ebank.util.Debug; 49 import com.sun.ebank.util.DomainUtil; 50 import com.sun.ebank.util.DBHelper; 51 import com.sun.ebank.util.AccountDetails; 52 import com.sun.ebank.util.EJBGetter; 53 import com.sun.ebank.util.CodedNames; 54 55 56 public class AccountControllerBean implements SessionBean { 57 private String accountId; 58 private AccountHome accountHome; 59 private Account account; 60 private Connection con; 61 62 public AccountControllerBean() { 63 } 64 65 public String createAccount(String customerId, String type, 67 String description, BigDecimal balance, BigDecimal creditLine, 68 BigDecimal beginBalance, java.util.Date beginBalanceTimeStamp) 69 throws IllegalAccountTypeException, CustomerNotFoundException, 70 InvalidParameterException { 71 Debug.print("AccountControllerBean createAccount"); 75 76 if (customerId == null) { 77 throw new InvalidParameterException("null customerId"); 78 } 79 80 if (type == null) { 81 throw new InvalidParameterException("null type"); 82 } 83 84 if (description == null) { 85 throw new InvalidParameterException("null description"); 86 } 87 88 if (beginBalanceTimeStamp == null) { 89 throw new InvalidParameterException("null beginBalanceTimeStamp"); 90 } 91 92 DomainUtil.checkAccountType(type); 93 94 if (customerExists(customerId) == false) { 95 throw new CustomerNotFoundException(customerId); 96 } 97 98 ArrayList customerIds = new ArrayList(); 99 customerIds.add(customerId); 100 101 try { 102 makeConnection(); 103 accountId = DBHelper.getNextAccountId(con); 104 account = 105 accountHome.create(accountId, type, description, balance, 106 creditLine, beginBalance, beginBalanceTimeStamp, customerIds); 107 insertXref(customerId, accountId); 108 releaseConnection(); 109 } catch (Exception ex) { 110 releaseConnection(); 111 throw new EJBException("createAccount: " + ex.getMessage()); 112 } 113 114 return accountId; 115 } 116 118 public void removeAccount(String accountId) 119 throws AccountNotFoundException, InvalidParameterException { 120 Debug.print("AccountControllerBean removeAccount"); 122 123 if (accountId == null) { 124 throw new InvalidParameterException("null accountId"); 125 } 126 127 if (accountExists(accountId) == false) { 128 throw new AccountNotFoundException(accountId); 129 } 130 131 try { 132 makeConnection(); 133 deleteAllAccountInXref(accountId); 134 releaseConnection(); 135 account.remove(); 136 } catch (Exception ex) { 137 releaseConnection(); 138 throw new EJBException("removeAccount: " + ex.getMessage()); 139 } 140 } 141 143 public void addCustomerToAccount(String customerId, String accountId) 145 throws AccountNotFoundException, CustomerNotFoundException, 146 CustomerInAccountException, InvalidParameterException { 147 Debug.print("AccountControllerBean addCustomerToAccount"); 149 150 if (customerId == null) { 151 throw new InvalidParameterException("null customerId"); 152 } 153 154 if (accountId == null) { 155 throw new InvalidParameterException("null accountId"); 156 } 157 158 ArrayList customerIds; 159 CustomerHome customerHome; 160 161 if (customerExists(customerId) == false) { 162 throw new CustomerNotFoundException(customerId); 163 } 164 165 if (accountExists(accountId) == false) { 166 throw new AccountNotFoundException(accountId); 167 } 168 169 try { 170 makeConnection(); 171 customerIds = getCustomerIds(accountId); 172 } catch (Exception ex) { 173 releaseConnection(); 174 throw new EJBException("addCustomerToAccount: " + ex.getMessage()); 175 } 176 177 if (customerIds.contains(customerId)) { 178 releaseConnection(); 179 throw new CustomerInAccountException("customer " + customerId + 180 " already assigned to account " + accountId); 181 } 182 183 try { 184 insertXref(customerId, accountId); 185 releaseConnection(); 186 } catch (Exception ex) { 187 releaseConnection(); 188 throw new EJBException("addCustomerToAccount: " + ex.getMessage()); 189 } 190 } 191 193 public void removeCustomerFromAccount(String customerId, String accountId) 194 throws AccountNotFoundException, CustomerRequiredException, 195 CustomerNotInAccountException, InvalidParameterException { 196 Debug.print("AccountControllerBean removeCustomerFromAccount"); 199 200 ArrayList customerIds; 201 202 if (customerId == null) { 203 throw new InvalidParameterException("null customerId"); 204 } 205 206 if (accountId == null) { 207 throw new InvalidParameterException("null accountId"); 208 } 209 210 if (accountExists(accountId) == false) { 211 throw new AccountNotFoundException(accountId); 212 } 213 214 try { 215 makeConnection(); 216 customerIds = getCustomerIds(accountId); 217 } catch (Exception ex) { 218 releaseConnection(); 219 throw new EJBException("removeCustomerFromAccount: " + 220 ex.getMessage()); 221 } 222 223 if (customerIds.size() == 1) { 224 releaseConnection(); 225 throw new CustomerRequiredException(); 226 } 227 228 if (customerIds.contains(customerId) == false) { 229 releaseConnection(); 230 throw new CustomerNotInAccountException("customer " + customerId + 231 " not assigned to account " + accountId); 232 } 233 234 try { 235 deleteOneCustomerInXref(customerId, accountId); 236 releaseConnection(); 237 } catch (Exception ex) { 238 releaseConnection(); 239 throw new EJBException("removeCustomerFromAccount: " + 240 ex.getMessage()); 241 } 242 } 243 245 public ArrayList getAccountsOfCustomer(String customerId) 247 throws AccountNotFoundException, InvalidParameterException { 248 Debug.print("AccountControllerBean getAccountsOfCustomer"); 252 253 Collection accountIds; 254 255 if (customerId == null) { 256 throw new InvalidParameterException("null customerId"); 257 } 258 259 try { 260 accountIds = accountHome.findByCustomerId(customerId); 261 262 if (accountIds.isEmpty()) { 263 throw new AccountNotFoundException(); 264 } 265 } catch (Exception ex) { 266 throw new AccountNotFoundException(); 267 } 268 269 ArrayList accountList = new ArrayList(); 270 271 Iterator i = accountIds.iterator(); 272 273 while (i.hasNext()) { 274 Account account = (Account) i.next(); 275 AccountDetails accountDetails = account.getDetails(); 276 accountList.add(accountDetails); 277 } 278 279 return accountList; 280 } 281 283 public AccountDetails getDetails(String accountId) 284 throws AccountNotFoundException, InvalidParameterException { 285 Debug.print("AccountControllerBean getDetails"); 287 288 AccountDetails result; 289 290 if (accountId == null) { 291 throw new InvalidParameterException("null accountId"); 292 } 293 294 if (accountExists(accountId) == false) { 295 throw new AccountNotFoundException(accountId); 296 } 297 298 result = account.getDetails(); 299 Debug.print(result.getAccountId()); 300 301 return result; 302 } 303 305 public void setType(String type, String accountId) 307 throws AccountNotFoundException, IllegalAccountTypeException, 308 InvalidParameterException { 309 Debug.print("AccountControllerBean setType"); 310 311 if (type == null) { 312 throw new InvalidParameterException("null type"); 313 } 314 315 if (accountId == null) { 316 throw new InvalidParameterException("null accountId"); 317 } 318 319 DomainUtil.checkAccountType(type); 320 321 if (accountExists(accountId) == false) { 322 throw new AccountNotFoundException(accountId); 323 } 324 325 account.setType(type); 326 } 327 329 public void setDescription(String description, String accountId) 330 throws AccountNotFoundException, InvalidParameterException { 331 Debug.print("AccountControllerBean setDescription"); 332 333 if (description == null) { 334 throw new InvalidParameterException("null description"); 335 } 336 337 if (accountId == null) { 338 throw new InvalidParameterException("null accountId"); 339 } 340 341 if (accountExists(accountId) == false) { 342 throw new AccountNotFoundException(accountId); 343 } 344 } 345 347 public void setBalance(BigDecimal balance, String accountId) 348 throws AccountNotFoundException, InvalidParameterException { 349 Debug.print("AccountControllerBean setBalance"); 350 351 if (accountId == null) { 352 throw new InvalidParameterException("null accountId"); 353 } 354 355 if (accountExists(accountId) == false) { 356 throw new AccountNotFoundException(accountId); 357 } 358 359 account.setBalance(balance); 360 } 361 363 public void setCreditLine(BigDecimal creditLine, String accountId) 364 throws EJBException, AccountNotFoundException, 365 InvalidParameterException { 366 Debug.print("AccountControllerBean setCreditLine"); 367 368 if (accountId == null) { 369 throw new InvalidParameterException("null accountId"); 370 } 371 372 if (accountExists(accountId) == false) { 373 throw new AccountNotFoundException(accountId); 374 } 375 376 account.setCreditLine(creditLine); 377 } 378 380 public void setBeginBalance(BigDecimal beginBalance, String accountId) 381 throws AccountNotFoundException, InvalidParameterException { 382 Debug.print("AccountControllerBean setBeginBalance"); 383 384 if (accountId == null) { 385 throw new InvalidParameterException("null accountId"); 386 } 387 388 if (accountExists(accountId) == false) { 389 throw new AccountNotFoundException(accountId); 390 } 391 392 account.setBeginBalance(beginBalance); 393 } 394 396 public void setBeginBalanceTimeStamp(java.util.Date beginBalanceTimeStamp, 397 String accountId) 398 throws AccountNotFoundException, InvalidParameterException { 399 Debug.print("AccountControllerBean setBeginBalanceTimeStamp"); 400 401 if (beginBalanceTimeStamp == null) { 402 throw new InvalidParameterException("null beginBalanceTimeStamp"); 403 } 404 405 if (accountId == null) { 406 throw new InvalidParameterException("null accountId"); 407 } 408 409 if (accountExists(accountId) == false) { 410 throw new AccountNotFoundException(accountId); 411 } 412 413 account.setBeginBalanceTimeStamp(beginBalanceTimeStamp); 414 } 415 417 public void ejbCreate() { 419 Debug.print("AccountControllerBean ejbCreate"); 420 421 try { 422 accountHome = EJBGetter.getAccountHome(); 423 } catch (Exception ex) { 424 throw new EJBException("ejbCreate: " + ex.getMessage()); 425 } 426 427 account = null; 428 accountId = null; 429 } 430 432 public void ejbRemove() { 433 } 434 435 public void ejbActivate() { 436 } 437 438 public void ejbPassivate() { 439 } 440 441 public void setSessionContext(SessionContext sc) { 442 } 443 444 private boolean accountExists(String accountId) { 446 Debug.print("AccountControllerBean accountExists"); 452 453 if (accountId.equals(this.accountId) == false) { 454 try { 455 account = accountHome.findByPrimaryKey(accountId); 456 this.accountId = accountId; 457 } catch (Exception ex) { 458 return false; 459 } 460 } 461 463 return true; 464 } 465 467 private boolean customerExists(String customerId) { 468 Debug.print("AccountControllerBean customerExists"); 469 470 CustomerHome customerHome; 471 472 try { 473 customerHome = EJBGetter.getCustomerHome(); 474 } catch (Exception ex) { 475 throw new EJBException("customerExists: " + ex.getMessage()); 476 } 477 478 try { 479 customerHome.findByPrimaryKey(customerId); 480 481 return true; 482 } catch (Exception ex) { 483 return false; 484 } 485 } 486 488 489 private void makeConnection() { 490 Debug.print("AccountControllerBean makeConnection"); 491 492 try { 493 InitialContext ic = new InitialContext(); 494 DataSource ds = (DataSource) ic.lookup(CodedNames.BANK_DATABASE); 495 con = ds.getConnection(); 496 } catch (Exception ex) { 497 throw new EJBException("Unable to connect to database. " + 498 ex.getMessage()); 499 } 500 } 501 503 private void releaseConnection() { 504 Debug.print("AccountControllerBean releaseConnection"); 505 506 try { 507 con.close(); 508 } catch (SQLException ex) { 509 throw new EJBException("releaseConnection: " + ex.getMessage()); 510 } 511 } 512 514 private void insertXref(String customerId, String accountId) 515 throws SQLException { 516 Debug.print("AccountControllerBean insertXref"); 517 518 String insertStatement = 519 "insert into customer_account_xref " + "values ( ? , ? )"; 520 PreparedStatement prepStmt = con.prepareStatement(insertStatement); 521 522 prepStmt.setString(1, customerId); 523 prepStmt.setString(2, accountId); 524 prepStmt.executeUpdate(); 525 prepStmt.close(); 526 } 527 528 private void deleteAllAccountInXref(String accountId) 529 throws SQLException { 530 Debug.print("AccountControllerBean deleteAllAccountInXref"); 531 532 String deleteStatement = 533 "delete from customer_account_xref " + "where account_id = ? "; 534 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 535 536 prepStmt.setString(1, accountId); 537 prepStmt.executeUpdate(); 538 prepStmt.close(); 539 } 540 541 private ArrayList getCustomerIds(String accountId) 542 throws SQLException { 543 Debug.print("AccountControllerBean getCustomerIds"); 544 545 String selectStatement = 546 "select customer_id " + "from customer_account_xref " + 547 "where account_id = ? "; 548 PreparedStatement prepStmt = con.prepareStatement(selectStatement); 549 550 prepStmt.setString(1, accountId); 551 552 ResultSet rs = prepStmt.executeQuery(); 553 ArrayList a = new ArrayList(); 554 555 while (rs.next()) { 556 a.add(rs.getString(1)); 557 } 558 559 prepStmt.close(); 560 561 return a; 562 } 563 565 private void deleteOneCustomerInXref(String customerId, String accountId) 566 throws SQLException { 567 Debug.print("AccountControllerBean deleteOneCustomerInXref"); 568 569 String deleteStatement = 570 "delete from customer_account_xref " + 571 "where account_id = ? and customer_id = ? "; 572 PreparedStatement prepStmt = con.prepareStatement(deleteStatement); 573 574 prepStmt.setString(1, accountId); 575 prepStmt.setString(2, customerId); 576 prepStmt.executeUpdate(); 577 prepStmt.close(); 578 } 579 } 580 | Popular Tags |