package ejava.examples.ejb.session.bean;

import ejava.examples.ejb.session.Account;
import ejava.examples.ejb.session.BankException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import ejava.examples.ejb.session.dao.AccountDAO;
import ejava.examples.ejb.session.Account;
import ejava.examples.ejb.session.BankException;


/**
* This class provides the basic implementation for a Teller Stateless
* Session Bean.
*/
public class TellerEJB implements SessionBean {
    private DataSource ds_;
    private AccountDAO dao_;

    public Account getAccount(int id) throws BankException {
        Connection conn = null;
        try {
           conn = ds_.getConnection();
           Account account = dao_.find(conn, id);
           if (account == null) {
              throw new BankException("account id not found:" + id);
           }
           return account;
        }
        catch (SQLException ex) {
           throw new EJBException(ex);
        }
        finally {
           try { if (conn != null) conn.close(); }
           catch (SQLException sqlex) { throw new EJBException(sqlex); }
        }
    }

    public void deposit(int id, double amount) throws BankException {
        Connection conn = null;
        try {
           conn = ds_.getConnection();
           Account account = dao_.find(conn, id);
           if (account == null) {
              throw new BankException("account id not found:" + id);
           }
           account.deposit(amount);
           dao_.update(conn, account);
        }
        catch (SQLException ex) {
           throw new EJBException(ex);
        }
        finally {
           try { if (conn != null) conn.close(); }
           catch (SQLException sqlex) { throw new EJBException(sqlex); }
        }
    }

    public void withdraw(int id, double amount) throws BankException {
        Connection conn = null;
        try {
           conn = ds_.getConnection();
           Account account = dao_.find(conn, id);
           if (account == null) {
              throw new BankException("account id not found:" + id);
           }
           account.withdraw(amount);
           dao_.update(conn, account);
        }
        catch (SQLException ex) {
           throw new EJBException(ex);
        }
        finally {
           try { if (conn != null) conn.close(); }
           catch (SQLException sqlex) { throw new EJBException(sqlex); }
        }
    }

    public void transfer(int fromId, int toId, double amount)
       throws BankException {
        Connection conn = null;
        try {
           conn = ds_.getConnection();
           Account fromAccount = dao_.find(conn, fromId);
           if (fromAccount == null) {
              throw new BankException("from account id not found:" + fromId);
           }
           fromAccount.withdraw(amount);

           Account toAccount = dao_.find(conn, toId);
           if (toAccount == null) {
              throw new BankException("to account id not found:" + toId);
           }
           toAccount.deposit(amount);

           dao_.update(conn, fromAccount);
           dao_.update(conn, toAccount);
        }
        catch (SQLException ex) {
           throw new EJBException(ex);
        }
        finally {
           try { if (conn != null) conn.close(); }
           catch (SQLException sqlex) { throw new EJBException(sqlex); }
        }
    }

    public void ejbCreate() {
       try {
          dao_ = new AccountDAO();
          InitialContext jndi = new InitialContext();
          ds_ = (DataSource)jndi.lookup("java:comp/env/jdbc/mydb");
       }
       catch (NamingException ex) {
          throw new EJBException(ex);
       }
    }

    public void ejbActivate() { }
    public void ejbPassivate() { }
    public void ejbRemove() { }
    public void setSessionContext(SessionContext ctx) { }
}

