0

I am working on a UCMA 3.0 workflow application and am attempting to generate queries into our client management system allowing end users to obtain data about specific clients via voice or instant message. I was wondering anyone knows how to create a generic questionanswer activity using UCMA that allows generic input. I know that I can set up expected inputs and grammars, but with the bi-capitalization options, and the likelihood that an end user would know the exact client name (or client number for that matter), I would prefer to allow the user to enter part of the name and then search the database for a list of names that might meet the criteria. Does anyone know of a way, and have sample code that might allow me to do this if it is possible?

1 Answer 1

0

I had the same problem and had to write a custom activity to capture generic input from a user. This needs some work to make it production ready. Note the classy catching of system.exception in multiple places, as well as throwing an exception if input isn't received within the timeout period instead of reprompting the user. Also no regex on the user input...

The InstanceDependencyProperty is something else that was frustrating about Workflow. Without using InstanceDependencyProperties you won't be able to retrieve any results after the activity is completed.

Hope this helps!

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading;

using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using Microsoft.Rtc.Collaboration;
using Microsoft.Rtc.Workflow.Activities;
using Microsoft.Rtc.Workflow.Common;


namespace ActivityLibrary
{
    public partial class CaptureIMInput : Activity, IInstanceDependencyContainer
    {
        #region Private member variables
        private CallProvider _callProvider;
        private Timer _maximumTimer;
        private string _imText;
        private bool messageReceived = false;
        private bool _maximumTimerElapsed = false;
        #endregion

        public CaptureIMInput()
        {
            InitializeComponent();
            _instanceDependencyProperties = new Dictionary<string, object>();
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                this._callProvider = Utilities.GetCallProviderFromParent<InstantMessagingCall>(this);
                InstantMessagingCall call = (InstantMessagingCall)this._callProvider.Call;

                try
                {

                    if (call.State == CallState.Established)
                    {
                        call.Flow.EndSendInstantMessage(call.Flow.BeginSendInstantMessage(MainPrompt, null, null));
                        _maximumTimer = new Timer(new TimerCallback(OnMaximumTimerFired), null, MaximumPrompt, new TimeSpan(0, 0, 0, 0, -1));
                        call.Flow.MessageReceived += this.InstantMessagingFlow_MessageReceived;
                        while (!messageReceived)
                            {
                                if (_maximumTimerElapsed)
                                    throw new TimeoutException("User input was not detected within alloted time");
                            }
                        if (!string.IsNullOrEmpty(_imText))
                        {
                            IMText = _imText;
                            {
                                this.Stop();
                                return ActivityExecutionStatus.Closed;
                            }
                        }
                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return ActivityExecutionStatus.Closed;
        }

        protected override ActivityExecutionStatus Cancel(ActivityExecutionContext executionContext)
        {
            this.Stop(); //Clean up timer
            return ActivityExecutionStatus.Canceling;
        }

        private void Stop()
        {
            if (_maximumTimer != null)
            {
                _maximumTimer.Dispose();
                _maximumTimer = null;
            }
        }

        private void InstantMessagingFlow_MessageReceived(object sender, InstantMessageReceivedEventArgs e)
        {
            //Can't set dependencyproperties directly from sub-thread
            _imText = e.TextBody;
            //Mark bool so main while loop exits
            messageReceived = true;
        }

        //Callback to 
        private void OnMaximumTimerFired(object state)
        {
            _maximumTimerElapsed = true;
        }


        #region InstanceDependency dictionary
        private Dictionary<string, object> _instanceDependencyProperties;
        [Browsable(false)]
        public Dictionary<string, object> InstanceDependencyProperties
        {
            get { return _instanceDependencyProperties; }
        }
        #endregion

        #region Maximum Prompt Timeout
        [NonSerialized]
        private static readonly InstanceDependencyProperty MaximumPromptProperty = InstanceDependencyProperty.Register("MaximumPrompt", typeof(TimeSpan), typeof(CaptureIMInput), new TimeSpan(0, 0, 30));

        [TypeConverter(typeof(TimeSpanConverter))]
        public TimeSpan MaximumPrompt
        {
            get
            {
                if (base.DesignMode)
                    return (TimeSpan)InstanceDependencyHelper.GetValue<CaptureIMInput>(this, MaximumPromptProperty);
                else
                    return (TimeSpan)InstanceDependencyHelper.GetValue<CaptureIMInput>(this, this.WorkflowInstanceId, MaximumPromptProperty);
            }
            set
            {
                if (base.DesignMode)
                    InstanceDependencyHelper.SetValue<CaptureIMInput>(this, MaximumPromptProperty, value);
                else
                    InstanceDependencyHelper.SetValue<CaptureIMInput>(this, this.WorkflowInstanceId, MaximumPromptProperty, value);
            }
        }
        #endregion

        #region MainPrompt
        public static DependencyProperty MainPromptProperty =
            DependencyProperty.Register("MainPrompt", typeof(string), typeof(CaptureIMInput));

        [ValidationOption(ValidationOption.Required)]
        public string MainPrompt
        {
            get
            {
                return (string)base.GetValue(MainPromptProperty);
            }
            set
            {
                base.SetValue(MainPromptProperty, value);
            }
        }
        #endregion

        #region Instant Message Text
        public static InstanceDependencyProperty IMTextProperty =
            InstanceDependencyProperty.Register("IMText",
                typeof(string),
                typeof(CaptureIMInput));

        [Description("InstantMessaging Text from user")]
        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public string IMText
        {
            get
            {
                if (base.DesignMode) return ((string)InstanceDependencyHelper.GetValue<CaptureIMInput>(this, CaptureIMInput.IMTextProperty));
                else return ((string)(InstanceDependencyHelper.GetValue<CaptureIMInput>(this, this.WorkflowInstanceId, CaptureIMInput.IMTextProperty)));
            }
            set
            {
                if (base.DesignMode) InstanceDependencyHelper.SetValue<CaptureIMInput>(this, CaptureIMInput.IMTextProperty, value);
                else InstanceDependencyHelper.SetValue<CaptureIMInput>(this, this.WorkflowInstanceId, CaptureIMInput.IMTextProperty, value);
            }
        }
        #endregion

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.