package ejava.ejb.personnel;

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

public class PersonBean implements EntityBean, PersonInfo {
   public String id_;
   public String firstName_;
   public String lastName_;
   public String address_;
   public String phoneNumber_;

   /** 
      Set when the state of the bean has changed and should
      be synchronized with the database. This is slightly
      container-specific.
   */
   private boolean dirty_ = false;
   public boolean isDirty()              { return dirty_; }

   public void setFirstName(String name) { firstName_ = name; dirty_= true; }
   public String getFirstName()          { return firstName_; }

   public void setLastName(String name)  { lastName_ = name; dirty_= true; }
   public String getLastName()           { return lastName_; }

   public void setAddress(String address) { address_ = address; dirty_= true; }
   public String getAddress()             { return address_; }

   public void setPhoneNumber(String number) 
                                       { phoneNumber_ = number; dirty_= true; }
   public String getPhoneNumber()      { return phoneNumber_; }

   public String toXML() {
      StringBuffer text = new StringBuffer();
      text.append("<ejava:person id=\"" + id_ + "\">\n");
      text.append("   <ejava:firstName>" + firstName_ + "</ejava:firstName>\n");
      text.append("   <ejava:lastName>" + lastName_ + "</ejava:lastName>\n");
      text.append("   <ejava:address>" + address_ + "</ejava:address>\n");
      text.append("   <ejava:phoneNumber>" +
                         phoneNumber_ + "</ejava:phoneNumber>\n");
      text.append("</ejava:person>\n");

      return text.toString();
   }

   /**
   */
   public PersonPK ejbCreate(String id) {
      id_ = id;
      return null;  //as per spec
   }
   public void ejbPostCreate(String id) {}

   /**
   */
   public PersonPK ejbCreate(String id, String firstName, String lastName,
       String address, String phoneNumber) {

       id_ = id;
       firstName_ = firstName;
       lastName_ = lastName;
       address_ = address;
       phoneNumber_ = phoneNumber;

       return null; //as per spec
   }
   public void ejbPostCreate(String id, String firstName, String lastName,
      String address, String phoneNumber) {}

   /**
      This method is invoked when an instance of this bean class is taken
      from the bean pool and associated with an EJBObject.
   */
   public void ejbActivate() {}

   /**
      This method is invoked when an instance if this bean class is about
      to be dis-associated from an EJBObject and returned to the bean pool.
   */
   public void ejbPassivate() {}

   /**
      This method is invoked prior to invoking a business or ejbRemove method
      as a part of synchronizing the cached values with the database. This
      method must make sure any non-container loaded attributes are fully
      initialized by the container loaded values.
   */
   public void ejbLoad()      { dirty_ = false; }

   /**
      This method is invoked just after a business method has changed the
      state of the cached values. This method must prepare the container-stored
      values for the container.
   */
   public void ejbStore() { dirty_ = false; }

   /**
      This method will be invoked just prior to removing the object from the
      database.
   */
   public void ejbRemove() {}

   private EntityContext entityCtx_;
   public void setEntityContext(EntityContext ctx) { entityCtx_ = ctx; }
   public void unsetEntityContext()                { entityCtx_ = null; }
}
