/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package org.jboss.ejb3.test.bank;

import java.util.Collection;
import java.util.Iterator;

import javax.ejb.SessionContext;
import javax.ejb.TimerService;
import javax.naming.InitialContext;
import javax.transaction.TransactionManager;
import org.jboss.logging.Logger;

/**
 * @see <related>
 * @author $Author: bill $
 * @version $Revision: 1.13.2.4 $
 */
public class TellerBean 
{
   private static final Logger log = Logger.getLogger(TellerBean.class);
   
   private TimerService ts;
   private TransactionManager tm;
   private Bank bank;
   private boolean constructed = false;
   private String defaultValue = "original";
   
   public boolean isConstructed()
   {
      return constructed;
   }

   public void setTransactionManager(TransactionManager tm)
   {
      this.tm = tm;
      System.out.println("TransactionManager set: " + tm);
   }
   
   public void transfer(Account from, Account to, float amount)
         throws BankException
   {
      try
      {
         from.withdraw(amount);
         to.deposit(amount);
      } catch (Exception e)
      {
         throw new BankException("Could not transfer " + amount + " from "
               + from + " to " + to, e);
      }
   }

   public Account createAccount(Customer customer, float balance)
         throws BankException
   {
      try
      {
        Bank bank = (Bank) new InitialContext()
               .lookup(Bank.JNDI_NAME);

         return null;
      } catch (Exception e)
      {
         log.debug("failed", e);
         throw new BankException("Could not create account", e);
      }
   }

   public Account getAccount(Customer customer, float balance)
         throws BankException
   {
      try
      {
         // Check for existing account
         Collection accounts = customer.getAccounts();
         if (accounts.size() > 0)
         {
            Iterator i = accounts.iterator();
            Account acct = (Account) i.next();
            // Set balance
            acct.withdraw(acct.getBalance() - balance);

            return acct;
         } else
         {
            // Create account
            return createAccount(customer, balance);
         }
      } catch (Exception e)
      {
         log.debug("failed", e);
         throw new BankException("Could not get account for " + customer, e);
      }
   }

   public Customer getCustomer(String name) throws BankException
   {
      try
      {
         // Check for existing customer
         
         return null;
      } catch (Exception e)
      {
         log.debug("failed", e);
         throw new BankException("Could not get customer for " + name, e);
      }
   }

   public void transferTest(Account from, Account to, float amount, int iter)
         throws java.rmi.RemoteException, BankException
   {
      for (int i = 0; i < iter; i++)
      {
         from.withdraw(amount);
         to.deposit(amount);
      }
   }

   public String greetWithRequiredTransaction(String greeting) throws Exception
   {
      if (tm.getTransaction() == null) throw new Exception("method has no tx set");
      return greeting;
   }
   
   public String greetWithNotSupportedTransaction(String greeting) throws Exception
   {
      if (tm.getTransaction() != null) throw new Exception("method has tx set");
      return greeting;
   }
   
   public String greetWithServiceTimer(String greeting) throws Exception
   {
      if (ts == null) throw new Exception("TimerService @Inject failed");
      return greeting;
   }
   
   public String greetUnchecked(String greeting) throws Exception
   {
      if (tm.getTransaction() == null) throw new Exception("method has no tx set");
      return greeting;
   }
   
   public String greetChecked(String greeting) throws Exception
   {
      if (tm.getTransaction() == null) throw new Exception("method has no tx set");
      return greeting;
   }
   
   public void storeCustomerId(String customerId) throws Exception
   {
      bank.storeCustomerId(customerId);
   }

   public String retrieveCustomerId() throws Exception
   {
      return bank.retrieveCustomerId();
   }
   
   public void excludedMethod()
   {
      
   }
   
   public void postConstruct()
   {
      constructed = true;
   }
   
   public String getDefaultValue()
   {
      return defaultValue;
   }
}
