Code for Example 4

package tut;
import java.util.Locale;
import com.ibm.telephony.beans.directtalk.ApplicationProperties;
import com.ibm.telephony.beans.media.*;
import com.ibm.telephony.wvr.*;
/**
 *
 * A simple WVR application
 */
public class InApp extends WVRApplication {
	// Define the category for all the voice segments in this application.
	private static final String CATEGORY = "Tutorials";
	
	// Define the menu items
    	private static final String ITEM1_MSG	   = "Message";
	private static final String ITEM2_NUMBER   = "Number";
	private static final String ITEM3_ORDER	   = "Order";
	private static final String ITEM4_OPERATOR = "Operator";
	private static final String ITEM5_HANGUP   = "Hangup";
	
	// Create an array to store the menu items
	private static final String[] MENU_ITEMS = { ITEM1_MSG, ITEM2_NUMBER, ITEM3_ORDER,
                                                        ITEM4_OPERATOR, ITEM5_HANGUP };
	
	// Create an array to represent the keys that will be used to select the menu items
	private static final String[] MENU_KEYS = { "1", "2", "3", "4", "5" };
	
	// Create a number of Voice Segment objects that are shared amongst all instances of this
	// application within the JVM.
	
	// Create the Welcome and Difficulties segment objects
	private static final VoiceSegment VS_WELCOME      = new VoiceSegment(CATEGORY, "Welcome");
	private static final VoiceSegment VS_DIFFICULTIES = new VoiceSegment(CATEGORY, "Difficulties");
	
	// Create the segment objects for the menu items, and store them in an array
	private static final MediaType[]  MENU_PROMPTS = {
		new VoiceSegment(CATEGORY, "Message"),
		new VoiceSegment(CATEGORY, "Number"),
		new VoiceSegment(CATEGORY, "Order"),
		new VoiceSegment(CATEGORY, "Operator"),
		new VoiceSegment(CATEGORY, "Hangup")
	};
		
	// Create the segment objects required for the menu properties
	private static final VoiceSegment VS_MENU_INVALID = new VoiceSegment(CATEGORY, "InvalidKey");
	private static final VoiceSegment VS_MENU_ERROR   = new VoiceSegment(CATEGORY, "Error");
	private static final VoiceSegment VS_MENU_ONEMORE = new VoiceSegment(CATEGORY, "OneMore");
	private static final VoiceSegment VS_MENU_HEADER  = new VoiceSegment(CATEGORY, "MenuHeader");
	private static final VoiceSegment VS_MENU_FOOTER  = new VoiceSegment(CATEGORY, "MenuFooter");	

	// Create the segment objects for the hangup menu item
	private static final VoiceSegment VS_THANKCALLING = new VoiceSegment(CATEGORY, "ThankCalling");
	// Create the segment objects for the record a message menu item
	private static final VoiceSegment VS_RECORD     = new VoiceSegment(CATEGORY, "Record");
	private static final VoiceSegment VS_PRESS_KEY  = new VoiceSegment(CATEGORY, "PressKey");
	private static final VoiceSegment VS_CALLER_MSG = new VoiceSegment(CATEGORY, "CallerMsg");
	private static final VoiceSegment VS_RESPOND    = new VoiceSegment(CATEGORY, "Respond");
	
	// Create some attribute objects for the menu
	private PlayAttributes menuPlayAtts = new PlayAttributes();
	private MenuAttributes menuAtts     = new MenuAttributes(MENU_PROMPTS, MENU_ITEMS, MENU_KEYS, null);
	private DTMFAttributes menuDTMFAtts = new DTMFAttributes();
	private RecoAttributes menuRecoAtts = null;
	// Create a flag to control whether we continue to loop around in the voiceMain() method taking calls
	private boolean keepTakingCalls = true;
	
