/// <summary>
        /// Adds the URL call parameter definition.
        /// </summary>
        /// <param name="assemblyCode">
        /// The assembly code.
        /// </param>
        /// <param name="parameterDefinition">
        /// The parameter definition.
        /// </param>
        /// <param name="variableCollection">
        /// The variable collection.
        /// </param>
        /// <returns>
        /// The <see cref="VariableInfo"/>.
        /// </returns>
        private VariableInfo AddUrlCallParameterDefinition(
            StringBuilder assemblyCode,
            IntegrationServiceUrlCallParameterDefinition parameterDefinition,
            VariableCollection variableCollection)
        {
            var variableInfo = variableCollection.NewVariable();
            var variableName = variableInfo.Name;

            assemblyCode.AppendFormat(@"
            var {0} = new Cebos.Veyron.SharedTypes.Integration.UrlCallParameterDefinition();
            {0}.Name = {1};
", variableName, parameterDefinition.Name.ToLiteral());

            if (!string.IsNullOrEmpty(parameterDefinition.ValueExpression))
            {
                var script = ExpressionService.PrepareScriptFromXml(parameterDefinition.ValueExpression);
                assemblyCode.AppendFormat(@"
            {0}.ValueExpression = item => Cebos.Veyron.SharedTypes.SafeTypeConverter.Convert<string>({1});", variableName, script);
            }

            return variableInfo;
        }
        /// <summary>
        /// Adds the URL call definition.
        /// </summary>
        /// <param name="assemblyCode">
        /// The assembly code.
        /// </param>
        /// <param name="process">
        /// The process.
        /// </param>
        /// <param name="callDefinition">
        /// The call definition.
        /// </param>
        /// <param name="variableCollection">
        /// The variable collection.
        /// </param>
        /// <returns>
        /// The <see cref="VariableInfo"/>.
        /// </returns>
        private VariableInfo AddUrlCallDefinition(
            StringBuilder assemblyCode,
            IProcessDefinition process,
            IntegrationServiceCallDefinition callDefinition,
            VariableCollection variableCollection)
        {
            var callSettings = (IntegrationServiceUrlCallDefinition)callDefinition.CallSettings;
            var variableInfo = variableCollection.NewVariable();
            var variableName = variableInfo.Name;

            assemblyCode.AppendFormat(
                @"
            var {0} = new Cebos.Veyron.SharedTypes.Integration.UrlCallDefinition();
            {0}.Name = {1};
            {0}.CallLocation = Cebos.Veyron.SharedTypes.UrlServiceCallLocation.{2};
            {0}.HttpMethod = Cebos.Veyron.SharedTypes.UrlServiceCallMethod.{3};
            {0}.Guid = System.Guid.Parse({4});
            {0}.ProcessDisplayName = {5};",
                variableName,
                callDefinition.Name.ToLiteral(),
                callSettings.CallLocation,
                callSettings.HttpMethod,
                callDefinition.Guid.ToString().ToLiteral(),
                process.DisplayName.ToLiteral());

            if (!string.IsNullOrEmpty(callSettings.AddressExpression))
            {
                var script = ExpressionService.PrepareScriptFromXml(callSettings.AddressExpression);
                assemblyCode.AppendFormat(@"
            {0}.AddressExpression = item => Cebos.Veyron.SharedTypes.SafeTypeConverter.Convert<string>({1});", variableName, script);
            }

            foreach (var parameterDefinition in callSettings.Parameters)
            {
                var parameterVariable = AddUrlCallParameterDefinition(assemblyCode, parameterDefinition, variableCollection);
                assemblyCode.AppendFormat(@"
            {0}.ParameterDefinitions.Add({1});
", variableName, parameterVariable.Name);
            }

            foreach (var rule in callDefinition.Rules)
            {
                assemblyCode.AppendFormat(@"
            {0}.Rules.Add({1}Metadata.RuleManager.GetRule(new System.Guid(""{2}"")));
", variableName, process.Name, rule.Guid);
            }

            return variableInfo;
        }
        /// <summary>
        /// Adds the web method call header definition.
        /// </summary>
        /// <param name="assemblyCode">
        /// The assembly code.
        /// </param>
        /// <param name="header">
        /// The header.
        /// </param>
        /// <param name="parameterMappings">
        /// The parameter mappings.
        /// </param>
        /// <param name="variableCollection">
        /// The variable collection.
        /// </param>
        /// <returns>
        /// The <see cref="VariableInfo"/>.
        /// </returns>
        private static VariableInfo AddWebMethodCallHeaderDefinition(
            StringBuilder assemblyCode,
            IntegrationServiceWebMethodCallHeaderDefinition header,
            IDictionary<string, string> parameterMappings,
            VariableCollection variableCollection)
        {
            var variableInfo = variableCollection.NewVariable();
            var variableName = variableInfo.Name;

            assemblyCode.AppendFormat(@"
            var {0} = new Cebos.Veyron.SharedTypes.Integration.WebMethodHeaderMappingDefinition();
            {0}.Name = {1};
", variableName, header.Name.ToLiteral());

            if (header.ValueMapping != null)
            {
                var subfieldVariable = AddWebMethodCallParameterDefinition(assemblyCode, header.ValueMapping, parameterMappings, variableCollection);
                assemblyCode.AppendFormat(@"
            {0}.ValueMapping = {1};
", variableName, subfieldVariable.Name);
            }

            if (header.MustUnderstandMapping != null)
            {
                var subfieldVariable = AddWebMethodCallParameterDefinition(assemblyCode, header.MustUnderstandMapping, parameterMappings, variableCollection);
                assemblyCode.AppendFormat(@"
            {0}.MustUnderstandMapping = {1};
", variableName, subfieldVariable.Name);
            }

            if (header.ActorMapping != null)
            {
                var subfieldVariable = AddWebMethodCallParameterDefinition(assemblyCode, header.ActorMapping, parameterMappings, variableCollection);
                assemblyCode.AppendFormat(@"
            {0}.ActorMapping = {1};
", variableName, subfieldVariable.Name);
            }

            if (header.RelayMapping != null)
            {
                var subfieldVariable = AddWebMethodCallParameterDefinition(assemblyCode, header.RelayMapping, parameterMappings, variableCollection);
                assemblyCode.AppendFormat(@"
            {0}.RelayMapping = {1};
", variableName, subfieldVariable.Name);
            }

            return variableInfo;
        }
        /// <summary>
        /// Adds the web method call parameter definition.
        /// </summary>
        /// <param name="assemblyCode">
        /// The assembly code.
        /// </param>
        /// <param name="parameter">
        /// The parameter.
        /// </param>
        /// <param name="parameterMappings">
        /// The parameter mappings.
        /// </param>
        /// <param name="variableCollection">
        /// The variable collection.
        /// </param>
        /// <returns>
        /// The <see cref="VariableInfo"/>.
        /// </returns>
        private static VariableInfo AddWebMethodCallParameterDefinition(
            StringBuilder assemblyCode,
            IntegrationServiceWebMethodCallParameterDefinition parameter,
            IDictionary<string, string> parameterMappings,
            VariableCollection variableCollection)
        {
            var variableInfo = variableCollection.NewVariable();
            var variableName = variableInfo.Name;

            assemblyCode.AppendFormat(@"
            var {0} = new Cebos.Veyron.SharedTypes.Integration.WebMethodParameterMappingDefinition();
            {0}.Name = {1};
            {0}.TypeName = {2};
            {0}.ArrayItemCount = {3};
", variableName, parameter.Name.ToLiteral(), parameter.TypeName.ToLiteral(), parameter.ArrayItemCount.ToLiteral());

            if (!string.IsNullOrEmpty(parameter.MappingKey) && parameterMappings.ContainsKey(parameter.MappingKey))
            {
                assemblyCode.AppendFormat(@"
            {0}.ValueExpression = context => {1};
", variableName, parameterMappings[parameter.MappingKey]);
            }

            foreach (var subfield in parameter.Subfields)
            {
                var subfieldVariable = AddWebMethodCallParameterDefinition(assemblyCode, subfield, parameterMappings, variableCollection);

                assemblyCode.AppendFormat(@"
            {0}.Subfields.Add({1});
", variableName, subfieldVariable.Name);
            }

            return variableInfo;
        }
        /// <summary>
        /// Adds the web method call definition.
        /// </summary>
        /// <param name="assemblyCode">
        /// The assembly code.
        /// </param>
        /// <param name="process">
        /// The process.
        /// </param>
        /// <param name="callDefinition">
        /// The call definition.
        /// </param>
        /// <param name="variableCollection">
        /// The variable collection.
        /// </param>
        /// <returns>
        /// The <see cref="VariableInfo"/>.
        /// </returns>
        private VariableInfo AddWebMethodCallDefinition(
            StringBuilder assemblyCode,
            IProcessDefinition process,
            IntegrationServiceCallDefinition callDefinition,
            VariableCollection variableCollection)
        {
            var callSettings = (IntegrationServiceWebMethodCallDefinition)callDefinition.CallSettings;
            var variableInfo = variableCollection.NewVariable();
            var variableName = variableInfo.Name;
            var soapHeaderMappings = ExpressionService.PrepareScriptsForDestinationNode(
                callSettings.ParametersMapping,
                Constants.WebMethodCallSoapHeadersExpressionName);
            var parameterMappings = ExpressionService.PrepareScriptsForDestinationNode(
                callSettings.ParametersMapping,
                Constants.WebMethodCallMethodParametersExpressionName);

            assemblyCode.AppendFormat(
                @"
            var {0} = new Cebos.Veyron.SharedTypes.Integration.WebMethodCallDefinition();
            {0}.Name = {1};
            {0}.Guid = System.Guid.Parse({2});
            {0}.ContractTypeName = {3};
            {0}.MethodName = {4};
            {0}.ServiceDescriptionId = {5};
            {0}.ProcessName = {6};
            {0}.ProcessDisplayName = {7};
            {0}.Username = {8};
            {0}.Password = {9};
            {0}.EndpointConfiguration = {10};
",
                variableName,
                callDefinition.Name.ToLiteral(),
                callDefinition.Guid.ToString().ToLiteral(),
                callSettings.ContractTypeName.ToLiteral(),
                callSettings.MethodName.ToLiteral(),
                callSettings.ServiceDescriptionId.ToLiteral(),
                process.Name.ToLiteral(),
                process.DisplayName.ToLiteral(),
                callSettings.Username.ToLiteral(),
                callSettings.Password.ToLiteral(),
                callSettings.EndpointConfiguration.ToLiteral());

            foreach (var header in callSettings.SoapHeaders)
            {
                var headerVariable = AddWebMethodCallHeaderDefinition(assemblyCode, header, soapHeaderMappings, variableCollection);
                assemblyCode.AppendFormat(@"
            {0}.HeaderMappings.Add({1});
", variableName, headerVariable.Name);
            }

            foreach (var parameter in callSettings.Parameters)
            {
                var parameterVariable = AddWebMethodCallParameterDefinition(assemblyCode, parameter, parameterMappings, variableCollection);
                assemblyCode.AppendFormat(@"
            {0}.ParameterMappings.Add({1});
", variableName, parameterVariable.Name);
            }

            foreach (var rule in callDefinition.Rules)
            {
                assemblyCode.AppendFormat(@"
            {0}.Rules.Add({1}Metadata.RuleManager.GetRule(new System.Guid(""{2}"")));", variableName, process.Name, rule.Guid);
            }

            var resultMappings = ExpressionService.PrepareScriptForMultipleDestinations(callSettings.ResultMapping, null);
            foreach (var fieldMapping in callSettings.DestinationFields)
            {
                var resultMappingVariable = AddFieldMapping(assemblyCode, fieldMapping, resultMappings, variableCollection);
                assemblyCode.AppendFormat(@"
            {0}.ResultMappings.Add({1});
", variableName, resultMappingVariable.Name);
            }

            return variableInfo;
        }
        private VariableInfo AddFieldMapping(
            StringBuilder assemblyCode,
            IntegrationServiceFieldMapping mapping,
            IDictionary<string, string> expressionScripts,
            VariableCollection variableCollection)
        {
            var variableInfo = variableCollection.NewVariable();

            assemblyCode.AppendFormat(@"
            var {0} = new Cebos.Veyron.SharedTypes.Integration.FieldMappingDefinition();
            {0}.FieldName = {1};
            {0}.IsKey = {2};
", variableInfo.Name, mapping.FieldName.ToLiteral(), mapping.IsKey.ToLiteral());

            if (!string.IsNullOrEmpty(mapping.MappingKey) && expressionScripts.ContainsKey(mapping.MappingKey))
            {
                assemblyCode.AppendFormat(@"
            {0}.ValueExpression = context => {1};
", variableInfo.Name, expressionScripts[mapping.MappingKey]);
            }

            foreach (var childMapping in mapping.Subfields)
            {
                var childMappingVariable = AddFieldMapping(assemblyCode, childMapping, expressionScripts, variableCollection);
                assemblyCode.AppendFormat(@"
            {0}.Subfields.Add({1});
", variableInfo.Name, childMappingVariable.Name);
            }

            return variableInfo;
        }