package ejava.examples.ejb.entity.bean;

import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import javax.ejb.EJBException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.Collection;

/**
   This class provides an example of Bean Managed Persistence.
*/
public class BookBMP extends BookEJB {
   private DataSource dataSource_;
   private EntityContext ctx_;
   private String id_;
   private String title_;
   private String author_;
   private String topic_;

   public void setId(String id)              { id_ = id; }
   public String getId()                     { return id_; }
   public void setAuthor(String author)      { author_ = author; }
   public String getAuthor()                 { return author_; }
   public void setTitle(String title)        { title_ = title; }
   public String getTitle()                  { return title_; }
   public void setTopic(String topic)        { topic_ = topic; }
   public String getTopic()                  { return topic_; }

   public String ejbCreate(
      String id, String title, String author, String topic) {
      super.ejbCreate(id, title, author, topic);

      Connection conn = null;
      PreparedStatement pstatement = null;
      try {
         conn = dataSource_.getConnection();
	 pstatement = conn.prepareStatement(
	    "insert into Book (id, title, author, topic) " +
	    "values (?, ?, ?, ?)");
         pstatement.setString(1,id_);
	 pstatement.setString(2,title_);
	 pstatement.setString(3,author_);
	 pstatement.setString(4,topic_);
	 pstatement.execute();
         return id_; 
      }
      catch(SQLException ex) {
         throw new EJBException(ex);
      }
      finally {
         try { if (pstatement!=null) pstatement.close(); } catch (Exception e){}
         try { if (conn!=null) conn.close(); } catch (Exception e){}
      }
   }

   public void ejbPostCreate(
      String id, String title, String author, String topic) { 
      super.ejbPostCreate(id, title, author, topic);
   }

   public String ejbCreate(String id) {
      super.ejbCreate(id);

      Connection conn = null;
      PreparedStatement pstatement = null;
      try {
         conn = dataSource_.getConnection();
	 pstatement = conn.prepareStatement(
	    "insert into Book (id, title, author, topic) " +
	    "values (?, ?, ?, ?)");
         pstatement.setString(1,id_);
	 pstatement.setString(2,title_);
	 pstatement.setString(3,author_);
	 pstatement.setString(4,topic_);
	 pstatement.executeUpdate();
         return id_; 
      }
      catch(SQLException ex) {
         throw new EJBException(getText(ex));
      }
      finally {
         try { if (pstatement!=null) pstatement.close(); } catch (Exception e){}
         try { if (conn!=null) conn.close(); } catch (Exception e){}
      }
   }
   public void ejbPostCreate(String id) {
      super.ejbPostCreate(id);
   }

   public void ejbActivate() {
      super.ejbActivate();
   }

   public void ejbPassivate() {
      super.ejbPassivate();
   }

   public void ejbLoad() {
      Connection conn = null;
      PreparedStatement pstatement = null;
      ResultSet rs = null;
      try {
         conn = dataSource_.getConnection();
	 pstatement = conn.prepareStatement(
	    "select id, title, author, topic from Book " +
	    "where id = ?");
         pstatement.setString(1, (String)ctx_.getPrimaryKey());
	 rs = pstatement.executeQuery();
	 if (rs.next()) {
	    id_ = rs.getString("id");
	    title_ = rs.getString("title");
	    author_ = rs.getString("author");
	    topic_ = rs.getString("topic");
	    super.ejbLoad();
	 }
	 else {
	    throw new EJBException("unable to locate row");
	 }
      }
      catch(SQLException ex) {
         throw new EJBException(getText(ex));
      }
      finally {
         try { if (rs!=null) rs.close(); } catch (Exception e){}
         try { if (pstatement!=null) pstatement.close(); } catch (Exception e){}
         try { if (conn!=null) conn.close(); } catch (Exception e){}
      }
   }

