public static async Task<FeatureMappingViewModel> GetModel(IDataContext context,
                                                                      FeatureMappingFilter filter)
        {
            FeatureMappingViewModel model;

            if (filter.Action == FeatureMappingAction.Delete 
                || filter.Action == FeatureMappingAction.Mapping
                || filter.Action == FeatureMappingAction.Copy)
            {
                model = await GetFullAndPartialViewModelForFeatureMapping(context, filter);
            }
            else if (filter.Action == FeatureMappingAction.Mappings ||
                filter.Action == FeatureMappingAction.CopyAll)
            {
                model = await GetFullAndPartialViewModelForFeatureMappings(context, filter);
            }
            else if (filter.Action == FeatureMappingAction.FeatureCodes)
            {
                model = await GetFullAndPartialViewModelForFeatureCodes(context, filter);
            }
            else
            {
                model = GetFullAndPartialViewModel(context, filter);
            }
            if (filter.Action != FeatureMappingAction.NotSet)
            {
                model.IdentifierPrefix = Enum.GetName(filter.Action.GetType(), filter.Action);
            }
            return model;
        }
        private static FeatureMappingViewModel GetFullAndPartialViewModel(IDataContext context,
                                                                             FeatureMappingFilter filter)
        {
            var model = new FeatureMappingViewModel(GetBaseModel(context))
            {
                Configuration = context.ConfigurationSettings,
            };
            HydrateModelWithCommonProperties(model, context);

            return model;
        }
        private static async Task<FeatureMappingViewModel> GetFullAndPartialViewModelForFeatureMapping
        (
            IDataContext context,
            FeatureMappingFilter filter
        )
        {
            var baseModel = GetBaseModel(context);
            var model = new FeatureMappingViewModel()
            {
                PageIndex = filter.PageIndex.HasValue ? filter.PageIndex.Value : 1,
                PageSize = filter.PageSize.HasValue ? filter.PageSize.Value : int.MaxValue,
                Configuration = context.ConfigurationSettings,
                CurrentUser = baseModel.CurrentUser,
                CurrentVersion = baseModel.CurrentVersion
            };
            var featureMapping = await context.Vehicle.GetFdpFeatureMapping(filter);
            var programmeFilter = new ProgrammeFilter()
            {
                ProgrammeId = featureMapping.ProgrammeId,
                Gateway = featureMapping.Gateway,
                Code = model.FeatureMapping.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 == FeatureMappingAction.Copy)
            {
                model.Gateways = model.Gateways.Where(g => !(g.Name.Equals(featureMapping.Gateway, StringComparison.InvariantCultureIgnoreCase)));
            }
            
            if (!(featureMapping is EmptyFdpFeatureMapping))
            {
                featureMapping.Programme = model.Programmes.FirstOrDefault(p => p.Id == featureMapping.ProgrammeId.GetValueOrDefault());
            }
            model.FeatureMapping = featureMapping;
           
            return model;
        }
 public async Task<PagedResults<OxoFeature>> ListOxoFeatures(FeatureMappingFilter filter)
 {
     filter.IncludeAllFeatures = true;
     filter.OxoFeaturesOnly = true;
     return await Task.FromResult(_featureDataStore.FdpOxoFeatureGetMany(filter));
 }
        private static async Task<FeatureMappingViewModel> GetFullAndPartialViewModelForFeatureCodes
        (
            IDataContext context,
            FeatureMappingFilter filter
        )
        {
            var baseModel = GetBaseModel(context);
            var model = new FeatureMappingViewModel(baseModel)
            {
                PageIndex = filter.PageIndex.HasValue ? filter.PageIndex.Value : 1,
                PageSize = filter.PageSize.HasValue ? filter.PageSize.Value : 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);

            model.OxoFeatures = await context.Vehicle.ListOxoFeatures(filter);
            model.TotalPages = model.OxoFeatures.TotalPages;
            model.TotalRecords = model.OxoFeatures.TotalRecords;
            model.TotalDisplayRecords = model.OxoFeatures.TotalDisplayRecords;

            foreach (var oxoFeature in model.OxoFeatures.CurrentPage)
            {
                oxoFeature.Programme = model.Programmes.FirstOrDefault(p => p.Id == oxoFeature.ProgrammeId.GetValueOrDefault());
                oxoFeature.Document = oxoFeature.Document = model.Documents.FirstOrDefault(d => d.Id == oxoFeature.DocumentId);
            }

            return model;
        }
 public async Task<PagedResults<FdpFeatureMapping>> ListFdpFeatureMappings(FeatureMappingFilter filter)
 {
     return await Task.FromResult(_featureDataStore.FdpFeatureMappingGetMany(filter));
 }
 public async Task<FdpFeatureMapping> GetFdpFeatureMapping(FeatureMappingFilter filter)
 {
     return await Task.FromResult(_featureDataStore.FdpFeatureMappingGet(filter));
 }
        public PagedResults<FdpFeatureMapping> FdpFeatureMappingGetMany(FeatureMappingFilter filter)
        {
            PagedResults<FdpFeatureMapping> retVal = null;

            using (IDbConnection 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);
                    }
                    para.Add("@DocumentId", filter.DocumentId, DbType.Int32);
                    if (filter.PageIndex.HasValue)
                    {
                        para.Add("@PageIndex", filter.PageIndex.Value, DbType.Int32);
                    }
                    if (filter.PageSize.HasValue)
                    {
                        para.Add("@PageSize", filter.PageSize.HasValue ? filter.PageSize.Value : 10, 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);
                    }
                    para.Add("@TotalPages", dbType: DbType.Int32, direction: ParameterDirection.Output);
                    para.Add("@TotalRecords", dbType: DbType.Int32, direction: ParameterDirection.Output);
                    para.Add("@TotalDisplayRecords", dbType: DbType.Int32, direction: ParameterDirection.Output);
                    if (filter.IncludeAllFeatures)
                    {
                        para.Add("@IncludeAllFeatures", true, DbType.Boolean);
                    }
                    if (filter.OxoFeaturesOnly)
                    {
                        para.Add("@OxoFeaturesOnly", filter.OxoFeaturesOnly, DbType.Boolean);
                    }
                    if (!string.IsNullOrEmpty(filter.FilterMessage))
                    {
                        para.Add("@FilterMessage", filter.FilterMessage, DbType.String);
                    }

                    var results = conn.Query<FdpFeatureMapping>("dbo.Fdp_FeatureMapping_GetMany", para, commandType: CommandType.StoredProcedure);

                    if (results.Any())
                    {
                        totalRecords = para.Get<int>("@TotalRecords");
                        totalDisplayRecords = para.Get<int>("@TotalDisplayRecords");
                    }
                    retVal = new PagedResults<FdpFeatureMapping>
                    {
                        PageIndex = filter.PageIndex.HasValue ? filter.PageIndex.Value : 1,
                        TotalRecords = totalRecords,
                        TotalDisplayRecords = totalDisplayRecords,
                        PageSize = filter.PageSize.HasValue ? filter.PageSize.Value : totalRecords
                    };

                    var currentPage = new List<FdpFeatureMapping>();

                    foreach (var result in results)
                    {
                        currentPage.Add(result);
                    }
                    retVal.CurrentPage = currentPage;
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return retVal;
        }
        public FdpFeatureMapping FdpFeatureMappingGet(FeatureMappingFilter filter)
        {
            FdpFeatureMapping retVal = new EmptyFdpFeatureMapping();
            using (IDbConnection conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    para.Add("@FdpFeatureMappingId", filter.FeatureMappingId.GetValueOrDefault(), DbType.Int32);

                    var results = conn.Query<FdpFeatureMapping>("Fdp_FeatureMapping_Get", para, commandType: CommandType.StoredProcedure);
                    if (results.Any())
                    {
                        retVal = results.First();
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return retVal;
        }
        public PagedResults<OxoFeature> FdpOxoFeatureGetMany(FeatureMappingFilter filter)
        {
            var results = FdpFeatureMappingGetMany(filter);
            var page = results.CurrentPage.Select(result => new OxoFeature(result)).ToList();
            return new PagedResults<OxoFeature>
            {
                PageIndex = results.PageIndex,
                PageSize = results.PageSize,
                TotalDisplayRecords = results.TotalDisplayRecords,
                TotalFail = results.TotalFail,
                TotalRecords = results.TotalRecords,
                TotalSuccess = results.TotalSuccess,
                CurrentPage = page
            };

        }