1 27 28 29 package com.sun.ebank.ejb.tx; 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.account.AccountHome; 39 import com.sun.ebank.ejb.account.Account; 40 import com.sun.ebank.ejb.exception.TxNotFoundException; 41 import com.sun.ebank.ejb.exception.AccountNotFoundException; 42 import com.sun.ebank.ejb.exception.InsufficientFundsException; 43 import com.sun.ebank.ejb.exception.InsufficientCreditException; 44 import com.sun.ebank.ejb.exception.IllegalAccountTypeException; 45 import com.sun.ebank.ejb.exception.InvalidParameterException; 46 import com.sun.ebank.util.Debug; 47 import com.sun.ebank.util.TxDetails; 48 import com.sun.ebank.util.DBHelper; 49 import com.sun.ebank.util.EJBGetter; 50 import com.sun.ebank.util.CodedNames; 51 import com.sun.ebank.util.DomainUtil; 52 53 54 public class TxControllerBean implements SessionBean { 55 private TxHome txHome; 56 private AccountHome accountHome; 57 private Connection con; 58 private SessionContext context; 59 private BigDecimal bigZero = new BigDecimal("0.00"); 60 61 public TxControllerBean() { 62 } 63 64 public ArrayList getTxsOfAccount(java.util.Date startDate, 65 java.util.Date endDate, String accountId) 66 throws InvalidParameterException { 67 Debug.print("TxControllerBean getTxsOfAccount"); 68 69 Collection txIds; 70 ArrayList txList = new ArrayList(); 71 72 if (startDate == null) { 73 throw new InvalidParameterException("null startDate"); 74 } 75 76 if (endDate == null) { 77 throw new InvalidParameterException("null endDate"); 78 } 79 80 if (accountId == null) { 81 throw new InvalidParameterException("null accountId"); 82 } 83 84 try { 85 txIds = txHome.findByAccountId(startDate, endDate, accountId); 86 } catch (Exception ex) { 87 return txList; 88 } 89 90 Iterator i = txIds.iterator(); 91 92 while (i.hasNext()) { 93 Tx tx = (Tx) i.next(); 94 TxDetails txDetail = tx.getDetails(); 95 txList.add(txDetail); 96 } 97 98 return txList; 99 } 100 102 public TxDetails getDetails(String txId) 103 throws TxNotFoundException, InvalidParameterException { 104 Debug.print("TxControllerBean getDetails"); 105 106 if (txId == null) { 107 throw new InvalidParameterException("null txId"); 108 } 109 110 try { 111 Tx tx = txHome.findByPrimaryKey(txId); 112 113 return tx.getDetails(); 114 } catch (Exception ex) { 115 throw new TxNotFoundException(txId); 116 } 117 } 118 120 public void withdraw(BigDecimal amount, String description, String accountId) 121 throws InvalidParameterException, AccountNotFoundException, 122 IllegalAccountTypeException, InsufficientFundsException { 123 Debug.print("TxControllerBean withdraw"); 124 125 Account account = checkAccountArgs(amount, description, accountId); 126 127 String type = account.getType(); 128 129 if (DomainUtil.isCreditAccount(type)) { 130 throw new IllegalAccountTypeException(type); 131 } 132 133 BigDecimal newBalance = account.getBalance() 134 .subtract(amount); 135 136 if (newBalance.compareTo(bigZero) == -1) { 137 throw new InsufficientFundsException(); 138 } 139 140 executeTx(amount.negate(), description, accountId, newBalance, account); 141 } 142 144 public void deposit(BigDecimal amount, String description, String accountId) 145 throws InvalidParameterException, AccountNotFoundException, 146 IllegalAccountTypeException { 147 Debug.print("TxControllerBean deposit"); 148 149 Account account = checkAccountArgs(amount, description, accountId); 150 151 String type = account.getType(); 152 153 if (DomainUtil.isCreditAccount(type)) { 154 throw new IllegalAccountTypeException(type); 155 } 156 157 BigDecimal newBalance = account.getBalance() 158 .add(amount); 159 executeTx(amount, description, accountId, newBalance, account); 160 } 161 163 public void makeCharge(BigDecimal amount, String description, 164 String accountId) 165 throws InvalidParameterException, AccountNotFoundException, 166 IllegalAccountTypeException, InsufficientCreditException { 167 Debug.print("TxControllerBean charge"); 168 169 Account account = checkAccountArgs(amount, description, accountId); 170 171 String type = account.getType(); 172 173 if (DomainUtil.isCreditAccount(type) == false) { 174 throw new IllegalAccountTypeException(type); 175 } 176 177 BigDecimal newBalance = account.getBalance() 178 .add(amount); 179 180 if (newBalance.compareTo(account.getCreditLine()) == 1) { 181 throw new InsufficientCreditException(); 182 } 183 184 executeTx(amount, description, accountId, newBalance, account); 185 } 186 188 public void makePayment(BigDecimal amount, String description, 189 String accountId) 190 throws InvalidParameterException, AccountNotFoundException, 191 IllegalAccountTypeException { 192 Debug.print("TxControllerBean makePayment"); 193 194 Account account = checkAccountArgs(amount, description, accountId); 195 196 String type = account.getType(); 197 198 if (DomainUtil.isCreditAccount(type) == false) { 199 throw new IllegalAccountTypeException(type); 200 } 201 202 BigDecimal newBalance = account.getBalance() 203 .subtract(amount); 204 executeTx(amount.negate(), description, accountId, newBalance, account); 205 } 206 208 public void transferFunds(BigDecimal amount, String description, 209 String fromAccountId, String toAccountId) 210 throws InvalidParameterException, AccountNotFoundException, 211 InsufficientFundsException, InsufficientCreditException { 212 Account fromAccount = 213 checkAccountArgs(amount, description, fromAccountId); 214 Account toAccount = checkAccountArgs(amount, description, toAccountId); 215 216 String fromType = fromAccount.getType(); 217 BigDecimal fromBalance = fromAccount.getBalance(); 218 219 if (DomainUtil.isCreditAccount(fromType)) { 220 BigDecimal fromNewBalance = fromBalance.add(amount); 221 222 if (fromNewBalance.compareTo(fromAccount.getCreditLine()) == 1) { 223 throw new InsufficientCreditException(); 224 } 225 226 executeTx(amount, description, fromAccountId, fromNewBalance, 227 fromAccount); 228 } else { 229 BigDecimal fromNewBalance = fromBalance.subtract(amount); 230 231 if (fromNewBalance.compareTo(bigZero) == -1) { 232 throw new InsufficientFundsException(); 233 } 234 235 executeTx(amount.negate(), description, fromAccountId, 236 fromNewBalance, fromAccount); 237 } 238 239 String toType = toAccount.getType(); 240 BigDecimal toBalance = toAccount.getBalance(); 241 242 if (DomainUtil.isCreditAccount(toType)) { 243 BigDecimal toNewBalance = toBalance.subtract(amount); 244 executeTx(amount.negate(), description, toAccountId, toNewBalance, 245 toAccount); 246 } else { 247 BigDecimal toNewBalance = toBalance.add(amount); 248 executeTx(amount, description, toAccountId, toNewBalance, toAccount); 249 } 250 } 251 253 private void executeTx(BigDecimal amount, String description, 255 String accountId, BigDecimal newBalance, Account account) { 256 Debug.print("TxControllerBean executeTx"); 257 258 try { 259 makeConnection(); 260 261 String txId = DBHelper.getNextTxId(con); 262 account.setBalance(newBalance); 263 264 Tx tx = 265 txHome.create(txId, accountId, new java.util.Date (), amount, 266 newBalance, description); 267 } catch (Exception ex) { 268 throw new EJBException("executeTx: " + ex.getMessage()); 269 } finally { 270 releaseConnection(); 271 } 272 } 273 275 private Account checkAccountArgs(BigDecimal amount, String description, 276 String accountId) 277 throws InvalidParameterException, AccountNotFoundException { 278 Account account = null; 279 280 if (description == null) { 281 throw new InvalidParameterException("null description"); 282 } 283 284 if (accountId == null) { 285 throw new InvalidParameterException("null accountId"); 286 } 287 288 if (amount.compareTo(bigZero) != 1) { 289 throw new InvalidParameterException("amount <= 0"); 290 } 291 292 try { 293 account = accountHome.findByPrimaryKey(accountId); 294 } catch (Exception ex) { 295 throw new AccountNotFoundException(accountId); 296 } 297 298 return account; 299 } 300 302 public void ejbCreate() { 304 Debug.print("TxControllerBean ejbCreate"); 305 306 try { 307 txHome = EJBGetter.getTxHome(); 308 accountHome = EJBGetter.getAccountHome(); 309 } catch (Exception ex) { 310 throw new EJBException("ejbCreate: " + ex.getMessage()); 311 } 312 } 313 315 public void setSessionContext(SessionContext context) { 316 this.context = context; 317 } 318 319 public void ejbRemove() { 320 } 321 322 public void ejbActivate() { 323 } 324 325 public void ejbPassivate() { 326 } 327 328 329 private void makeConnection() { 330 Debug.print("TxControllerBean makeConnection"); 331 332 try { 333 InitialContext ic = new InitialContext(); 334 DataSource ds = (DataSource) ic.lookup(CodedNames.BANK_DATABASE); 335 con = ds.getConnection(); 336 } catch (Exception ex) { 337 throw new EJBException("Unable to connect to database. " + 338 ex.getMessage()); 339 } 340 } 341 343 private void releaseConnection() { 344 Debug.print("TxControllerBean releaseConnection"); 345 346 try { 347 con.close(); 348 } catch (SQLException ex) { 349 throw new EJBException("releaseConnection: " + ex.getMessage()); 350 } 351 } 352 } 354 | Popular Tags |