Jobs/ZUGFeRD: Remove Firebird Database

This commit is contained in:
Jonathan Jenne 2023-07-25 15:26:41 +02:00
parent 05a590b169
commit 2288bc3fb9
3 changed files with 115 additions and 138 deletions

View File

@ -9,61 +9,60 @@ Public Class EmailFunctions
Private ReadOnly _logConfig As LogConfig Private ReadOnly _logConfig As LogConfig
Private ReadOnly _logger As Logger Private ReadOnly _logger As Logger
Private ReadOnly _mssql As MSSQLServer Private ReadOnly _mssql As MSSQLServer
Private ReadOnly _firebird As Firebird
Public Sub New(LogConfig As LogConfig, MSSQL As MSSQLServer, Firebird As Firebird) Public Sub New(LogConfig As LogConfig, MSSQL As MSSQLServer)
_logConfig = LogConfig _logConfig = LogConfig
_logger = _logConfig.GetLogger() _logger = _logConfig.GetLogger()
_mssql = MSSQL _mssql = MSSQL
_firebird = Firebird
End Sub End Sub
Public Sub AddToEmailQueueFB(MessageId As String, BodyText As String, EmailData As EmailData, NamePortal As String) 'Public Sub AddToEmailQueueFB(MessageId As String, BodyText As String, EmailData As EmailData, NamePortal As String)
If EmailData Is Nothing Then ' If EmailData Is Nothing Then
_logger.Warn("EmailData is empty. Email will not be sent!") ' _logger.Warn("EmailData is empty. Email will not be sent!")
Exit Sub ' Exit Sub
End If ' End If
Try ' Try
Dim oJobId = RandomValue(1, 10000) ' Dim oJobId = RandomValue(1, 10000)
Dim oReference = MessageId ' Dim oReference = MessageId
Dim oEmailTo = "" ' Dim oEmailTo = ""
Dim oSubject = EmailStrings.EMAIL_SUBJECT_REJECTED.Replace(EmailStrings.constNAME_ZUGFERD_PORTAL, NamePortal) ' Dim oSubject = EmailStrings.EMAIL_SUBJECT_REJECTED.Replace(EmailStrings.constNAME_ZUGFERD_PORTAL, NamePortal)
Dim oAccountId = 1 ' Dim oAccountId = 1
Dim oCreatedWho = "ZUGFeRD Service" ' Dim oCreatedWho = "ZUGFeRD Service"
Dim oFinalBodyText = String.Format(EmailStrings.EMAIL_WRAPPING_TEXT.Replace(EmailStrings.constNAME_ZUGFERD_PORTAL, NamePortal), BodyText) ' Dim oFinalBodyText = String.Format(EmailStrings.EMAIL_WRAPPING_TEXT.Replace(EmailStrings.constNAME_ZUGFERD_PORTAL, NamePortal), BodyText)
Dim oEmailAddress = EmailData.From ' Dim oEmailAddress = EmailData.From
Dim oAttachment = EmailData.Attachment ' Dim oAttachment = EmailData.Attachment
If IsNothing(oEmailAddress) OrElse String.IsNullOrWhiteSpace(oEmailAddress) Then ' If IsNothing(oEmailAddress) OrElse String.IsNullOrWhiteSpace(oEmailAddress) Then
_logger.Warn("Could not find email-address for MessageId {0}", MessageId) ' _logger.Warn("Could not find email-address for MessageId {0}", MessageId)
oEmailTo = String.Empty ' oEmailTo = String.Empty
Else ' Else
oEmailTo = oEmailAddress ' oEmailTo = oEmailAddress
End If ' End If
_logger.Debug("Generated Email:") ' _logger.Debug("Generated Email:")
_logger.Debug("To: {0}", oEmailTo) ' _logger.Debug("To: {0}", oEmailTo)
_logger.Debug("Subject: {0}", oSubject) ' _logger.Debug("Subject: {0}", oSubject)
_logger.Debug("Body {0}", oFinalBodyText) ' _logger.Debug("Body {0}", oFinalBodyText)
Dim osql = $"select * from TBEDM_EMAIL_QUEUE where REFERENCE1 = '{oReference} and EMAIL_TO = ''{oEmailTo}' and EMAIL_SUBJ = '{oSubject}'" ' Dim osql = $"select * from TBEDM_EMAIL_QUEUE where REFERENCE1 = '{oReference} and EMAIL_TO = ''{oEmailTo}' and EMAIL_SUBJ = '{oSubject}'"
Dim oDTResult As DataTable = _firebird.GetDatatable(osql) ' Dim oDTResult As DataTable = _firebird.GetDatatable(osql)
' If oDTResult.Rows.Count = 0 Then
' Dim oSQLInsert = $"INSERT INTO TBEDM_EMAIL_QUEUE "
' oSQLInsert &= "(JOB_ID, REFERENCE1, EMAIL_ACCOUNT_ID, EMAIL_TO, EMAIL_SUBJ, EMAIL_BODY, CREATEDWHO, EMAIL_ATTMT1) VALUES "
' oSQLInsert &= $"({oJobId}, '{oReference}', {oAccountId}, '{oEmailTo}', '{oSubject}', '{oFinalBodyText.Replace("'", "''")}', '{oCreatedWho}', '{oAttachment}')"
' _firebird.ExecuteNonQuery(oSQLInsert)
' _logger.Debug("Email Queue updated for MessageId {0}.", MessageId, oEmailTo)
' Else
' _logger.Debug("Email has already been sent!!")
' End If
' Catch ex As Exception
' _logger.Error(ex)
' End Try
'End Sub
If oDTResult.Rows.Count = 0 Then
Dim oSQLInsert = $"INSERT INTO TBEDM_EMAIL_QUEUE "
oSQLInsert &= "(JOB_ID, REFERENCE1, EMAIL_ACCOUNT_ID, EMAIL_TO, EMAIL_SUBJ, EMAIL_BODY, CREATEDWHO, EMAIL_ATTMT1) VALUES "
oSQLInsert &= $"({oJobId}, '{oReference}', {oAccountId}, '{oEmailTo}', '{oSubject}', '{oFinalBodyText.Replace("'", "''")}', '{oCreatedWho}', '{oAttachment}')"
_firebird.ExecuteNonQuery(oSQLInsert)
_logger.Debug("Email Queue updated for MessageId {0}.", MessageId, oEmailTo)
Else
_logger.Debug("Email has already been sent!!")
End If
Catch ex As Exception
_logger.Error(ex)
End Try
End Sub
Public Sub AddToEmailQueueMSSQL(MessageId As String, BodyText As String, pEmailData As EmailData, SourceProcedure As String, pEmailAccountId As Integer, NamePortal As String) Public Sub AddToEmailQueueMSSQL(MessageId As String, BodyText As String, pEmailData As EmailData, SourceProcedure As String, pEmailAccountId As Integer, NamePortal As String)
If pEmailData Is Nothing Then If pEmailData Is Nothing Then
_logger.Warn("EmailData is empty. Email will not be sent!") _logger.Warn("EmailData is empty. Email will not be sent!")
@ -107,13 +106,9 @@ Public Class EmailFunctions
Dim osql = $"Select MAX(GUID) FROM TBEMLP_HISTORY WHERE EMAIL_MSGID = '{MessageId}'" Dim osql = $"Select MAX(GUID) FROM TBEMLP_HISTORY WHERE EMAIL_MSGID = '{MessageId}'"
Dim oHistoryID = _mssql.GetScalarValue(osql) Dim oHistoryID = _mssql.GetScalarValue(osql)
'osql = $"select * from TBEMLP_EMAIL_OUT where REFERENCE_ID = {oHistoryID} and EMAIL_ADRESS = '{oEmailTo}' and EMAIL_SUBJ = '{oSubject}'"
'Dim oDTResult As DataTable = _mssql.GetDatatable(osql)
If IsNumeric(oHistoryID) Then If IsNumeric(oHistoryID) Then
Dim oInsert = $"INSERT INTO [dbo].[TBEMLP_EMAIL_OUT] ( Dim oInsert = $"INSERT INTO [dbo].[TBEMLP_EMAIL_OUT]
[REMINDER_TYPE_ID] ([REMINDER_TYPE_ID]
,[SENDING_PROFILE] ,[SENDING_PROFILE]
,[REFERENCE_ID] ,[REFERENCE_ID]
,[REFERENCE_STRING] ,[REFERENCE_STRING]
@ -138,11 +133,7 @@ Public Class EmailFunctions
,'{oAttachmentPathEscaped}')" ,'{oAttachmentPathEscaped}')"
_mssql.ExecuteNonQuery(oInsert) _mssql.ExecuteNonQuery(oInsert)
Else Else
'If oDTResult.Rows.Count = 0 Then
' _logger.Debug("Email has already been sent!!")
'Else
_logger.Warn("Could not get oHistoryID in AddToEmailQueueMSSQL!!") _logger.Warn("Could not get oHistoryID in AddToEmailQueueMSSQL!!")
' End If
End If End If
Catch ex As Exception Catch ex As Exception
_logger.Error(ex) _logger.Error(ex)
@ -150,9 +141,9 @@ Public Class EmailFunctions
End Sub End Sub
Public Function GetEmailDataForMessageId(MessageId As String) As EmailData Public Function GetEmailDataForMessageId(MessageId As String) As EmailData
Dim oSQL = $"SELECT EMAIL_FROM, EMAIL_SUBJECT, EMAIL_ATTMT1 FROM TBEDM_EMAIL_PROFILER_HISTORY WHERE EMAIL_MSGID = '{MessageId}'" Dim oSQL = $"SELECT EMAIL_FROM, EMAIL_SUBJECT, EMAIL_ATTMT1 FROM TBEMLP_HISTORY WHERE EMAIL_MSGID = '{MessageId}'"
Try Try
Dim oDatatable = _firebird.GetDatatable(oSQL) Dim oDatatable = _mssql.GetDatatable(oSQL)
Dim oRow As DataRow Dim oRow As DataRow
If oDatatable.Rows.Count = 0 Then If oDatatable.Rows.Count = 0 Then
@ -186,9 +177,8 @@ Public Class EmailFunctions
End Function End Function
Public Function GetOriginalEmailPath(OriginalEmailDirectory As String, MessageId As String) As String Public Function GetOriginalEmailPath(OriginalEmailDirectory As String, MessageId As String) As String
Dim oAttachmentDirectory = OriginalEmailDirectory
Dim oAttachmentFile = MessageId & ".eml" Dim oAttachmentFile = MessageId & ".eml"
Dim oAttachmentPath = Path.Combine(oAttachmentDirectory, oAttachmentFile) Dim oAttachmentPath = Path.Combine(OriginalEmailDirectory, oAttachmentFile)
If File.Exists(oAttachmentPath) Then If File.Exists(oAttachmentPath) Then
Return oAttachmentPath Return oAttachmentPath