	// Create a constructor method to set up the attributes
	public InApp() {
		// Set the input attributes of the menu
		menuAtts.setHeaderMessage(VS_MENU_HEADER);
		menuAtts.setFooterMessage(VS_MENU_FOOTER);		
		menuAtts.setInvalidInputMessage(VS_MENU_INVALID);
		menuAtts.setNoMoreRepeatsMessage(VS_MENU_ERROR);
		menuAtts.setOneMoreRepeatMessage(VS_MENU_ONEMORE);
		menuAtts.setTimeout(10);
		menuAtts.setNumberOfRepeats(2);	
		
		// Set the DTMF attributes of the menu
		menuDTMFAtts.setDelimiterKeys("");
		menuDTMFAtts.setMaximumKeys(1);
		
	}//InApp
	
	/**
	 * @see com.ibm.telephony.wvr.WVRApplication#voiceMain()
	 */
	public void voiceMain() throws WVRException {	
		
		// Define the Call object
		Call call = null; 			
		// Set the application properties
		ApplicationProperties applicationProperties = null;
		applicationProperties = this.getApplicationProperties();

		// Create the WVR object and set the wait time
		WVR wvr = new WVR(this, applicationProperties);
		wvr.setWaitTime(-1);
			
		// Create a variable that will be used to give each call a unique number
		int callNumber = 0;
		try {
			while (keepTakingCalls) {
				// Increment callNumber
				callNumber++;
				
				// Wait for a call
				call = wvr.waitForCall("Call: " + callNumber);
				
				// Create a flag to indicate whether the call is in progress
				boolean callInProgress = true;
				try {			
					// Play welcome message
					call.play(VS_WELCOME);
					while (callInProgress) {						
						// Play the menu and get the caller's response.
						InputResult input = call.playAndGetInput(menuPlayAtts, menuAtts, menuDTMFAtts, menuRecoAtts);
						String choice = input.getValue();	
						
						// Check which menu option has been selected and take
						// the appropriate action. Each action returns a boolean
						// stating whether the call should continue or not
						if (choice.equals(ITEM1_MSG)) {
							callInProgress = recordMessage(call);
						}
						else if (choice.equals(ITEM5_HANGUP)) {
							callInProgress = hangUp(call);
						}
						else {					
							// Print out the caller's choice.
							System.out.println("Caller chose option " + choice);
						}//if						
					}//while
				}				
				catch (WVRHungUpException e) {
					// Caller has hung up - ignore exception, program will go to finally block
				}
				catch (WVRInvalidInputException e) {
					// Caller has entered invalid input - ignore exception, application will go to finally block
				}
				catch (WVRException e) {
					// Check if Call object has been created. If yes, play the Difficulties message.
					if (call.isActive()) call.play(VS_DIFFICULTIES);
					// Print debugging information
					e.printStackTrace();
				}				
				finally {
					// Return the call
					call.returnCall();
				}//try
									
			}//while
		}
		
		catch (WVRException e) {
			
			// Check if Call object has been created. If yes, play the Difficulties message.
			// Print debugging information
			e.printStackTrace();
		}//try
		
	}//voiceMain()
	/**
	 * Method recordMessage.
	 * @param call
	 * @return boolean
	 */
	private boolean recordMessage(Call call) throws WVRException {
		// Play segments to instruct the caller on how to record their message
		call.play(VS_RECORD);
		call.play(VS_PRESS_KEY);
		// Record the message
		call.record(VS_CALLER_MSG, 180);
		// Play a message to acknowledge the recording
		call.play(VS_RESPOND);
		return true;		
	}//recordMessage()
	/**
	 * Method hangUp.
	 * @param call
	 * @return boolean
	 */
	private boolean hangUp(Call call) throws WVRException {
		// Thank the user for calling
		call.play(VS_THANKCALLING);
		
		return false;
	}//hangUp()
	// Tell the application to stop taking calls when the node shuts down	
	public void voiceAppStop() { keepTakingCalls = false; }
}//InApp