public void AddCategorisedData(WorksheetInfo ws, RowLabels rowLabels, IList<CoreDataSet> dataList,
     Dictionary<int, IArea> categoryIdToAreaMap)
 {
     IRange cells = ws.Worksheet.Cells;
     foreach (CoreDataSet data in dataList)
     {
         IArea area = categoryIdToAreaMap[data.CategoryId];
         AddDataRow(ws, rowLabels, area, data, cells, null, null);
     }
 }
        public override IList<CoreDataSet> AddMultipleAreaData(RowLabels rowLabels, Grouping grouping, 
            TimePeriod timePeriod, IndicatorMetadata metadata, Dictionary<string, Area> areaCodeToParentMap)
        {
            var dataList = GroupDataReader.GetCoreDataListForAllCategoryAreasOfCategoryAreaType(
                grouping, timePeriod, _categoryAreaTypeId, AreaCodes.England);

            ProfileDataWriter.AddCategorisedData(Worksheet, rowLabels, dataList, subnationalCategoryIdToCategoryAreaMap);

            return dataList;
        }
        public void AddData(WorksheetInfo ws, RowLabels rowLabels, IList<CoreDataSet> dataList,
            Dictionary<string, IArea> areaCodeToAreaMap, Dictionary<string, Area> areaCodeToParentMap)
        {
            IRange cells = ws.Worksheet.Cells;
            foreach (CoreDataSet data in dataList)
            {
                IArea area = areaCodeToAreaMap[data.AreaCode];
                Area parentArea;

                string parentAreaName = "",
                    parentAreaCode = "";

                if (areaCodeToParentMap.TryGetValue(data.AreaCode, out parentArea))
                {
                    parentAreaCode = areaCodeToParentMap[data.AreaCode].Code;
                    parentAreaName = areaCodeToParentMap[data.AreaCode].Name;
                }

                AddDataRow(ws, rowLabels, area, data, cells, parentAreaCode, parentAreaName);
            }
        }
 public void AddData(WorksheetInfo ws, RowLabels rowLabels, CoreDataSet coreData, IArea area)
 {
     if (coreData != null)
     {
         IRange cells = ws.Worksheet.Cells;
         AddDataRow(ws, rowLabels, area, coreData, cells, null, null);
     }
 }
        private static void AddDataRow(WorksheetInfo ws, RowLabels rowLabels, IArea area, CoreDataSet coreData,
            IRange cells, string parentAreaCode, string parentAreaName)
        {
            try
            {
                int rowIndex = ws.NextRow;
                int column = 0;

                // Indicator / time period
                cells[rowIndex, column++].Value = rowLabels.IndicatorName;
                cells[rowIndex, column++].Value = rowLabels.TimePeriod;

                // Parent area
                cells[rowIndex, column++].Value = parentAreaCode ?? string.Empty;
                cells[rowIndex, column++].Value = parentAreaName ?? string.Empty;

                // Area code
                var category = area as CategoryArea;
                if (category != null)
                {
                    AddValue(cells[rowIndex, column++], Convert.ToDouble(category.CategoryId));
                }
                else
                {
                    cells[rowIndex, column++].Value = area.Code;
                }

                // Area name
                cells[rowIndex, column++].Value = area.Name;

                // Value / CIs
                AddValue(cells[rowIndex, column++], coreData.Value);
                AddValue(cells[rowIndex, column++], coreData.LowerCI);
                AddValue(cells[rowIndex, column++], coreData.UpperCI);

                // Count
                if (coreData.IsCountValid)
                {
                    AddValue(cells[rowIndex, column], coreData.Count.Value);
                }
                column++;

                // Denominator
                AddValue(cells[rowIndex, column++], coreData.Denominator);

                // Sex / age
                cells[rowIndex, column++].Value = rowLabels.Sex;
                cells[rowIndex, column++].Value = rowLabels.Age;

                // Value note
                if (coreData.ValueNoteId > 0)
                {
                    cells[rowIndex, column].Value = rowLabels.ValueNoteLookUp[coreData.ValueNoteId];
                }
            }
            catch (Exception ex)
            {
                ExceptionLog.LogException(ex, "");
            }
        }
 public abstract IList<CoreDataSet> AddMultipleAreaData(RowLabels rowLabels, Grouping grouping, TimePeriod timePeriod,
     IndicatorMetadata metadata, Dictionary<string, Area> areaCodeToParentMap);
        private void WriteCoreData(GroupData data, ParentArea parentArea)
        {
            if (data.IsDataOk)
            {
                var valueNoteLookUp = ValueNotes.ToDictionary(x => x.Id, x => x.Text);

                // Child areas
                var childAreaCodes = data.Areas.Select(x => x.Code).ToArray();
                var childAreaCodeToAreaMap = data.Areas.ToDictionary(area => area.Code);

                // Worksheets
                WorksheetInfo childAreaWorksheet = _profileDataWriter.GetWorksheetInfo(_childAreaTypeSheetName);
                WorksheetInfo subnationalWorksheet = _profileDataWriter.GetWorksheetInfo(_subnationalAreaTypeSheetName);
                WorksheetInfo nationalWorksheet = _profileDataWriter.GetWorksheetInfo(NationalLabel);

                // Subnational areas
                ParentDataWriter parentDataWriter = ParentDataWriterFactory.New(_areasReader, _groupDataReader,
                    subnationalWorksheet, _profileDataWriter, _subnationalAreaType);
                var parentAreaDataProvider = new CoreDataSetProviderFactory().New(_parentArea);

                // No subnational sheet if direct parent area is country
                var isSubnationalSheet = subnationalWorksheet != null;

                var areMultipleSubnationalAreasRequired = AreMultipleSubnationalAreasRequired();

                foreach (var groupRoot in data.GroupRoots)
                {
                    // Check this data has not already been writen
                    var key = new GroupRootUniqueKey(groupRoot).Key + parentArea.AreaCode;
                    if (_groupRootKeys.Contains(key))
                    {
                        // This added has already been written
                        continue;
                    }
                    _groupRootKeys.Add(key);

                    var indicatorMetadata = data.GetIndicatorMetadataById(groupRoot.IndicatorId);

                    // Adding here means order in metadata sheet is same as in data sheet
                    AddMetadata(indicatorMetadata);

                    var timePeriodFormatter = new TimePeriodTextFormatter(indicatorMetadata);
                    var coreDataCollector = new CoreDataCollector();

                    var grouping = groupRoot.Grouping.FirstOrDefault();
                    if (grouping != null)
                    {
                        var sex = _labelReader.LookUpSexLabel(grouping.SexId);
                        var age = _labelReader.LookUpAgeLabel(grouping.AgeId);

                        var timePeriods = grouping.GetTimePeriodIterator(indicatorMetadata.YearType).TimePeriods;

                        // Write core data
                        foreach (TimePeriod timePeriod in timePeriods)
                        {
                            string timeString = timePeriodFormatter.Format(timePeriod);

                            var rowLabels = new RowLabels
                            {
                                Age = age,
                                Sex = sex,
                                TimePeriod = timeString,
                                IndicatorName = indicatorMetadata.Descriptive[IndicatorMetadataTextColumnNames.Name],
                                ValueNoteLookUp = valueNoteLookUp
                            };

                            // Write child area data
                            var coreDataList = _groupDataReader.GetCoreData(grouping, timePeriod, childAreaCodes);
                            _profileDataWriter.AddData(childAreaWorksheet, rowLabels, coreDataList, childAreaCodeToAreaMap, _areaCodeToParentMap);

                            // Subnational data
                            if (isSubnationalSheet)
                            {
                                if (areMultipleSubnationalAreasRequired)
                                {
                                    // Multiple parent areas
                                    var dataList = parentDataWriter.AddMultipleAreaData(rowLabels, grouping, timePeriod,
                                        indicatorMetadata, _areaCodeToParentMap);
                                    coreDataCollector.AddDataList(dataList);
                                }
                                else
                                {
                                    // One parent area
                                    var coreData = parentAreaDataProvider.GetData(grouping, timePeriod, indicatorMetadata);
                                    _profileDataWriter.AddData(subnationalWorksheet, rowLabels, coreData, _parentArea);
                                    coreDataCollector.AddData(coreData);
                                }
                            }

                            // Write national data
                            var nationalData = new CoreDataSetProviderFactory().New(_nationalArea).GetData(grouping, timePeriod, indicatorMetadata);
                            _profileDataWriter.AddData(nationalWorksheet, rowLabels, nationalData, _nationalArea);
                        }

                        // Write trend data
                        var trendMarkerWriter = TrendMarkerWriterFactory.New(_profileDataWriter, groupRoot.PolarityId, timePeriods, _profile.HasTrendMarkers);

                        // Child area trend markers
                        trendMarkerWriter.WriteChildTrendMarkers(childAreaWorksheet, groupRoot.RecentTrends, childAreaCodes);

                        // Subnational trend markers
                        if (isSubnationalSheet)
                        {
                            if (areMultipleSubnationalAreasRequired)
                            {
                                trendMarkerWriter.WriteMultipleSubnationalTrendMarkers(subnationalWorksheet, grouping,
                                    indicatorMetadata, coreDataCollector, parentDataWriter.CategoryIdAndAreaCodes);
                            }
                            else
                            {
                                trendMarkerWriter.WriteSingleSubnationalTrendMarker(subnationalWorksheet, grouping,
                                    indicatorMetadata, coreDataCollector);
                            }
                        }

                        // National trend markers
                        trendMarkerWriter.WriteNationalTrendMarkers(nationalWorksheet, groupRoot.RecentTrends,
                            _nationalArea.Code);
                    }
                }
            }
        }