package org.jboss.test.jca.bank.ejb;
import java.util.Collection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.sql.DataSource;
import java.sql.SQLException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.NoSuchEntityException;
import javax.naming.InitialContext;
import java.util.ArrayList;
import org.apache.log4j.Category;
import org.jboss.test.jca.bank.interfaces.AccountLocal;
import org.jboss.test.jca.bank.interfaces.AccountLocalHome;
public class CustomerBean
implements EntityBean
{
private Connection c;
public Integer id;
public String name;
public Collection accounts;
private EntityContext ctx;
public Integer getId()
{
return id;
}
public void setId(final Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(final String name)
{
this.name = name;
}
public Collection getAccounts()
{
return accounts;
}
public void addAccount(AccountLocal account)
{
try
{
account.setCustomerId(id);
accounts.add(account);
}
catch (Exception e)
{
throw new EJBException("Problem in addAccount: " + e);
}
}
public void removeAccount(AccountLocal account)
{
try
{
accounts.remove(account);
account.remove();
}
catch (Exception e)
{
throw new EJBException("Problem in removeAccount: " + e);
}
}
public Integer ejbCreate(Integer id, String name)
throws CreateException
{
setId(id);
setName(name);
PreparedStatement ps = null;
try
{
ps = getConnection().prepareStatement("INSERT INTO CCBMPCUSTOMER (ID, NAME) VALUES (?, ?)");
ps.setInt(1, id.intValue());
ps.setString(2, name);
accounts = new ArrayList();
}
catch (Exception e)
{
throw new CreateException("Problem in ejbCreate: " + e);
} finally
{
try
{
ps.close();
}
catch (SQLException e)
{
Category.getInstance(getClass().getName()).info("SQLException closing ps: " + e);
} } return id;
}
public void ejbPostCreate(Integer id, String name)
{
}
public Integer ejbFindByPrimaryKey(final Integer id)
throws FinderException
{
PreparedStatement ps = null;
try
{
ps = getConnection().prepareStatement("SELECT ID FROM CCBMPCUSTOMER WHERE ID = ?");
ps.setInt(1, id.intValue());
ResultSet rs = ps.executeQuery();
if (!rs.next())
{
throw new ObjectNotFoundException("No such customer: " + id);
} rs.close();
}
catch (Exception e)
{
throw new EJBException("problem in findByPK: " + e);
} finally
{
try
{
ps.close();
}
catch (SQLException e)
{
Category.getInstance(getClass().getName()).info("SQLException closing ps: " + e);
} } return id;
}
public void ejbPostCreate(String id, String name)
{
}
public void ejbActivate()
{
}
public void ejbPassivate()
{
if (c != null)
{
try
{
c.close();
}
catch (SQLException e)
{
Category.getInstance(getClass().getName()).info("SQLException closing c: " + e);
} c = null;
} }
public void ejbLoad()
{
id = (Integer) ctx.getPrimaryKey();
if (id == null)
throw new EJBException("Null id!");
PreparedStatement ps = null;
try
{
ps = getConnection().prepareStatement("SELECT NAME FROM CCBMPCUSTOMER WHERE ID = ?");
ps.setInt(1, id.intValue());
ResultSet rs = ps.executeQuery();
if (rs.next() == false)
throw new NoSuchEntityException("Customer does not exist " + id.toString());
this.name = rs.getString(1);
AccountLocalHome ah = (AccountLocalHome)new InitialContext().lookup("AccountLocal");
accounts = ah.findByCustomerId(id);
}
catch (Exception e)
{
throw new EJBException("Problem in ejbLoad: " + e);
} finally
{
try
{
ps.close();
}
catch (SQLException e)
{
Category.getInstance(getClass().getName()).info("SQLException closing ps: " + e);
} } }
public void ejbStore()
{
PreparedStatement ps = null;
try
{
ps = getConnection().prepareStatement("UPDATE CCBMPCUSTOMER SET NAME = ? WHERE ID = ?");
ps.setString(1, name);
ps.setInt(2, id.intValue());
ps.execute();
}
catch (Exception e)
{
throw new EJBException("Problem in ejbStore: " + e);
} finally
{
try
{
ps.close();
}
catch (SQLException e)
{
Category.getInstance(getClass().getName()).info("SQLException closing ps: " + e);
} }
}
public void ejbRemove()
{
PreparedStatement ps = null;
try
{
ps = getConnection().prepareStatement("DELETE FROM CCBMPCUSTOMER WHERE ID = ?");
ps.setInt(1, id.intValue());
ps.execute();
}
catch (Exception e)
{
throw new EJBException("Problem in ejbRemove: " + e);
} finally
{
try
{
ps.close();
}
catch (SQLException e)
{
Category.getInstance(getClass().getName()).info("SQLException closing ps: " + e);
} }
}
public void setEntityContext(EntityContext ctx)
{
this.ctx = ctx;
}
public void unsetEntityContext()
{
ctx = null;
}
private Connection getConnection() throws Exception
{
if (c == null)
{
DataSource ds = (DataSource)new InitialContext().lookup("java:/DefaultDS");
c = ds.getConnection();
}
return c;
}
}