using EntityFirst.Module.BusinessObjects;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;

// namespaces...
namespace EntityFirst.Module.Interceptors
{
    // internal classes...
    internal class TenantCommandInterceptor : IDbCommandInterceptor
    {
        // private methods...
        private static void SetTenantParameterValue(DbCommand command)
        {
            foreach (DbParameter param in command.Parameters)
            {
                if (param.ParameterName != TenantAwareAttribute.TenantIdFilterParameterName)
                {
                    return;
                }
                param.Value = Global_Attributes.CompanyFilter;
            }
        }
        // public methods...
        public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
        }
        public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            SetTenantParameterValue(command);
        }
        public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
        }
        public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
            SetTenantParameterValue(command);
        }
        public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
        }
        public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
            SetTenantParameterValue(command);
        }
    }
}
