Imports DigitalData.Modules.Database Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Base Imports GdPicture14 Imports Quartz Imports System.Security.Cryptography Imports System.IO Imports EnvelopeGenerator.Common.Jobs.FinalizeDocument.FinalizeDocumentExceptions Imports EnvelopeGenerator.Common.Jobs.FinalizeDocument Imports EnvelopeGenerator.Common.Constants Imports DevExpress.DataProcessing Imports System.Data.SqlClient Imports DevExpress.XtraRichEdit.Layout.Engine Namespace Jobs Public Class SendInvitationMailJob Implements IJob Private LogConfig As LogConfig Private Logger As Logger Private Database As MSSQLServer Private Config As DbConfig Private ConfigModel As ConfigModel Private EnvelopeModel As EnvelopeModel Private ReceiverModel As ReceiverModel Private ActionService As ActionService Private ReadOnly CompleteWaitTime As Integer = 1 Private myTempFiles As TempFiles Private Class EnvelopeData Public EnvelopeId As Integer Public EnvelopeUUID As String Public DocumentPath As String End Class Public Function Execute(pContext As IJobExecutionContext) As Task Implements IJob.Execute LogConfig = pContext.MergedJobDataMap.Item(Constants.LOGCONFIG) Logger = LogConfig.GetLogger() myTempFiles = New TempFiles(LogConfig) myTempFiles.Create() Dim JobId = pContext.JobDetail.Key Logger.Info("SendInvMail - Starting job {0}", JobId) Try Logger.Debug("SendInvMail - Loading Database..") Database = GetDatabase(pContext, LogConfig) Logger.Debug("SendInvMail - Loading Models & Services") Dim oState = GetState() InitializeModels(oState) Logger.Debug("SendInvMail - Loading Configuration..") Config = ConfigModel.LoadConfiguration() oState.DbConfig = Config InitializeServices(oState) Config.DocumentPath = Config.DocumentPath Logger.Debug("SendInvMail - ExportPath: [{0}]", Config.ExportPath) Dim oCompleteStatus As Integer = Constants.EnvelopeStatus.EnvelopeCompletelySigned Dim oSql = $"SELECT * FROM TBSIG_ENVELOPE where SOURCE = 'API' AND STATUS = 1003 order by guid" Dim oTable = Database.GetDatatable(oSql) Dim oEnvelopeIds As List(Of Integer) = oTable.Rows.Cast(Of DataRow). Select(Function(r) r.Item("GUID")). Cast(Of Integer). ToList() If oEnvelopeIds.Count > 0 Then Logger.Info("SendInvMail - Found [{0}] envelopes.", oEnvelopeIds.Count) End If Dim oTotal As Integer = oEnvelopeIds.Count Dim oCurrent As Integer = 1 For Each oId In oEnvelopeIds Logger.Info("SendInvMail - Gathering Info for Envelope [{0}] ({1}/{2})", oId, oCurrent, oTotal) Logger.Debug("SendInvMail - Loading Envelope..") Try Dim oEnvelope = EnvelopeModel.GetById(oId) If oEnvelope Is Nothing Then Logger.Warn("SendInvMail - Envelope could not be loaded for Id [{0}]!", oId) Throw New ArgumentNullException("EnvelopeData") End If Logger.Debug("SendInvMail - Loading Envelope Data..") Dim oEnvelopeData = GetEnvelopeData(oId) oEnvelope.Receivers = ReceiverModel.ListEnvelopeReceivers(oEnvelope.Id).ToList() Logger.Debug("SendInvMail - Created Reveivers!") If oEnvelopeData Is Nothing Then Logger.Warn("SendInvMail - EnvelopeData could not be loaded for Id [{0}]!", oId) Throw New ArgumentNullException("EnvelopeData") End If Logger.Info("SendInvMail - Sending InvitationMails for Envelope [{0}]", oId) If ActionService.SendEnvelope(oEnvelope) = False Then Throw New ArgumentNullException("EnvelopeData") End If Catch ex As Exception Logger.Warn(ex, $"Unhandled exception while working envelope [{oId}]") End Try oCurrent += 1 Logger.Info("SendInvMail - Envelope finalized!") Next Logger.Debug("SendInvMail - Completed job {0} successfully!", JobId) Catch ex As Exception Logger.Warn("SendInvMail job failed!") Logger.Error(ex) Finally Logger.Info("SendInvMail execution for [{0}] ended", JobId) End Try Return Task.FromResult(True) End Function Private Sub InitializeModels(pState As State) ConfigModel = New ConfigModel(pState) EnvelopeModel = New EnvelopeModel(pState) ReceiverModel = New ReceiverModel(pState) End Sub Private Sub InitializeServices(pState As State) ActionService = New ActionService(pState) End Sub Private Function GetDatabase(pContext As IJobExecutionContext, pLogConfig As LogConfig) As MSSQLServer Dim oConnectionString As String = pContext.MergedJobDataMap.Item(Constants.DATABASE) Dim Database = New MSSQLServer(pLogConfig, MSSQLServer.DecryptConnectionString(oConnectionString)) Return Database End Function Private Function GetEnvelopeData(pEnvelopeId As Integer) As EnvelopeData Dim oSql = $"SELECT T.GUID, T.ENVELOPE_UUID,T2.FILEPATH, T2.BYTE_DATA FROM [dbo].[TBSIG_ENVELOPE] T JOIN TBSIG_ENVELOPE_DOCUMENT T2 ON T.GUID = T2.ENVELOPE_ID WHERE T.GUID = {pEnvelopeId}" Dim oTable As DataTable = Database.GetDatatable(oSql) Dim oRow As DataRow = oTable.Rows.Cast(Of DataRow).SingleOrDefault() If oRow Is Nothing Then Return Nothing End If Dim oData As New EnvelopeData With { .EnvelopeId = pEnvelopeId, .DocumentPath = oRow.ItemEx("FILEPATH", ""), .EnvelopeUUID = oRow.ItemEx("ENVELOPE_UUID", "") } Logger.Debug("Document path: [{0}]", oData.DocumentPath) Return oData End Function Private Function GetState() As State Return New State With { .LogConfig = LogConfig, .Database = Database, .UserId = 0, .Config = Nothing, .DbConfig = Nothing } End Function End Class End Namespace