/*
 * COPYRIGHT (c) 1998-1999 Jan-Henrik Haukeland, http://www.tildeslash.com/
 */

package ts.mail;

import java.util.Vector ;
import java.util.Hashtable ;
import java.util.Enumeration;

/** 
 * This class implements the MailModel interface and represents
 * a generic mail messsage.
 *
 * @author  Jan-Henrik Haukeland &lt;hauk@tildeslash.com&gt;
 * @version $Id: DefaultMailModel.java,v 1.4 1999/02/21 08:47:12 hauk Exp $ 
 */
public class DefaultMailModel implements MailModel
{

  // ---------------------- private members ---------------------- //

  /** Mail id field */
  private String      myId=                   NO_FIELD_VALUE;

  /** Mail from field */
  private String      myFromField=            NO_FIELD_VALUE;

  /** Mail to field */
  private String      myToField=              NO_FIELD_VALUE;

  /** Mail date */
  private String      myDateField=            NO_FIELD_VALUE;

  /** Mail subject field*/
  private String      mySubjectField=         NO_FIELD_VALUE;

  /** Mail Reply-to field*/
  private String      myReplyToField=         NO_FIELD_VALUE;

  /** Mail organization field*/
  private String      myOrganizationField=    NO_FIELD_VALUE; 

  /** Mail content */
  private String      myMailBody=             NO_FIELD_VALUE;


  /** 
   * Optional carbon-copy fields
   */
  private Vector      myCCHeaders;

  /** 
   * Optional extension-field X-something headers
   */
  private Hashtable   myXHeaders;


  // ----------------------- Constructors ----------------------- //


  /**
   * Create an empty object, with default values
   */
  public DefaultMailModel()
  {
    myXHeaders=  new Hashtable();
    myCCHeaders= new Vector();
  }
  

  // ------------------------- Set methods ------------------------ //
  
  
  /**
   * Set the mail message id
   *
   * @param aId - The mail message id
   * @exception IllegalArgumentException  Is thrown if the parameter is null
   */
  public void setIdField(String aId) throws IllegalArgumentException 
  {
    if ( aId == null )
	throw new IllegalArgumentException("Got null value for message id");
    myId= aId;
  }


  /**
   * Set the mail's to-field
   *
   * @param aToField - The mail's to-field
   * @exception IllegalArgumentException  Is thrown if the parameter is null
   */
  public void setToField(String aToField) throws IllegalArgumentException
  {
    if ( aToField == null )
	throw new IllegalArgumentException("Got null value for the "+
					   "to-field");
    myToField= aToField;
  }
  
  
  /**
   * Set the mail's from-field
   *
   * @param aFromField - The mail's from-field
   * @exception IllegalArgumentException  Is thrown if the parameter is null
   */
  public void setFromField(String aFromField) throws IllegalArgumentException
  {
    if ( aFromField == null )
	throw new IllegalArgumentException("Got null value for the "+
					   "from-field");
    myFromField= aFromField; 
  }


  /**
   * Set the mail date
   *
   * @param aDate - The mail's date as a string
   * @exception IllegalArgumentException  Is thrown if the parameter is null
   */
  public void setDateField(String aDate) throws IllegalArgumentException
  {
    if ( aDate == null )
	throw new IllegalArgumentException("Got null value for the "+
					   "date-field");
    myDateField= aDate; 
  } 


  /**
   * Set the mail's subject-field. No exception is thrown since
   * the subject field is allowed to be empty.
   *
   * @param aSubjectField - The mail's subject-field
   */
  public void setSubjectField(String aSubjectField) 
  {
    if ( aSubjectField == null )
	mySubjectField= NO_FIELD_VALUE; 
    else
	mySubjectField= aSubjectField;
  }

  /**
   * Set the mail's Reply-to field. No exception is thrown 
   * since this field is allowed to be empty
   *
   * @param aReplyToField - The mail's Reply-to field
   */
  public void setReplyToField(String aReplyToField)
  {
    if ( aReplyToField == null )
	myReplyToField= NO_FIELD_VALUE; 
    else
	myReplyToField= aReplyToField;
  }


  /**
   * Set the mail's organization field. No exception is thrown 
   * since this field is allowed to be empty
   *
   * @param aOrganizationField The mail's organization field
   */
  public void setOrganizationField(String aOrganizationField)
  {
    if ( aOrganizationField == null )
	myOrganizationField= NO_FIELD_VALUE; 
    else
	myOrganizationField= aOrganizationField;
  }


  /**
   * Set the mail's content. No exception is thrown since
   * the mail body is allowed to be empty.
   *
   * @param aMailBody - The mail's content
   */
  public void setMailBody(String aMailBody)
  {
    if ( aMailBody == null )
	myMailBody= NO_FIELD_VALUE; 
    else
	myMailBody= aMailBody;
  }


