public static int GetCompositeAreaTypeId(Area area)
 {
     int areaTypeId = area.IsCounty
         ? AreaTypeIds.CountyAndUnitaryAuthority
         : AreaTypeIds.DistrictAndUnitaryAuthority;
     return areaTypeId;
 }
        private void SetSingleAreaValue(IGroupDataReader groupDataReader, Area area, Grouping grouping, TimePeriod period)
        {
            IList<CoreDataSet> dataList = groupDataReader.GetCoreData(grouping, period, area.Code);
            if (dataList.Count == 1)
            {
                var data = dataList.First();
                if (data.IsValueValid)
                {
                    Value = data.Value;

                    if (area.IsGpPractice == false)
                    {
                        // Calculate average

                        var areasReader = ReaderFactory.GetAreasReader();

                        int count = area.IsCountry ?
                            areasReader.GetAreaCountForAreaType(grouping.AreaTypeId) :
                            areasReader.GetChildAreaCount(area.Code, grouping.AreaTypeId);

                        Value /= count;
                    }
                }
            }
            else if (dataList.Count > 1)
            {
                throw new FingertipsException(string.Format(
                    "More data than expected on determination of QOF list size: area:{0}", area.Code));
            }
        }
 public CcgCoreDataSetProvider(Area area, CcgPopulationProvider ccgPopulationProvider,
     CoreDataSetListProvider coreDataSetListProvider, IGroupDataReader groupDataReader)
     : base(area)
 {
     this.ccgPopulationProvider = ccgPopulationProvider;
     this.coreDataSetListProvider = coreDataSetListProvider;
     this.groupDataReader = groupDataReader;
 }
        public void TestWhenAreaIsCountry()
        {
            var reader = Reader();
            reader.Setup(x => x.GetCoreDataForAllAreasOfType(It.IsAny<Grouping>(), It.IsAny<TimePeriod>()))
                .Returns(new List<CoreDataSet>());

            var area = new Area
            {
                AreaTypeId = AreaTypeIds.Country
            };

            GetChildAreaData(reader, area);

            reader.VerifyAll();
        }
        public void TestWhenAreaIsNotCountry()
        {
            string areaCode = "a";

            var reader = Reader();
            reader.Setup(x => x.GetCoreDataListForChildrenOfArea(It.IsAny<Grouping>(), It.IsAny<TimePeriod>(), areaCode))
                .Returns(new List<CoreDataSet>());

            var area = new Area
                {
                    Code = areaCode,
                    AreaTypeId = AreaTypeIds.Sha
                };
            GetChildAreaData(reader, area);

            reader.VerifyAll();
        }
        public QofListSizeProvider(IGroupDataReader groupDataReader, Area area, int groupId, int dataPointOffset,
            YearType yearType)
        {
            Grouping grouping = groupDataReader.GetGroupingsByGroupIdAndIndicatorId(groupId, IndicatorId);
            var period = new DataPointOffsetCalculator(grouping, dataPointOffset, yearType).TimePeriod;

            if (area.IsCcg)
            {
                //Note: zeroes may occur and should be included
                Value = groupDataReader
                    .GetCoreDataListForChildrenOfArea(grouping, period, area.Code)
                    .Where(x => x.IsValueValid)
                    .Average(x => x.Value);
            }
            else
            {
                SetSingleAreaValue(groupDataReader, area, grouping, period);
            }
        }
 private static void AddArea(Area area, Dictionary<string, Area> map, string code)
 {
     if (area != null && map.ContainsKey(code) == false)
     {
         map.Add(code, area);
     }
 }
 private static void ValidateArea(string code, Area area)
 {
     if (area == null)
     {
         throw new FingertipsException("Area cannot be found: '" + code + "'");
     }
 }
        private void AssignKeyMessages(Area area)
        {
            KeyMessageData data = new HealthProfilesKeyMessageDataBuilder(area, coreDataSetProvider,
                benchmarkDataProvider, indicatorMetadataCollection, groupRootSelector).BuildData();

            IList<KeyMessageOverride> overrides = pholioReader.GetKeyMessageOverrides(
                ProfileIds.HealthProfiles, area.Code);
            var staticMessageProvider = new StaticMessageProvider(overrides);

            var message5 = new KeyMessageOverrideCleaner(staticMessageProvider.GetMessage(5))
                .CleanMessage;

            content.KeyMessages = new List<string>
            {
                staticMessageProvider.GetMessage(1) ??
                    new HealthProfilesKeyMessage1Builder().ProcessKeyMessage(data),
                staticMessageProvider.GetMessage(2) ??
                    new HealthProfilesKeyMessage2Builder().ProcessKeyMessage(data),
                staticMessageProvider.GetMessage(3) ??
                    new HealthProfilesKeyMessage3Builder().ProcessKeyMessage(data),
                staticMessageProvider.GetMessage(4) ??
                    new HealthProfilesKeyMessage4Builder().ProcessKeyMessage(data),
                message5
            };
        }
 private static CoreDataSetProvider Provider(int id)
 {
     Area area = new Area { AreaTypeId = id };
     var provider = new CoreDataSetProviderFactory().New(area);
     return provider;
 }
        private IList<IndicatorMetadata> AddPopulationData(Area area, IEnumerable<Area> ccgs)
        {
            Grouping grouping = groupDataReader.GetGroupingsByGroupIdAndIndicatorId(
                GroupIds.First(), // only ever 1 group Id for population data
                IndicatorIds.QuinaryPopulations);
            var metadataList = IndicatorMetadataRepository.Instance
                .GetIndicatorMetadataCollection(grouping)
                .IndicatorMetadata;

            IEnumerable<int> ageIds = QuinaryPopulationSorter.GetAgeIdsToOver95();
            IEnumerable<int> sexIds = new[] { SexIds.Male, SexIds.Female };

            var metadata = metadataList.First(); // Why First??
            IList<TimePeriod> timePeriods = grouping.GetTimePeriodIterator(metadata.YearType).TimePeriods;
            var periodLabels = GetPeriodLabels(metadata, timePeriods);
            AddPopulationTitles(sexIds, periodLabels);

            foreach (TimePeriod timePeriod in timePeriods)
            {
                foreach (int sexId in sexIds)
                {
                    grouping.SexId = sexId;

                    foreach (int ageId in ageIds)
                    {
                        grouping.AgeId = ageId;

                        // Add data
                        writer.AddPracticeIndicatorValues(GetPracticeDataMap(area, grouping, timePeriod));
                    }
                }
            }

            foreach (var ccg in ccgs)
            {
                List<QuinaryPopulation> populations = new List<QuinaryPopulation>();

                foreach (TimePeriod timePeriod in timePeriods)
                {
                    foreach (int sexId in sexIds)
                    {
                        // Get all populations
                        populations.Add(practiceReader.GetCcgQuinaryPopulation(grouping.IndicatorId, timePeriod, ccg.Code, sexId));
                    }
                }

                writer.AddCcgPopulationValues(populations);
            }

            writer.AddIndicatorMetadata(metadataList);
            return metadataList;
        }
        private CoreDataSetListProvider ValidDataListProvider(Grouping grouping, Area area, TimePeriod timePeriod)
        {
            var childPracticeDataList = new List<CoreDataSet>
            {
                new CoreDataSet {AreaCode = "a", Value = practiceValue},
                new CoreDataSet {AreaCode = "b", Value = practiceValue}
            };

            return DataListProvider(grouping, area, timePeriod, childPracticeDataList);
        }
 public SimpleCoreDataSetProvider(Area area, IGroupDataReader groupDataReader)
     : base(area)
 {
     this.groupDataReader = groupDataReader;
 }
        private void AddPracticeAddresses(Area area, IList<string> practiceCodes)
        {
            IList<AreaAddress> practiceAddresses;
            if (area.IsGpPractice)
            {
                practiceAddresses = new List<AreaAddress>();
                AddAddressToList(area.Code, practiceAddresses);
            }
            else if (area.IsCountry)
            {
                practiceAddresses = areasReader.GetAreaWithAddressByAreaTypeId(AreaTypeIds.GpPractice);
            }
            else
            {
                practiceAddresses = new List<AreaAddress>();
                foreach (var practiceCode in practiceCodes)
                {
                    AddAddressToList(practiceCode, practiceAddresses);
                }
            }

            Dictionary<string, AreaAddress> practiceCodeToAddressMap =
                practiceAddresses.ToDictionary(a => a.Code);
            writer.AddPracticeAddresses(practiceCodeToAddressMap);
        }
 private static CoreDataSetListProvider DataListProvider(Grouping grouping, Area area, TimePeriod timePeriod, List<CoreDataSet> childPracticeDataList)
 {
     var dataListProvider = new Mock<CoreDataSetListProvider>(MockBehavior.Strict);
     dataListProvider.Protected();
     dataListProvider.Setup(x => x
         .GetChildAreaData(grouping, area, timePeriod))
         .Returns(childPracticeDataList);
     return dataListProvider.Object;
 }
 public ChildAreasForPdfs(ParentAreaWithChildAreas parentAreaWithChildAreas, Area benchmark)
 {
     Parent = parentAreaWithChildAreas.Parent;
     Children = parentAreaWithChildAreas.Children;
     Benchmark = benchmark;
 }
 private void InitPracticeToParentAreaMaps(Area area, IList<string> practiceCodes)
 {
     if (area.IsCountry)
     {
         // Add all Areas
         practiceCodeToParentMap =
             areasReader.GetParentsFromChildAreaIdAndParentAreaTypeId(AreaTypeId, AreaTypeIds.GpPractice);
     }
     else if (area.IsGpPractice)
     {
         practiceCodeToParentMap = new Dictionary<string, Area>();
         var code = area.Code;
         IList<Area> parentAreas = areasReader.GetParentAreas(code);
         AddCcgParent(practiceCodeToParentMap, code, parentAreas);
     }
     else if (area.IsCounty || area.IsUa || area.IsCcg)
     {
         practiceCodeToParentMap = new Dictionary<string, Area>();
         foreach (var code in practiceCodes)
         {
             IList<Area> parentAreas = areasReader.GetParentAreas(code);
             if (area.IsCounty)
             {
                 AddCountyParent(practiceCodeToParentMap, code, parentAreas);
             }
             else if (area.IsUa)
             {
                 AddUaParent(practiceCodeToParentMap, code, parentAreas);
             }
             else
             {
                 AddCcgParent(practiceCodeToParentMap, code, parentAreas);
             }
         }
     }
 }
 private Dictionary<string, CoreDataSet> GetPracticeDataMap(Area area, Grouping grouping, TimePeriod timePeriod)
 {
     Dictionary<string, CoreDataSet> dataMap;
     if (area.IsCountry)
     {
         dataMap = practiceReader.GetPracticeCodeToBaseDataMap(grouping, timePeriod);
     }
     else if (area.IsGpPractice)
     {
         dataMap = new Dictionary<string, CoreDataSet>();
         CoreDataSet data = groupDataReader.GetCoreData(grouping, timePeriod, area.Code).FirstOrDefault();
         if (data != null && data.IsValueValid)
         {
             dataMap.Add(area.Code, data);
         }
     }
     else
     {
         dataMap = practiceReader.GetPracticeCodeToBaseDataMap(grouping, timePeriod, area.Code);
     }
     return dataMap;
 }
        private IList<string> GetPracticeCodes(Area area)
        {
            if (area.IsCountry)
            {
                return areasReader.GetAreaCodesForAreaType(AreaTypeIds.GpPractice);
            }

            if (area.IsGpPractice)
            {
                return new List<string> { AreaCode };
            }

            // CCG
            return areasReader.GetChildAreaCodes(AreaCode, AreaTypeIds.GpPractice);
        }
        private IList<IndicatorMetadata> AddTopicData(Area area, IEnumerable<Area> parentAreas)
        {
            IndicatorMetadataCollection allMetadata = new IndicatorMetadataCollection();

            foreach (var groupId in GroupIds)
            {
                IList<Grouping> groupings = groupDataReader.GetGroupingsByGroupId(groupId);

                IList<GroupRoot> roots = new GroupRootBuilder().BuildGroupRoots(groupings);

                IndicatorMetadataCollection metadataCollection =
                    IndicatorMetadataRepository.Instance.GetIndicatorMetadataCollection(groupings);

                allMetadata.AddIndicatorMetadata(metadataCollection.IndicatorMetadata);

                foreach (GroupRoot root in roots)
                {
                    Grouping grouping = root.FirstGrouping;

                    // Add indicator information
                    IndicatorMetadata metadata = metadataCollection.GetIndicatorMetadataById(grouping.IndicatorId);
                    IList<TimePeriod> periods = grouping.GetTimePeriodIterator(metadata.YearType).TimePeriods;
                    writer.AddPracticeIndicatorTitles(metadata, periods);
                    writer.AddCcgIndicatorTitles(metadata, periods);

                    foreach (TimePeriod timePeriod in periods)
                    {
                        // Add data
                        writer.AddPracticeIndicatorData(GetPracticeDataMap(area, grouping, timePeriod));
                        writer.AddAreaIndicatorData(GetParentAreaDataMap(grouping, timePeriod, parentAreas, metadata));
                    }
                }
            }
            return allMetadata.IndicatorMetadata;
        }
 private static void CheckSerialisation(string propertyName, Area area)
 {
     JsonTestHelper.AssertPropertyIsSerialised(area, propertyName);
     JsonTestHelper.AssertPropertyIsNotSerialised(new Area(), propertyName);
 }
 private Area Area()
 {
     Area area = new Area { Code = areaCode };
     return area;
 }
 public ShapeCoreDataSetProvider(Area area, PracticeDataAccess practiceDataAccess)
     : base(area)
 {
     this.practiceDataAccess = practiceDataAccess;
 }
        private void BuildCategoryAreaMap(CategoryAreaType categoryAreaType, ParentArea parentArea)
        {
            var categories = _areasReader.GetCategories(categoryAreaType.CategoryTypeId);
            var subnationalCategoryIdToCategoryAreaMap = categories
                .ToDictionary<Category, int, IArea>(
                    category => category.Id,
                    CategoryArea.New
                );

            foreach (
                var categorisedArea in
                    _areasReader.GetCategorisedAreasForAllCategories(AreaTypeIds.Country, parentArea.ChildAreaTypeId,
                        categoryAreaType.CategoryTypeId))
            {
                var area = new Area
                {
                    Code = categorisedArea.CategoryId.ToString(),
                    Name = subnationalCategoryIdToCategoryAreaMap[categorisedArea.CategoryId].Name
                };

                _areaCodeToParentMap.Add(categorisedArea.AreaCode, area);
            }
        }
 private static CoreDataSetListProvider EmptyDataListProvider(Grouping grouping, Area area, TimePeriod timePeriod)
 {
     return DataListProvider(grouping, area, timePeriod, new List<CoreDataSet>());
 }
        public void Build()
        {
            var metadataRepo = IndicatorMetadataRepository.Instance;

            const int indicatorId = IndicatorIds.QuinaryPopulations;

            Grouping grouping = groupDataReader.GetGroupingsByGroupIdAndIndicatorId(GroupId, indicatorId);

            area = areasReader.GetAreaFromCode(AreaCode);

            var metadata = metadataRepo.GetIndicatorMetadata(indicatorId);
            period = new DataPointOffsetCalculator(grouping, DataPointOffset, metadata.YearType).TimePeriod;

            // Get data for each sex
            int overallTotal = 0;
            foreach (var sexId in sexIds)
            {
                IEnumerable<double> vals;

                if (area.IsCcg)
                {
                    QuinaryPopulation population = practiceReader.GetCcgQuinaryPopulation(indicatorId, period, AreaCode, sexId);
                    vals = new QuinaryPopulationSorter(population.Values).SortedValues;
                }
                else
                {
                    IList<CoreDataSet> data = groupDataReader.GetCoreDataForAllAges(indicatorId, period, AreaCode, sexId);
                    vals = new QuinaryPopulationSorter(data).SortedValues;
                }

                // Add total
                int total = Convert.ToInt32(Math.Round(vals.Sum(), MidpointRounding.AwayFromZero));
                overallTotal += total;

                Values.Add(sexId, vals);
            }

            // Convert to %
            foreach (var sexId in sexIds)
            {
                Values[sexId] = Values[sexId].Select(x => Math.Round((x / overallTotal) * 100, 2, MidpointRounding.AwayFromZero));
            }

            // List size
            var val = new QofListSizeProvider(groupDataReader, area, GroupId, DataPointOffset, metadata.YearType).Value;
            if (val.HasValue)
            {
                metadata = metadataRepo.GetIndicatorMetadata(QofListSizeProvider.IndicatorId);
                ListSize = new ValueData { Value = val.Value };

                var formatter = NumericFormatterFactory.New(metadata, groupDataReader);
                formatter.Format(ListSize);
            }

            if (area.IsGpPractice && AreOnlyPopulationsRequired == false)
            {
                SetEthnicityText();

                SetDeprivationDecile();

                Shape = practiceReader.GetShape(AreaCode);

                AdHocValues = new PracticePerformanceIndicatorValues(groupDataReader, AreaCode, DataPointOffset).IndicatorToValue;
            }
        }
        private void IndexParentArea(Area area, IndexWriter writer, int childAreaTypeId)
        {
            Document doc = new Document();
            AddAnalysedField(doc, FieldNames.PlaceName, area.Name.ToLower());
            AddNameFormatted(area.Name, doc);
            AddCounty(string.Empty, doc);
            AddParentArea(area.Code, area.Name, doc, childAreaTypeId);

            // Null value for all other possible parent area types
            foreach (var areaTypeId in parentAreaTypeIds)
            {
                if (areaTypeId != childAreaTypeId)
                {
                    AddNullParentAreaCode(doc, areaTypeId);
                }
            }

            AddPlaceTypeWeighting(3/*After cities but before towns*/, doc);
            writer.AddDocument(doc);
        }
        private CoreDataSet GetFormattedValueData(TimePeriod period, IList<CoreDataSet> coreDataSetList, Grouping grouping,
            IEnumerable<string> childAreaCodes)
        {
            CoreDataSet benchmarkData = GetDataAtSpecificTimePeriod(coreDataSetList, period);

            var parentArea = new Area { Code = coreDataSetList.Select(x => x.AreaCode).FirstOrDefault() };
            if (benchmarkData == null && grouping != null)
            {
                //Get child area data only when necessary
                var childAreaData = new CoreDataSetListProvider(groupDataReader).GetChildAreaData(grouping, parentArea, period);
                var filteredChildAreaData = new CoreDataSetFilter(childAreaData).SelectWithAreaCode(childAreaCodes);
                benchmarkData = AverageCalculatorFactory.New(filteredChildAreaData, IndicatorMetadata).Average;
            }

            if (benchmarkData == null)
            {
                benchmarkData = CoreDataSet.GetNullObject(parentArea.Code);
            }


            dataProcessor.FormatAndTruncate(benchmarkData);
            return benchmarkData;
        }