diff --git a/EnvelopeGenerator.Common/Jobs/FinalizeDocument/FinalizeDocumentJob.vb b/EnvelopeGenerator.Common/Jobs/FinalizeDocument/FinalizeDocumentJob.vb index 0f7ae28b..9c3abc31 100644 --- a/EnvelopeGenerator.Common/Jobs/FinalizeDocument/FinalizeDocumentJob.vb +++ b/EnvelopeGenerator.Common/Jobs/FinalizeDocument/FinalizeDocumentJob.vb @@ -144,76 +144,81 @@ Namespace Jobs For Each oId In oEnvelopeIds Logger.Info("Finalizing Envelope [{0}] ({1}/{2})", oId, oCurrent, oTotal) Logger.Debug("Loading Envelope..") - Dim oEnvelope = EnvelopeModel.GetById(oId) - If oEnvelope Is Nothing Then - Logger.Warn("Envelope could not be loaded for Id [{0}]!", oId) - Throw New ArgumentNullException("EnvelopeData") - End If - Logger.Debug("Loading Envelope Data..") - Dim oEnvelopeData = GetEnvelopeData(oId) - - If oEnvelopeData Is Nothing Then - Logger.Warn("EnvelopeData could not be loaded for Id [{0}]!", oId) - Throw New ArgumentNullException("EnvelopeData") - End If - If Config.DOCUMENT_PATH_MOVE_AFTSEND <> String.Empty Then - oEnvelopeData.DocumentPath.Replace(Config.DocumentPathOrigin, Config.DOCUMENT_PATH_MOVE_AFTSEND) - Logger.Debug("Replaced Path in oEnvelopeData.DocumentPath!") - End If - Logger.Debug("Burning Annotations to pdf ...") - Dim oBurnedDocument As Byte() = BurnAnnotationsToPdf(oEnvelopeData) - If oBurnedDocument Is Nothing Then - Logger.Warn("Document could not be finalized!") - Throw New ApplicationException("Document could not be finalized") - End If - - If ActionService.CreateReport(oEnvelope) = False Then - Logger.Warn("Document Report could not be created!") - Throw New ApplicationException("Document Report could not be created") - End If - - Logger.Debug("Creating report..") - Dim oReport As Byte() = ReportCreator.CreateReport(oEnvelope) - Logger.Debug("Report created!") - - Logger.Debug("Merging documents ...") - Dim oMergedDocument As Byte() = PDFMerger.MergeDocuments(oBurnedDocument, oReport) - Logger.Debug("Documents merged!") - - Dim oOutputDirectoryPath = Path.Combine(Config.ExportPath, ParentFolderUID) - If Not Directory.Exists(oOutputDirectoryPath) Then - Directory.CreateDirectory(oOutputDirectoryPath) - End If - Dim oOutputFilePath = Path.Combine(oOutputDirectoryPath, $"{oEnvelope.Uuid}.pdf") - Logger.Info("Writing finalized Pdf to disk..") - Logger.Info("Output path is [{0}]", oOutputFilePath) - Try - File.WriteAllBytes(oOutputFilePath, oMergedDocument) - Catch ex As Exception - Logger.Warn("Could not export final document to disk!") - Throw New ExportDocumentException("Could not export final document to disk!", ex) - End Try + Dim oEnvelope = EnvelopeModel.GetById(oId) + If oEnvelope Is Nothing Then + Logger.Warn("Envelope could not be loaded for Id [{0}]!", oId) + Throw New ArgumentNullException("EnvelopeData") + End If + Logger.Debug("Loading Envelope Data..") + Dim oEnvelopeData = GetEnvelopeData(oId) - If Config.NetUse_Finish = True Then - If Config.FINISHED_PATH_EX_DMZ <> String.Empty Then - If My.Settings.NetUse_PW <> String.Empty And My.Settings.NetUse_Usr <> String.Empty Then - Clean_DNZ_PAth(Config.FINISHED_PATH_EX_DMZ) + If oEnvelopeData Is Nothing Then + Logger.Warn("EnvelopeData could not be loaded for Id [{0}]!", oId) + Throw New ArgumentNullException("EnvelopeData") + End If + If Config.DOCUMENT_PATH_MOVE_AFTSEND <> String.Empty Then + oEnvelopeData.DocumentPath.Replace(Config.DocumentPathOrigin, Config.DOCUMENT_PATH_MOVE_AFTSEND) + Logger.Debug("Replaced Path in oEnvelopeData.DocumentPath!") + End If + Logger.Debug("Burning Annotations to pdf ...") + Dim oBurnedDocument As Byte() = BurnAnnotationsToPdf(oEnvelopeData) + If oBurnedDocument Is Nothing Then + Logger.Warn("Document could not be finalized!") + Throw New ApplicationException("Document could not be finalized") + End If + + If ActionService.CreateReport(oEnvelope) = False Then + Logger.Warn("Document Report could not be created!") + Throw New ApplicationException("Document Report could not be created") + End If + + Logger.Debug("Creating report..") + Dim oReport As Byte() = ReportCreator.CreateReport(oEnvelope) + Logger.Debug("Report created!") + + Logger.Debug("Merging documents ...") + Dim oMergedDocument As Byte() = PDFMerger.MergeDocuments(oBurnedDocument, oReport) + Logger.Debug("Documents merged!") + + Dim oOutputDirectoryPath = Path.Combine(Config.ExportPath, ParentFolderUID) + If Not Directory.Exists(oOutputDirectoryPath) Then + Directory.CreateDirectory(oOutputDirectoryPath) + End If + Dim oOutputFilePath = Path.Combine(oOutputDirectoryPath, $"{oEnvelope.Uuid}.pdf") + Logger.Info("Writing finalized Pdf to disk..") + Logger.Info("Output path is [{0}]", oOutputFilePath) + + Try + File.WriteAllBytes(oOutputFilePath, oMergedDocument) + Catch ex As Exception + Logger.Warn("Could not export final document to disk!") + Throw New ExportDocumentException("Could not export final document to disk!", ex) + End Try + + If Config.NetUse_Finish = True Then + If Config.FINISHED_PATH_EX_DMZ <> String.Empty Then + If My.Settings.NetUse_PW <> String.Empty And My.Settings.NetUse_Usr <> String.Empty Then + Clean_DNZ_PAth(Config.FINISHED_PATH_EX_DMZ) + End If End If End If - End If - Logger.Info("Writing EB-bytes to database...") - Update_File_DB(oOutputFilePath, oEnvelope.Id) + Logger.Info("Writing EB-bytes to database...") + Update_File_DB(oOutputFilePath, oEnvelope.Id) + + Logger.Info("Sending finalized report-mails..") + If SendFinalEmails(oEnvelope) = False Then ', oOutputFilePath + Throw New ApplicationException("Final emails could not be sent!") + End If + Logger.Debug("Setting envelope status..") + If ActionService.FinalizeEnvelope(oEnvelope) = False Then + Logger.Warn("Envelope could not be finalized!") + Throw New ApplicationException("Envelope could not be finalized") + End If + Catch ex As Exception + Logger.Warn($"Unhandled exception while working envelope [{oId}] - [{ex.Message}]") + End Try - Logger.Info("Sending finalized report-mails..") - If SendFinalEmails(oEnvelope) = False Then ', oOutputFilePath - Throw New ApplicationException("Final emails could not be sent!") - End If - Logger.Debug("Setting envelope status..") - If ActionService.FinalizeEnvelope(oEnvelope) = False Then - Logger.Warn("Envelope could not be finalized!") - Throw New ApplicationException("Envelope could not be finalized") - End If oCurrent += 1 Logger.Info("Envelope finalized!")