Skip to main content
RSS feed Subscribe to feed

 

Transformation: Prompting the User

This custom transformation shows how to prompt the user for values to be used.

Prompting is performed by returning the prompt models in the DataTransformationConnection object. It is usually sufficient to use the static factory methods that allow a list of prompt models as input.

  • When the ExecuteTransformation method is called, all the prompt models will have been successfully prompted.
  • If the prompting was canceled then the ExecuteTransformation method will not be called.
private bool NeedsPrompting
{
    get
    {
        // check if all parameter needs for transformation execution is set.
        return true;
    }
}


protected override DataTransformationConnection ConnectCore(
    ImportContext importContext, DataRowReader input)
{
    PromptService ps = importContext.PromptService;

    // create a new list were we add the prompt models if needed
    List<object> promptModels = new List<object>();

    // check if we are allowed to prompt.
    if (ps.IsPromptingAllowed)
    {
        switch (ps.PromptMode)
        {
            case PromptMode.NotAllowed:
                // require prompting but are not allowed.
                if (NeedsPrompting)
                {
                    throw
                        new ImportException(
                             "Lambda transformation could not be performed."
                            );
                }
                break;
            case PromptMode.Allowed:
                if (NeedsPrompting)
                {
                    // add this as a prompt object
                    promptModels.Add(this);

                    // the registred prompt view on this object 
                    // will modify this objects settings.

                }
                break;
            case PromptMode.Always:
                // add this as a prompt object
                promptModels.Add(this);

                // the registred prompt view on this object 
                // will modify this objects settings.

                break;
        }
    }

    // create connection using the factory method and provide
    // the promptmodels as the final parameter.
    return DataTransformationConnection.CreateConnection(
        delegate
        {
            // when this delegate is called all prompting will
            // already has been performed.
            return new LambdaTransformationReader(importContext, input);
        },
        promptModels);            
}

The transformation may either provide a specific prompt object or use itself, as in this example, to perform the prompting. In addition to this code a UI component which performs the prompting will must be added, a normal windows forms component that is registered in the AddIn for the custom transformation. Here the windows forms dialog LambdaTransformationPromptDialog is registered as a view for the LambdaTransformation, thereby enabling the code above to work as expected.

protected override void RegisterViews(ViewRegistrar registrar)
{
    base.RegisterViews(registrar);

    registrar.Register(
        typeof(Form),
        typeof(LambdaTransformation),
        typeof(LambdaTransformationPromptDialog));
}