Imports System.IO Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Base Imports EnvelopeGenerator.Common.Jobs.FinalizeDocument.FinalizeDocumentExceptions Public Class ReportCreator Inherits BaseClass Private Envelope As Envelope Private ReadOnly ReportModel As ReportModel Private ReadOnly EnvelopeModel As EnvelopeModel Public Sub New(pLogConfig As LogConfig, pState As State) MyBase.New(pLogConfig) ReportModel = New ReportModel(pState) EnvelopeModel = New EnvelopeModel(pState) End Sub Public Function CreateReport(pEnvelope As Envelope) As Byte() Try Logger.Debug("Loading report data..") Dim oTable = ReportModel.List(pEnvelope.Id) Dim oItems = GetReportSource(oTable) Envelope = pEnvelope If oItems.Count = 0 Then Throw New CreateReportException("No report data found!") End If Logger.Debug("Creating report with [{0}] items..", oItems.Count) Dim oBuffer = DoCreateReport(oItems) Logger.Debug("Report created!") Return oBuffer Catch ex As Exception Logger.Error(ex) Throw New CreateReportException("Could not prepare report data!", ex) End Try End Function Private Function GetReportSource(pDataTable As DataTable) As List(Of ReportItem) Logger.Debug("Preparing report data") Return pDataTable.Rows. Cast(Of DataRow). Select(AddressOf ToReportItem). OrderByDescending(Function(r) r.ItemDate). ToList() End Function Private Function DoCreateReport(pReportItems As List(Of ReportItem)) As Byte() Dim oItems = pReportItems.Select(AddressOf MergeEnvelope).ToList() Dim oSource As New ReportSource With {.Items = oItems} Dim oReport As New rptEnvelopeHistory() With {.DataSource = oSource, .DataMember = "Items"} Logger.Debug("Creating report in memory..") oReport.CreateDocument() Logger.Debug("Exporting report to stream..") Using oStream As New MemoryStream() oReport.ExportToPdf(oStream) Logger.Debug("Writing report to buffer..") Return oStream.ToArray() End Using End Function Private Function MergeEnvelope(pItem As ReportItem) As ReportItem If pItem.Envelope Is Nothing Then pItem.Envelope = Envelope End If Return pItem End Function Private Function ToReportItem(pRow As DataRow) As ReportItem Try Return New ReportItem() With { .EnvelopeId = pRow.Item("ENVELOPE_ID"), .EnvelopeTitle = pRow.ItemEx("HEAD_TITLE", String.Empty), .EnvelopeSubject = pRow.ItemEx("HEAD_SUBJECT", String.Empty), .ItemDate = pRow.ItemEx(Of Date)("POS_WHEN", Nothing), .ItemStatus = pRow.ItemEx("POS_STATUS", 0), .ItemUserReference = pRow.ItemEx("POS_WHO", "") } Catch ex As Exception Logger.Error(ex) Throw New CreateReportException("Could not read data from database!", ex) End Try End Function End Class