public void TestFormat()
 {
     var builder = new TimePeriodTextFormatter(
         new IndicatorMetadata { YearTypeId = (int)YearTypeIds.Calendar});
     var periodString = builder.Format(new TimePeriod { Year = 2000, YearRange = 1 });
     Assert.AreEqual("2000", periodString);
 }
        private static void AddIndicatorTitle(int startCol, IList<TimePeriod> periods, IWorksheet ws, IndicatorMetadata metadata)
        {
            int col = startCol;
            int endColumn = col + (periods.Count * ColumnsPerIndicator) - 1;
            IRange range = ws.Cells[0, col, 0, endColumn];
            range.Merge();
            range.Value = metadata.Descriptive[IndicatorMetadataTextColumnNames.Name];

            // Time
            const int dateRowIndex = 1;
            const int labelRowIndex = 2;

            var timePeriodFormatter = new TimePeriodTextFormatter(metadata);

            foreach (TimePeriod timePeriod in periods)
            {
                var endColumnForPeriod = col + ColumnsPerIndicator - 1;
                // Time title
                range = ws.Cells[dateRowIndex, col, dateRowIndex, endColumnForPeriod];
                range.Merge();
                range.Value = timePeriodFormatter.Format(timePeriod);

                // Val, LCI, UCI, Count, Denominator
                ws.Cells[labelRowIndex, col++].Value = "Value";
                ws.Cells[labelRowIndex, col++].Value = "Lower CI";
                ws.Cells[labelRowIndex, col++].Value = "Upper CI";
                ws.Cells[labelRowIndex, col++].Value = "Count";
                ws.Cells[labelRowIndex, col++].Value = "Denom";
            }

            // Format
            range = ws.Cells[0, startCol, StartingDataRow - 1, col];
            FormatAsTitle(range);
        }
        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);
                    }
                }
            }
        }