diff --git a/BMA.EHR.Retirement.Service/Services/RetirementReportService.cs b/BMA.EHR.Retirement.Service/Services/RetirementReportService.cs index 82bf2b27..48e301be 100644 --- a/BMA.EHR.Retirement.Service/Services/RetirementReportService.cs +++ b/BMA.EHR.Retirement.Service/Services/RetirementReportService.cs @@ -126,6 +126,10 @@ namespace BMA.EHR.Retirement.Service.Services if (IsSingleParagraphFormat(rows)) return new SingleParagraphTableStrategy(); + // retire-1 format: 2 rows, 3 columns (root row + data row per profile) + if (IsTwoRowPerProfileFormat(rows)) + return new TwoRowPerProfileTableStrategy(); + // retire-3 format: 2+ rows (header + template) return new MultiRowTableStrategy(); } @@ -135,6 +139,11 @@ namespace BMA.EHR.Retirement.Service.Services rows[0].Elements().Count() == 1 && rows[0].Elements().First().Elements().Count() == 1; + private static bool IsTwoRowPerProfileFormat(List rows) => + rows.Count == 2 && + rows[0].Elements().Count() == 3 && + rows[1].Elements().Count() == 3; + #endregion #region PDF Conversion @@ -634,5 +643,56 @@ namespace BMA.EHR.Retirement.Service.Services } } + internal class TwoRowPerProfileTableStrategy : ITableStrategy + { + public void Process(Table table, List rows, System.Collections.IEnumerable profiles) + { + // retire-1 format: 2 rows per profile (root row + data row) + var rootRow = rows[0]; + var dataRow = rows[1]; + + // Remove template rows from table + rootRow.Remove(); + dataRow.Remove(); + + var profileList = profiles.Cast().ToList(); + + foreach (var profile in profileList) + { + var props = profile.GetType() + .GetProperties() + .Where(p => p.GetIndexParameters().Length == 0) + .ToList(); + + // Check root value - skip root row if empty or null + var rootValue = props.FirstOrDefault(p => p.Name.Equals("root", StringComparison.OrdinalIgnoreCase)) + ?.GetValue(profile)?.ToString() ?? string.Empty; + + // Clone root row and fill with placeholders (only if root has value) + if (!string.IsNullOrWhiteSpace(rootValue)) + { + var newRootRow = (TableRow)rootRow.CloneNode(true); + foreach (var prop in props) + { + var value = prop.GetValue(profile)?.ToString() ?? string.Empty; + var placeholder = $"{{{{{prop.Name}}}}}"; + TextReplacer.ReplaceInRow(newRootRow, placeholder, value); + } + table.AppendChild(newRootRow); + } + + // Clone data row and fill with placeholders (always show) + var newDataRow = (TableRow)dataRow.CloneNode(true); + foreach (var prop in props) + { + var value = prop.GetValue(profile)?.ToString() ?? string.Empty; + var placeholder = $"{{{{{prop.Name}}}}}"; + TextReplacer.ReplaceInRow(newDataRow, placeholder, value); + } + table.AppendChild(newDataRow); + } + } + } + #endregion }