package org.jboss.test.jca.bank.ejb;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Collection;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.NoSuchEntityException;
import javax.ejb.ObjectNotFoundException;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import java.util.ArrayList;
import java.sql.SQLException;
import org.apache.log4j.Category;
public class AccountBean
implements EntityBean
{
private Connection c;
private Integer id;
private int balance;
private Integer customerId;
private EntityContext ctx;
public Integer getId()
{
return id;
}
public void setId(final Integer id)
{
this.id = id;
}
public int getBalance()
{
return balance;
}
public void setBalance(final int balance)
{
this.balance = balance;
}
public Integer getCustomerId()
{
return customerId;
}
public void setCustomerId(final Integer customerId)
{
this.customerId = customerId;
}
public void deposit(int amount)
{
setBalance(getBalance()+amount);
}
public void withdraw(int amount)
{
setBalance(getBalance()-amount);
}
public Integer ejbCreate(final Integer id, final int balance, final Integer customerId)
throws CreateException
{
setId(id);
setBalance(balance);
setCustomerId(customerId);
PreparedStatement ps = null;
try
{
ps = getConnection().prepareStatement("INSERT INTO CCBMPACCOUNT (ID, BALANCE, CUSTOMERID) VALUES (?, ?, ?)");
ps.setInt(1, id.intValue());
ps.setInt(2, balance);
ps.setObject(3, customerId);
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception in ejbCreate", e);
throw new CreateException("Can't insert: " + e);
} finally
{
try
{
if (ps != null) {ps.close();}
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception closing ps: " + e);
}
} return id;
}
public void ejbPostCreate(final Integer id, final int balance, final Integer customerId)
{
}
public Integer ejbFindByPrimaryKey(final Integer id)
throws FinderException
{
PreparedStatement ps = null;
try
{
ps = getConnection().prepareStatement("SELECT ID FROM CCBMPACCOUNT WHERE ID = ?");
ps.setInt(1, id.intValue());
ResultSet rs = ps.executeQuery();
if (!rs.next())
{
throw new ObjectNotFoundException("No such account: " + id);
} rs.close();
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception in findByPK", e);
throw new EJBException("Problem in findByPrimaryKey: " + e);
} finally
{
try
{
if (ps != null) {ps.close();}
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception closing ps: " + e);
} } return id;
}
public Collection ejbFindByCustomerId(final Integer customerId)
{
PreparedStatement ps = null;
try
{
ps = getConnection().prepareStatement("SELECT ID FROM CCBMPACCOUNT WHERE CUSTOMERID = ?");
ps.setInt(1, customerId.intValue());
ResultSet rs = ps.executeQuery();
Collection result = new ArrayList();
while (rs.next())
{
result.add(new Integer(rs.getInt(1)));
} rs.close();
return result;
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception in findbyCustomerID", e);
throw new EJBException(e);
} finally
{
try
{
if (ps != null) {ps.close();}
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception closing ps: " + e);
} } }
public void ejbActivate()
{
}
public void ejbPassivate()
{
if (c != null)
{
try
{
c.close();
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception closing c: " + e);
} c = null;
} }
public void ejbLoad()
{
id = (Integer) ctx.getPrimaryKey();
if (id == null)
{
Category.getInstance(getClass().getName()).info("null id!");
}
PreparedStatement ps = null;
try
{
ps = getConnection().prepareStatement("SELECT BALANCE, CUSTOMERID FROM CCBMPACCOUNT WHERE ID = ?");
if (ps == null)
{
Category.getInstance(getClass().getName()).info("WFT? null ps!");
}
ps.setInt(1, id.intValue());
ResultSet rs = ps.executeQuery();
if (rs.next() == false)
throw new NoSuchEntityException("Account does not exist " + id.toString());
this.balance = rs.getInt(1);
this.customerId = (Integer)rs.getObject(2);
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception in ejbLoad", e);
throw new EJBException(e);
} finally
{
try
{
if (ps != null) {ps.close();}
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception closing ps: " + e);
} } }
public void ejbStore()
{
PreparedStatement ps = null;
try
{
ps = getConnection().prepareStatement("UPDATE CCBMPACCOUNT SET BALANCE = ?, CUSTOMERID = ? WHERE ID = ?");
ps.setInt(1, balance);
ps.setObject(2, customerId);
ps.setInt(3, id.intValue());
ps.execute();
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception in ejbStore", e);
throw new EJBException(e);
} finally
{
try
{
if (ps != null) {ps.close();}
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception closing ps: " + e);
} }
}
public void ejbRemove()
{
PreparedStatement ps = null;
try
{
ps = getConnection().prepareStatement("DELETE FROM CCBMPACCOUNT WHERE ID = ?");
ps.setInt(1, id.intValue());
ps.execute();
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception in ejbRemove", e);
throw new EJBException(e);
} finally
{
try
{
if (ps != null) {ps.close();}
}
catch (Exception e)
{
Category.getInstance(getClass().getName()).info("Exception 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;
}
}