FileDocCategorySizeDatePackage
AccountTransactionSession.javaAPI DocExample3255Fri Aug 25 09:22:14 BST 2000com.imaginary.bank

AccountTransactionSession

public class AccountTransactionSession extends com.imaginary.lwp.BaseSession implements AccountTransaction

Fields Summary
Constructors Summary
public AccountTransactionSession()

        super();
    
Methods Summary
public voiddeposit(com.imaginary.lwp.Identifier id, AccountFacade acct, double amt)

        Transaction trans = Transaction.getCurrent(id);
        boolean proc = trans.isInProcess();
        boolean success = false;

        if( amt < 0.0 ) {
            withdraw(id, acct, -amt);
        }
        else {
            if( !proc ) {
                trans.begin();
            }
            try {
                acct.credit(id, amt);
                success = true;
            }
            finally {
                if( success ) {
                    if( !proc ) {
                        try { trans.end(); }
                        catch( TransactionException e ) { }
                    }
                }
                else {
                    trans.rollback();
                }
            }
        }
    
public voidtransfer(com.imaginary.lwp.Identifier id, AccountFacade src, AccountFacade targ, double amt)

        Transaction trans = Transaction.getCurrent(id);
        boolean success = false;

        if( amt < 0.0 ) {
            AccountFacade tmp = targ;
            
            amt = -amt;
            targ = src;
            src = tmp;
        }
        trans.begin();
        try {
            withdraw(id, src, amt);
            deposit(id, targ, amt);
            success = true;
        }
        finally {
            if( success ) {
                try { trans.end(); }
                catch( TransactionException e ) { }
            }
            else {
                trans.rollback();
            }
        }
    
public voidwithdraw(com.imaginary.lwp.Identifier id, AccountFacade acct, double amt)

        Transaction trans = Transaction.getCurrent(id);
        boolean proc = trans.isInProcess();
        boolean success = false;

        if( amt < 0.0 ) {
            deposit(id, acct, -amt);
        }
        else {
            double bal;
            
            if( !proc ) {
                trans.begin();
            }
            try {
                bal = acct.getBalance();
                if( bal < amt ) {
                    throw new InsufficientFundsException();
                }
                acct.credit(id, -amt);
                success = true;
            }
            finally {
                if( success ) {
                    if( !proc ) {
                        try { trans.end(); }
                        catch( TransactionException e ) { }
                    }
                }
                else {
                    trans.rollback();
                }
            }
        }