<?xml version="1.0" encoding="UTF-8"?>
<ccxml xmlns="http://www.w3.org/2002/09/ccxml" version="1.0">

<!-- 

Implements a "Personal Assistant" application. 

The dialog goes as follows:
1.) Somebody calls your Personal Assistant
2.) They are asked to record their name
3.) The Personal Assistant calls you and whispers the recording, so you know who tries to contact you
4.) You can accept or reject the call
5.) When accepting, a transfer happens
6.) When rejecting, the caller may leave a message

Integrates with VoiceObjects for the dialog portions of the call flow. Based on the sample code provided in the W3C
specification on CCXML: http://www.w3.org/TR/ccxml/#s11.3

-->

        <var expr="'initial'" name="currentstate" />
        <var name="in_connectionid" />
        <var name="dlg_onhold" />
        <var name="out_connectionid" />
        <var name="AcceptStatus" />
        <var name="VOReturnURL"/>
        
        <eventprocessor statevariable="currentstate">
        
                <transition event="connection.alerting"  state="initial">
                        <assign expr="event$.connectionid" name="in_connectionid" />
						<log expr="'***** ACCEPTING CALL *****'" />
                        <accept />
                </transition>
                
                <transition event="connection.connected" state="initial">
                        <assign expr="'welcoming_caller'" name="currentstate" />
						<log expr="'***** Dialog: Welcome caller *****'" />
                        <dialogstart src="'http://localhost:8070/VoiceObjects/DialogMapping?VSN=testService'" />
                </transition>
                
                <transition event="dialog.exit" state="welcoming_caller">
                        <!-- Collect VOReturnURL retrieved from the VoiceObjects dialog -->
                        <assign expr="event$.values.exprVOReturnURL" name="VOReturnURL" />
                        <!-- place the caller on hold -->
						<log expr="'***** Dialog: Put the caller on hold *****'" />
                        <dialogstart dialogid="dlg_onhold" connectionid="in_connectionid" src="'http://localhost:8070/VoiceObjects/Resources/PersonalAssistant/HoldMusic.vxml'" />
                        <!-- Contact the target  -->
                        <assign expr="'contacting_target'" name="currentstate" />
						<log expr="'***** CREATING CALL TO TARGET *****'" />
						<!-- Places a call to sip:myself@localhost:9000. Modify this to suit your softphone environment -->
                        <createcall dest="'sip:myself@localhost:9000'" connectionid="out_connectionid" />
                </transition>

                <transition event="connection.connected" state="contacting_target">
                        <!-- Ask the target if they would like to accept the call -->
                        <assign expr="'waiting_for_target_answer'" name="currentstate" />
						<log expr="'***** Dialog: Ask target whether they want to accept the call *****'" />
                        <dialogstart src="VOReturnURL" />
                </transition>
                
                <transition event="dialog.exit"  state="waiting_for_target_answer">
                        <assign expr="event$.values.varStatus" name="AcceptStatus" />
                        <if cond="AcceptStatus == 'Rejected'">
                                <!-- disconnect the called party (but still notify the other one) -->
								<log expr="'***** TARGET REJECTED CALL *****'" />
                                <disconnect connectionid="out_connectionid" />
                        </if>
                        <assign expr="'stop_hold'" name="currentstate" />
						<log expr="'***** STOPPING HOLD MUSIC *****'" />
                        <dialogterminate dialogid="dlg_onhold" />
                </transition>
                
                <transition event="dialog.exit"  state="stop_hold">
                        <if cond="AcceptStatus == 'Rejected'">
                                <assign expr="'voice_mail'" name="currentstate" />
                                <!-- Continue with Voice Mail dialog; next dialog step defined by VO logic -->
								<log expr="'***** Dialog: Allow recording of message *****'" />
                                <dialogstart connectionid="in_connectionid" src="VOReturnURL" />
                        <else />
                                <assign expr="'playing_connecting'" name="currentstate" />
                                <!-- Connect both parties -->
                                <dialogstart connectionid="in_connectionid" src="VOReturnURL" />
                        </if>
                </transition>
                
                <transition event="dialog.exit" state="playing_connecting">
						<log expr="'***** CONNECTING PARTIES *****'" />
                        <join id1="in_connectionid" id2="out_connectionid" />
                        <assign expr="'talking'" name="currentstate" />
                </transition>
                
                <transition event="connection.disconnected" >
                	<if cond="event$.connectionid == in_connectionid">
                        <exit />
                    </if>
                </transition>

                <transition event="dialog.exit" state="talking">
                        <exit />
                </transition>

                <transition event="dialog.exit" state="voice_mail">
                        <exit />
                </transition>

				<transition event="error.*">
					<log expr="'*****  ERROR: (' + event$.reason + ')'" />
					<log expr="'*****  EVENT$.NAME = ' + event$.name" />
					<log expr="'*****  EVENT$.REASON = ' + event$.reason" />
					<log expr="'*****  EVENT$.TAGNAME = ' + event$.tagname" />
					<log expr="'*****  EVENT$.EVENTID = ' + event$.eventid" />
					<log expr="'*****  EVENT$.EVENTSOURCE = ' + event$.eventsource" />
					<log expr="'*****  EVENT$.EVENTSOURCETYPE = ' + event$.eventsourcetype" />
		
					<exit />
				</transition>                
        </eventprocessor>
</ccxml>