View File

@ -3,6 +3,7 @@ Imports System.Data
Imports System.IO Imports System.IO
Imports System.Linq Imports System.Linq
Imports System.Security.Cryptography Imports System.Security.Cryptography
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Database Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Interfaces Imports DigitalData.Modules.Interfaces
Imports DigitalData.Modules.Interfaces.Exceptions Imports DigitalData.Modules.Interfaces.Exceptions
@ -32,7 +33,6 @@ Public Class ImportZUGFeRDFiles
Private ReadOnly _logger As Logger Private ReadOnly _logger As Logger
Private ReadOnly _logConfig As LogConfig Private ReadOnly _logConfig As LogConfig
Private ReadOnly _firebird As Firebird
Private ReadOnly _filesystem As Filesystem.File Private ReadOnly _filesystem As Filesystem.File
Private ReadOnly _mssql As MSSQLServer Private ReadOnly _mssql As MSSQLServer
Private ReadOnly _email As EmailFunctions Private ReadOnly _email As EmailFunctions
@ -44,17 +44,14 @@ Public Class ImportZUGFeRDFiles
Private Class DatabaseConnections Private Class DatabaseConnections
Public Property SQLServerConnection As SqlConnection Public Property SQLServerConnection As SqlConnection
Public Property SQLServerTransaction As SqlTransaction Public Property SQLServerTransaction As SqlTransaction
Public Property FirebirdConnection As FbConnection
Public Property FirebirdTransaction As FbTransaction
End Class End Class
Public Sub New(LogConfig As LogConfig, Firebird As Firebird, Optional MSSQL As MSSQLServer = Nothing) Public Sub New(LogConfig As LogConfig, Optional MSSQL As MSSQLServer = Nothing)
_logConfig = LogConfig _logConfig = LogConfig
_logger = LogConfig.GetLogger() _logger = LogConfig.GetLogger()
_firebird = Firebird
_filesystem = New Filesystem.File(_logConfig) _filesystem = New Filesystem.File(_logConfig)
_mssql = MSSQL _mssql = MSSQL
_email = New EmailFunctions(LogConfig, _mssql, _firebird) _email = New EmailFunctions(LogConfig, _mssql)
_logger.Debug("Registering GDPicture License") _logger.Debug("Registering GDPicture License")
If _mssql IsNot Nothing Then If _mssql IsNot Nothing Then
@ -67,6 +64,9 @@ Public Class ImportZUGFeRDFiles
End Sub End Sub
Private Function MoveAndRenameEmailToRejected(Args As WorkerArgs, MessageId As String) As EmailData Private Function MoveAndRenameEmailToRejected(Args As WorkerArgs, MessageId As String) As EmailData
_logger.Info("Moving Mail with MessageId [{0}] to Rejected folder", MessageId)
_logger.Debug("Fetching Email Data")
Dim oEmailData = _email.GetEmailDataForMessageId(MessageId) Dim oEmailData = _email.GetEmailDataForMessageId(MessageId)
Dim oSource = _email.GetOriginalEmailPath(Args.OriginalEmailDirectory, MessageId) Dim oSource = _email.GetOriginalEmailPath(Args.OriginalEmailDirectory, MessageId)
Dim oDateSubDirectoryName As String = Now.ToString("yyyy-MM-dd") Dim oDateSubDirectoryName As String = Now.ToString("yyyy-MM-dd")
@ -104,7 +104,7 @@ Public Class ImportZUGFeRDFiles
Try Try
_logger.Info("Moving email from {0} to {1}", oSource, oFinalFileName) _logger.Info("Moving email from {0} to {1}", oSource, oFinalFileName)
IO.File.Move(oSource, oFinalFileName) File.Move(oSource, oFinalFileName)
oEmailData.Attachment = oFinalFileName oEmailData.Attachment = oFinalFileName
Catch ex As Exception Catch ex As Exception
_logger.Warn("File {0} could not be moved! Original Filename will be used!", oSource) _logger.Warn("File {0} could not be moved! Original Filename will be used!", oSource)
@ -171,24 +171,15 @@ Public Class ImportZUGFeRDFiles
For Each oFileGroup In oGrouped For Each oFileGroup In oGrouped
' Start a new transaction for each file group. ' Start a new transaction for each file group.
' This way we can rollback database changes for the whole filegroup in case something goes wrong. ' This way we can rollback database changes for the whole filegroup in case something goes wrong.
Dim oFBConnection As FbConnection = _firebird.GetConnection()
Dim oFBTransaction As FbTransaction = oFBConnection.BeginTransaction()
Dim oSQLConnection As SqlConnection = _mssql.GetConnection() Dim oSQLConnection As SqlConnection = _mssql.GetConnection()
Dim oSQLTransaction As SqlTransaction = oSQLConnection?.BeginTransaction() Dim oSQLTransaction As SqlTransaction = oSQLConnection?.BeginTransaction()
Dim oConnections As New DatabaseConnections() With { Dim oConnections As New DatabaseConnections() With {
.SQLServerConnection = oSQLConnection, .SQLServerConnection = oSQLConnection,
.SQLServerTransaction = oSQLTransaction, .SQLServerTransaction = oSQLTransaction
.FirebirdConnection = oFBConnection,
.FirebirdTransaction = oFBTransaction
} }
If oSQLConnection Is Nothing Then
_logger.Warn("SQL Connection was not set. No INSERTs for MSSQL Server will be performed!")
oArgs.InsertIntoSQLServer = False
End If
' Count the amount of ZUGFeRD files ' Count the amount of ZUGFeRD files
Dim oZUGFeRDCount As Integer = 0 Dim oZUGFeRDCount As Integer = 0
@ -298,7 +289,7 @@ Public Class ImportZUGFeRDFiles
End If End If
' Check the Checksum and rejection status ' Check the Checksum and rejection status
oMD5CheckSum = GenerateAndCheck_MD5Sum(oFile.FullName, oArgs.IgnoreRejectionStatus) oMD5CheckSum = GenerateAndCheck_MD5Sum(oFile.FullName, oMessageId, oArgs.IgnoreRejectionStatus)
' Check the document against the configured property map and return: ' Check the document against the configured property map and return:
' - a List of valid properties ' - a List of valid properties
@ -360,7 +351,7 @@ Public Class ImportZUGFeRDFiles
'If no errors occurred... 'If no errors occurred...
'Log the History 'Log the History
If oMD5CheckSum <> String.Empty Then If oMD5CheckSum <> String.Empty Then
Create_HistoryEntry(oMessageId, oMD5CheckSum, "SUCCESS", oFBTransaction) Create_HistoryEntry(oMessageId, oMD5CheckSum, "SUCCESS", oSQLTransaction)
'Dim oInsertCommand = $"INSERT INTO TBEDM_ZUGFERD_HISTORY_IN (MESSAGE_ID, MD5HASH) VALUES ('{oMessageId}', '{oMD5CheckSum}')" 'Dim oInsertCommand = $"INSERT INTO TBEDM_ZUGFERD_HISTORY_IN (MESSAGE_ID, MD5HASH) VALUES ('{oMessageId}', '{oMD5CheckSum}')"
'_firebird.ExecuteNonQueryWithConnection(oInsertCommand, oFBConnection, Firebird.TransactionMode.ExternalTransaction, oFBTransaction) '_firebird.ExecuteNonQueryWithConnection(oInsertCommand, oFBConnection, Firebird.TransactionMode.ExternalTransaction, oFBTransaction)
@ -373,7 +364,7 @@ Public Class ImportZUGFeRDFiles
' HISTORY_ID = 0 ' HISTORY_ID = 0
'End Try 'End Try
Else Else
Create_HistoryEntry(oMessageId, String.Empty, "SUCCESS (with empty MD5Hash)", oFBTransaction) Create_HistoryEntry(oMessageId, String.Empty, "SUCCESS (with empty MD5Hash)", oSQLTransaction)
End If End If
oIsSuccess = True oIsSuccess = True
@ -384,7 +375,7 @@ Public Class ImportZUGFeRDFiles
Dim oErrors = ex.ValidationErrors Dim oErrors = ex.ValidationErrors
Dim oMessage = "REJECTED - ZUGFeRD yes but formal validation failed!" Dim oMessage = "REJECTED - ZUGFeRD yes but formal validation failed!"
Update_HistoryEntry(oMessageId, oMD5CheckSum, oMessage, oFBTransaction) Update_HistoryEntry(oMessageId, oMD5CheckSum, oMessage, oSQLTransaction)
Dim oErrorList As String = "" Dim oErrorList As String = ""
For Each oError In oErrors For Each oError In oErrors
@ -400,8 +391,10 @@ Public Class ImportZUGFeRDFiles
Catch ex As MD5HashException Catch ex As MD5HashException
_logger.Error(ex) _logger.Error(ex)
' When MD5HashException is thrown, we don't have a MD5Hash yet.
' That 's why we set it to String.Empty here.
Dim oMessage = "REJECTED - Already processed (MD5Hash)" Dim oMessage = "REJECTED - Already processed (MD5Hash)"
Update_HistoryEntry(oMessageId, oMD5CheckSum, oMessage, oFBTransaction) Update_HistoryEntry(oMessageId, String.Empty, oMessage, oSQLTransaction)
Dim oBody = EmailStrings.EMAIL_MD5_ERROR Dim oBody = EmailStrings.EMAIL_MD5_ERROR
Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId)
@ -413,7 +406,7 @@ Public Class ImportZUGFeRDFiles
' When UnsupportedFerdException is thrown, we don't have a MD5Hash yet. ' When UnsupportedFerdException is thrown, we don't have a MD5Hash yet.
' That 's why we set it to String.Empty here. ' That 's why we set it to String.Empty here.
Create_HistoryEntry(oMessageId, String.Empty, "REJECTED - ZUGFeRD yes but unsupported format", oFBTransaction) Create_HistoryEntry(oMessageId, String.Empty, "REJECTED - ZUGFeRD yes but unsupported format", oSQLTransaction)
Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId)
Dim oBody As String = String.Format(EmailStrings.EMAIL_UNSUPPORTED_DOCUMENT, oEmailData.Subject, ex.XmlFile) Dim oBody As String = String.Format(EmailStrings.EMAIL_UNSUPPORTED_DOCUMENT, oEmailData.Subject, ex.XmlFile)
@ -426,7 +419,7 @@ Public Class ImportZUGFeRDFiles
' When InvalidFerdException is thrown, we don't have a MD5Hash yet. ' When InvalidFerdException is thrown, we don't have a MD5Hash yet.
' That 's why we set it to String.Empty here. ' That 's why we set it to String.Empty here.
Create_HistoryEntry(oMessageId, String.Empty, "REJECTED - ZUGFeRD yes but incorrect format", oFBTransaction) Create_HistoryEntry(oMessageId, String.Empty, "REJECTED - ZUGFeRD yes but incorrect format", oSQLTransaction)
Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId)
Dim oBody = String.Format(EmailStrings.EMAIL_INVALID_DOCUMENT, oEmailData.Subject) Dim oBody = String.Format(EmailStrings.EMAIL_INVALID_DOCUMENT, oEmailData.Subject)
@ -437,7 +430,7 @@ Public Class ImportZUGFeRDFiles
Catch ex As TooMuchFerdsException Catch ex As TooMuchFerdsException
_logger.Error(ex) _logger.Error(ex)
Create_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - More than one ZUGFeRD-document in email", oFBTransaction) Create_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - More than one ZUGFeRD-document in email", oSQLTransaction)
Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId)
Dim oBody = String.Format(EmailStrings.EMAIL_TOO_MUCH_FERDS, oEmailData.Subject) Dim oBody = String.Format(EmailStrings.EMAIL_TOO_MUCH_FERDS, oEmailData.Subject)
@ -448,7 +441,7 @@ Public Class ImportZUGFeRDFiles
Catch ex As NoFerdsException Catch ex As NoFerdsException
_logger.Error(ex) _logger.Error(ex)
Create_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - no ZUGFeRD-Document in email", oFBTransaction) Create_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - no ZUGFeRD-Document in email", oSQLTransaction)
Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId)
Dim oBody = String.Format(EmailStrings.EMAIL_NO_FERDS, oEmailData.Subject) Dim oBody = String.Format(EmailStrings.EMAIL_NO_FERDS, oEmailData.Subject)
@ -469,7 +462,7 @@ Public Class ImportZUGFeRDFiles
oMessage &= $"- {prop}" oMessage &= $"- {prop}"
Next Next
Create_HistoryEntry(oMessageId, oMD5CheckSum, $"REJECTED - Missing Required Properties: [{oMessage}]", oFBTransaction) Create_HistoryEntry(oMessageId, oMD5CheckSum, $"REJECTED - Missing Required Properties: [{oMessage}]", oSQLTransaction)
Dim oBody = CreateBodyForMissingProperties(ex.File.Name, oMissingProperties) Dim oBody = CreateBodyForMissingProperties(ex.File.Name, oMissingProperties)
Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId)
@ -479,7 +472,7 @@ Public Class ImportZUGFeRDFiles
Catch ex As FileSizeLimitReachedException Catch ex As FileSizeLimitReachedException
_logger.Error(ex) _logger.Error(ex)
Create_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - File size limit reached", oFBTransaction) Create_HistoryEntry(oMessageId, oMD5CheckSum, "REJECTED - File size limit reached", oSQLTransaction)
Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId) Dim oEmailData = MoveAndRenameEmailToRejected(oArgs, oMessageId)
@ -506,10 +499,7 @@ Public Class ImportZUGFeRDFiles
} }
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "OutOfMemoryException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "OutOfMemoryException", _EmailOutAccountId, oArgs.NamePortal)
' Rollback Firebird ' Rollback Transaction
oFBTransaction.Rollback()
' Rollback MSSQL
oSQLTransaction.Rollback() oSQLTransaction.Rollback()
oMoveDirectory = DIRECTORY_DONT_MOVE oMoveDirectory = DIRECTORY_DONT_MOVE
@ -528,10 +518,7 @@ Public Class ImportZUGFeRDFiles
} }
_email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "UnhandledException", _EmailOutAccountId, oArgs.NamePortal) _email.AddToEmailQueueMSSQL(oMessageId, oBody, oEmailData, "UnhandledException", _EmailOutAccountId, oArgs.NamePortal)
' Rollback Firebird ' Rollback Transaction
oFBTransaction.Rollback()
' Rollback MSSQL
oSQLTransaction.Rollback() oSQLTransaction.Rollback()
oMoveDirectory = DIRECTORY_DONT_MOVE oMoveDirectory = DIRECTORY_DONT_MOVE
@ -567,11 +554,8 @@ Public Class ImportZUGFeRDFiles
' finally commit all changes To the Database ' finally commit all changes To the Database
' ================================================================== ' ==================================================================
If oIsSuccess Or oExpectedError Then If oIsSuccess Or oExpectedError Then
' Commit SQL Transaction ' Commit Transaction
oSQLTransaction.Commit() oSQLTransaction.Commit()
' Commit Firebird Transaction
oFBTransaction.Commit()
End If End If
Catch ex As Exception Catch ex As Exception
_logger.Error(ex) _logger.Error(ex)
@ -579,7 +563,6 @@ Public Class ImportZUGFeRDFiles
End Try End Try
Try Try
oFBConnection.Close()
oSQLConnection.Close() oSQLConnection.Close()
Catch ex As Exception Catch ex As Exception
_logger.Error(ex) _logger.Error(ex)
@ -601,22 +584,12 @@ Public Class ImportZUGFeRDFiles
Dim oDelSQL = $"DELETE FROM TBEDMI_ITEM_VALUE where REFERENCE_GUID = '{pMessageId}'" Dim oDelSQL = $"DELETE FROM TBEDMI_ITEM_VALUE where REFERENCE_GUID = '{pMessageId}'"
Dim oStep As String Dim oStep As String
oStep = "Firebird TBEDMI_ITEM_VALUE Delete messageID Items" oStep = "TBEDMI_ITEM_VALUE Delete MessageID Items"
Try Try
_firebird.ExecuteNonQueryWithConnection(oDelSQL, pConnections.FirebirdConnection, Firebird.TransactionMode.ExternalTransaction, pConnections.FirebirdTransaction) _mssql.ExecuteNonQueryWithConnectionObject(oDelSQL, pConnections.SQLServerConnection, MSSQLServer.TransactionMode.ExternalTransaction, pConnections.SQLServerTransaction)
Catch ex As Exception Catch ex As Exception
_logger.Error(ex)
_logger.Warn("Step [{0}] with SQL [{1}] was not successful.", oStep, oDelSQL) _logger.Warn("Step [{0}] with SQL [{1}] was not successful.", oStep, oDelSQL)
End Try End Try
If pArgs.InsertIntoSQLServer = True Then
oStep = "MSSQL TBEDMI_ITEM_VALUE Delete messageID Items"
Try
_mssql.ExecuteNonQueryWithConnectionObject(oDelSQL, pConnections.SQLServerConnection, MSSQLServer.TransactionMode.ExternalTransaction, pConnections.SQLServerTransaction)
Catch ex As Exception
_logger.Warn("Step [{0}] with SQL [{1}] was not successful.", oStep, oDelSQL)
End Try
End If
End Sub End Sub
Private Sub InsertPropertyValue(pMessageId As String, pArgs As WorkerArgs, pConnections As DatabaseConnections, pProperty As PropertyValues.ValidProperty) Private Sub InsertPropertyValue(pMessageId As String, pArgs As WorkerArgs, pConnections As DatabaseConnections, pProperty As PropertyValues.ValidProperty)
@ -632,14 +605,11 @@ Public Class ImportZUGFeRDFiles
('{pMessageId}', '{pProperty.Description}', '{pProperty.Value.Replace("'", "''")}', {oGroupCounterValue},'{pProperty.TableColumn}','{pProperty.IsRequired}')" ('{pMessageId}', '{pProperty.Description}', '{pProperty.Value.Replace("'", "''")}', {oGroupCounterValue},'{pProperty.TableColumn}','{pProperty.IsRequired}')"
_logger.Debug("Mapping Property [{0}] with value [{1}], Will be inserted into table [{2}]", pProperty.TableColumn, pProperty.Value.Replace("'", "''"), pProperty.TableName) _logger.Debug("Mapping Property [{0}] with value [{1}], Will be inserted into table [{2}]", pProperty.TableColumn, pProperty.Value.Replace("'", "''"), pProperty.TableName)
' Insert into SQL Server ' Insert into SQL Server
If pArgs.InsertIntoSQLServer = True Then
Dim oResult = _mssql.ExecuteNonQueryWithConnectionObject(oCommand, pConnections.SQLServerConnection, MSSQLServer.TransactionMode.ExternalTransaction, pConnections.SQLServerTransaction) Dim oResult = _mssql.ExecuteNonQueryWithConnectionObject(oCommand, pConnections.SQLServerConnection, MSSQLServer.TransactionMode.ExternalTransaction, pConnections.SQLServerTransaction)
If oResult = False Then If oResult = False Then
_logger.Warn($"SQL Command [{oCommand}] was not successful. Check the log.") _logger.Warn($"SQL Command [{oCommand}] was not successful. Check the log.")
End If
End If End If
' Insert into Firebird
_firebird.ExecuteNonQueryWithConnection(oCommand, pConnections.FirebirdConnection, Firebird.TransactionMode.ExternalTransaction, pConnections.FirebirdTransaction)
End Sub End Sub
Private Function DoGetPropertyMapFor(pWorkerArgs As WorkerArgs, pSpecification As String) As Dictionary(Of String, XmlItemProperty) Private Function DoGetPropertyMapFor(pWorkerArgs As WorkerArgs, pSpecification As String) As Dictionary(Of String, XmlItemProperty)
@ -779,17 +749,19 @@ Public Class ImportZUGFeRDFiles
End Try End Try
End Function End Function
Private Function Create_HistoryEntry(MessageId As String, MD5Checksum As String, Message As String, Transaction As FbTransaction) As Boolean Private Function Create_HistoryEntry(MessageId As String, MD5Checksum As String, Message As String, Transaction As SqlTransaction) As Boolean
Try Try
Dim oConnection = _firebird.GetConnection() _logger.Info("Creating History Entry for MessageId [{0}] with comment [{1}]", MessageId, Message)
Dim oSQL = $"INSERT INTO TBEDM_ZUGFERD_HISTORY_IN (COMMENT, MD5HASH, MESSAGE_ID) VALUES ('{Message}', '{MD5Checksum}', '{MessageId}')" Dim oSQL = $"INSERT INTO TBEDM_ZUGFERD_HISTORY_IN (COMMENT, MD5HASH, MESSAGE_ID) VALUES ('{Message}', '{MD5Checksum}', '{MessageId}')"
' 09.07.2021: This can't be in the transaction since the history Using oConnection = _mssql.GetConnection()
' Entry needs to be accessed by MoveAndRenameEmailToRejected shortly after ' 09.07.2021: This can't be in the transaction since the history
_firebird.ExecuteNonQueryWithConnection(oSQL, oConnection, Firebird.TransactionMode.WithTransaction) ' Entry needs to be accessed by MoveAndRenameEmailToRejected shortly after
_mssql.ExecuteNonQueryWithConnectionObject(oSQL, Transaction.Connection, MSSQLServer.TransactionMode.NoTransaction)
End Using
_logger.Debug("History Entry created!")
' Close the connection
oConnection.Close()
If Message.Contains("REJECTED") Then If Message.Contains("REJECTED") Then
oSQL = $"UPDATE TBEMLP_HISTORY SET STATUS = 'REJECTED', COMMENT = '{Message}', CUST_REJECTED = 1,CUST_REJECTED_WHEN = GETDATE() WHERE EMAIL_MSGID = '{MessageId}'" oSQL = $"UPDATE TBEMLP_HISTORY SET STATUS = 'REJECTED', COMMENT = '{Message}', CUST_REJECTED = 1,CUST_REJECTED_WHEN = GETDATE() WHERE EMAIL_MSGID = '{MessageId}'"
_mssql.ExecuteNonQuery(oSQL) _mssql.ExecuteNonQuery(oSQL)
@ -804,15 +776,19 @@ Public Class ImportZUGFeRDFiles
End Try End Try
End Function End Function
Private Function Update_HistoryEntry(MessageId As String, MD5Checksum As String, Message As String, Transaction As FbTransaction) As Boolean Private Function Update_HistoryEntry(MessageId As String, MD5Checksum As String, Message As String, Transaction As SqlTransaction) As Boolean
Try Try
Dim oConnection = _firebird.GetConnection() _logger.Info("Updating History Entry for MessageId [{0}] with comment [{1}]", MessageId, Message)
Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = '{Message}' WHERE MD5HASH = '{MD5Checksum}' AND MESSAGE_ID = '{MessageId}'" ' Only look for history entry by MessageId since the MD5 Hash might not be generated yet
Dim oSQL = $"UPDATE TBEDM_ZUGFERD_HISTORY_IN SET COMMENT = '{Message}' WHERE MESSAGE_ID = '{MessageId}'"
_firebird.ExecuteNonQueryWithConnection(oSQL, oConnection, Firebird.TransactionMode.WithTransaction) Using oConnection = _mssql.GetConnection()
' 09.07.2021: This can't be in the transaction since the history
' Entry needs to be accessed by MoveAndRenameEmailToRejected shortly after
_mssql.ExecuteNonQueryWithConnectionObject(oSQL, Transaction.Connection, MSSQLServer.TransactionMode.NoTransaction)
End Using
' Close the connection _logger.Debug("History Entry created!")
oConnection.Close()
Return True Return True
Catch ex As Exception Catch ex As Exception
@ -830,7 +806,7 @@ Public Class ImportZUGFeRDFiles
''' <param name="pIgnoreRejectionStatus">Should the check take into account the rejection status of the file?</param> ''' <param name="pIgnoreRejectionStatus">Should the check take into account the rejection status of the file?</param>
''' <returns>The MD5 Checksum of the file, or an empty string, if the Checksum could not be created</returns> ''' <returns>The MD5 Checksum of the file, or an empty string, if the Checksum could not be created</returns>
''' <exception cref="MD5HashException">Throws, when the file should be rejected, ie. if it already exists in the table</exception> ''' <exception cref="MD5HashException">Throws, when the file should be rejected, ie. if it already exists in the table</exception>
Private Function GenerateAndCheck_MD5Sum(pFilePath As String, pIgnoreRejectionStatus As Boolean) As String Private Function GenerateAndCheck_MD5Sum(pFilePath As String, pMessageId As String, pIgnoreRejectionStatus As Boolean) As String
Dim oMD5CheckSum = CreateMD5(pFilePath) Dim oMD5CheckSum = CreateMD5(pFilePath)
' Exit if MD5 could not be created ' Exit if MD5 could not be created
@ -840,8 +816,13 @@ Public Class ImportZUGFeRDFiles
End If End If
' Check if Checksum exists in History Table ' Check if Checksum exists in History Table
Dim oCheckCommand = $"SELECT * FROM TBEDM_ZUGFERD_HISTORY_IN WHERE GUID = (SELECT MAX(GUID) FROM TBEDM_ZUGFERD_HISTORY_IN WHERE UPPER(MD5HASH) = UPPER('{oMD5CheckSum}'))" 'Dim oCheckCommand = $"SELECT * FROM TBEDM_ZUGFERD_HISTORY_IN WHERE GUID = (SELECT MAX(GUID) FROM TBEDM_ZUGFERD_HISTORY_IN WHERE UPPER(MD5HASH) = UPPER('{oMD5CheckSum}'))"
Dim oTable As DataTable = _firebird.GetDatatable(oCheckCommand, Firebird.TransactionMode.NoTransaction) 'Dim oTable As DataTable = _firebird.GetDatatable(oCheckCommand, Firebird.TransactionMode.NoTransaction)
' Check if Checksum exists in History Table
' TODO: WHAT THE FUCK IS THIS
Dim oCheckCommand = $"SELECT * FROM TBEMLP_HISTORY WHERE GUID = (SELECT MAX(GUID) FROM TBEMLP_HISTORY WHERE UPPER(MD5HASH) = UPPER('{oMD5CheckSum}'))"
Dim oTable As DataTable = _mssql.GetDatatable(oCheckCommand, MSSQLServer.TransactionMode.NoTransaction)
' If History entries could not be fetched, just return the MD5 Checksum ' If History entries could not be fetched, just return the MD5 Checksum
If IsNothing(oTable) Then If IsNothing(oTable) Then
@ -865,7 +846,7 @@ Public Class ImportZUGFeRDFiles
' Try to read Rejected Status and History Id ' Try to read Rejected Status and History Id
Try Try
Dim oRow As DataRow = oTable.Rows.Item(0) Dim oRow As DataRow = oTable.Rows.Item(0)
oRejected = DirectCast(oRow.Item("REJECTED"), Boolean) oRejected = oRow.ItemEx("CUST_REJECTED", False)
oHistoryId = oRow.Item("GUID") oHistoryId = oRow.Item("GUID")
Catch ex As Exception Catch ex As Exception
@ -873,7 +854,9 @@ Public Class ImportZUGFeRDFiles
oRejected = False oRejected = False
End Try End Try
_logger.Info("File has already been processed...")
_logger.Info("File with MessageId [{0}] has already been processed.", pMessageId)
' If the file was already rejected, it is allowed to be processed again, ' If the file was already rejected, it is allowed to be processed again,
' even if the Checksum exists in the history entries (default case) ' even if the Checksum exists in the history entries (default case)
' Which means, if it was not rejected before, it will be rejected in any case! ' Which means, if it was not rejected before, it will be rejected in any case!
@ -881,10 +864,15 @@ Public Class ImportZUGFeRDFiles
' This logic can be overwritten by the IgnoreRejectionStatus parameter. ' This logic can be overwritten by the IgnoreRejectionStatus parameter.
' If it is set to true, the file will be rejected if the file exists in the history entries, ' If it is set to true, the file will be rejected if the file exists in the history entries,
' regardless of the rejected parameter. ' regardless of the rejected parameter.
If oRejected = True And pIgnoreRejectionStatus = True Then If oRejected = True And pIgnoreRejectionStatus = True Then
_logger.Info("ZuGFeRDFile already has been processed, but formerly obviously was rejected!") _logger.Info("ZuGFeRDFile already has been processed, but Rejection Status will be ignored!")
ElseIf oRejected = False Then
_logger.Info("ZuGFeRDFile already has been processed. Will be rejected.")
Throw New MD5HashException($"There is already an identical invoice! - HistoryID [{oHistoryId}] - Case 1")
Else Else
Throw New MD5HashException($"There is already an identical invoice! - HistoryID [{oHistoryId}]") _logger.Info("ZuGFeRDFile already has been processed. Will be rejected.")
Throw New MD5HashException($"There is already an identical invoice! - HistoryID [{oHistoryId}] - Case 2")
End If End If
Return oMD5CheckSum Return oMD5CheckSum

View File

@ -18,7 +18,6 @@ Public Class WorkerArgs
Public EmailOutProfileId As Integer = 0 Public EmailOutProfileId As Integer = 0
' Misc Flag Parameters ' Misc Flag Parameters
Public InsertIntoSQLServer As Boolean = False
Public ExceptionEmailAddress As String = Nothing Public ExceptionEmailAddress As String = Nothing
Public IgnoreRejectionStatus As Boolean = False Public IgnoreRejectionStatus As Boolean = False
Public MaxAttachmentSizeInMegaBytes As Integer = -1 Public MaxAttachmentSizeInMegaBytes As Integer = -1