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.AccountSession;
import org.jboss.test.banknew.interfaces.AccountSessionHome;
import org.jboss.test.banknew.interfaces.Constants;
import org.jboss.test.banknew.interfaces.Customer;
import org.jboss.test.banknew.interfaces.CustomerData;
import org.jboss.test.banknew.interfaces.CustomerHome;
import org.jboss.test.banknew.interfaces.CustomerPK;
public class CustomerSessionBean
extends SessionSupport
{
public CustomerData createCustomer( String pBankId, String pName, float pInitialDeposit )
throws CreateException, RemoteException
{
Customer lCustomer = getCustomerHome().create(
pBankId,
pName
);
CustomerData lNew = lCustomer.getData();
getAccountHome().create().createAccount(
lNew.getId(),
Constants.CHECKING,
pInitialDeposit
);
return lNew;
}
public void removeCustomer( String pCustomerId )
throws RemoveException, RemoteException
{
try {
getCustomerHome().findByPrimaryKey(
new CustomerPK( pCustomerId )
).remove();
}
catch( FinderException fe ) {
}
}
public CustomerData getCustomer( String pCustomerId )
throws FinderException, RemoteException
{
Customer lCustomer = getCustomerHome().findByPrimaryKey(
new CustomerPK( pCustomerId )
);
return lCustomer.getData();
}
public Collection getCustomers( String pBankId )
throws FinderException, RemoteException
{
Collection lCustomers = getCustomerHome().findByBank(
pBankId
);
Collection lList = new ArrayList( lCustomers.size() );
Iterator i = lCustomers.iterator();
while( i.hasNext() ) {
lList.add( ( (Customer) i.next() ).getData() );
}
return lList;
}
public Collection getAccounts( String pCustomerId )
throws FinderException, RemoteException
{
try {
return getAccountHome().create().getAccounts( pCustomerId );
}
catch( CreateException ce ) {
throw new EJBException( ce );
}
}
public AccountData createAccount( String pCustomerId, int pType, float pInitialDeposit )
throws CreateException, RemoteException
{
return getAccountHome().create().createAccount(
pCustomerId,
pType,
pInitialDeposit
);
}
public void removeAccount( String pCustomerId, int pType )
throws RemoveException, RemoteException
{
try {
getAccountHome().create().removeAccount( pCustomerId, pType );
}
catch( CreateException ce ) {
}
}
private AccountSessionHome getAccountHome() {
try {
return (AccountSessionHome) new InitialContext().lookup( AccountSessionHome.COMP_NAME );
}
catch( NamingException ne ) {
throw new EJBException( ne );
}
}
private CustomerHome getCustomerHome() {
try {
return (CustomerHome) new InitialContext().lookup( CustomerHome.COMP_NAME );
}
catch( NamingException ne ) {
throw new EJBException( ne );
}
}
public void setSessionContext(SessionContext context)
{
super.setSessionContext(context);
}
}