   public void ejbStore() {
      Connection conn = null;
      PreparedStatement pstatement = null;
      try {
         super.ejbStore();
         conn = dataSource_.getConnection();
	 pstatement = conn.prepareStatement(
	    "update Book set title=?, author=?, topic=? " +
	    "where id = ?");
	 pstatement.setString(1,title_);
	 pstatement.setString(2,author_);
	 pstatement.setString(3,topic_);
         pstatement.setString(4,id_);
	 pstatement.executeUpdate();
      }
      catch(SQLException ex) {
         throw new EJBException(getText(ex));
      }
      finally {
         try { if (pstatement!=null) pstatement.close(); } catch (Exception e){}
         try { if (conn!=null) conn.close(); } catch (Exception e){}
      }
   }

   public void ejbRemove() {

      Connection conn = null;
      PreparedStatement pstatement = null;
      try {
         super.ejbRemove();
         conn = dataSource_.getConnection();
	 pstatement = conn.prepareStatement(
	    "delete from Book " +
	    "where id = ?");
         pstatement.setString(1, (String)ctx_.getPrimaryKey());
	 pstatement.executeUpdate();
      }
      catch(SQLException ex) {
         throw new EJBException(getText(ex));
      }
      finally {
         try { if (pstatement!=null) pstatement.close(); } catch (Exception e){}
         try { if (conn!=null) conn.close(); } catch (Exception e){}
      }
   }

   public String ejbFindByPrimaryKey(String pk) throws FinderException {
      Connection conn = null;
      PreparedStatement pstatement = null;
      ResultSet rs = null;
      try {
         conn = dataSource_.getConnection();
	 pstatement = conn.prepareStatement(
	    "select id from Book " +
	    "where id = ?");
         pstatement.setString(1, pk);
	 rs = pstatement.executeQuery();
	 if (rs.next()) {
	    return rs.getString("id");
	 }
	 else {
	    throw new ObjectNotFoundException(pk + " no found");
	 }
      }
      catch(SQLException ex) {
         throw new EJBException(getText(ex));
      }
      finally {
         try { if (rs!=null) rs.close(); } catch (Exception e){}
         try { if (pstatement!=null) pstatement.close(); } catch (Exception e){}
         try { if (conn!=null) conn.close(); } catch (Exception e){}
      }
   }

   public Collection ejbFindAll() throws FinderException {
      Connection conn = null;
      PreparedStatement pstatement = null;
      ResultSet rs = null;
      try {
         Vector pKeys = new Vector();
         conn = dataSource_.getConnection();
	 pstatement = conn.prepareStatement(
	    "select id from Book ");
	 rs = pstatement.executeQuery();
	 while (rs.next()) {
	    pKeys.add(rs.getString("id"));
	 }
	 return pKeys;
      }
      catch(SQLException ex) {
         throw new EJBException(getText(ex));
      }
      finally {
         try { if (rs!=null) rs.close(); } catch (Exception e){}
         try { if (pstatement!=null) pstatement.close(); } catch (Exception e){}
         try { if (conn!=null) conn.close(); } catch (Exception e){}
      }
   }

   public void setEntityContext(EntityContext ctx) { 
      super.setEntityContext(ctx);
      ctx_ = ctx; 

      InitialContext jndi = null;
      try {
         jndi = new InitialContext();
	 dataSource_ = (DataSource)jndi.lookup("java:comp/env/jdbc/mydb");
      }
      catch (NamingException ex) {
         throw new EJBException(ex);
      }
      finally {
         try { if (jndi!=null) jndi.close(); } catch (Exception e) {}     
      }
   }

   public void unsetEntityContext() { 
      super.unsetEntityContext();
      ctx_ = null; 
   }

   private String getText(SQLException sqlex) {
      StringBuffer text = new StringBuffer(sqlex.toString());
      sqlex = sqlex.getNextException(); 
      while (sqlex!=null) {
         text.append(":" + sqlex);
	 sqlex = sqlex.getNextException();
      }
      return text.toString();
   }

}

