Refactor report generation to use async repository access
Replaces obsolete DataTable-based logic in ReportCreator with async repository queries for EnvelopeReport entities. Refactors ReportItem to use explicit header and detail fields, removing legacy Envelope references. Updates report designer bindings to match new ReportItem properties. Improves exception handling and overall type safety.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System.Data;
|
||||
using DevExpress.Xpo;
|
||||
using DigitalData.Core.Abstraction.Application.Repository;
|
||||
using EnvelopeGenerator.Domain.Constants;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
@@ -7,55 +7,40 @@ using EnvelopeGenerator.ServiceHost.Extensions;
|
||||
|
||||
namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
|
||||
|
||||
[Obsolete("Instead of ReportModel create and use EnvelopeReport mediator queries")]
|
||||
public class ReportCreator(ReportModel ReportModel, ILogger<ReportCreator> Logger, IRepository<EnvelopeReport> reportRepo)
|
||||
public class ReportCreator(ILogger<ReportCreator> Logger, IRepository<EnvelopeReport> reportRepo)
|
||||
{
|
||||
[Obsolete("Solve the spaghetti...")]
|
||||
private Envelope? _envelope;
|
||||
|
||||
[Obsolete("Instead of ReportModel create and use EnvelopeReport mediator queries and solve this spaghetti...")]
|
||||
public byte[] CreateReport(Envelope envelope)
|
||||
public async Task<byte[]> CreateReportAsync(Envelope envelope, CancellationToken cancel = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
Logger.LogDebug("Loading report data..");
|
||||
var table = ReportModel.List(envelope.Id);
|
||||
var items = GetReportSource(table);
|
||||
var reports = await reportRepo.Where(r => r.EnvelopeId == envelope.Id).ToListAsync(cancel);
|
||||
|
||||
_envelope = envelope;
|
||||
|
||||
if (items.Count == 0)
|
||||
if (reports.Count == 0)
|
||||
{
|
||||
throw new CreateReportException("No report data found!");
|
||||
}
|
||||
|
||||
Logger.LogDebug("Creating report with [{count}] items..", items.Count);
|
||||
var items = reports
|
||||
.Select(r => ToReportItem(r, envelope))
|
||||
.OrderByDescending(r => r.ItemDate)
|
||||
.ToList();
|
||||
|
||||
var buffer = DoCreateReport(items);
|
||||
Logger.LogDebug("Report created!");
|
||||
|
||||
return buffer;
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch (Exception ex) when (ex is not CreateReportException)
|
||||
{
|
||||
Logger.LogError(ex);
|
||||
throw new CreateReportException("Could not prepare report data!", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private List<ReportItem> GetReportSource(DataTable dataTable)
|
||||
{
|
||||
Logger.LogDebug("Preparing report data");
|
||||
return dataTable.Rows
|
||||
.Cast<DataRow>()
|
||||
.Select(ToReportItem)
|
||||
.OrderByDescending(r => r.ItemDate)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private byte[] DoCreateReport(List<ReportItem> reportItems)
|
||||
{
|
||||
var items = reportItems.Select(MergeEnvelope).ToList();
|
||||
var source = new ReportSource { Items = items };
|
||||
var source = new ReportSource { Items = reportItems };
|
||||
var report = new rptEnvelopeHistory { DataSource = source, DataMember = "Items" };
|
||||
|
||||
Logger.LogDebug("Creating report in memory..");
|
||||
@@ -69,35 +54,20 @@ public class ReportCreator(ReportModel ReportModel, ILogger<ReportCreator> Logge
|
||||
return stream.ToArray();
|
||||
}
|
||||
|
||||
[Obsolete("Solve this spaghetti...")]
|
||||
private ReportItem MergeEnvelope(ReportItem item)
|
||||
private static ReportItem ToReportItem(EnvelopeReport report, Envelope envelope)
|
||||
{
|
||||
if (item.Envelope is null)
|
||||
return new ReportItem
|
||||
{
|
||||
item.Envelope = _envelope;
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
private ReportItem ToReportItem(DataRow row)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new ReportItem
|
||||
{
|
||||
EnvelopeId = row.ItemEx("ENVELOPE_ID", 0),
|
||||
EnvelopeTitle = row.ItemEx("HEAD_TITLE", string.Empty),
|
||||
EnvelopeSubject = row.ItemEx("HEAD_SUBJECT", string.Empty),
|
||||
ItemDate = row.ItemEx("POS_WHEN", DateTime.MinValue),
|
||||
ItemStatus = (EnvelopeStatus)row.ItemEx("POS_STATUS", 0),
|
||||
ItemUserReference = row.ItemEx("POS_WHO", string.Empty)
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogError(ex);
|
||||
throw new CreateReportException("Could not read data from database!", ex);
|
||||
}
|
||||
CreatorFullName = envelope.User is not null
|
||||
? $"{envelope.User.Prename} {envelope.User.Name}".Trim()
|
||||
: string.Empty,
|
||||
CreatorEmailAddress = envelope.User?.Email ?? string.Empty,
|
||||
EnvelopeTitle = report.HeadTitle ?? string.Empty,
|
||||
EnvelopeUuid = report.HeadUuid ?? string.Empty,
|
||||
EnvelopeCertificationType = envelope.CertificationType is int certType ? ((CertificationType)certType).ToString() : "null",
|
||||
ItemStatus = (EnvelopeStatus)report.PosStatus,
|
||||
ItemUserReference = report.PosWho ?? string.Empty,
|
||||
ItemDate = report.PosWhen ?? DateTime.MinValue
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,19 @@
|
||||
using EnvelopeGenerator.Domain.Constants;
|
||||
using EnvelopeGenerator.Domain.Entities;
|
||||
|
||||
namespace EnvelopeGenerator.ServiceHost.Jobs.FinalizeDocument;
|
||||
|
||||
public class ReportItem
|
||||
{
|
||||
public Envelope? Envelope { get; set; }
|
||||
public int EnvelopeId { get; set; }
|
||||
// Header fields (from Envelope)
|
||||
public string CreatorFullName { get; set; } = string.Empty;
|
||||
public string CreatorEmailAddress { get; set; } = string.Empty;
|
||||
public string EnvelopeTitle { get; set; } = string.Empty;
|
||||
public string EnvelopeSubject { get; set; } = string.Empty;
|
||||
public string EnvelopeUuid { get; set; } = string.Empty;
|
||||
public string EnvelopeCertificationType { get; set; } = string.Empty;
|
||||
|
||||
// Detail fields (from EnvelopeReport)
|
||||
public EnvelopeStatus ItemStatus { get; set; }
|
||||
|
||||
public string ItemStatusTranslated => ItemStatus.ToString();
|
||||
|
||||
public string ItemUserReference { get; set; } = string.Empty;
|
||||
public DateTime ItemDate { get; set; }
|
||||
}
|
||||
|
||||
@@ -257,7 +257,7 @@ public partial class rptEnvelopeHistory : XtraReport
|
||||
this.XrTableCell17.BorderColor = Color.Empty;
|
||||
this.XrTableCell17.Borders = BorderSide.None;
|
||||
this.XrTableCell17.Dpi = 254F;
|
||||
this.XrTableCell17.ExpressionBindings.AddRange(new ExpressionBinding[] { new ExpressionBinding("BeforePrint", "Text", "[Envelope].[User].[FullName]") });
|
||||
this.XrTableCell17.ExpressionBindings.AddRange(new ExpressionBinding[] { new ExpressionBinding("BeforePrint", "Text", "[CreatorFullName]") });
|
||||
this.XrTableCell17.Font = new Font("Arial", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
|
||||
this.XrTableCell17.ForeColor = Color.Black;
|
||||
this.XrTableCell17.Multiline = true;
|
||||
@@ -296,7 +296,7 @@ public partial class rptEnvelopeHistory : XtraReport
|
||||
this.XrTableCell18.BorderColor = Color.Empty;
|
||||
this.XrTableCell18.Borders = BorderSide.None;
|
||||
this.XrTableCell18.Dpi = 254F;
|
||||
this.XrTableCell18.ExpressionBindings.AddRange(new ExpressionBinding[] { new ExpressionBinding("BeforePrint", "Text", "[Envelope].[Title]") });
|
||||
this.XrTableCell18.ExpressionBindings.AddRange(new ExpressionBinding[] { new ExpressionBinding("BeforePrint", "Text", "[EnvelopeTitle]") });
|
||||
this.XrTableCell18.Font = new Font("Arial", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
|
||||
this.XrTableCell18.ForeColor = Color.Black;
|
||||
this.XrTableCell18.Multiline = true;
|
||||
@@ -342,7 +342,7 @@ public partial class rptEnvelopeHistory : XtraReport
|
||||
this.XrTableCell20.BorderColor = Color.Empty;
|
||||
this.XrTableCell20.Borders = BorderSide.None;
|
||||
this.XrTableCell20.Dpi = 254F;
|
||||
this.XrTableCell20.ExpressionBindings.AddRange(new ExpressionBinding[] { new ExpressionBinding("BeforePrint", "Text", "[Envelope].[User].[EmailAddress]") });
|
||||
this.XrTableCell20.ExpressionBindings.AddRange(new ExpressionBinding[] { new ExpressionBinding("BeforePrint", "Text", "[CreatorEmailAddress]") });
|
||||
this.XrTableCell20.Font = new Font("Arial", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
|
||||
this.XrTableCell20.ForeColor = Color.Black;
|
||||
this.XrTableCell20.Multiline = true;
|
||||
@@ -381,7 +381,7 @@ public partial class rptEnvelopeHistory : XtraReport
|
||||
this.XrTableCell22.BorderColor = Color.Empty;
|
||||
this.XrTableCell22.Borders = BorderSide.None;
|
||||
this.XrTableCell22.Dpi = 254F;
|
||||
this.XrTableCell22.ExpressionBindings.AddRange(new ExpressionBinding[] { new ExpressionBinding("BeforePrint", "Text", "[Envelope].[Uuid]") });
|
||||
this.XrTableCell22.ExpressionBindings.AddRange(new ExpressionBinding[] { new ExpressionBinding("BeforePrint", "Text", "[EnvelopeUuid]") });
|
||||
this.XrTableCell22.Font = new Font("Arial", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
|
||||
this.XrTableCell22.ForeColor = Color.Black;
|
||||
this.XrTableCell22.Multiline = true;
|
||||
@@ -463,7 +463,7 @@ public partial class rptEnvelopeHistory : XtraReport
|
||||
this.XrTableCell26.BorderColor = Color.Empty;
|
||||
this.XrTableCell26.Borders = BorderSide.None;
|
||||
this.XrTableCell26.Dpi = 254F;
|
||||
this.XrTableCell26.ExpressionBindings.AddRange(new ExpressionBinding[] { new ExpressionBinding("BeforePrint", "Text", "[Envelope].[CertificationType]") });
|
||||
this.XrTableCell26.ExpressionBindings.AddRange(new ExpressionBinding[] { new ExpressionBinding("BeforePrint", "Text", "[EnvelopeCertificationType]") });
|
||||
this.XrTableCell26.Font = new Font("Arial", 8.25F, FontStyle.Regular, GraphicsUnit.Point, 0);
|
||||
this.XrTableCell26.ForeColor = Color.Black;
|
||||
this.XrTableCell26.Multiline = true;
|
||||
|
||||
Reference in New Issue
Block a user