private static async Task<FeatureViewModel> GetFullAndPartialViewModelForFeature(IDataContext context,
                                                                                        FeatureFilter filter)
 {
     var model = new FeatureViewModel()
     {
         PageIndex = filter.PageIndex.HasValue ? filter.PageIndex.Value : 1,
         PageSize = filter.PageSize.HasValue ? filter.PageSize.Value : Int32.MaxValue,
         Configuration = context.ConfigurationSettings
     };
     var feature = await context.Vehicle.GetFdpFeature(filter);
     var programmeFilter = new ProgrammeFilter()
     {
         ProgrammeId = feature.ProgrammeId,
         Gateway = feature.Gateway
     };
     HydrateModelWithCommonProperties(model, context, programmeFilter);
     
     if (!(feature is EmptyFdpFeature))
     {
         feature.Programme = model.Programmes.FirstOrDefault(p => p.Id == feature.ProgrammeId.GetValueOrDefault());
     }
     model.Feature = feature;
    
     return model;
 }
        private static FeatureViewModel GetFullAndPartialViewModel(IDataContext context,
                                                                      FeatureFilter filter)
        {
            var model = new FeatureViewModel(SharedModelBase.GetBaseModel(context))
            {
                Configuration = context.ConfigurationSettings,
            };
            HydrateModelWithCommonProperties(model, context);

            return model;
        }
        public IEnumerable<FdpFeature> FeatureGetManyByDocumentId(FeatureFilter filter)
        {
            var retVal = Enumerable.Empty<FdpFeature>();

            using (var conn = DbHelper.GetDBConnection())
            {
                try
                {
                    var para = new DynamicParameters();
                    para.Add("@DocumentId", filter.DocumentId.Value, DbType.Int32);

                    retVal = conn.Query<FdpFeature>("Fdp_ProgrammeFeature_GetMany", para, commandType: CommandType.StoredProcedure);
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return retVal;
        }
        public static async Task<FeatureViewModel> GetModel(IDataContext context, FeatureFilter featureFilter)
        {
            FeatureViewModel model = null;

            if (featureFilter.Action == FeatureAction.Delete || featureFilter.Action == FeatureAction.Feature)
            {
                model = await GetFullAndPartialViewModelForFeature(context, featureFilter);
            }
            else if (featureFilter.Action == FeatureAction.Features)
            {
                model = await GetFullAndPartialViewModelForFeatures(context, featureFilter);
            }
            else
            {
                model = GetFullAndPartialViewModel(context, featureFilter);
            }
            if (featureFilter.Action != FeatureAction.NotSet)
            {
                model.IdentifierPrefix = Enum.GetName(featureFilter.Action.GetType(), featureFilter.Action);
            }
           
            return model;
        }
        private static async Task<ImportViewModel> GetFullAndPartialViewModelForException(IDataContext context,
                                                                                          ImportQueueFilter filter)
        {
            var model = new ImportViewModel(GetBaseModel(context))
            {
                PageIndex = filter.PageIndex ?? 1,
                PageSize = filter.PageSize ?? int.MaxValue,
                Configuration = context.ConfigurationSettings,
                CurrentException = await context.Import.GetException(filter)
            };

            var programmeFilter = new ProgrammeFilter(model.CurrentException.ProgrammeId) { DocumentId = model.CurrentException.DocumentId };
            var featureFilter = new FeatureFilter { ProgrammeId = model.CurrentException.ProgrammeId, DocumentId = model.CurrentException.DocumentId };
            
            model.Programme = context.Vehicle.GetProgramme(programmeFilter);
            programmeFilter.VehicleId = model.Programme.VehicleId;

            model.Gateway = model.CurrentException.Gateway;
            model.AvailableDocuments = context.Vehicle.ListPublishedDocuments(programmeFilter);
            model.AvailableEngines = context.Vehicle.ListEngines(programmeFilter);
            model.AvailableTransmissions = context.Vehicle.ListTransmissions(programmeFilter);
            model.AvailableBodies = context.Vehicle.ListBodies(programmeFilter);
            model.AvailableSpecialFeatures = await context.TakeRate.ListSpecialFeatures(programmeFilter);
            model.AvailableMarkets = await context.Market.ListAvailableMarkets();
            model.AvailableFeatures = await context.Vehicle.ListFeatures(featureFilter);
            model.AvailableFeatureGroups = context.Vehicle.ListFeatureGroups(programmeFilter);
            model.AvailableTrimLevels = context.Vehicle.ListTrimLevels(programmeFilter);

            var derivativeFilter = new DerivativeFilter
            {
                CarLine = model.Programme.VehicleName,
                ModelYear = model.Programme.ModelYear,
                Gateway = model.Gateway,
                ProgrammeId = model.CurrentException.ProgrammeId,
            };

            model.AvailableDerivatives = context.Vehicle.ListDerivatives(derivativeFilter);
            model.AvailableImportDerivatives = await ListImportDerivatives(model.CurrentException.ImportQueueId, context);
            model.AvailableImportTrimLevels = await ListImportTrimLevels(model.CurrentException.ImportQueueId, context);
            model.AvailableImportFeatures = await ListImportFeatures(model.CurrentException.ImportQueueId, context);

            derivativeFilter.Bmc = model.CurrentException.ImportDerivativeCode;
           
            var trimFilter = new TrimMappingFilter
            {
                CarLine = model.Programme.VehicleName,
                ModelYear = model.Programme.ModelYear,
                Gateway = model.Gateway,
                DocumentId = model.CurrentException is EmptyOxoDocument ? (int?) null : model.CurrentException.DocumentId,
                IncludeAllTrim = false
            };
            model.AvailableTrim = (await context.Vehicle.ListOxoTrim(trimFilter)).CurrentPage;
            model.Document = model.AvailableDocuments.FirstOrDefault(d => d.Id == model.CurrentException.DocumentId);

            return model;
        }
 public async Task<PagedResults<FdpFeature>> ListFdpFeatures(FeatureFilter filter)
 {
     return await Task.FromResult(_featureDataStore.FdpFeatureGetMany(filter));
 }
 public async Task<FdpFeature> GetFdpFeature(FeatureFilter filter)
 {
     return await Task.FromResult(_featureDataStore.FdpFeatureGet(filter));
 }
 public async Task<IEnumerable<FdpFeature>> ListFeatures(FeatureFilter filter)
 {
     return await Task.FromResult(_featureDataStore.FeatureGetManyByDocumentId(filter));
 }
        public PagedResults<FdpFeature> FdpFeatureGetMany(FeatureFilter filter)
        {
            PagedResults<FdpFeature> 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);
                    }
                    if (filter.ProgrammeId.HasValue)
                    {
                        para.Add("@ProgrammeId", filter.ProgrammeId, DbType.Int32);
                    }
                    if (!string.IsNullOrEmpty(filter.Gateway))
                    {
                        para.Add("@Gateway", filter.Gateway, DbType.String);
                    }
                    if (!string.IsNullOrEmpty(filter.FilterMessage))
                    {
                        para.Add("@Filter", filter.FilterMessage, 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);

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

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

                    var currentPage = new List<FdpFeature>();

                    foreach (var result in results)
                    {
                        currentPage.Add(result);
                    }
                    retVal.CurrentPage = currentPage;
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                    throw;
                }
            }
            return retVal;
        }
 public FdpFeature FdpFeatureGet(FeatureFilter filter)
 {
     FdpFeature retVal = new EmptyFdpFeature();
     using (IDbConnection conn = DbHelper.GetDBConnection())
     {
         try
         {
             var para = new DynamicParameters();
             para.Add("@FdpFeatureId", filter.FeatureId.GetValueOrDefault(), DbType.Int32);
             retVal = conn.Query<FdpFeature>("dbo.Fdp_Feature_Get", para, commandType: CommandType.StoredProcedure).FirstOrDefault();
         }
         catch (Exception ex)
         {
             Log.Error(ex);
             throw;
         }
     }
     return retVal;
 }
        private static async Task<FeatureViewModel> GetFullAndPartialViewModelForFeatures(IDataContext context,
                                                                                                FeatureFilter filter)
        {
            var baseModel = SharedModelBase.GetBaseModel(context);
            var model = new FeatureViewModel()
            {
                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 programmeFilter = new ProgrammeFilter() {
                ProgrammeId = filter.ProgrammeId,
                Gateway = filter.Gateway
            };
            HydrateModelWithCommonProperties(model, context, programmeFilter);

            model.Features = await context.Vehicle.ListFdpFeatures(filter);
            model.TotalPages = model.Features.TotalPages;
            model.TotalRecords = model.Features.TotalRecords;
            model.TotalDisplayRecords = model.Features.TotalDisplayRecords;

            foreach (var feature in model.Features.CurrentPage)
            {
                feature.Programme = model.Programmes.FirstOrDefault(p => p.Id == feature.ProgrammeId.GetValueOrDefault());
            }

            return model;
        }