/* * * Copyright 2000 Sun Microsystems, Inc. All Rights Reserved. * * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. * */ import java.sql.*; import javax.sql.*; import java.util.*; import javax.ejb.*; import javax.naming.*; import javax.rmi.PortableRemoteObject; public class SalesRepEJB implements EntityBean { private String salesRepId; private String name; private ArrayList customerIds; private Connection con; private String dbName = "java:comp/env/jdbc/SalesDB"; private EntityContext context; private CustomerHome customerHome; public ArrayList getCustomerIds() { System.out.println("in getCustomerIds"); return customerIds; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String ejbCreate(String salesRepId, String name) throws CreateException { System.out.println("in ejbCreate"); try { insertSalesRep(salesRepId, name); } catch (Exception ex) { throw new EJBException("ejbCreate: " + ex.getMessage()); } this.salesRepId = salesRepId; this.name = name; System.out.println("about to leave ejbCreate"); return salesRepId; } public String ejbFindByPrimaryKey(String primaryKey) throws FinderException { boolean result; try { result = selectByPrimaryKey(primaryKey); } catch (Exception ex) { throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); } if (result) { return primaryKey; } else { throw new ObjectNotFoundException ("Row for id " + primaryKey + " not found."); } } public void ejbRemove() { try { deleteSalesRep(salesRepId); } catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } } public void setEntityContext(EntityContext context) { System.out.println("in setEntityContext"); this.context = context; customerIds = new ArrayList(); try { makeConnection(); Context initial = new InitialContext(); Object objref = initial.lookup("java:comp/env/ejb/Customer"); customerHome = (CustomerHome)PortableRemoteObject.narrow(objref, CustomerHome.class); } catch (Exception ex) { throw new EJBException("setEntityContext: " + ex.getMessage()); } System.out.println("leaving setEntityContext"); } public void unsetEntityContext() { try { con.close(); } catch (SQLException ex) { throw new EJBException("unsetEntityContext: " + ex.getMessage()); } } public void ejbActivate() { salesRepId = (String)context.getPrimaryKey(); } public void ejbPassivate() { salesRepId = null; } public void ejbLoad() { System.out.println("in ejbLoad"); try { loadSalesRep(); loadCustomerIds(); } catch (Exception ex) { throw new EJBException("ejbLoad: " + ex.getMessage()); } System.out.println("leaving ejbLoad"); } private void loadCustomerIds() { System.out.println("in loadCustomerIds"); customerIds.clear(); try { Collection c = customerHome.findBySalesRep(salesRepId); Iterator i=c.iterator(); while (i.hasNext()) { Customer customer = (Customer)i.next(); String id = (String)customer.getPrimaryKey(); System.out.println("adding " + id + " to list"); customerIds.add(id); } } catch (Exception ex) { throw new EJBException("Exception in loadCustomerIds: " + ex.getMessage()); } } public void ejbStore() { System.out.println("in ejbStore"); try { storeSalesRep(); } catch (Exception ex) { throw new EJBException("ejbStore: " + ex.getMessage()); } System.out.println("leaving ejbStore"); } public void ejbPostCreate(String salesRepId, String name) { } /*********************** Database Routines *************************/ private void makeConnection() throws NamingException, SQLException { InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup(dbName); con = ds.getConnection(); } private void insertSalesRep (String salesRepId, String name) throws SQLException { System.out.println("in insertSalesRep"); String insertStatement = "insert into salesrep values ( ? , ? )"; PreparedStatement prepStmt = con.prepareStatement(insertStatement); prepStmt.setString(1, salesRepId); prepStmt.setString(2, name); prepStmt.executeUpdate(); prepStmt.close(); System.out.println("leaving insertSalesRep"); } private boolean selectByPrimaryKey(String primaryKey) throws SQLException { String selectStatement = "select salesrepid " + "from salesrep where salesrepid = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, primaryKey); ResultSet rs = prepStmt.executeQuery(); boolean result = rs.next(); prepStmt.close(); return result; } private void deleteSalesRep(String salesRepId) throws SQLException { String deleteStatement = "delete from salesrep " + "where salesrepid = ?"; PreparedStatement prepStmt = con.prepareStatement(deleteStatement); prepStmt.setString(1, salesRepId); prepStmt.executeUpdate(); prepStmt.close(); } private void loadSalesRep() throws SQLException { String selectStatement = "select name " + "from salesRep where salesrepid = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, salesRepId); ResultSet rs = prepStmt.executeQuery(); if (rs.next()) { name = rs.getString(1); prepStmt.close(); } else { prepStmt.close(); throw new NoSuchEntityException("Row for salesRepId " + salesRepId + " not found in database."); } } private void storeSalesRep() throws SQLException { String updateStatement = "update salesrep set name = ? " + "where salesrepid = ?"; PreparedStatement prepStmt = con.prepareStatement(updateStatement); prepStmt.setString(1, name); prepStmt.setString(2, salesRepId); int rowCount = prepStmt.executeUpdate(); prepStmt.close(); if (rowCount == 0) { throw new EJBException("Storing row for salesRepId " + salesRepId + " failed."); } } } // SalesRepEJB