<!--- 
Contact.cfc 
Ben Edwards, modified by Leon Chalnick

This creates a contact object. It's methods are used to instantiate
and manage the data associated with contacts in the database.
--->
<cfcomponent>

  <cfset VARIABLES.ContactID = 0 />
  <cfset VARIABLES.firstName = "" />
  <cfset VARIABLES.lastName = "" />
  <cfset VARIABLES.Address = "" />
  <cfset VARIABLES.city = "" />
  <cfset VARIABLES.state = "" />
  <cfset VARIABLES.zip = "" />
  <cfset VARIABLES.country = "" />
  <cfset VARIABLES.email = "" />
  <cfset VARIABLES.phone = "" />
  <cfset VARIABLES.userLogin = "" />
  <cfset VARIABLES.userPassword = "" />
  <cfset VARIABLES.mailingList = false />

  
  <cffunction name="init" output="No">
    <cfargument name="ContactID" type="numeric" required="true" />
    <cfargument name="firstName" type="string" required="true" />
    <cfargument name="lastName" type="string" required="true" />
    <cfargument name="Address" type="string" required="true" />
    <cfargument name="city" type="string" required="true" />
    <cfargument name="state" type="string" required="true" />
    <cfargument name="zip" type="string" required="true" />
    <cfargument name="country" type="string" required="true" />
    <cfargument name="email" type="string" required="true" />
    <cfargument name="userLogin" type="string" required="true" />
    <cfargument name="userPassword" type="string" required="true" />
    <cfargument name="mailingList" type="boolean" required="true" />
    
    <cfinvoke component="#this#" method="setInfo" info="#ARGUMENTS#" />
  </cffunction>

  <cffunction name="getAll" returntype="query" output="No"> 
    <cflock scope="APPLICATION" type="READONLY" timeout="10">
      <cfset dsn = application.appDSN />
    </cflock>

    <cfquery name="getContacts" datasource="#VARIABLES.dsn#">
      SELECT 
        ContactID, 
        FirstName, 
        LastName, 
        Phone, 
        Email,  
        UCase(Left(LastName, 1)) as con_group
      FROM  contacts
      ORDER BY LastName
    </cfquery>
    <cfreturn getContacts />
  </cffunction>

  <cffunction name="saveContact" returntype="void" output="No">
    <cfargument name="contactID" type="numeric" required="true" />
    <cfargument name="firstName" type="string" required="true" />
    <cfargument name="lastName" type="string" required="true" />
    <cfargument name="Address" type="string" required="true" />
    <cfargument name="city" type="string" required="true" />
    <cfargument name="state" type="string" required="true" />
    <cfargument name="country" type="string" required="true" />
    <cfargument name="zip" type="string" required="true" />
    <cfargument name="phone" type="string" required="true" />
    <cfargument name="email" type="string" required="true" />
    <cfargument name="userLogin" type="string" required="true" />
    <cfargument name="userPassword" type="string" required="true" />
    <cfargument name="mailingList" type="boolean" required="false" default="false" />
    
    <cflock scope="APPLICATION" timeout="10" type="READONLY">
      <cfset dsn = application.appDSN />
    </cflock>

    <cfquery name="updateConatct"  datasource="#VARIABLES.dsn#">
      UPDATE Contacts SET
        FirstName = <cfif ARGUMENTS.FirstName neq "">'#ARGUMENTS.FirstName#'<cfelse>Null</cfif>,
        LastName = '#ARGUMENTS.LastName#', 
        Address = <cfif ARGUMENTS.address neq "">'#ARGUMENTS.Address#'<cfelse>Null</cfif>, 
        City = <cfif ARGUMENTS.city neq "">'#ARGUMENTS.city#'<cfelse>Null</cfif>, 
        State = <cfif ARGUMENTS.state neq "">'#ARGUMENTS.state#'<cfelse>Null</cfif>, 
        Zip = <cfif ARGUMENTS.zip neq "">'#ARGUMENTS.zip#'<cfelse>Null</cfif>, 
        Country = <cfif ARGUMENTS.country neq "">'#ARGUMENTS.country#'<cfelse>Null</cfif>,
        Email = <cfif ARGUMENTS.email neq "">'#ARGUMENTS.email#'<cfelse>Null</cfif>, 
        Phone = <cfif ARGUMENTS.phone neq "">'#ARGUMENTS.phone#'<cfelse>Null</cfif>, 
        UserLogin = '#ARGUMENTS.userlogin#',
        UserPassword = '#ARGUMENTS.UserPassword#',
        MailingList = #ARGUMENTS.MailingList#
      WHERE ContactID = #ARGUMENTS.ContactID#
    </cfquery>
  </cffunction>

  <cffunction name="saveNewContact" returntype="void" output="No">
    <cfargument name="firstName" type="string" required="true" />
    <cfargument name="lastName" type="string" required="true" />
    <cfargument name="Address" type="string" required="true" />
    <cfargument name="city" type="string" required="true" />
    <cfargument name="state" type="string" required="true" />
    <cfargument name="country" type="string" required="true" />
    <cfargument name="zip" type="string" required="true" />
    <cfargument name="phone" type="string" required="true" />
    <cfargument name="email" type="string" required="true" />
    <cfargument name="userLogin" type="string" required="true" />
    <cfargument name="userPassword" type="string" required="true" />
    <cfargument name="mailingList" type="boolean" required="true" />
    
    <cflock scope="APPLICATION" timeout="10" type="READONLY">
      <cfset dsn = application.appDSN />
    </cflock>
    
    <cfquery name="addContact" datasource="#VARIABLES.dsn#">
      INSERT INTO Contacts (
        FirstName,
        LastName,
        Address,
        City,
        State,
        Country,
        Zip,
        Phone,
        userLogin,
        userPassword,
        email,
        mailingList,
        userRoleID
      ) VALUES (
        <cfif ARGUMENTS.FirstName neq "">'#ARGUMENTS.FirstName#'<cfelse>Null</cfif>,
        '#ARGUMENTS.LastName#',
        <cfif ARGUMENTS.address neq "">'#ARGUMENTS.Address#'<cfelse>Null</cfif>,
        <cfif ARGUMENTS.city neq "">'#ARGUMENTS.city#'<cfelse>Null</cfif>,
        <cfif ARGUMENTS.state neq "">'#ARGUMENTS.state#'<cfelse>Null</cfif>,
        <cfif ARGUMENTS.country neq "">'#ARGUMENTS.country#'<cfelse>Null</cfif>,
        <cfif ARGUMENTS.zip neq "">'#ARGUMENTS.zip#'<cfelse>Null</cfif>, 
        <cfif ARGUMENTS.phone neq "">'#ARGUMENTS.phone#'<cfelse>Null</cfif>, 
        '#ARGUMENTS.userlogin#',
        '#ARGUMENTS.UserPassword#',
        <cfif ARGUMENTS.email neq "">'#ARGUMENTS.email#'<cfelse>Null</cfif>, 
        #ARGUMENTS.MailingList#,
        5
      )
    </cfquery>

  </cffunction>

  <cffunction name="fetchContact" returntype="ows.38.mii_ContactMgr.model.Contact" output="No">
    <cfargument name="ContactID" type="numeric" required="Yes" />

    <cflock scope="APPLICATION" timeout="10" type="READONLY">
      <cfset dsn = application.appDSN />
    </cflock>
    
    <cfquery name="getContact"  datasource="#VARIABLES.dsn#">
      SELECT 
        ContactID, 
        FirstName, 
        LastName, 
        address, 
        city,
        state, 
        country, 
        zip, 
        phone,
        userlogin, 
        userpassword,
        mailinglist, 
        email
      FROM Contacts
      WHERE ContactID = #ARGUMENTS.ContactID#
    </cfquery>

    <cfset thisContact = CreateObject("component", "ows.38.mii_ContactMgr.model.Contact") >

    <cfset thisContact.setContactID(getContact.ContactID) />
    <cfset thisContact.setfirstname(getContact.firstname) />
    <cfset thisContact.setlastname(getcontact.lastname) />
    <cfset thisContact.setaddress(getcontact.address) />
    <cfset thisContact.setcity(getcontact.city) />
    <cfset thisContact.setstate(getcontact.state) />
    <cfset thisContact.setzip(getcontact.zip) />
    <cfset thisContact.setphone(getcontact.phone) />
    <cfset thisContact.setcountry(getcontact.country) />
    <cfset thisContact.setuserlogin(getcontact.userlogin) />
    <cfset thisContact.setuserpassword(getcontact.userpassword) />
    <cfset thisContact.setmailinglist(getcontact.mailinglist) />
    <cfset thisContact.setEmail(getcontact.Email) />
    <cfreturn thisContact>      
  </cffunction>

  <cffunction name="deleteThisContact" output="No">
    <cfargument name="ContactID" type="numeric" required="true" />
  
    <cflock scope="APPLICATION" timeout="10" type="READONLY">
      <cfset dsn = application.appDSN />
    </cflock>

    <cfquery name="deleteContact" datasource="#VARIABLES.dsn#">
      DELETE FROM contacts
      WHERE contactID = #ARGUMENTS.ContactID#
    </cfquery>
  </cffunction>

  <cffunction name="setInfo" access="public" output="No">
    <cfargument name="info" type="struct" required="true" />
    
    <cfset setContactID(ARGUMENTS.info.ContactID) />
    <cfset setFirstName(ARGUMENTS.info.firstName) />
    <cfset setLastName(ARGUMENTS.info.lastName) />
    <cfset setAddress(ARGUMENTS.info.Address) />
    <cfset setCity(ARGUMENTS.info.city) />
    <cfset setState(ARGUMENTS.info.state) />
    <cfset setZip(ARGUMENTS.info.zip) />
    <cfset setCountry(ARGUMENTS.info.Country) />
    <cfset setPhone(ARGUMENTS.info.Phone) />
    <cfset setUserLogin(ARGUMENTS.info.userLogin) />
    <cfset setUserPassword(ARGUMENTS.info.userPassword) />
    <cfset setMailingList(ARGUMENTS.info.mailingList) />
  </cffunction>
  
  
  <cffunction name="setContactID" access="public" output="false">
    <cfargument name="ContactID" type="numeric" required="true" />
    <cfset VARIABLES.ContactID = ARGUMENTS.ContactID />
  </cffunction>
  <cffunction name="getContactID" access="public" returntype="numeric" output="false">
    <cfreturn VARIABLES.ContactID />
  </cffunction>
  
  <cffunction name="setFirstName" access="public" output="false">
    <cfargument name="firstName" type="string" required="true" />
    <cfset VARIABLES.firstName = ARGUMENTS.firstName />
  </cffunction>
  <cffunction name="getFirstName" access="public" returntype="string" output="false">
    <cfreturn VARIABLES.firstName />
  </cffunction>
  
  <cffunction name="setLastName" access="public" output="false">
    <cfargument name="lastName" type="string" required="true" />
    <cfset VARIABLES.lastName = ARGUMENTS.lastName />
  </cffunction>
  <cffunction name="getLastName" access="public" returntype="string" output="false">
    <cfreturn VARIABLES.lastName />
  </cffunction>
  
  <cffunction name="setAddress" access="public" output="false">
    <cfargument name="Address" type="string" required="true" />
    <cfset VARIABLES.Address = ARGUMENTS.Address />
  </cffunction>
  <cffunction name="getAddress" access="public" returntype="string" output="false">
    <cfreturn VARIABLES.Address />
  </cffunction>
  
  <cffunction name="setCity" access="public" output="false">
    <cfargument name="city" type="string" required="true" />
    <cfset VARIABLES.city = ARGUMENTS.city />
  </cffunction>
  <cffunction name="getCity" access="public" returntype="string" output="false">
    <cfreturn VARIABLES.city />
  </cffunction>
  
  <cffunction name="setState" access="public" output="false">
    <cfargument name="state" type="string" required="true" />
    <cfset VARIABLES.state = ARGUMENTS.state />
  </cffunction>
  <cffunction name="getState" access="public" returntype="string" output="false">
    <cfreturn VARIABLES.state />
  </cffunction>
  
  <cffunction name="setZip" access="public" output="false">
    <cfargument name="zip" type="string" required="true" />
    <cfset VARIABLES.zip = ARGUMENTS.zip />
  </cffunction>
  <cffunction name="getZip" access="public" returntype="string" output="false">
    <cfreturn VARIABLES.zip />
  </cffunction>

  <cffunction name="setCountry" access="public" output="false">
    <cfargument name="Country" type="string" required="true" />
    <cfset VARIABLES.Country = ARGUMENTS.Country />
  </cffunction>
  <cffunction name="getCountry" access="public" returntype="string" output="false">
    <cfreturn VARIABLES.Country />
  </cffunction>

  <cffunction name="setPhone" access="public" output="false">
    <cfargument name="Phone" type="string" required="true" />
    <cfset VARIABLES.Phone = ARGUMENTS.Phone />
  </cffunction>
  <cffunction name="getPhone" access="public" returntype="string" output="false">
    <cfreturn VARIABLES.Phone />
  </cffunction>

  <cffunction name="setUserLogin" access="public" output="false">
    <cfargument name="userLogin" type="string" required="true" />
    <cfset VARIABLES.userLogin = ARGUMENTS.userLogin />
  </cffunction>
  <cffunction name="getUserLogin" access="public" returntype="string" output="false">
    <cfreturn VARIABLES.userLogin />
  </cffunction>

  <cffunction name="setUserPassword" access="public" output="false">
    <cfargument name="userPassword" type="string" required="true" />
    <cfset VARIABLES.userPassword = ARGUMENTS.userPassword />
  </cffunction>
  <cffunction name="getUserPassword" access="public" returntype="string" output="false">
    <cfreturn VARIABLES.userPassword />
  </cffunction>

  <cffunction name="setEmail" access="public" output="false">
    <cfargument name="email" type="string" required="true" />
    <cfset VARIABLES.Email = ARGUMENTS.Email />
  </cffunction>
  <cffunction name="getEmail" access="public" returntype="string" output="false">
    <cfreturn VARIABLES.email />
  </cffunction>

  <cffunction name="setMailingList" access="public" output="false">
    <cfargument name="mailingList" type="boolean" default="true" required="false" />
    <cfset VARIABLES.mailingList = ARGUMENTS.mailingList />
  </cffunction>
  <cffunction name="getMailingList" access="public" returntype="boolean" output="false">
    <cfreturn VARIABLES.mailingList />
  </cffunction>

</cfcomponent>