Advanced IVR Tutorial Lesson 1: Write a Java Class

The execution of a Java action involves the following steps:

  • - Load the defined Jar files
  • - Create a new object of the defined class for none-static method
    • The class must have a default constructor, it will be used to create the object.
  • - Prepare arguments of the method based on parameter types and values defined
    • The parameter types is used to match the method signature
  • - Invoke the Java method
    • Values are resolved and used to call the defined method
  • - Set the action return variable based on method return
    • If the method returns java.util.Properties, all the name value pairs are assigned to the action return variable.
    • For other return types the value is converted to string by calling the toString method of the class. The string value is set to be the value of the action return variable, and the name of the return is "result".

The Java Class

The Java method we use simply list all files with the specified extension under the specified directory.

package voicent.ivrsample;

import java.util.Properties;

public class IvrSampleGetFiles
{
  // must have default constructor
  public
IvrSampleGetFiles() {}

  public Properties get(String dir, String ext)
  {
    Properties props = new Properties();

    int total = 0;
    String prompt = "Please ";
    String list = "";

    File listDir = new File(dir);
    if (listDir.exists()) {
      File files[] = listDir.listFiles();
      if (files != null) {
        for (int i = 0; i < files.length; i++) {
          String fname = files[i].getName();
          if (! fname.endsWith(ext))
            continue;

          total++;
          String nameonly = fname.substring(0, fname.length() - ext.length());

          prompt += "press "+Integer.toString(total)+" for "+nameonly + "; ";
          list += "\"" + dir + "\\" + fname + "\" ";
        }
      }
    }

    props.setProperty("total", Integer.toString(total));
    props.setProperty("promptmsg", prompt);
    props.setProperty("list", list);

    return props;  
  }

  // for test independent of IVR Studio
  public static void main(String[] args)
    throws IOException
  {
    IvrSampleGetFiles obj = new IvrSampleGetFiles();
    Properties props = obj.get("C:\\Call List", ".voc");
    props.store(System.out, "test");
  }
}

For example, if the C:\Call List directory contains the following two files: list A.voc and list B.voc. Calling get("C:\Call List", ".voc") returns the following name value pairs in the Properties object:
total=2
"promptmsg=Please press 0 for list A, press 1 for list B"
"list="C:\Call List\list A.voc" "C:\Call List\list B.voc"