1 22 package org.jboss.test.banknew.ejb; 23 24 import java.rmi.RemoteException ; 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 29 import javax.ejb.CreateException ; 30 import javax.ejb.EJBException ; 31 import javax.ejb.FinderException ; 32 import javax.ejb.RemoveException ; 33 import javax.ejb.SessionContext ; 34 import javax.naming.InitialContext ; 35 import javax.naming.NamingException ; 36 37 import org.jboss.test.banknew.interfaces.Account; 38 import org.jboss.test.banknew.interfaces.AccountData; 39 import org.jboss.test.banknew.interfaces.AccountHome; 40 import org.jboss.test.banknew.interfaces.AccountPK; 41 import org.jboss.test.banknew.interfaces.Constants; 42 import org.jboss.test.banknew.interfaces.Transaction; 43 import org.jboss.test.banknew.interfaces.TransactionHome; 44 import org.jboss.test.util.ejb.SessionSupport; 45 46 70 public class AccountSessionBean 71 extends SessionSupport 72 { 73 74 76 78 80 82 84 85 private static final long serialVersionUID = 1963027300468464478L; 86 87 90 public AccountData getAccount( String pAccountId ) 91 throws FinderException , RemoteException 92 { 93 return getAccountHome().findByPrimaryKey( 94 new AccountPK( pAccountId ) 95 ).getData(); 96 } 97 98 101 public AccountData getAccount( String pCustomerId, int pType ) 102 throws FinderException , RemoteException 103 { 104 return getAccountHome().findByCustomerAndType( pCustomerId, pType ).getData(); 105 } 106 107 110 public Collection getAccounts( String pCustomerId ) 111 throws FinderException , RemoteException 112 { 113 Collection lAccounts = getAccountHome().findByCustomer( pCustomerId ); 114 Collection lList = new ArrayList ( lAccounts.size() ); 115 Iterator i = lAccounts.iterator(); 116 while( i.hasNext() ) { 117 lList.add( ( (Account) i.next() ).getData() ); 118 } 119 return lList; 120 } 121 122 125 public Collection getTransactions( String pAccountId ) 126 throws FinderException , RemoteException 127 { 128 Collection lTransactions = getTransactionHome().findByAccount( pAccountId ); 129 Iterator i = lTransactions.iterator(); 130 Collection lList = new ArrayList ( lTransactions.size() ); 131 while( i.hasNext() ) { 132 lList.add( ( (Transaction) i.next() ).getData() ); 133 } 134 return lList; 135 } 136 137 140 public AccountData createAccount( String pCustomerId, int pType, float pInitialDeposit ) 141 throws CreateException , RemoteException 142 { 143 AccountData lData = new AccountData(); 144 lData.setCustomerId( pCustomerId ); 145 lData.setType( pType ); 146 lData.setBalance( pInitialDeposit ); 147 Account lAccount = getAccountHome().create( lData ); 148 AccountData lNew = lAccount.getData(); 149 getTransactionHome().create( 150 lNew.getId(), 151 Constants.INITIAL_DEPOSIT, 152 pInitialDeposit, 153 "Account Creation" 154 ); 155 return lNew; 156 } 157 158 161 public void removeAccount( String pAccountId ) 162 throws RemoveException , RemoteException 163 { 164 try { 165 Account lAccount = getAccountHome().findByPrimaryKey( 166 new AccountPK( pAccountId ) 167 ); 168 removeAccount( lAccount ); 169 } 170 catch( FinderException fe ) { 171 } 173 catch( CreateException ce ) { 174 throw new EJBException ( ce ); 175 } 176 } 177 178 181 public void removeAccount( String pCustomerId, int pType ) 182 throws RemoveException , RemoteException 183 { 184 try { 185 Account lAccount = getAccountHome().findByCustomerAndType( 186 pCustomerId, 187 pType 188 ); 189 removeAccount( lAccount ); 190 } 191 catch( FinderException fe ) { 192 } 194 catch( CreateException ce ) { 195 throw new EJBException ( ce ); 196 } 197 } 198 199 202 public void removeTransactions( String pAccountId ) 203 throws RemoveException , RemoteException 204 { 205 try { 206 Collection lTransactions = getTransactionHome().findByAccount( pAccountId ); 207 Iterator i = lTransactions.iterator(); 208 while( i.hasNext() ) { 209 Transaction lTransaction = (Transaction) i.next(); 210 lTransaction.remove(); 211 } 212 } 213 catch( FinderException fe ) { 214 } 215 } 216 217 private void removeAccount( Account pAccount ) 218 throws RemoveException , CreateException , RemoteException 219 { 220 AccountData lData = pAccount.getData(); 221 pAccount.remove(); 222 getTransactionHome().create( 223 lData.getId(), 224 Constants.FINAL_WITHDRAW, 225 lData.getBalance(), 226 "Account Closure" 227 ); 228 } 229 230 233 public void deposit( String pAccountId, float pAmount ) 234 throws FinderException , RemoteException 235 { 236 Account lAccount = getAccountHome().findByPrimaryKey( 237 new AccountPK( pAccountId ) 238 ); 239 AccountData lData = lAccount.getData(); 240 lData.setBalance( lData.getBalance() + pAmount ); 241 lAccount.setData( lData ); 242 try { 243 getTransactionHome().create( 244 lData.getId(), 245 Constants.DEPOSIT, 246 pAmount, 247 "Account Deposit" 248 ); 249 } 250 catch( CreateException ce ) { 251 throw new EJBException ( ce ); 252 } 253 } 254 255 258 public void withdraw( String pAccountId, float pAmount ) 259 throws FinderException , RemoteException 260 { 261 Account lAccount = getAccountHome().findByPrimaryKey( 262 new AccountPK( pAccountId ) 263 ); 264 AccountData lData = lAccount.getData(); 265 lData.setBalance( lData.getBalance() - pAmount ); 266 lAccount.setData( lData ); 267 try { 268 getTransactionHome().create( 269 lData.getId(), 270 Constants.WITHDRAW, 271 pAmount, 272 "Account Withdraw" 273 ); 274 } 275 catch( CreateException ce ) { 276 throw new EJBException ( ce ); 277 } 278 } 279 280 283 public void transfer( String pFromAccountId, String pToAccountId, float pAmount ) 284 throws FinderException , RemoteException 285 { 286 try { 287 withdraw( pFromAccountId, pAmount ); 288 deposit( pToAccountId, pAmount ); 289 } 290 catch( RemoteException re ) { 291 re.printStackTrace(); 292 throw re; 293 } 294 } 295 296 private AccountHome getAccountHome() { 297 try { 298 return (AccountHome) new InitialContext ().lookup( AccountHome.COMP_NAME ); 299 } 300 catch( NamingException ne ) { 301 throw new EJBException ( ne ); 302 } 303 } 304 305 private TransactionHome getTransactionHome() { 306 try { 307 return (TransactionHome) new InitialContext ().lookup( TransactionHome.COMP_NAME ); 308 } 309 catch( NamingException ne ) { 310 throw new EJBException ( ne ); 311 } 312 } 313 314 public void setSessionContext(SessionContext context) 316 { 317 super.setSessionContext(context); 318 } 319 } 320 321 346 | Popular Tags |