<!--- Tag Attributes --->
<CFPARAM NAME="ATTRIBUTES.Action" TYPE="string">
<CFPARAM NAME="ATTRIBUTES.DataSource" TYPE="string" DEFAULT="ows">

<!--- Examples of use:
  <!--- Create a new record in Films table --->
  <CF_FilmRecord
    Action="Create"
    MovieTitle="movie_title"
    PitchText="pitch_text"
    AmountBudgeted="number"
    ReturnVariable="variable_name">

  <!--- Update an existing record in Films table --->
  <CF_FilmRecord
    Action="Update"
    FilmID="film_id_number"
    MovieTitle="movie_title"
    PitchText="pitch_text"
    AmountBudgeted="number">
    
  <!--- Delete an existing record from Films table --->
  <CF_FilmRecord
    Action="Delete"
    FilmID="film_id_number">
    
  <!--- Retrieve an existing record from Films table --->
  <CF_FilmRecord
    Action="GetData"
    FilmID="film_id_number"
    ReturnVariable="variable_name">
    
  <!--- Display film record as a callout on page --->
  <CF_FilmRecord
    Action="ShowInCallout"
    FilmID="film_id_number">
--->

<!--- ** Different processing for the various Actions --->
<CFSWITCH EXPRESSION="#ATTRIBUTES.Action#">
  
  <!--- ** Action="Create" --->
  <CFCASE VALUE="Create">
    <!--- Additional Parameters for this Action --->
    <CFPARAM NAME="ATTRIBUTES.MovieTitle" TYPE="string">
    <CFPARAM NAME="ATTRIBUTES.PitchText" TYPE="string">
    <CFPARAM NAME="ATTRIBUTES.AmountBudgeted" TYPE="numeric">
    <CFPARAM NAME="ATTRIBUTES.ReturnVariable" TYPE="variableName">
    
    <!--- Convert Title to Title Case, via Custom Tag --->
    <CF_TextToTitleCase
      Input="#ATTRIBUTES.MovieTitle#"
      Output="TitleCase">
      
    <!--- Insert Film Record --->
    <CFQUERY DATASOURCE="#ATTRIBUTES.DataSource#">
      INSERT INTO Films(MovieTitle, PitchText, AmountBudgeted)
      VALUES ('#TitleCase#', '#ATTRIBUTES.PitchText#', #ATTRIBUTES.AmountBudgeted#)
    </CFQUERY>
    <!--- Get ID number of just-inserted film --->
    <CFQUERY DATASOURCE="#ATTRIBUTES.DataSource#" NAME="GetNew">
      SELECT Max(FilmID) As NewID FROM Films
    </CFQUERY>
    <!--- Return new FilmID to calling template --->
    <CFSET "Caller.#ATTRIBUTES.ReturnVariable#" = GetNew.NewID>
  </CFCASE>


  <!--- ** Action="Update" --->
  <CFCASE VALUE="Update">
    <!--- Additional Parameter for this Action --->
    <CFPARAM NAME="ATTRIBUTES.FilmID" TYPE="numeric">

    <!--- Were MovieTitle/PitchText/AmountBudgeted given? --->
    <CFSET HasTitle  = IsDefined("ATTRIBUTES.MovieTitle")>
    <CFSET HasPitch  = IsDefined("ATTRIBUTES.PitchText")>
    <CFSET HasBudget = IsDefined("ATTRIBUTES.AmountBudgeted")>

    <!--- Convert Title to Title Case, via Custom Tag --->
    <CFIF HasTitle>
      <CF_TextToTitleCase
        Input="#ATTRIBUTES.MovieTitle#"
        Output="TitleCase">
    </CFIF>  

    <!--- Assuming at least one of the three was given --->    
    <CFIF HasTitle OR HasPitch OR HasBudget>
      <!--- Update Record in database --->    
      <CFQUERY DATASOURCE="#ATTRIBUTES.DataSource#">
        UPDATE Films SET
        <CFIF HasTitle>
          MovieTitle = '#TitleCase#'
          <CFIF HasPitch OR HasBudget>,</CFIF>        
        </CFIF>
        <CFIF HasPitch>
          PitchText  = '#ATTRIBUTES.PitchText#'
          <CFIF HasBudget>,</CFIF>        
        </CFIF>
        <CFIF HasBudget>
          AmountBudgeted = #ATTRIBUTES.AmountBudgeted#
        </CFIF>
        WHERE FilmID = #ATTRIBUTES.FilmID#
      </CFQUERY>
    </CFIF>
  </CFCASE>


  <!--- ** Action="Delete" --->
  <CFCASE VALUE="Delete">
    <!--- Additional Parameter for this Action --->
    <CFPARAM NAME="ATTRIBUTES.FilmID" TYPE="numeric">
  
    <!--- Delete Record from database --->    
    <CFQUERY DATASOURCE="#ATTRIBUTES.DataSource#">
      DELETE FROM Films 
      WHERE FilmID = #ATTRIBUTES.FilmID#
    </CFQUERY>
  </CFCASE>


  <!--- ** Action="GetData" --->
  <CFCASE VALUE="GetData">
    <!--- Additional Parameter for this Action --->
    <CFPARAM NAME="ATTRIBUTES.FilmID" TYPE="numeric">
    <CFPARAM NAME="ATTRIBUTES.ReturnVariable" TYPE="variableName">

    <!--- Get Film Record --->
    <CFQUERY DATASOURCE="#ATTRIBUTES.DataSource#" NAME="GetFilm">
      SELECT * FROM Films
      WHERE FilmID = #ATTRIBUTES.FilmID#
    </CFQUERY>
    
    <!--- Get Associcated Actor Records --->
    <CFQUERY DATASOURCE="#ATTRIBUTES.DataSource#" NAME="GetActors">
      SELECT * FROM Actors
      WHERE ActorID IN
        (SELECT ActorID FROM FilmsActors
         WHERE FilmID = #ATTRIBUTES.FilmID#)
    </CFQUERY>
    
    <!--- Get Associcated Merchandise Records --->
    <CFQUERY DATASOURCE="#ATTRIBUTES.DataSource#" NAME="GetMerch">
      SELECT * FROM Merchandise
      WHERE FilmID = #ATTRIBUTES.FilmID#
    </CFQUERY>

    <!--- Create a Structure and place query results in it --->
    <CFSET s = StructNew()>
    <CFSET s.Film   = GetFilm>
    <CFSET s.Actors = GetActors>
    <CFSET s.Merch  = GetMerch>
    
    <!--- Return Structure to calling template --->
    <CFSET "Caller.#ATTRIBUTES.ReturnVariable#" = s>
  </CFCASE>


  <!--- ** Action="ShowInCallout" --->
  <CFCASE VALUE="ShowInCallout">
    <!--- Additional Parameter for this Action --->
    <CFPARAM NAME="ATTRIBUTES.FilmID" TYPE="numeric">

    <!--- Display Film info as "callout", via Custom Tag --->
    <CF_ShowMovieCallout
      FilmID="#ATTRIBUTES.FilmID#"
      DataSource="#Attributes.DataSource#">  
  </CFCASE>

  
  <!--- Show error if an unknown Action attribute is provided --->
  <CFDEFAULTCASE>
    <CFTHROW
      DETAIL="Unknown Action parameter." 
      MESSAGE="Allowable values are Create, Update, Delete, GetData, and ShowInCallout.">
  </CFDEFAULTCASE>
  
</CFSWITCH>