package org.jboss.test.banknew.ejb;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ejb.EJBException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.SessionContext;
import org.jboss.test.util.ejb.SessionSupport;
import org.jboss.test.banknew.interfaces.Account;
import org.jboss.test.banknew.interfaces.AccountData;
import org.jboss.test.banknew.interfaces.AccountHome;
import org.jboss.test.banknew.interfaces.AccountPK;
import org.jboss.test.banknew.interfaces.Constants;
import org.jboss.test.banknew.interfaces.Transaction;
import org.jboss.test.banknew.interfaces.TransactionHome;
public class AccountSessionBean
extends SessionSupport
{
public AccountData getAccount( String pAccountId )
throws FinderException, RemoteException
{
return getAccountHome().findByPrimaryKey(
new AccountPK( pAccountId )
).getData();
}
public AccountData getAccount( String pCustomerId, int pType )
throws FinderException, RemoteException
{
return getAccountHome().findByCustomerAndType( pCustomerId, pType ).getData();
}
public Collection getAccounts( String pCustomerId )
throws FinderException, RemoteException
{
Collection lAccounts = getAccountHome().findByCustomer( pCustomerId );
Collection lList = new ArrayList( lAccounts.size() );
Iterator i = lAccounts.iterator();
while( i.hasNext() ) {
lList.add( ( (Account) i.next() ).getData() );
}
return lList;
}
public Collection getTransactions( String pAccountId )
throws FinderException, RemoteException
{
Collection lTransactions = getTransactionHome().findByAccount( pAccountId );
Iterator i = lTransactions.iterator();
Collection lList = new ArrayList( lTransactions.size() );
while( i.hasNext() ) {
lList.add( ( (Transaction) i.next() ).getData() );
}
return lList;
}
public AccountData createAccount( String pCustomerId, int pType, float pInitialDeposit )
throws CreateException, RemoteException
{
AccountData lData = new AccountData();
lData.setCustomerId( pCustomerId );
lData.setType( pType );
lData.setBalance( pInitialDeposit );
Account lAccount = getAccountHome().create( lData );
AccountData lNew = lAccount.getData();
getTransactionHome().create(
lNew.getId(),
Constants.INITIAL_DEPOSIT,
pInitialDeposit,
"Account Creation"
);
return lNew;
}
public void removeAccount( String pAccountId )
throws RemoveException, RemoteException
{
try {
Account lAccount = getAccountHome().findByPrimaryKey(
new AccountPK( pAccountId )
);
removeAccount( lAccount );
}
catch( FinderException fe ) {
}
catch( CreateException ce ) {
throw new EJBException( ce );
}
}
public void removeAccount( String pCustomerId, int pType )
throws RemoveException, RemoteException
{
try {
Account lAccount = getAccountHome().findByCustomerAndType(
pCustomerId,
pType
);
removeAccount( lAccount );
}
catch( FinderException fe ) {
}
catch( CreateException ce ) {
throw new EJBException( ce );
}
}
public void removeTransactions( String pAccountId )
throws RemoveException, RemoteException
{
try {
Collection lTransactions = getTransactionHome().findByAccount( pAccountId );
Iterator i = lTransactions.iterator();
while( i.hasNext() ) {
Transaction lTransaction = (Transaction) i.next();
lTransaction.remove();
}
}
catch( FinderException fe ) {
}
}
private void removeAccount( Account pAccount )
throws RemoveException, CreateException, RemoteException
{
AccountData lData = pAccount.getData();
pAccount.remove();
getTransactionHome().create(
lData.getId(),
Constants.FINAL_WITHDRAW,
lData.getBalance(),
"Account Closure"
);
}
public void deposit( String pAccountId, float pAmount )
throws FinderException, RemoteException
{
Account lAccount = getAccountHome().findByPrimaryKey(
new AccountPK( pAccountId )
);
AccountData lData = lAccount.getData();
lData.setBalance( lData.getBalance() + pAmount );
lAccount.setData( lData );
try {
getTransactionHome().create(
lData.getId(),
Constants.DEPOSIT,
pAmount,
"Account Deposit"
);
}
catch( CreateException ce ) {
throw new EJBException( ce );
}
}
public void withdraw( String pAccountId, float pAmount )
throws FinderException, RemoteException
{
Account lAccount = getAccountHome().findByPrimaryKey(
new AccountPK( pAccountId )
);
AccountData lData = lAccount.getData();
lData.setBalance( lData.getBalance() - pAmount );
lAccount.setData( lData );
try {
getTransactionHome().create(
lData.getId(),
Constants.WITHDRAW,
pAmount,
"Account Withdraw"
);
}
catch( CreateException ce ) {
throw new EJBException( ce );
}
}
public void transfer( String pFromAccountId, String pToAccountId, float pAmount )
throws FinderException, RemoteException
{
try {
withdraw( pFromAccountId, pAmount );
deposit( pToAccountId, pAmount );
}
catch( RemoteException re ) {
re.printStackTrace();
throw re;
}
}
private AccountHome getAccountHome() {
try {
return (AccountHome) new InitialContext().lookup( AccountHome.COMP_NAME );
}
catch( NamingException ne ) {
throw new EJBException( ne );
}
}
private TransactionHome getTransactionHome() {
try {
return (TransactionHome) new InitialContext().lookup( TransactionHome.COMP_NAME );
}
catch( NamingException ne ) {
throw new EJBException( ne );
}
}
public void setSessionContext(SessionContext context)
{
super.setSessionContext(context);
}
}