  /**
   * Set a Carbon-copy (cc:) address
   *
   * @param  aAddress The cc: mail address
   * @exception IllegalArgumentException  Is thrown if the parameter is null
   */
  public void setCCHeader(String aCopy) throws IllegalArgumentException 
  {
    if ( aCopy == null )
	throw
	    new IllegalArgumentException("Got null value for Cc header");
    
    myCCHeaders.addElement(aCopy);
  }


  /**
   * Set a X-something header. The method does no syntax checking
   * on the submitted header, because this class should not know
   * the format of any of it's fields.
   *
   * @param  aXkey   The X-header name
   * @param  aXvalue The X-Header's value
   * @exception IllegalArgumentException  Is thrown if the parameters is null
   *                                      or if the key does not start with
   *                                      "X-"
   */
  public void setXHeader(String aXkey, 
			 String aXvalue) throws IllegalArgumentException
  {
    if ( aXkey == null )
	throw
	    new IllegalArgumentException("Got null value for X-key header");

    if ( aXkey.indexOf("X-") != 0 )
	throw
	    new IllegalArgumentException("X header: "+aXkey+" does not "+
					 "start with 'X-'");

    if ( aXvalue == null )
	throw
	    new IllegalArgumentException("Got null value for X-value header");

    myXHeaders.put(aXkey, aXvalue);
  }  


  
  // ------------------------- Get methods ------------------------ //


  /**
   * @return The mail message id
   */
  public String getIdField() 
  {
    return myId;
  }


  /**
   * @return  Return the to field
   */
  public String getToField()
  {
    return myToField;
  }


  /**
   * @return  Return the from field
   */
  public String getFromField()
  {
    return myFromField;
  }


  /**
   * @return The date field
   */
  public String getDateField()
  {
    return myDateField;
  }


  /**
   * @return  Return the subject field
   */
  public String getSubjectField()
  {
    return mySubjectField;
  }


  /**
   * @return  Return the reply-to field
   */
  public String getReplyToField()
  {
    return myReplyToField;
  }


  /**
   * @return  Return the organization field
   */
  public String getOrganizationField()
  {
    return myOrganizationField;
  }


  /**
   * @return An enumeration of the Carbon-copy headers
   */
  public Enumeration getCCHeaders()
  {
    return myCCHeaders.elements();
  }


  /**
   * @return An enumeration of the X-header keys
   */
  public Enumeration getXHeaderKeys()
  {
    return myXHeaders.keys();
  }


  /**
   * @return The value for the given X-header key
   */
  public String getXHeaderValue(String aKey)
  {
    return (String)myXHeaders.get(aKey);
  }


  /**
   * @return  Return the mail body 
   */
  public String getMailBody()
  {
    return myMailBody;
  }



  // -------------------------------------------------------------- //

  /**
   * @return true if the mail has any cc headers
   */
  public boolean hasCCHeaders()
  {
    return myCCHeaders.isEmpty()?false:true;
  }


  /**
   * @return true if the mail has any X- headers
   */
  public boolean hasXHeaders()
  {
    return myXHeaders.isEmpty()?false:true;
  } 


  /**
   * This method validate the mail message fields. An exception is 
   * thrown if any of the required fields is empty. A warning is
   * printed if the subject-field or mail body are empty.
   *
   * @exception IllegalStateException Is thrown if invalid
   */
  public void validate() throws IllegalStateException
  {
    StringBuffer warning= new StringBuffer();
    StringBuffer error=   new StringBuffer();
   
    // Throw a IllegalStateException if required fields has no value
    if ( myFromField.equals(NO_FIELD_VALUE) ) 
	error.append("Required From-field is empty\n");

    if ( myToField.equals(NO_FIELD_VALUE) ) 
	error.append("Required To-field is empty\n");

    if ( error.length() > 0 ) {
      throw new IllegalStateException(error.toString());
    }

    // Print a warning if body and or subject is empty

    if ( myMailBody.equals(NO_FIELD_VALUE) ) {
      if ( mySubjectField.equals(NO_FIELD_VALUE) ) 
	  warning.append("No message, no subject; ");
      else
	  warning.append("Null message body; ");
    }
    
    if ( warning.length() > 0 ) {
      System.err.println(warning.toString() + "hope that's ok");
    }
  }



  /**
   * Overriding java.object.toString: 
   * Print the content nicely formated
   *
   * @return A string representation of the Mail object
   */
  public String toString()
  {
    return
	new String("Message id   = "+myId+
 	           "\nFrom field   = "+myFromField+
		   "\nDate field   = "+myDateField+
		   "\nTo field     = "+myToField+
		   "\nSubject      = "+mySubjectField+
		   "\nReply-to     = "+myReplyToField+
		   "\nOrganization = "+myOrganizationField+
		   "\nCC-Headers   = "+myCCHeaders+
		   "\nX-Headers    = "+myXHeaders+
		   "\nMail Body    = "+myMailBody);
  }

}

  

  
