private static async Task <TakeRateViewModel> GetFullAndPartialViewModelForTakeRateDataPageExcludingData(IDataContext context, TakeRateFilter filter)
        {
            var modelBase     = GetBaseModel(context);
            var takeRateModel = new TakeRateViewModel(modelBase)
            {
                Document           = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration      = context.ConfigurationSettings,
                MarketReviewStatus = filter.MarketReviewStatus,
                Filter             = filter.Filter
            };

            await HydrateOxoDocument(context, takeRateModel);
            await HydrateFdpVolumeHeader(context, takeRateModel);

            //await HydrateFdpVolumeHeadersFromOxoDocument(context, takeRateModel);
            await HydrateVehicle(context, takeRateModel);
            await HydrateMarket(context, takeRateModel);
            await HydrateMarketGroup(context, takeRateModel);
            await HydrateMarketGroups(context, takeRateModel);
            await HydrateModelsByMarket(context, takeRateModel);
            await HydrateMarketReview(context, takeRateModel);
            await HydratePublish(context, takeRateModel);

            return(takeRateModel);
        }
        private static async Task <RawTakeRateData> HydrateRawData(IDataContext context,
                                                                   TakeRateViewModel takeRateViewModel)
        {
            takeRateViewModel.RawData = await GetRawData(context, takeRateViewModel);

            return(takeRateViewModel.RawData);
        }
        private static async Task <OXODoc> HydrateOxoDocument(IDataContext context, TakeRateViewModel volumeModel)
        {
            var    watch  = Stopwatch.StartNew();
            OXODoc retVal = new EmptyOxoDocument();

            if (!(volumeModel.Document.UnderlyingOxoDocument is EmptyOxoDocument))
            {
                volumeModel.Document.UnderlyingOxoDocument =
                    await GetOxoDocument(context, volumeModel.Document.UnderlyingOxoDocument);
            }
            else
            {
                volumeModel.Document.UnderlyingOxoDocument =
                    await GetOxoDocumentFromTakeRateFile(context, volumeModel.Document);
            }

            volumeModel.Document.Vehicle.ProgrammeId = volumeModel.Document.UnderlyingOxoDocument.ProgrammeId;
            volumeModel.Document.Vehicle.Gateway     = volumeModel.Document.UnderlyingOxoDocument.Gateway;

            retVal = volumeModel.Document.UnderlyingOxoDocument;

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);

            return(retVal);
        }
        public static TakeRateFilter FromTakeRateViewModel(TakeRateViewModel takeRateViewModel)
        {
            var filter = new TakeRateFilter()
            {
                ProgrammeId = takeRateViewModel.Document.UnderlyingOxoDocument.ProgrammeId,
                Gateway = takeRateViewModel.Document.UnderlyingOxoDocument.Gateway
            };

            if (!(takeRateViewModel.Document.UnderlyingOxoDocument is EmptyOxoDocument))
            {
                filter.TakeRateId = takeRateViewModel.Document.TakeRateId;
                filter.DocumentId = takeRateViewModel.Document.UnderlyingOxoDocument.Id;
            }

            if (!(takeRateViewModel.Document.Market is EmptyMarket))
                filter.MarketId = takeRateViewModel.Document.Market.Id;

            if (!(takeRateViewModel.Document.MarketGroup is EmptyMarketGroup))
                filter.MarketGroupId = takeRateViewModel.Document.MarketGroup.Id;

            if (!(takeRateViewModel.Document.Vehicle is EmptyVehicle))
                filter.Models = takeRateViewModel.Document.Vehicle.AvailableModels;

            filter.Mode = takeRateViewModel.Document.Mode;
            filter.FeatureCode = takeRateViewModel.Document.FeatureCode;
            filter.ShowCombinedPackOptions = takeRateViewModel.Document.ShowCombinedPackOptions;
            filter.ExcludeOptionalPackItems = takeRateViewModel.Document.ExcludeOptionalPackItems;

            return filter;
        }
        private static async Task <TakeRateViewModel> GetFullAndPartialViewModelForTakeRates(IDataContext context, TakeRateFilter filter)
        {
            var model = new TakeRateViewModel(GetBaseModel(context))
            {
                Configuration = context.ConfigurationSettings, TakeRates = await context.TakeRate.ListTakeRateDocuments(filter), Statuses = await context.TakeRate.ListTakeRateStatuses()
            };

            return(model);
        }
 private static async Task <RawTakeRateData> GetRawData(IDataContext context, TakeRateViewModel takeRateViewModel)
 {
     return
         (await context.TakeRate.GetRawData(new TakeRateFilter()
     {
         TakeRateId = takeRateViewModel.Document.TakeRateId,
         MarketId = takeRateViewModel.Document.Market.Id
     }));
 }
        private static async Task HydrateMarketReview(IDataContext context, TakeRateViewModel volumeModel)
        {
            var watch = Stopwatch.StartNew();

            volumeModel.TakeRate.MarketReview = await GetMarketReview(context, volumeModel.Document);

            volumeModel.MarketReviewStatus =
                (Enumerations.MarketReviewStatus)volumeModel.TakeRate.MarketReview.FdpMarketReviewStatusId;

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);
        }
        private static async Task <TakeRateViewModel> GetPartialViewModelForTakeRateDataPage(IDataContext context, TakeRateFilter filter)
        {
            var watch = Stopwatch.StartNew();

            var modelBase     = GetBaseModel(context);
            var takeRateModel = new TakeRateViewModel(modelBase)
            {
                Document = (TakeRateDocument)TakeRateDocument.FromFilter(filter), Configuration = context.ConfigurationSettings
            };

            takeRateModel.Document.PageIndex = filter.PageIndex;
            takeRateModel.Document.PageSize  = filter.PageSize;
            if (takeRateModel.Document.PageSize == -1)
            {
                takeRateModel.Document.PageSize = int.MaxValue;
            }

            if (!takeRateModel.Document.PageIndex.HasValue)
            {
                takeRateModel.Document.PageIndex = 1;
            }
            if (!takeRateModel.Document.PageSize.HasValue)
            {
                var configuredPageSize = context.ConfigurationSettings.GetInteger("TakeRateDataPageSize");
                if (configuredPageSize == -1)
                {
                    configuredPageSize = int.MaxValue;
                }
                takeRateModel.Document.PageSize = configuredPageSize;
            }
            takeRateModel.Document.ShowCombinedPackOptions  = filter.ShowCombinedPackOptions;
            takeRateModel.Document.ExcludeOptionalPackItems = filter.ExcludeOptionalPackItems;

            await HydrateOxoDocument(context, takeRateModel);
            await HydrateFdpVolumeHeader(context, takeRateModel);

            //await HydrateFdpVolumeHeadersFromOxoDocument(context, takeRateModel);
            await HydrateVehicle(context, takeRateModel);
            await HydrateMarket(context, takeRateModel);
            await HydrateMarketGroup(context, takeRateModel);
            await HydrateMarketGroups(context, takeRateModel);
            await HydrateModelsByMarket(context, takeRateModel);
            await HydrateData(context, takeRateModel);

            //await HydrateDataFeatures(context, takeRateModel);
            await HydrateMarketReview(context, takeRateModel);
            await HydratePublish(context, takeRateModel);

            watch.Stop();
            Log.Debug("GetFullAndPartialViewModelForTakeRateDataPage : " + watch.ElapsedMilliseconds);

            return(takeRateModel);
        }
        private static async Task <TakeRateViewModel> GetFullAndPartialViewModelForPowertrain(IDataContext context, TakeRateFilter filter)
        {
            var modelBase     = GetBaseModel(context);
            var takeRateModel = new TakeRateViewModel(modelBase)
            {
                Document      = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration = context.ConfigurationSettings
            };

            await HydrateFdpVolumeHeader(context, takeRateModel);
            await HydratePowertrain(context, takeRateModel);
            await HydratePublish(context, takeRateModel);

            return(takeRateModel);
        }
        private static async Task <Market> HydrateMarket(IDataContext context, TakeRateViewModel volumeModel)
        {
            var watch = Stopwatch.StartNew();

            if (volumeModel.Document.Market is EmptyMarket)
            {
                return(volumeModel.Document.Market);
            }

            volumeModel.Document.Market = await GetMarket(context, volumeModel.Document);

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);
            return(volumeModel.Document.Market);
        }
        private static async Task <TakeRateViewModel> GetViewModelForTakeRateDataPage(IDataContext context,
                                                                                      TakeRateFilter filter)
        {
            var modelBase     = GetBaseModel(context);
            var takeRateModel = new TakeRateViewModel(modelBase)
            {
                Document      = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration = context.ConfigurationSettings
            };

            takeRateModel.Document.PageIndex = filter.PageIndex;
            takeRateModel.Document.PageSize  = filter.PageSize;
            if (takeRateModel.Document.PageSize == -1)
            {
                takeRateModel.Document.PageSize = int.MaxValue;
            }

            if (!takeRateModel.Document.PageIndex.HasValue)
            {
                takeRateModel.Document.PageIndex = 1;
            }
            if (!takeRateModel.Document.PageSize.HasValue)
            {
                var configuredPageSize = context.ConfigurationSettings.GetInteger("TakeRateDataPageSize");
                if (configuredPageSize == -1)
                {
                    configuredPageSize = int.MaxValue;
                }
                takeRateModel.Document.PageSize = configuredPageSize;
            }

            await HydrateOxoDocument(context, takeRateModel);
            await HydrateFdpVolumeHeader(context, takeRateModel);

            await HydrateVehicle(context, takeRateModel);
            await HydrateMarket(context, takeRateModel);
            await HydrateMarketGroups(context, takeRateModel);

            await HydrateModels(context, takeRateModel);
            await HydrateFeatures(context, takeRateModel);

            await HydrateMarketReview(context, takeRateModel);
            await HydratePublish(context, takeRateModel);

            return(takeRateModel);
        }
        private static async Task <TakeRateViewModel> GetFullAndPartialViewModelForFilter(IDataContext context,
                                                                                          TakeRateFilter filter)
        {
            var modelBase     = GetBaseModel(context);
            var takeRateModel = new TakeRateViewModel(modelBase)
            {
                Document           = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration      = context.ConfigurationSettings,
                MarketReviewStatus = filter.MarketReviewStatus,
                Filter             = filter.Filter
            };

            await HydrateOxoDocument(context, takeRateModel);
            await HydrateFdpVolumeHeader(context, takeRateModel);

            return(takeRateModel);
        }
        private static async Task <IVehicle> HydrateVehicle(IDataContext context, TakeRateViewModel volumeModel)
        {
            var watch = Stopwatch.StartNew();

            if (!(volumeModel.Document.Vehicle is EmptyVehicle))
            {
                return(volumeModel.Document.Vehicle);
            }

            volumeModel.Document.Vehicle = (Vehicle)(await GetVehicle(context, volumeModel.Document.Vehicle, volumeModel.Document.UnderlyingOxoDocument));
            // Set this prior to filtering by market
            volumeModel.Document.TotalDerivatives = volumeModel.Document.Vehicle.AvailableModels.Count();

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);

            return(volumeModel.Document.Vehicle);
        }
        private static async Task <TakeRateData> HydrateData(IDataContext context, TakeRateViewModel takeRateModel)
        {
            //var cacheKey = string.Format("TakeRateData_{0}_{1}_{2}_{3}", takeRateModel.Document.TakeRateId, takeRateModel.Document.Market.Id, takeRateModel.PageIndex, takeRateModel.PageSize);
            //var cachedData = HttpContext.Current.Cache.Get(cacheKey);
            //if (cachedData != null)
            //{
            //    takeRateModel.Document.TakeRateData = (TakeRateData) cachedData;
            //}
            //else
            //{
            takeRateModel.Document.TakeRateData = await ListTakeRateData(context, takeRateModel);

            //    HttpContext.Current.Cache.Insert(cacheKey, takeRateModel.Document.TakeRateData, null,
            //        DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
            //}


            return(takeRateModel.Document.TakeRateData);
        }
        private static async Task <TakeRateViewModel> GetFullAndPartialViewModelForValidation(IDataContext context, TakeRateFilter filter)
        {
            var watch = Stopwatch.StartNew();

            var modelBase     = GetBaseModel(context);
            var takeRateModel = new TakeRateViewModel(modelBase)
            {
                Document      = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration = context.ConfigurationSettings
            };

            await HydrateFdpVolumeHeader(context, takeRateModel);
            await HydrateRawData(context, takeRateModel);

            watch.Stop();
            Log.Debug("GetFullAndPartialViewModelForTakeRateDataPage : " + watch.ElapsedMilliseconds);

            return(takeRateModel);
        }
        private static async Task <TakeRateSummary> HydrateFdpVolumeHeader(IDataContext context,
                                                                           TakeRateViewModel volumeModel)
        {
            var watch         = Stopwatch.StartNew();
            var volumeSummary = new TakeRateSummary {
                TakeRateId = volumeModel.Document.TakeRateId.GetValueOrDefault()
            };

            volumeSummary = await GetTakeRateDocumentHeader(context, volumeSummary);

            volumeModel.Document.TakeRateSummary = new List <TakeRateSummary> {
                volumeSummary
            };

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);

            return(volumeSummary);
        }
        private static async Task <TakeRateViewModel> GetFullAndPartialViewModelForClone(IDataContext context,
                                                                                         TakeRateFilter filter)
        {
            var takeRateModel = new TakeRateViewModel(GetBaseModel(context))
            {
                Document      = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration = context.ConfigurationSettings,
            };

            await HydrateOxoDocument(context, takeRateModel);
            await HydrateFdpVolumeHeader(context, takeRateModel);
            await HydrateVehicle(context, takeRateModel);

            takeRateModel.AvailableProgrammes = await
                                                Task.FromResult(context.Vehicle.ListProgrammes(new ProgrammeFilter()));

            takeRateModel.AvailableDocuments = await
                                               Task.FromResult(context.Vehicle.ListPublishedDocuments(new ProgrammeFilter()));

            return(takeRateModel);
        }
        private static async Task <TakeRateViewModel> GetFullAndPartialViewModelForTakeRateDataItem(IDataContext context, TakeRateFilter filter)
        {
            var takeRateModel = new TakeRateViewModel(GetBaseModel(context))
            {
                Document                = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration           = context.ConfigurationSettings,
                CurrentTakeRateDataItem = await context.TakeRate.GetDataItem(filter)
            };

            await HydrateOxoDocument(context, takeRateModel);
            await HydrateVehicle(context, takeRateModel);
            await HydrateMarket(context, takeRateModel);
            await HydrateMarketGroup(context, takeRateModel);
            await HydrateModelsByMarket(context, takeRateModel);
            await HydrateFeatures(context, takeRateModel);
            await HydrateCurrentModel(context, takeRateModel);
            await HydrateCurrentFeature(context, takeRateModel);
            await HydratePublish(context, takeRateModel);

            return(takeRateModel);
        }
        private static async Task<FdpFeature> HydrateCurrentFeature(IDataContext context, TakeRateViewModel takeRateModel)
        {
            FdpFeature feature = new EmptyFdpFeature();

            if (takeRateModel.CurrentTakeRateDataItem.FeatureId.HasValue)
            {
                feature = takeRateModel.Document.Vehicle.AvailableFeatures.First(f => f.Id == takeRateModel.CurrentTakeRateDataItem.FeatureId.Value);
            }
            else if (takeRateModel.CurrentTakeRateDataItem.FdpFeatureId.HasValue)
            {
                feature = takeRateModel.Document.Vehicle.AvailableFeatures.First(f => f.FdpFeatureId == takeRateModel.CurrentTakeRateDataItem.FdpFeatureId.Value);
            }
            takeRateModel.CurrentTakeRateDataItem.Feature = feature;

            return await Task.FromResult(feature);
        }
        private static async Task<TakeRateViewModel> GetExportViewModelForTakeRateDataPage(IDataContext context, TakeRateFilter filter)
        {
            var watch = Stopwatch.StartNew();

            var modelBase = GetBaseModel(context);
            var takeRateModel = new TakeRateViewModel(modelBase)
            {
                Document = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration = context.ConfigurationSettings
            };
            takeRateModel.Document.PageIndex = filter.PageIndex;
            takeRateModel.Document.PageSize = filter.PageSize;
            if (takeRateModel.Document.PageSize == -1)
            {
                takeRateModel.Document.PageSize = int.MaxValue;
            }

            if (!takeRateModel.Document.PageIndex.HasValue)
            {
                takeRateModel.Document.PageIndex = 1;
            }
            if (!takeRateModel.Document.PageSize.HasValue)
            {
                var configuredPageSize = context.ConfigurationSettings.GetInteger("TakeRateDataPageSize");
                if (configuredPageSize == -1)
                {
                    configuredPageSize = int.MaxValue;
                }
                takeRateModel.Document.PageSize = configuredPageSize;
            }
            takeRateModel.Document.ShowCombinedPackOptions = filter.ShowCombinedPackOptions;

            await HydrateOxoDocument(context, takeRateModel);
            await HydrateFdpVolumeHeader(context, takeRateModel);
            //await HydrateFdpVolumeHeadersFromOxoDocument(context, takeRateModel);
            await HydrateVehicle(context, takeRateModel);
            await HydrateMarket(context, takeRateModel);
            await HydrateMarketGroup(context, takeRateModel);
            await HydrateMarketGroups(context, takeRateModel);
            await HydrateExportModelsByMarket(context, takeRateModel);
            await HydrateData(context, takeRateModel);
            await HydrateMarketReview(context, takeRateModel);

            watch.Stop();
            Log.Debug("GetFullAndPartialViewModelForTakeRateDataPage : " + watch.ElapsedMilliseconds);

            return takeRateModel;
        }
        private static async Task<TakeRateViewModel> GetFullAndPartialViewModelForValidation(IDataContext context, TakeRateFilter filter)
        {
            var watch = Stopwatch.StartNew();

            var modelBase = GetBaseModel(context);
            var takeRateModel = new TakeRateViewModel(modelBase)
            {
                Document = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration = context.ConfigurationSettings
            };

            await HydrateFdpVolumeHeader(context, takeRateModel);
            await HydrateRawData(context, takeRateModel);

            watch.Stop();
            Log.Debug("GetFullAndPartialViewModelForTakeRateDataPage : " + watch.ElapsedMilliseconds);

            return takeRateModel;
        }
        private static async Task<TakeRateViewModel> GetFullAndPartialViewModelForTakeRates(IDataContext context, TakeRateFilter filter)
        {
            var model = new TakeRateViewModel(GetBaseModel(context))
            {
                Configuration = context.ConfigurationSettings, TakeRates = await context.TakeRate.ListTakeRateDocuments(filter), Statuses = await context.TakeRate.ListTakeRateStatuses()
            };

            return model;
        }
        private static async Task<TakeRateViewModel> GetFullAndPartialViewModelForClone(IDataContext context,
            TakeRateFilter filter)
        {
            var takeRateModel = new TakeRateViewModel(GetBaseModel(context))
            {
                Document = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration = context.ConfigurationSettings,
            };

            await HydrateOxoDocument(context, takeRateModel);
            await HydrateFdpVolumeHeader(context, takeRateModel);
            await HydrateVehicle(context, takeRateModel);

            takeRateModel.AvailableProgrammes = await
                Task.FromResult(context.Vehicle.ListProgrammes(new ProgrammeFilter()));
            takeRateModel.AvailableDocuments = await
                Task.FromResult(context.Vehicle.ListPublishedDocuments(new ProgrammeFilter()));

            return takeRateModel;
        }
        public static async Task <TakeRateViewModel> GetModel(IDataContext context, TakeRateFilter filter)
        {
            TakeRateViewModel model = null;

            // Here we determine which parts of the complex take rate model we need to hydrate dependant upon the
            // action being performed. This eliminates unnecessary database calls if the properties in question
            // aren't being used

            switch (filter.Action)
            {
            case TakeRateDataItemAction.TakeRates:
                model = await GetFullAndPartialViewModelForTakeRates(context, filter);

                break;

            case TakeRateDataItemAction.TakeRateDataHeader:
            case TakeRateDataItemAction.TakeRateDataPage:
                model = await GetPartialViewModelForTakeRateDataPage(context, filter);

                break;

            case TakeRateDataItemAction.Export:
                model = await GetExportViewModelForTakeRateDataPage(context, filter);

                break;

            case TakeRateDataItemAction.Validate:
                model = await GetFullAndPartialViewModelForValidation(context, filter);

                break;

            case TakeRateDataItemAction.TakeRateDataItemDetails:
            case TakeRateDataItemAction.UndoChange:
            case TakeRateDataItemAction.AddNote:
                model = await GetFullAndPartialViewModelForTakeRateDataItem(context, filter);

                break;

            case TakeRateDataItemAction.NotSet:
                break;

            case TakeRateDataItemAction.SaveChanges:
            case TakeRateDataItemAction.History:
            case TakeRateDataItemAction.HistoryDetails:
            case TakeRateDataItemAction.MarketReview:
            case TakeRateDataItemAction.Publish:
            case TakeRateDataItemAction.ValidationSummary:
                model = await GetFullAndPartialViewModelForTakeRateDataPageExcludingData(context, filter);

                break;

            case TakeRateDataItemAction.Filter:
                model = await GetFullAndPartialViewModelForFilter(context, filter);

                break;

            case TakeRateDataItemAction.Changeset:
                model = await GetFullAndPartialViewModelForChangeset(context, filter);

                break;

            case TakeRateDataItemAction.Clone:
                model = await GetFullAndPartialViewModelForClone(context, filter);

                break;

            case TakeRateDataItemAction.Powertrain:
                model = await GetFullAndPartialViewModelForPowertrain(context, filter);

                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(model);
        }
        private static async Task <TakeRateData> ListTakeRateDataFeatures(IDataContext context, TakeRateViewModel takeRateViewModel)
        {
            if (takeRateViewModel.Document.UnderlyingOxoDocument is EmptyOxoDocument)
            {
                return(new TakeRateData());
            }

            return(await context.TakeRate.GetTakeRateDocumentFeatures(TakeRateFilter.FromTakeRateViewModel(takeRateViewModel)));
        }
        private static async Task<TakeRateData> HydrateData(IDataContext context, TakeRateViewModel takeRateModel)
        {
            //var cacheKey = string.Format("TakeRateData_{0}_{1}_{2}_{3}", takeRateModel.Document.TakeRateId, takeRateModel.Document.Market.Id, takeRateModel.PageIndex, takeRateModel.PageSize);
            //var cachedData = HttpContext.Current.Cache.Get(cacheKey);
            //if (cachedData != null)
            //{
            //    takeRateModel.Document.TakeRateData = (TakeRateData) cachedData;
            //}
            //else
            //{
                takeRateModel.Document.TakeRateData = await ListTakeRateData(context, takeRateModel);
            //    HttpContext.Current.Cache.Insert(cacheKey, takeRateModel.Document.TakeRateData, null,
            //        DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
            //}
            

            return takeRateModel.Document.TakeRateData;
        }
        private static async Task<OXODoc> HydrateOxoDocument(IDataContext context, TakeRateViewModel volumeModel)
        {
            var watch = Stopwatch.StartNew();
            OXODoc retVal = new EmptyOxoDocument();

            if (!(volumeModel.Document.UnderlyingOxoDocument is EmptyOxoDocument))
            {
                volumeModel.Document.UnderlyingOxoDocument =
                    await GetOxoDocument(context, volumeModel.Document.UnderlyingOxoDocument);
            }
            else
            {
                volumeModel.Document.UnderlyingOxoDocument =
                    await GetOxoDocumentFromTakeRateFile(context, volumeModel.Document);
            }

            volumeModel.Document.Vehicle.ProgrammeId = volumeModel.Document.UnderlyingOxoDocument.ProgrammeId;
            volumeModel.Document.Vehicle.Gateway = volumeModel.Document.UnderlyingOxoDocument.Gateway;

            retVal = volumeModel.Document.UnderlyingOxoDocument;

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);

            return retVal;
        }
 private static async Task HydratePublish(IDataContext context, TakeRateViewModel volumeModel)
 {
     volumeModel.TakeRate.Publish = await GetPublishState(context, volumeModel.Document);
 }
        private static async Task HydrateMarketReview(IDataContext context, TakeRateViewModel volumeModel)
        {
            var watch = Stopwatch.StartNew();
            volumeModel.TakeRate.MarketReview = await GetMarketReview(context, volumeModel.Document);
            volumeModel.MarketReviewStatus =
                (Enumerations.MarketReviewStatus) volumeModel.TakeRate.MarketReview.FdpMarketReviewStatusId;

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);
        }
        private static async Task<IEnumerable<TakeRateSummary>> HydrateFdpVolumeHeaders(IDataContext context, TakeRateViewModel volumeModel)
        {
            var watch = Stopwatch.StartNew();
            var volumeSummary = new List<TakeRateSummary>();
            foreach (var header in volumeModel.Document.TakeRateSummary)
            {
                await GetTakeRateDocumentHeader(context, header);
            }

            volumeModel.Document.TakeRateSummary = volumeSummary;

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);

            return volumeSummary;
        }
        private static async Task<TakeRateSummary> HydrateFdpVolumeHeader(IDataContext context,
            TakeRateViewModel volumeModel)
        {
            var watch = Stopwatch.StartNew();
            var volumeSummary = new TakeRateSummary {TakeRateId = volumeModel.Document.TakeRateId.GetValueOrDefault()};
            volumeSummary = await GetTakeRateDocumentHeader(context, volumeSummary);

            volumeModel.Document.TakeRateSummary = new List<TakeRateSummary> { volumeSummary };

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);

            return volumeSummary;
        }
        private static async Task <TakeRateData> HydrateDataFeatures(IDataContext context, TakeRateViewModel takeRateModel)
        {
            takeRateModel.Document.TakeRateData = await ListTakeRateDataFeatures(context, takeRateModel);

            return(takeRateModel.Document.TakeRateData);
        }
        private static async Task<MarketGroup> HydrateMarketGroup(IDataContext context, TakeRateViewModel volumeModel)
        {
            var watch = Stopwatch.StartNew();
            if (volumeModel.Document.MarketGroup is EmptyMarketGroup)
                return volumeModel.Document.MarketGroup;

            volumeModel.Document.MarketGroup = await GetMarketGroup(context, volumeModel.Document);
            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);
            return volumeModel.Document.MarketGroup;
        }
 private static async Task<TakeRateData> HydrateDataFeatures(IDataContext context, TakeRateViewModel takeRateModel)
 {
     takeRateModel.Document.TakeRateData = await ListTakeRateDataFeatures(context, takeRateModel);
     
     return takeRateModel.Document.TakeRateData;
 }
 private static async Task <IEnumerable <RawPowertrainDataItem> > ListPowertrainData(IDataContext context,
                                                                                     TakeRateViewModel takeRateViewModel)
 {
     return(await context.TakeRate.ListPowertrainData(TakeRateFilter.FromTakeRateViewModel(takeRateViewModel)));
 }
        private static async Task<IVehicle> HydrateVehicle(IDataContext context, TakeRateViewModel volumeModel)
        {
            var watch = Stopwatch.StartNew();
            if (!(volumeModel.Document.Vehicle is EmptyVehicle))
                return volumeModel.Document.Vehicle;

            volumeModel.Document.Vehicle = (Vehicle) (await GetVehicle(context, volumeModel.Document.Vehicle, volumeModel.Document.UnderlyingOxoDocument));
            // Set this prior to filtering by market
            volumeModel.Document.TotalDerivatives = volumeModel.Document.Vehicle.AvailableModels.Count();

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);

            return volumeModel.Document.Vehicle;
        }
 private static async Task<RawTakeRateData> GetRawData(IDataContext context, TakeRateViewModel takeRateViewModel)
 {
     return
             await context.TakeRate.GetRawData(new TakeRateFilter()
             {
                 TakeRateId = takeRateViewModel.Document.TakeRateId,
                 MarketId = takeRateViewModel.Document.Market.Id
             });
 }
        private static async Task <IEnumerable <Model> > HydrateExportModelsByMarket(IDataContext context, TakeRateViewModel volumeModel)
        {
            var watch = Stopwatch.StartNew();

            volumeModel.Document.Vehicle.AvailableModels = await ListAvailableModels(context, volumeModel.Document);

            watch.Stop();

            Log.Debug(watch.ElapsedMilliseconds);
            return(volumeModel.Document.Vehicle.AvailableModels);
        }
 private static async Task<RawTakeRateData> HydrateRawData(IDataContext context,
     TakeRateViewModel takeRateViewModel)
 {
     takeRateViewModel.RawData = await GetRawData(context, takeRateViewModel);
     return takeRateViewModel.RawData;
 }
        private static async Task <IEnumerable <RawPowertrainDataItem> > HydratePowertrain(IDataContext context,
                                                                                           TakeRateViewModel takeRateViewModel)
        {
            if (takeRateViewModel.Document.TakeRateData is EmptyTakeRateData)
            {
                takeRateViewModel.Document.TakeRateData = new TakeRateData();
            }

            takeRateViewModel.Document.TakeRateData.PowertrainData = await ListPowertrainData(context, takeRateViewModel);

            return(takeRateViewModel.Document.TakeRateData.PowertrainData);
        }
        private static async Task<TakeRateViewModel> GetFullAndPartialViewModelForTakeRateDataItem(IDataContext context, TakeRateFilter filter)
        {
            var takeRateModel = new TakeRateViewModel(GetBaseModel(context))
            {
                Document = (TakeRateDocument) TakeRateDocument.FromFilter(filter), 
                Configuration = context.ConfigurationSettings, 
                CurrentTakeRateDataItem = await context.TakeRate.GetDataItem(filter)
            };

            await HydrateOxoDocument(context, takeRateModel);
            await HydrateVehicle(context, takeRateModel);
            await HydrateMarket(context, takeRateModel);
            await HydrateMarketGroup(context, takeRateModel);
            await HydrateModelsByMarket(context, takeRateModel);
            await HydrateFeatures(context, takeRateModel);
            await HydrateCurrentModel(context, takeRateModel);
            await HydrateCurrentFeature(context, takeRateModel);
            await HydratePublish(context, takeRateModel);

            return takeRateModel;
        }
        private static async Task <FdpModel> HydrateCurrentModel(IDataContext context, TakeRateViewModel takeRateModel)
        {
            FdpModel model = new EmptyFdpModel();

            if (takeRateModel.CurrentTakeRateDataItem.ModelId.HasValue)
            {
                model = takeRateModel.Document.Vehicle.AvailableModels.First(m => m.Id == takeRateModel.CurrentTakeRateDataItem.ModelId.Value);
            }
            else if (takeRateModel.CurrentTakeRateDataItem.FdpModelId.HasValue)
            {
                model = takeRateModel.Document.Vehicle.AvailableModels.First(m => m.FdpModelId == takeRateModel.CurrentTakeRateDataItem.FdpModelId.Value);
            }
            takeRateModel.CurrentTakeRateDataItem.Model = model;

            return(await Task.FromResult(model));
        }
        private static async Task<TakeRateViewModel> GetViewModelForTakeRateDataPage(IDataContext context,
            TakeRateFilter filter)
        {
            var modelBase = GetBaseModel(context);
            var takeRateModel = new TakeRateViewModel(modelBase)
            {
                Document = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration = context.ConfigurationSettings
            };
            takeRateModel.Document.PageIndex = filter.PageIndex;
            takeRateModel.Document.PageSize = filter.PageSize;
            if (takeRateModel.Document.PageSize == -1)
            {
                takeRateModel.Document.PageSize = int.MaxValue;
            }

            if (!takeRateModel.Document.PageIndex.HasValue)
            {
                takeRateModel.Document.PageIndex = 1;
            }
            if (!takeRateModel.Document.PageSize.HasValue)
            {
                var configuredPageSize = context.ConfigurationSettings.GetInteger("TakeRateDataPageSize");
                if (configuredPageSize == -1)
                {
                    configuredPageSize = int.MaxValue;
                }
                takeRateModel.Document.PageSize = configuredPageSize;
            }

            await HydrateOxoDocument(context, takeRateModel);
            await HydrateFdpVolumeHeader(context, takeRateModel);
            
            await HydrateVehicle(context, takeRateModel);
            await HydrateMarket(context, takeRateModel);
            await HydrateMarketGroups(context, takeRateModel);

            await HydrateModels(context, takeRateModel);
            await HydrateFeatures(context, takeRateModel);

            await HydrateMarketReview(context, takeRateModel);
            await HydratePublish(context, takeRateModel);

            return takeRateModel;
        }
        private static async Task<IEnumerable<RawPowertrainDataItem>> HydratePowertrain(IDataContext context,
            TakeRateViewModel takeRateViewModel)
        {
            if (takeRateViewModel.Document.TakeRateData is EmptyTakeRateData)
                takeRateViewModel.Document.TakeRateData = new TakeRateData();

            takeRateViewModel.Document.TakeRateData.PowertrainData = await ListPowertrainData(context, takeRateViewModel);
            return takeRateViewModel.Document.TakeRateData.PowertrainData;
        }
 private static async Task<TakeRateViewModel> GetFullAndPartialViewModelForChangeset(IDataContext context, TakeRateFilter filter)
 {
     var modelBase = GetBaseModel(context);
     var takeRateModel = new TakeRateViewModel(modelBase)
     {
         Document = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
         Configuration = context.ConfigurationSettings
     };
     await HydrateOxoDocument(context, takeRateModel);
     await HydrateFdpVolumeHeaders(context, takeRateModel);
     //await HydrateFdpVolumeHeadersFromOxoDocument(context, takeRateModel);
     await HydratePublish(context, takeRateModel);
     
     return takeRateModel;
 }
        private static async Task<FdpModel> HydrateCurrentModel(IDataContext context, TakeRateViewModel takeRateModel)
        {
            FdpModel model = new EmptyFdpModel();

            if (takeRateModel.CurrentTakeRateDataItem.ModelId.HasValue)
            {
                model = takeRateModel.Document.Vehicle.AvailableModels.First(m => m.Id == takeRateModel.CurrentTakeRateDataItem.ModelId.Value);
            }
            else if (takeRateModel.CurrentTakeRateDataItem.FdpModelId.HasValue)
            {
                model = takeRateModel.Document.Vehicle.AvailableModels.First(m => m.FdpModelId == takeRateModel.CurrentTakeRateDataItem.FdpModelId.Value);
            }
            takeRateModel.CurrentTakeRateDataItem.Model = model;

            return await Task.FromResult(model);
        }
        private static async Task<IEnumerable<FdpModel>> HydrateModels(IDataContext context, TakeRateViewModel takeRateModel)
        {
            var watch = Stopwatch.StartNew();
            takeRateModel.Document.Vehicle.AvailableModels = await ListAvailableModels(context, takeRateModel.Document);

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);
            return takeRateModel.Document.Vehicle.AvailableModels;
        }
        private static async Task<IEnumerable<Model>> HydrateExportModelsByMarket(IDataContext context, TakeRateViewModel volumeModel)
        {
            var watch = Stopwatch.StartNew();
            volumeModel.Document.Vehicle.AvailableModels = await ListAvailableModels(context, volumeModel.Document);
            watch.Stop();

            Log.Debug(watch.ElapsedMilliseconds);
            return volumeModel.Document.Vehicle.AvailableModels;
        }
        private static async Task <IEnumerable <MarketGroup> > HydrateMarketGroups(IDataContext context, TakeRateViewModel takeRateModel)
        {
            var watch = Stopwatch.StartNew();

            takeRateModel.Document.Vehicle.AvailableMarketGroups = await ListAvailableMarketGroups(context, takeRateModel.Document);

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);
            return(takeRateModel.Document.Vehicle.AvailableMarketGroups);
        }
        private static async Task<TakeRateData> ListTakeRateDataFeatures(IDataContext context, TakeRateViewModel takeRateViewModel)
        {
            if (takeRateViewModel.Document.UnderlyingOxoDocument is EmptyOxoDocument)
                return new TakeRateData();

            return await context.TakeRate.GetTakeRateDocumentFeatures(TakeRateFilter.FromTakeRateViewModel(takeRateViewModel));
        }
 private static async Task<IEnumerable<RawPowertrainDataItem>> ListPowertrainData(IDataContext context,
     TakeRateViewModel takeRateViewModel)
 {
     return await context.TakeRate.ListPowertrainData(TakeRateFilter.FromTakeRateViewModel(takeRateViewModel));
 }
 private static async Task HydratePublish(IDataContext context, TakeRateViewModel volumeModel)
 {
     volumeModel.TakeRate.Publish = await GetPublishState(context, volumeModel.Document);
 }
        private static async Task <FdpFeature> HydrateCurrentFeature(IDataContext context, TakeRateViewModel takeRateModel)
        {
            FdpFeature feature = new EmptyFdpFeature();

            if (takeRateModel.CurrentTakeRateDataItem.FeatureId.HasValue)
            {
                feature = takeRateModel.Document.Vehicle.AvailableFeatures.First(f => f.Id == takeRateModel.CurrentTakeRateDataItem.FeatureId.Value);
            }
            else if (takeRateModel.CurrentTakeRateDataItem.FdpFeatureId.HasValue)
            {
                feature = takeRateModel.Document.Vehicle.AvailableFeatures.First(f => f.FdpFeatureId == takeRateModel.CurrentTakeRateDataItem.FdpFeatureId.Value);
            }
            takeRateModel.CurrentTakeRateDataItem.Feature = feature;

            return(await Task.FromResult(feature));
        }
        private static async Task<TakeRateViewModel> GetFullAndPartialViewModelForFilter(IDataContext context,
            TakeRateFilter filter)
        {
            var modelBase = GetBaseModel(context);
            var takeRateModel = new TakeRateViewModel(modelBase)
            {
                Document = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration = context.ConfigurationSettings,
                MarketReviewStatus = filter.MarketReviewStatus,
                Filter = filter.Filter
            };

            await HydrateOxoDocument(context, takeRateModel);
            await HydrateFdpVolumeHeader(context, takeRateModel);

            return takeRateModel;
        }
        private static async Task <IEnumerable <TakeRateSummary> > HydrateFdpVolumeHeaders(IDataContext context, TakeRateViewModel volumeModel)
        {
            var watch         = Stopwatch.StartNew();
            var volumeSummary = new List <TakeRateSummary>();

            foreach (var header in volumeModel.Document.TakeRateSummary)
            {
                await GetTakeRateDocumentHeader(context, header);
            }

            volumeModel.Document.TakeRateSummary = volumeSummary;

            watch.Stop();
            Log.Debug(watch.ElapsedMilliseconds);

            return(volumeSummary);
        }
        private static async Task<TakeRateViewModel> GetFullAndPartialViewModelForTakeRateDataPageExcludingData(IDataContext context, TakeRateFilter filter)
        {
            var modelBase = GetBaseModel(context);
            var takeRateModel = new TakeRateViewModel(modelBase)
            {
                Document = (TakeRateDocument)TakeRateDocument.FromFilter(filter),
                Configuration = context.ConfigurationSettings,
                MarketReviewStatus = filter.MarketReviewStatus,
                Filter = filter.Filter
            };

            await HydrateOxoDocument(context, takeRateModel);
            await HydrateFdpVolumeHeader(context, takeRateModel);
            //await HydrateFdpVolumeHeadersFromOxoDocument(context, takeRateModel);
            await HydrateVehicle(context, takeRateModel);
            await HydrateMarket(context, takeRateModel);
            await HydrateMarketGroup(context, takeRateModel);
            await HydrateMarketGroups(context, takeRateModel);
            await HydrateModelsByMarket(context, takeRateModel);
            await HydrateMarketReview(context, takeRateModel);
            await HydratePublish(context, takeRateModel);

            return takeRateModel;
        }