/// <summary>
        /// Updates the integration service web method call settings.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <exception cref="System.ArgumentNullException">dto</exception>
        /// <exception cref="System.Data.DBConcurrencyException"></exception>
        public void UpdateIntegrationServiceWebMethodCallSettings(IntegrationServiceWebMethodCallSettingsDto dto)
        {
            const string CommandText = @"
UPDATE [dbo].[IntegrationServiceWebMethodCallSettings]
SET
     [SettingsId] = @settingsId
    ,[LastModifiedOn] = GETDATE()
    ,[Address] = @address
    ,[Username] = @username
    ,[Password] = @password
    ,[ServiceDescriptionId] = @serviceDescriptionId
    ,[ContractTypeName] = @contractTypeName
    ,[MethodName] = @methodName
    ,[ParametersMapping] = @parametersMapping
    ,[ResultMapping] = @resultMapping
    ,[EndpointConfiguration] = @endpointConfiguration
WHERE [Id] = @id;";

            if (dto == null)
                throw new ArgumentNullException("dto");

            using (var cmd = new SqlCommand(CommandText))
            {
                cmd.Parameters.AddWithValue("@id", dto.Id);
                cmd.Parameters.AddWithValue("@settingsId", dto.ParentSettingsId);
                cmd.Parameters.AddWithValue("@address", dto.Address);
                cmd.Parameters.AddWithValue("@username", dto.Username);
                cmd.Parameters.AddWithValue("@password", dto.Password);
                cmd.Parameters.AddWithValue("@serviceDescriptionId", AdoHelper.NullCheck(dto.ServiceDescriptionId));
                cmd.Parameters.AddWithValue("@contractTypeName", dto.ContractTypeName);
                cmd.Parameters.AddWithValue("@methodName", dto.MethodName);
                cmd.Parameters.AddWithValue("@parametersMapping", dto.ParametersMapping);
                cmd.Parameters.AddWithValue("@resultMapping", dto.ResultMapping);
                cmd.Parameters.AddWithValue("@endpointConfiguration", dto.EndpointConfiguration);

                var rowsAffected = Database.Execute(cmd);

                if (rowsAffected == 0)
                    throw new DBConcurrencyException(ConcurencyException);
            }
        }
        /// <summary>
        /// Inserts the integration service web method call settings.
        /// </summary>
        /// <param name="dto">The dto.</param>
        /// <exception cref="System.ArgumentNullException">dto</exception>
        /// <exception cref="System.Data.DBConcurrencyException"></exception>
        public void InsertIntegrationServiceWebMethodCallSettings(IntegrationServiceWebMethodCallSettingsDto dto)
        {
            const string CommandText = @"
INSERT INTO [dbo].[IntegrationServiceWebMethodCallSettings]
(
     [SettingsId]
    ,[LastModifiedOn]
    ,[Address]
    ,[Username]
    ,[Password]
    ,[ServiceDescriptionId]
    ,[ContractTypeName]
    ,[MethodName]
    ,[ParametersMapping]
    ,[ResultMapping]
    ,[EndpointConfiguration]
)
VALUES
(
     @settingsId
    ,GETDATE()
    ,@address
    ,@username
    ,@password
    ,@serviceDescriptionId
    ,@contractTypeName
    ,@methodName
    ,@parametersMapping
    ,@resultMapping
    ,@endpointConfiguration
);

SET @id = SCOPE_IDENTITY();";

            if (dto == null)
                throw new ArgumentNullException("dto");

            using (var cmd = new SqlCommand(CommandText))
            {
                var idParam = cmd.Parameters.Add("@id", SqlDbType.Int);

                idParam.Direction = ParameterDirection.Output;

                cmd.Parameters.AddWithValue("@settingsId", dto.ParentSettingsId);
                cmd.Parameters.AddWithValue("@address", dto.Address);
                cmd.Parameters.AddWithValue("@username", dto.Username);
                cmd.Parameters.AddWithValue("@password", dto.Password);
                cmd.Parameters.AddWithValue("@serviceDescriptionId", AdoHelper.NullCheck(dto.ServiceDescriptionId));
                cmd.Parameters.AddWithValue("@contractTypeName", dto.ContractTypeName);
                cmd.Parameters.AddWithValue("@methodName", dto.MethodName);
                cmd.Parameters.AddWithValue("@parametersMapping", dto.ParametersMapping);
                cmd.Parameters.AddWithValue("@resultMapping", dto.ResultMapping);
                cmd.Parameters.AddWithValue("@endpointConfiguration", dto.EndpointConfiguration);

                var rowsAffected = Database.Execute(cmd);

                if (rowsAffected == 0)
                    throw new DBConcurrencyException(ConcurencyException);

                dto.Id = (int)idParam.Value;
            }
        }
        /// <summary>
        /// Reads the integration service web method call parameters.
        /// </summary>
        /// <param name="settingsDto">The settings dto.</param>
        /// <param name="reader">The reader.</param>
        private static void ReadIntegrationServiceWebMethodCallParameters(IntegrationServiceWebMethodCallSettingsDto settingsDto, IDataReader reader)
        {
            reader.NextResult();

            var parameters = new List<IntegrationServiceWebMethodCallParameterDto>();
            var parametersMap = new Dictionary<int, IntegrationServiceWebMethodCallParameterDto>();

            while (reader.Read())
            {
                var dto = new IntegrationServiceWebMethodCallParameterDto
                              {
                                  Id = reader.GetInt32(0),
                                  ParentId = reader.GetNullableInt(1),
                                  Guid = reader.GetGuid(2),
                                  Name = reader.GetString(3),
                                  TypeCode =
                                      (WebServiceTypeCode)
                                      Enum.Parse(typeof(WebServiceTypeCode), reader.GetString(4), true),
                                  TypeName = reader.GetString(5),
                                  TypeDisplayName = reader.GetString(6),
                                  IsNull = reader.GetBoolean(7),
                                  IsArray = reader.GetBoolean(8),
                                  ArrayItemCount = reader.GetNullableInt(9),
                                  IsEnum = reader.GetBoolean(10),
                                  IsSoapHeader = reader.GetBoolean(11)
                              };

                parameters.Add(dto);
                parametersMap[dto.Id] = dto;
            }

            foreach (var parameter in parameters)
            {
                if (!parameter.ParentId.HasValue)
                    settingsDto.MethodParameters.Add(parameter);
                else
                    parametersMap[parameter.ParentId.Value].Subfields.Add(parameter);
            }
        }
        /// <summary>
        /// Reads the integration service web method call result fields.
        /// </summary>
        /// <param name="settingsDto">
        /// The settings DTO.
        /// </param>
        /// <param name="reader">
        /// The reader.
        /// </param>
        private static void ReadIntegrationServiceWebMethodCallResultFields(
            IntegrationServiceWebMethodCallSettingsDto settingsDto,
            IDataReader reader)
        {
            reader.NextResult();

            var resultFields = new List<IntegrationServiceWebMethodCallResultFieldDto>();
            var resultFieldsMap = new Dictionary<int, IntegrationServiceWebMethodCallResultFieldDto>();

            while (reader.Read())
            {
                var dto = new IntegrationServiceWebMethodCallResultFieldDto
                          {
                              Id = reader.GetInt32(0),
                              ParentId = reader.GetNullableInt(1),
                              Name = reader.GetString(2),
                              DeclaringTypeName = reader.GetString(3)
                          };

                resultFields.Add(dto);
                resultFieldsMap[dto.Id] = dto;
            }

            foreach (var field in resultFields)
            {
                if (!field.ParentId.HasValue)
                    settingsDto.ResultFields.Add(field);
                else
                    resultFieldsMap[field.ParentId.Value].Fields.Add(field);
            }
        }
        /// <summary>
        /// Fetches the integration service web method call settings.
        /// </summary>
        /// <param name="parentSettingsId">The parent settings identifier.</param>
        /// <returns>IntegrationServiceWebMethodCallSettingsDto.</returns>
        public IntegrationServiceWebMethodCallSettingsDto FetchIntegrationServiceWebMethodCallSettings(int parentSettingsId)
        {
            const string CommandText = @"
DECLARE @settingsId INT;

SELECT @settingsId = [Id]
FROM
    [dbo].[IntegrationServiceWebMethodCallSettings]
WHERE [SettingsId] = @parentSettingsId;

SELECT
     [Id]
    ,[Address]
    ,[ServiceDescriptionId]
    ,[ContractTypeName]
    ,[MethodName]
    ,[ParametersMapping]
    ,[ResultMapping]
    ,[Username]
    ,[Password]
    ,[EndpointConfiguration]
FROM
    [dbo].[IntegrationServiceWebMethodCallSettings]
WHERE [Id] = @settingsId;

SELECT
     [Id]
    ,[ParentId]
    ,[Guid]
    ,[Name]
    ,[TypeCode]
    ,[TypeName]
    ,[TypeDisplayName]
    ,[IsNull]
    ,[IsArray]
    ,[ArrayItemCount]
    ,[IsEnum]
    ,[IsSoapHeader]
FROM
    [dbo].[IntegrationServiceWebMethodCallParameters]
WHERE [SettingsId] = @settingsId;

SELECT
     [Id]
    ,[ParentId]
    ,[Name]
    ,[DeclaringTypeName]
FROM
    [dbo].[IntegrationServiceWebMethodCallResultFields]
WHERE [SettingsId] = @settingsId;";

            using (var ctx = ConnectionManager<SqlConnection>.GetManager(Database.VeyronMeta, false))
            {
                using (var cmd = new SqlCommand(CommandText, ctx.Connection))
                {
                    cmd.Parameters.AddWithValue("@parentSettingsId", parentSettingsId);

                    using (var reader = new SafeDataReader(cmd.ExecuteReader()))
                    {
                        if (reader.Read())
                        {
                            var result = new IntegrationServiceWebMethodCallSettingsDto
                                         {
                                             Id = reader.GetInt32(0),
                                             Address = reader.GetString(1),
                                             ServiceDescriptionId = reader.GetNullableInt(2),
                                             ContractTypeName = reader.GetString(3),
                                             MethodName = reader.GetString(4),
                                             ParametersMapping = reader.GetString(5),
                                             ResultMapping = reader.GetString(6),
                                             Username = reader.GetString("Username"),
                                                 Password = reader.GetString("Password"),
                                                 EndpointConfiguration = reader.GetString("EndpointConfiguration")
                                         };

                            ReadIntegrationServiceWebMethodCallParameters(result, reader);
                            ReadIntegrationServiceWebMethodCallResultFields(result, reader);

                            return result;
                        }
                    }
                }
            }

            return null;
        }