public static async Task<DerivativeMappingViewModel> GetModel(IDataContext context,
                                                                      DerivativeMappingFilter filter)
        {
            DerivativeMappingViewModel model = null;

            if (filter.Action == DerivativeMappingAction.Delete 
                || filter.Action == DerivativeMappingAction.Mapping
                || filter.Action == DerivativeMappingAction.Copy)
            {
                model = await GetFullAndPartialViewModelForDerivativeMapping(context, filter);
            }
            else if (filter.Action == DerivativeMappingAction.Mappings ||
                filter.Action == DerivativeMappingAction.CopyAll)
            {
                model = await GetFullAndPartialViewModelForDerivativeMappings(context, filter);
            }
            else if (filter.Action == DerivativeMappingAction.BrochureModelCodes)
            {
                model = await GetFullAndPartialViewModelForBrochureModelCodes(context, filter);
            }
            else
            {
                model = GetFullAndPartialViewModel(context, filter);
            }
            if (filter.Action != DerivativeMappingAction.NotSet)
            {
                model.IdentifierPrefix = Enum.GetName(filter.Action.GetType(), filter.Action);
            }
            return model;
        }
        private static DerivativeMappingViewModel GetFullAndPartialViewModel(IDataContext context,
                                                                             DerivativeMappingFilter filter)
        {
            var model = new DerivativeMappingViewModel(SharedModelBase.GetBaseModel(context))
            {
                Configuration = context.ConfigurationSettings,
            };
            HydrateModelWithCommonProperties(model, context);

            return model;
        }
        private static async Task<DerivativeMappingViewModel> GetFullAndPartialViewModelForDerivativeMapping
        (
            IDataContext context,
            DerivativeMappingFilter filter
        )
        {
            var baseModel = SharedModelBase.GetBaseModel(context);
            var model = new DerivativeMappingViewModel()
            {
                PageIndex = filter.PageIndex.HasValue ? filter.PageIndex.Value : 1,
                PageSize = filter.PageSize.HasValue ? filter.PageSize.Value : Int32.MaxValue,
                Configuration = context.ConfigurationSettings,
                CurrentUser = baseModel.CurrentUser,
                CurrentVersion = baseModel.CurrentVersion
            };
            var derivativeMapping = await context.Vehicle.GetFdpDerivativeMapping(filter);
            var programmeFilter = new ProgrammeFilter()
            {
                ProgrammeId = derivativeMapping.ProgrammeId,
                Gateway = derivativeMapping.Gateway,
                Code = model.DerivativeMapping.Programme.VehicleName // In order to filter the gateways specific to the programme
            };
            HydrateModelWithCommonProperties(model, context, programmeFilter);
            model.Gateways = context.Vehicle.ListGateways(programmeFilter);

            // If we are copying to another gateway, we need to remove the source gateway from the list of available gateways
            if (filter.Action == DerivativeMappingAction.Copy)
            {
                model.Gateways = model.Gateways.Where(g => !(g.Name.Equals(derivativeMapping.Gateway, StringComparison.InvariantCultureIgnoreCase)));
            }
            
            if (!(derivativeMapping is EmptyFdpDerivativeMapping))
            {
                derivativeMapping.Programme = model.Programmes.FirstOrDefault(p => p.Id == derivativeMapping.ProgrammeId.GetValueOrDefault());
                derivativeMapping.Body = model.Bodies.FirstOrDefault(b => b.Id == derivativeMapping.BodyId);
                derivativeMapping.Engine = model.Engines.FirstOrDefault(e => e.Id == derivativeMapping.EngineId);
                derivativeMapping.Transmission = model.Transmissions.FirstOrDefault(t => t.Id == derivativeMapping.TransmissionId);
            }
            model.DerivativeMapping = derivativeMapping;
           
            return model;
        }
        public FdpDerivativeMapping FdpDerivativeMappingGet(DerivativeMappingFilter filter)
        {
            FdpDerivativeMapping retVal = new EmptyFdpDerivativeMapping();
            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    para.Add("@FdpDerivativeMappingId", filter.DerivativeMappingId.GetValueOrDefault(), DbType.Int32);

                    var results = conn.Query<FdpDerivativeMapping>("Fdp_DerivativeMapping_Get", para, commandType: CommandType.StoredProcedure);
                    if (results.Any())
                    {
                        retVal = results.First();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return retVal;
        }
        private static async Task<DerivativeMappingViewModel> GetFullAndPartialViewModelForBrochureModelCodes
        (
            IDataContext context,
            DerivativeMappingFilter filter
        )
        {
            var baseModel = GetBaseModel(context);
            var model = new DerivativeMappingViewModel(baseModel)
            {
                PageIndex = filter.PageIndex ?? 1,
                PageSize = filter.PageSize ?? int.MaxValue,
                Configuration = context.ConfigurationSettings,
                CurrentUser = baseModel.CurrentUser,
                CurrentVersion = baseModel.CurrentVersion
            };

            var programmeFilter = new ProgrammeFilter()
            {
                ProgrammeId = filter.ProgrammeId,
                Gateway = filter.Gateway
            };
            HydrateModelWithCommonProperties(model, context, programmeFilter);

            filter.IncludeAllDerivatives = false;

            model.OxoDerivatives = await context.Vehicle.ListOxoDerivatives(filter);
            model.TotalPages = model.OxoDerivatives.TotalPages;
            model.TotalRecords = model.OxoDerivatives.TotalRecords;
            model.TotalDisplayRecords = model.OxoDerivatives.TotalDisplayRecords;

            foreach (var oxoDerivative in model.OxoDerivatives.CurrentPage)
            {
                oxoDerivative.Document = model.Documents
                    .FirstOrDefault(d => d.Id == oxoDerivative.DocumentId);
                oxoDerivative.Programme = model.Programmes
                    .FirstOrDefault(p => p.Id == oxoDerivative.ProgrammeId.GetValueOrDefault());
                oxoDerivative.Body = model.Bodies
                    .FirstOrDefault(b => b.DocumentId == oxoDerivative.DocumentId && b.Id == oxoDerivative.BodyId);
                oxoDerivative.Engine = model.Engines
                    .FirstOrDefault(e => e.DocumentId == oxoDerivative.DocumentId && e.Id == oxoDerivative.EngineId);
                oxoDerivative.Transmission = model.Transmissions
                    .FirstOrDefault(t => t.DocumentId == oxoDerivative.DocumentId && t.Id == oxoDerivative.TransmissionId);
            }

            return model;
        }
 public async Task<PagedResults<OxoDerivative>> ListOxoDerivatives(DerivativeMappingFilter filter)
 {
     return await Task.FromResult(_derivativeDataStore.FdpOxoDerivativeGetMany(filter));
 }
 public async Task<FdpDerivativeMapping> GetFdpDerivativeMapping(DerivativeMappingFilter filter)
 {
     return await Task.FromResult(_derivativeDataStore.FdpDerivativeMappingGet(filter));
 }
        public PagedResults<OxoDerivative> FdpOxoDerivativeGetMany(DerivativeMappingFilter filter)
        {
            PagedResults<FdpDerivativeMapping> interimResults;

            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = DynamicParameters.FromCDSId(CurrentCDSID);
                    var totalRecords = 0;
                    var totalDisplayRecords = 0;

                    if (!string.IsNullOrEmpty(filter.CarLine))
                    {
                        para.Add("@CarLine", filter.CarLine, DbType.String);
                    }
                    if (!string.IsNullOrEmpty(filter.ModelYear))
                    {
                        para.Add("@ModelYear", filter.ModelYear, DbType.String);
                    }
                    if (!string.IsNullOrEmpty(filter.Gateway))
                    {
                        para.Add("@Gateway", filter.Gateway, DbType.String);
                    }
                    if (filter.PageIndex.HasValue)
                    {
                        para.Add("@PageIndex", filter.PageIndex.Value, DbType.Int32);
                    }
                    if (filter.PageSize.HasValue)
                    {
                        para.Add("@PageSize", filter.PageSize.Value, DbType.Int32);
                    }
                    if (filter.SortIndex.HasValue)
                    {
                        para.Add("@SortIndex", filter.SortIndex.Value, DbType.Int32);
                    }
                    if (filter.SortDirection != SortDirection.NotSet)
                    {
                        var direction = filter.SortDirection == SortDirection.Descending ? "DESC" : "ASC";
                        para.Add("@SortDirection", direction, DbType.String);
                    }
                    if (filter.IncludeAllDerivatives)
                    {
                        para.Add("@IncludeAllDerivatives", true, DbType.Boolean);
                    }
                    if (filter.OxoDerivativesOnly)
                    {
                        para.Add("@OxoDerivativesOnly", filter.OxoDerivativesOnly, DbType.Boolean);
                    }
                    para.Add("@DocumentId", filter.DocumentId, DbType.Int32);
                    para.Add("@TotalPages", DbType.Int32, direction: ParameterDirection.Output);
                    para.Add("@TotalRecords", DbType.Int32, direction: ParameterDirection.Output);
                    para.Add("@TotalDisplayRecords", DbType.Int32, direction: ParameterDirection.Output);

                    var dbResults = conn.Query<FdpDerivativeMapping>("dbo.Fdp_OxoDerivative_GetMany", para, commandType: CommandType.StoredProcedure);

                    var fdpDerivativeMappings = dbResults as IList<FdpDerivativeMapping> ?? dbResults.ToList();
                    if (fdpDerivativeMappings.Any())
                    {
                        totalRecords = para.Get<int>("@TotalRecords");
                        totalDisplayRecords = para.Get<int>("@TotalDisplayRecords");
                    }
                    interimResults = new PagedResults<FdpDerivativeMapping>
                    {
                        PageIndex = filter.PageIndex ?? 1,
                        TotalRecords = totalRecords,
                        TotalDisplayRecords = totalDisplayRecords,
                        PageSize = filter.PageSize ?? totalRecords
                    };

                    var currentPage = fdpDerivativeMappings.ToList();

                    interimResults.CurrentPage = currentPage;
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            
            var page = interimResults.CurrentPage.Select(result => new OxoDerivative(result)).ToList();
            return new PagedResults<OxoDerivative>
            {
                PageIndex = interimResults.PageIndex,
                PageSize = interimResults.PageSize,
                TotalDisplayRecords = interimResults.TotalDisplayRecords,
                TotalFail = interimResults.TotalFail,
                TotalRecords = interimResults.TotalRecords,
                TotalSuccess = interimResults.TotalSuccess,
                CurrentPage = page
            };
        }