package ejava.examples.ejb.entity.bean;

import javax.ejb.EntityBean;
import javax.ejb.EntityContext;

/**
   This class is the abstract base class which the container (or BMP
   implementation) will supply the concrete implementation that will
   ultimately map it into the database.
*/
public abstract class BookEJB implements EntityBean {
   /** This is our reference into the container. */
   private EntityContext ctx_;

   //this is our abstract data model. It is mapped to the DB by the
   //container-generated class or a developer-written BMP class
   public abstract void setId(String id);
   public abstract String getId();
   public abstract void setAuthor(String author);
   public abstract String getAuthor();
   public abstract void setTitle(String title);
   public abstract String getTitle();
   public abstract void setTopic(String topic);
   public abstract String getTopic();

   /**
      This method must set the primary key for the object and optionally
      set any of the fields that will get mapped into the database.
   */
   public String ejbCreate(
      String id, String title, String author, String topic) {
      log("ejbCreate(" + id + ",...");
      setId(id);
      setTitle(title);
      setAuthor(author);
      setTopic(topic);
      return null;  //container gets primary key another way
   }

   /**
      This method is where we establish any relationships to our object.
   */
   public void ejbPostCreate(
      String id, String title, String author, String topic) { 
      log("ejbPostCreate(" + id + ",...");
   }

   public String ejbCreate(String id) {
      log("ejbCreate(" + id + ")");
      setId(id);
      return null;
   }
   public void ejbPostCreate(String id) {
      log("ejbPostCreate(" + id + ")");
   }

   /** 
      This method is invoked just after the bean is associated with an
      object. We have a primary key, but out abstract data model has not
      yet been synchronized with the database. We can allocate any
      object-specific resources (that do not depend on the database values)
      at this point.
   */
   public void ejbActivate() {
      log("ejbActivate:" + ctx_.getPrimaryKey());
   }

   /**
      This method is invoked just prior to disassociating the object from
      the bean. The state of the abstract data model has already been
      synchronized with the database. We should release any object-specific
      resources as this point.
   */
   public void ejbPassivate() {
      log("ejbPassivate:" + ctx_.getPrimaryKey());
   }

   /**
      This method is called just after the abstract data model has been
      restored from the database. This method should prepare any transient
      variables for use by the business methods as this time.
   */
   public void ejbLoad() {
      log("ejbLoad:" + getId() + ":" + getTitle());
   }

   /**
      This method is called just before the abstract data model gets stored
      to the database. This method should prepare the abstract data model
      variables for their stored state.
   */
   public void ejbStore() {
      log("ejbStore:" + getId() + ":" + getTitle());
   }

   /**
      This method is called just before the row(s) representing the
      abstract data model get removed from the database.
   */
   public void ejbRemove() {
      log("BookEJB.ejbRemove:" + getId() + ":" + getTitle());
   }

   /**
      This method is invoked after the bean class is instantiated and just
      prior to being available in the pool. The bean should acquire any
      object-neutral resources at this point. Note that the bean is not
      associated with an object at this point and any calls to the
      context's getPrimaryKey() will result in an exception.
   */
   public void setEntityContext(EntityContext ctx) {
      log("setEntityContext");
      ctx_ = ctx;
   }

   /** 
      This method is invoked just after removing the bean from the pool and
      finalizing it. The bean should release any object-neural resources at
      this point. Note that the bean is not associated with an object at
      this point and any attempt to access the primary key or abstract data
      model will result in an error.
   */
   public void unsetEntityContext() {
      log("unsetEntityContext");
      ctx_ = null;
   }

   private static final boolean PRINT_DEBUG = true;
   protected void log(String message) {
      if (PRINT_DEBUG == true) {
         System.out.println("BookEJB(" + this + ")." + message);
      }
   }
      
}

