package cakeinabox.beans.AccountEJB; import javax.ejb.*; import java.rmi.RemoteException; import java.sql.*; import javax.sql.*; import java.util.*; import javax.naming.*; import org.postgresql.*; public class AccountBean implements EntityBean { private Integer m_id; private double m_credit; private double m_discount; private java.util.Date m_openedDate; private java.util.Date m_closedDate; private Connection m_con; private EntityContext m_context; public AccountBean() { } /////////////////////// // container methods // /////////////////////// public Integer ejbCreate(Integer id, double credit , double discount, java.util.Date openedDate, java.util.Date closedDate) throws CreateException { try { insertRow(id, credit, discount, openedDate, closedDate); } catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } this.m_id = id; this.m_credit = credit; this.m_discount = discount; this.m_openedDate = openedDate; this.m_closedDate = closedDate; return this.m_id; } public void ejbPostCreate (Integer id, double credit , double discount, java.util.Date openedDate, java.util.Date closedDate) throws CreateException { } public void ejbRemove() { try { deleteRow(m_id); } catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } } public void setEntityContext(EntityContext context) { this.m_context = context; try { makeConnection(); } catch (Exception ex) { throw new EJBException("Unable to connect to database. " + ex.getMessage()); } } public void unsetEntityContext() { m_context = null; } public void ejbActivate() { this.m_id = ((Integer)m_context.getPrimaryKey()); } public void ejbPassivate() { this.m_id = null; this.m_credit = 0; this.m_discount = 0; this.m_openedDate = null; this.m_openedDate = null; try { m_con.close(); } catch (SQLException ex) { throw new EJBException("unsetEntityContext: " + ex.getMessage()); } } public void ejbLoad() { try { loadRow(); } catch (Exception ex) { throw new EJBException("ejbLoad: " + ex.getMessage()); } } public void ejbStore() { try { storeRow(); } catch (Exception ex) { throw new EJBException("ejbStore: " + ex.getMessage()); } } public Collection ejbFindAll() throws FinderException { Collection result; try { result = selectAll(); } catch (Exception ex) { throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); } if(result.isEmpty()==false) { return result; } else { throw new ObjectNotFoundException("No rows found."); } } public Integer ejbFindByPrimaryKey(Integer primaryKey) throws FinderException { boolean result; try { result = selectByPrimaryKey(primaryKey); } catch (Exception ex) { throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); } if(result==true) { return primaryKey; } else { throw new ObjectNotFoundException("Row for id " + primaryKey + " not found."); } } ////////////////////// // business methods // ////////////////////// // set methods public void setId(Integer id) { m_id = id; } public void setCredit(double credit) { m_credit = credit; } public void setDiscount(double discount) { m_discount = discount; } public void setOpenedDate(java.util.Date openedDate) { m_openedDate = openedDate; } public void setClosedDate(java.util.Date closedDate) { m_closedDate = closedDate; } //get methods public Integer getId() { return m_id; } public double getCredit() { return m_credit; } public double getDiscount() { return m_discount; } public java.util.Date getOpenedDate() { return m_openedDate; } public java.util.Date getClosedDate() { return m_closedDate; } /////////////////////// // Database Routines // /////////////////////// private void makeConnection() throws NamingException, SQLException { try { Class.forName("org.postgresql.Driver"); } catch(ClassNotFoundException e) { System.err.println("Driver not found!"); e.printStackTrace(); } m_con = DriverManager.getConnection("jdbc:postgresql://localhost/cakeinabox","cakeinabox","xxxxxxxx"); m_con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); } private boolean selectByPrimaryKey(Integer primaryKey) throws SQLException { String selectStatement = "select id from Account where id = ? "; PreparedStatement prepStmt = m_con.prepareStatement(selectStatement); prepStmt.setInt(1, primaryKey.intValue()); ResultSet rs = prepStmt.executeQuery(); boolean result = rs.next(); prepStmt.close(); return result; } private Collection selectAll() throws SQLException { String selectStatement = "select id from account"; PreparedStatement prepStmt = m_con.prepareStatement(selectStatement); ResultSet rs = prepStmt.executeQuery(); ArrayList a = new ArrayList(); while (rs.next()) { a.add(new Integer(rs.getInt(1))); } prepStmt.close(); return a; } private void insertRow (Integer id, double credit, double discount, java.util.Date openedDate, java.util.Date closedDate) throws SQLException { String insertStatement = "insert into Account (id, credit, discount, openedDate, closedDate) " + "values ( ? , ? , ? , ? , ?)"; PreparedStatement prepStmt = m_con.prepareStatement(insertStatement); prepStmt.setInt(1, id.intValue()); prepStmt.setDouble(2, credit); prepStmt.setDouble(3, discount); prepStmt.setDate(4, new java.sql.Date(openedDate.getTime())); prepStmt.setDate(5, new java.sql.Date(closedDate.getTime())); prepStmt.executeUpdate(); prepStmt.close(); } private void deleteRow(Integer id) throws SQLException { String deleteStatement = "delete from Account where id = ? "; PreparedStatement prepStmt = m_con.prepareStatement(deleteStatement); prepStmt.setInt(1, id.intValue()); prepStmt.executeUpdate(); prepStmt.close(); } private void loadRow() throws SQLException { String selectStatement = "select id, credit, discount, openedDate, closedDate " + "from Account where id = ? "; PreparedStatement prepStmt = m_con.prepareStatement(selectStatement); prepStmt.setInt(1, this.m_id.intValue()); ResultSet rs = prepStmt.executeQuery(); if (rs.next()) { this.m_id = new Integer(rs.getInt(1)); this.m_credit = rs.getDouble(2); this.m_discount = rs.getDouble(3); this.m_openedDate = new java.util.Date(rs.getDate(4).getTime()); this.m_closedDate = new java.util.Date(rs.getDate(5).getTime()); prepStmt.close(); } else { prepStmt.close(); throw new NoSuchEntityException("Row for id " + m_id + " not found in database."); } } private void storeRow() throws SQLException { String updateStatement = "update account set " + "credit = ?, " + "discount = ?, " + "openedDate = ?, " + "closedDate = ? " + "where id = ?"; PreparedStatement prepStmt = m_con.prepareStatement(updateStatement); prepStmt.setDouble(1, this.m_credit); prepStmt.setDouble(2, this.m_discount); prepStmt.setDate(3, new java.sql.Date(this.m_openedDate.getTime())); prepStmt.setDate(4, new java.sql.Date(this.m_closedDate.getTime())); prepStmt.setInt(5, this.m_id.intValue()); int rowCount = prepStmt.executeUpdate(); prepStmt.close(); if (rowCount == 0) { throw new EJBException("Storing row for id " + m_id + " failed."); } } private void debugPrivateVariables(String method) { System.out.println("\n\nPrinting Private Variables - " + method + "\n\n"); System.out.println("id: " + this.m_id.intValue()); System.out.println("credit: " + this.m_credit); System.out.println("discount: " + this.m_discount); System.out.println("openedDate: " + this.m_openedDate); System.out.println("closedDate: " + this.m_closedDate + "\n\n"); } }