package ejava.examples.personnel.ejb20;

import ejava.examples.personnel.Person;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.EJBException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Set;

/**
   This class implements the business and callback methods for the Person
   bean. It is implemented using CMP and simple attributes, so many of the
   container callback methods are no-oped.
*/
public abstract class PersonEJB implements PersonLBI, PersonBI, EntityBean {
   public abstract String getId();
   public abstract void setId(String id);
   public abstract String getFirstName();
   public abstract void setFirstName(String fname);
   public abstract String getLastName();
   public abstract void setLastName(String lname);
   public abstract String getAddress();
   public abstract void setAddress(String address);
   public abstract String getPhoneNumber();
   public abstract void setPhoneNumber(String number);
   public abstract Set ejbSelectPhoneNumbers() throws FinderException;
   
   public Person getValues() {
      return new Person(getId(), getFirstName(), getLastName(),
                        getAddress(), getPhoneNumber());
   }

   public void setValues(Person values) {
      setFirstName(values.getFirstName());
      setLastName(values.getLastName());
      setAddress(values.getAddress());
      setPhoneNumber(values.getPhoneNumber());
   }

   public Set ejbHomeGetPhoneNumbers() throws FinderException {
      return ejbSelectPhoneNumbers();
   }

   /**
      This version of the create is called to created an empty person.
   */
   public PersonPK ejbCreate(String id) {
      setId(id);
      return null;
   }
   public void ejbPostCreate(String id) {}

   /**
      This version of the create is called to create a person from a values
      object.
   */
   public PersonPK ejbCreate(Person values) {
      setId(values.getId());
      setFirstName(values.getFirstName());
      setLastName(values.getLastName());
      setAddress(values.getAddress());
      setPhoneNumber(values.getPhoneNumber());
      return null;
   }
   public void ejbPostCreate(Person id) {}

   /**
      This version of the create is called to create a person from
      individual values.
   */
   public PersonPK ejbCreate(String id, String firstName, String lastName,
                          String address, String phone) {
      setId(id);
      setFirstName(firstName);
      setLastName(lastName);
      setAddress(address);
      setPhoneNumber(phone);
      return null;
   }
   public void ejbPostCreate(String id, String firstName, String lastName,
                          String address, String phone) {}

      //since all of our attributes are directly mappable to the database,
      //we do not have to implement the various persistence methods
   public void ejbPassivate() {}
   public void ejbActivate() {}
   public void ejbLoad() {}
   public void ejbStore() {}
   public void ejbRemove() {}

   /**
      This method is called after this class is instantiated and before it
      is placed in the pool. There are no actions required by this example,
      however a simple string property is supplied to demo how such a value
      would be supplied.
   */
   public void setEntityContext(EntityContext ctx) {
      try {
         Context jndi = new InitialContext();
         String example = (String)jndi.lookup("java:comp/env/example");
      }
      catch (NamingException ex) {
         throw new EJBException("error initializing PersonEJB:"+ex);
      }
   }
   public void unsetEntityContext() {}
}
