<%@ page language="java" %>
<%@ page import="java.sql.*"  %>
<%@ page import="java.awt.*"  %>
<%@ page import="java.util.*" %>
  
<%

// ---------------------------------------------------------------
//
// -----------------------------------------------------
// copyright 2000 by voxeo corporation. (see LGPL.txt)
//
// US Postal Codes database, Copyright (c) 2000 CD Light
//                           All Rights Reserved.
// -----------------------------------------------------
//
//
// v1.0 Coded in JSP
// v1.0 Coded by Stephen J. Lewis
//
// This application is prompts users over the phone for a 5 digit
// zipcode, then accesses a database containing the US Postal Code
// information for the year 2000 to find cities (and states) that
// match the zipcode.  It will read the results to the caller,
// then ask for another zipcode.  The same application has been
// written as a Java servlet, in ASP, PHP, Perl, ColdFusion and as a
// java servlet to demonstrate how to use CallXML and backend 
// scripting languages.  
//
// This file is associated with the following files:				 
//     US Postal Codes.mdb   <---- The actual database file itself      
//
// Several audio files are associated with this application:
//           MainMenu.wav
//           Results.wav
//           MutlipleResults.wav
//           NotFound.wav
//           YouHaveTakenTooLong.wav
//
// All audio files are PCM format, 8-bit, mono, unsigned.
//
//----------------------------------------------------------------
  
//----------------------------------------------------------------------------------------


String PathString = "http://jrun.voxeo.com:8100/zipcode/";

out.println ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
out.println ("<callxml>");
 
//------------------------------------------------------------------------
// This annoying little variable is simply incremented and passed along to
// avoid the AGS//s caching.  This helps to ensure that a different URL
// query string will be passed.
//------------------------------------------------------------------------

if (request.getParameter("cachekiller") == null) {
  out.println ("<assign var=\"cachekiller\" value=\"1\"/>");
}  // if statement
else {
  out.println ("<assign var=\"cachekiller\" value=\"" + (request.getParameter("cachekiller") + 1) + "\"/>");
}  // else statement

//---------------------------------------------------
// The Querystring variable zipcode determine whether 
// we need to go to the main menu, or if a caller has
// just entered in a ZipCode.
//---------------------------------------------------

if (request.getParameter("zipcode") != null) {
  //-----------------------------------------------------------------------------
  // Initialize our databasing routines and select out all matches to the zipcode
  //-----------------------------------------------------------------------------
  
  String Username = "";
  String Password = "";
 
  Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
    
  Connection TempConnection = DriverManager.getConnection("jdbc:odbc:USPostalCodes", 
 							  Username,
							  Password);

  
  Statement TempStatement = TempConnection.createStatement();
  
  String TempSQL = "select * from PostalCodes where Zipcode = '" + request.getParameter("zipcode") + "'";
  
  ResultSet TempDatabase = TempStatement.executeQuery(TempSQL);

  out.println ("<block>");  
  
  int Loopy          = 0;
  String CallXMLText = "";
  
  if (TempDatabase != null) {
    while ( TempDatabase.next() ) {
      Loopy++;
      String City  = TempDatabase.getString("City");
      String State = TempDatabase.getString("State");  
	
      //-----------------------------------------------------
      // We have to check for certain states because the
	  // callxml TTS engine will not pronounce them correctly
      //-----------------------------------------------------
	  
   	  if (State == "AL") { State = "Alabama";       }
	  if (State == "CT") { State = "Connecticut";   }
	  if (State == "CO") { State = "Colorado";      }
	  if (State == "DE") { State = "Delaware";      }
	  if (State == "HI") { State = "Hawaii";        }
	  if (State == "ID") { State = "Idaho";         }
	  if (State == "IN") { State = "Indiana";       }
	  if (State == "LA") { State = "Louisiana";     }
	  if (State == "MA") { State = "Massachusetts"; }
	  if (State == "MD") { State = "Maryland";      }
	  if (State == "ME") { State = "Maine";         }
	  if (State == "MI") { State = "Michigan";      }
	  if (State == "MO") { State = "Missouri";      }
	  if (State == "MS") { State = "Mississippi";   }
	  if (State == "MT") { State = "Montana";       }
	  if (State == "NE") { State = "Nebraska";      }
	  if (State == "OH") { State = "Ohio";          }
	  if (State == "OK") { State = "Oklahoma";      }
	  if (State == "OR") { State = "Oregon";        }
	  if (State == "PA") { State = "Pennsylvania";  }
	  if (State == "WA") { State = "Washington";    }
	  if (State == "WY") { State = "Wyoming";       }
	  
  	  CallXMLText = CallXMLText + "<text>" + City + "," + State + "</text>";
    }
  }  // if statement
  
  //-------------------------------------------
  // We need to play the appropriate prompt for 
  // no matches, 1 match, or multiple matches.
  //-------------------------------------------  
  
  if (Loopy == 0) {
    out.println ("<playaudio format=\"audio/wav\" value=\"NotFound.wav\" />");
  }  // if statement   
  else if (Loopy == 1) {
    out.println ("<playaudio format=\"audio/wav\" value=\"Results.wav\" />");
  }  // elseif statement
  else {
    out.println ("<playaudio format=\"audio/wav\" value=\"MultipleResults.wav\" />");
  }  // else statement
  
  out.println (CallXMLText);
  
  //------------------------------------------------------
  // We want to pause for 2 seconds, clear out the zipcode 
  // variable, and then return to the main menu.
  //------------------------------------------------------  
  
  out.println ("<wait value=\"2s\"/>");
  out.println ("<clear var=\"ZipCode\"/>");
  out.println ("<goto value=\"" + PathString + "zipcode.jsp\" submit=\"*\" method=\"get\" />");
  out.println ("</block>");
  
  TempStatement.close();
  TempConnection.close();

}  // ZipCodeMenu
else { 
  out.println ("<inputdigits label=\"input_selection\" " +
                    "repeat=\"3\" " +
				    "var=\"ZipCode\" " +
				    "format=\"audio/wav\" " + 
				    "value=\"MainMenu.wav\" " + 
				    "cleardigits=\"TRUE\" " +
				    "includetermdigit=\"FALSE\" " +
				    "maxdigits=\"5\" " +
				    "maxtime=\"15s\" " +
				    "maxsilence=\"5s\" >");

  out.println ("<onmaxdigits>");
  out.println ("<goto value=\"" + PathString + "zipcode.jsp\" submit=\"*\" method=\"get\" />");
  out.println ("</onmaxdigits>");
  
  out.println ("<onmaxtime>");
  out.println ("<playaudio format=\"audio/wav\" value=\"YouHaveTakenTooLong.wav\" />"); 
  out.println ("<hangup/>");
  out.println ("</onmaxtime>");
  
  out.println ("<onmaxsilence>");
  out.println ("<playaudio format=\"audio/wav\" value=\"YouHaveTakenTooLong.wav\" />"); 
  out.println ("<hangup/>");
  out.println ("</onmaxsilence>");
  
  out.println ("</inputdigits>");
  
  //-----------------------------------
  // One last message in case the block 
  // loops through before maxsilence or
  // maxtime has been reached.
  //-----------------------------------
  
  out.println ("<playaudio format=\"audio/wav\" value=\"YouHaveTakenTooLong.wav\" />");      
}  // MainMenu

out.println ("</callxml>");


%>