/* * * 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.util.*; import javax.ejb.*; import java.sql.*; import javax.sql.*; import java.util.*; import javax.naming.*; public class StockEJB implements EntityBean { private String productId; private int quantityOnHand; private int reorderLevel; private EntityContext context; private Connection con; private String dbName = "java:comp/env/jdbc/StockDB"; public void updateOnHand(int quantity) { System.out.println("StockEJB: updateOnHand()"); quantityOnHand -= quantity; } public boolean reorderNeeded() { System.out.println("StockEJB: reorderNeeded()"); return (quantityOnHand <= reorderLevel); } public String ejbCreate(String productId, int quantityOnHand, int reorderLevel) { System.out.println("StockEJB: ejbCreate()"); try { insertRow(productId, quantityOnHand, reorderLevel); } catch (Exception ex) { throw new EJBException("ejbCreate: " + ex.getMessage()); } this.productId = productId; this.quantityOnHand = quantityOnHand; this.reorderLevel = reorderLevel; return productId; } public String ejbFindByPrimaryKey(String primaryKey) throws FinderException { productId = primaryKey; boolean result; try { result = selectByPrimaryKey(); } catch (Exception ex) { throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); } if (result) { try { loadRow(); } catch (SQLException ex) { throw new EJBException("ejbFindByPrimaryKey: " + ex.getMessage()); } return productId; } else { throw new ObjectNotFoundException ("ejbFindByPrimaryKey: Row for primaryKey " + primaryKey + " not found."); } } public void ejbRemove() { try { deleteRow(productId); } catch (Exception ex) { throw new EJBException("ejbRemove: " + ex.getMessage()); } } public void ejbActivate() { productId = (String)context.getPrimaryKey(); } public void ejbPassivate() { productId = null; } public void setEntityContext(EntityContext context) { this.context = context; try { makeConnection(); } catch (Exception ex) { throw new EJBException("Unable to connect to database. " + ex.getMessage()); } } public void unsetEntityContext() { try { 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("ejbLoad: " + ex.getMessage()); } } public void ejbPostCreate(String productId, int quantityOnHand, int reorderLevel) { } private void debugState() { System.out.println(" productId = " + this.productId + " quantityOnHand = " + this.quantityOnHand + " reorderLevel = " + this.reorderLevel); } /*********************** Database Routines **********************/ private void makeConnection() throws NamingException, SQLException { InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup(dbName); con = ds.getConnection(); } private void insertRow (String productId, int quantityOnHand, int reorderLevel) throws SQLException { String insertStatement = "insert into stock values ( ? , ? , ? )"; PreparedStatement prepStmt = con.prepareStatement(insertStatement); prepStmt.setString(1, productId); prepStmt.setInt(2, quantityOnHand); prepStmt.setInt(3, reorderLevel); prepStmt.executeUpdate(); prepStmt.close(); } private void deleteRow(String id) throws SQLException { String deleteStatement = "delete from stock where product_id = ? "; PreparedStatement prepStmt = con.prepareStatement(deleteStatement); prepStmt.setString(1, id); prepStmt.executeUpdate(); prepStmt.close(); } private boolean selectByPrimaryKey() throws SQLException { String selectStatement = "select product_id " + "from stock where product_id = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, productId); ResultSet rs = prepStmt.executeQuery(); boolean result = rs.next(); prepStmt.close(); return result; } private void loadRow() throws SQLException { String selectStatement = "select quantity_on_hand, reorder_level " + "from stock where product_id = ? "; PreparedStatement prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, productId); ResultSet rs = prepStmt.executeQuery(); if (rs.next()) { quantityOnHand = rs.getInt(1); reorderLevel = rs.getInt(2); prepStmt.close(); } else { prepStmt.close(); throw new NoSuchEntityException("loadRow: Row for productId " + productId + " not found in database."); } } private void storeRow() throws SQLException { String updateStatement = "update stock set quantity_on_hand = ? ," + "reorder_level = ? " + "where product_id = ? "; PreparedStatement prepStmt = con.prepareStatement(updateStatement); prepStmt.setInt(1, quantityOnHand); prepStmt.setInt(2, reorderLevel); prepStmt.setString(3, productId); int rowCount = prepStmt.executeUpdate(); prepStmt.close(); if (rowCount == 0) { throw new EJBException("Storing row for productId " + productId + " failed."); } } } // StockEJB