package com.papajo.examples.pab.struts;

import com.papajo.examples.pab.struts.*;
import com.papajo.examples.pab.*;

public class ContactForm extends ValidatingFormAdapter
{
  private String id = "0",
                 firstName,
                 lastName,
                 middleName,
                 emailAddress,
                 streetAddress,
                 city,
                 state,
                 postalCode,
                 phone,
                 birthDate;

  public final String getId            (){ return this.id; }
  public final String getFirstName     (){ return this.firstName; }
  public final String getLastName      (){ return this.lastName; }
  public final String getMiddleName    (){ return this.middleName; }
  public final String getEmailAddress  (){ return this.emailAddress; }
  public final String getStreetAddress (){ return this.streetAddress; }
  public final String getCity          (){ return this.city; }
  public final String getState         (){ return this.state; }
  public final String getPostalCode    (){ return this.postalCode; }
  public final String getPhone         (){ return this.phone; }
  public final String getBirthDate     (){ return this.birthDate; }

  public void setId            (String s){ this.id = s; }
  public void setFirstName     (String s){ this.firstName = s; }
  public void setLastName      (String s){ this.lastName = s; }
  public void setMiddleName    (String s){ this.middleName = s; }
  public void setEmailAddress  (String s){ this.emailAddress = s; }
  public void setStreetAddress (String s){ this.streetAddress = s; }
  public void setCity          (String s){ this.city = s; }
  public void setState         (String s){ this.state = s; }
  public void setPostalCode    (String s){ this.postalCode = s; }
  public void setPhone         (String s){ this.phone = s; }
  public void setBirthDate     (String s){ this.birthDate = s; }

  public void populate(ContactBean contact)
  {
    this.setId            ("" + contact.getId());
    this.setFirstName     (null == contact.getFirstName()     ? null : contact.getFirstName());
    this.setLastName      (null == contact.getLastName()      ? null : contact.getLastName());
    this.setMiddleName    (null == contact.getMiddleName()    ? null : contact.getMiddleName());
    this.setEmailAddress  (null == contact.getEmailAddress()  ? null : contact.getEmailAddress());
    this.setStreetAddress (null == contact.getStreetAddress() ? null : contact.getStreetAddress());
    this.setCity          (null == contact.getCity()          ? null : contact.getCity());
    this.setState         (null == contact.getState()         ? null : contact.getState());
    this.setPostalCode    (null == contact.getPostalCode()    ? null : contact.getPostalCode());
    this.setPhone         (null == contact.getPhone()         ? null : contact.getPhone());
    this.setBirthDate     (null == contact.getBirthDate()     ? null : SC.formatDate(contact.getBirthDate()));
  }

  public ContactBean getContact()
  {
    ContactBean contact = new ContactBean();
    //set our id, default to zero if we can't parse getId()
    try {
      contact.setId( Integer.parseInt(getId()));
    } catch (NumberFormatException e) {
      contact.setId(0);
    }
    // populate the rest of our contact fields
    contact.setFirstName     (null == getFirstName()     ? null : getFirstName());
    contact.setLastName      (null == getLastName()      ? null : getLastName());
    contact.setMiddleName    (null == getMiddleName()    ? null : getMiddleName());
    contact.setEmailAddress  (null == getEmailAddress()  ? null : getEmailAddress());
    contact.setStreetAddress (null == getStreetAddress() ? null : getStreetAddress());
    contact.setCity          (null == getCity()          ? null : getCity());
    contact.setState         (null == getState()         ? null : getState());
    contact.setPostalCode    (null == getPostalCode()    ? null : getPostalCode());
    contact.setPhone         (null == getPhone()         ? null : getPhone());
    contact.setBirthDate     (null == getBirthDate()     ? null : SC.toDate(getBirthDate()));
    return contact;
  }

  public String toString()
  {
    StringBuffer sb = new StringBuffer();
    sb.append("\n{(id=");         sb.append(id);            sb.append("),\n");
    sb.append("(firstName=");     sb.append(firstName);     sb.append("),\n");
    sb.append("(lastName=");      sb.append(lastName);      sb.append("),\n");
    sb.append("(middleName=");    sb.append(middleName);    sb.append("),\n");
    sb.append("(emailAddress=");  sb.append(emailAddress);  sb.append("),\n");
    sb.append("(streetAddress="); sb.append(streetAddress); sb.append("),\n");
    sb.append("(city=");          sb.append(city);          sb.append("),\n");
    sb.append("(state=");         sb.append(state);         sb.append("),\n");
    sb.append("(postalCode=");    sb.append(postalCode);    sb.append("),\n");
    sb.append("(phone=");         sb.append(phone);         sb.append("),\n");
    sb.append("(birthDate=");     sb.append(birthDate);     sb.append(")},\n");
    return sb.toString();
  }
}