ZUGFeRDService: Rework MD5 Checksum checking to allow for configuration of rejected state
This commit is contained in:
parent
f90ea3133d
commit
5d61488d92
@ -240,32 +240,8 @@ Public Class ImportZUGFeRDFiles
|
||||
oEmbeddedAttachmentFiles.AddRange(oAttachments)
|
||||
End If
|
||||
|
||||
oMD5CheckSum = CreateMD5(oFile.FullName)
|
||||
If oMD5CheckSum <> String.Empty Then
|
||||
Dim oCheckCommand = $"SELECT * FROM TBEDM_ZUGFERD_HISTORY_IN WHERE GUID = (SELECT MAX(GUID) FROM TBEDM_ZUGFERD_HISTORY_IN WHERE UPPER(MD5HASH) = UPPER('{oMD5CheckSum}'))"
|
||||
Dim oMD5DT As DataTable = _firebird.GetDatatable(oCheckCommand, Firebird.TransactionMode.NoTransaction)
|
||||
If Not IsNothing(oMD5DT) Then
|
||||
If oMD5DT.Rows.Count = 1 Then
|
||||
Dim oRejected As Boolean
|
||||
Try
|
||||
oRejected = CBool(oMD5DT.Rows(0).Item("REJECTED"))
|
||||
Catch ex As Exception
|
||||
_logger.Warn("Error while converting REJECTED: " & ex.Message)
|
||||
oRejected = False
|
||||
End Try
|
||||
If oRejected = False Then
|
||||
Dim oHistoryId = oMD5DT.Rows(0).Item("GUID")
|
||||
Throw New MD5HashException($"There is already an identical invoice! - HistoryID [{oHistoryId}]")
|
||||
Else
|
||||
_logger.Info("ZuGFeRDFile already has been worked, but formerly obviously was rejected!")
|
||||
End If
|
||||
End If
|
||||
Else
|
||||
_logger.Warn("Be careful: oExistsDT is nothing!")
|
||||
End If
|
||||
Else
|
||||
_logger.Warn("Be careful: oMD5CheckSum is nothing!")
|
||||
End If
|
||||
' TODO MD5
|
||||
oMD5CheckSum = Check_MD5Sum(oFile.FullName)
|
||||
|
||||
' Check if there are more than one ZUGFeRD files
|
||||
If oZUGFeRDCount = 1 Then
|
||||
@ -691,4 +667,74 @@ Public Class ImportZUGFeRDFiles
|
||||
Return False
|
||||
End Try
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Generates the MD5 Checksum of a file and checks it against the histroy table TBEDM_ZUGFERD_HISTORY_IN
|
||||
''' </summary>
|
||||
''' <param name="pFilePath">The path of the file to be checked</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>
|
||||
''' <exception cref="MD5HashException">Throws, when the file should be rejected, ie. if it already exists in the table</exception>
|
||||
Private Function Check_MD5Sum(pFilePath As String, pIgnoreRejectionStatus As Boolean) As String
|
||||
Dim oMD5CheckSum = CreateMD5(pFilePath)
|
||||
|
||||
' Exit if MD5 could not be created
|
||||
If oMD5CheckSum = String.Empty Then
|
||||
_logger.Warn("MD5 Checksum is nothing for file [{0}]!", pFilePath)
|
||||
Return oMD5CheckSum
|
||||
End If
|
||||
|
||||
' 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 oTable As DataTable = _firebird.GetDatatable(oCheckCommand, Firebird.TransactionMode.NoTransaction)
|
||||
|
||||
' If History entries could not be fetched, just return the MD5 Checksum
|
||||
If IsNothing(oTable) Then
|
||||
_logger.Warn("Be careful: oExistsDT is nothing for file [{0}]!", pFilePath)
|
||||
Return oMD5CheckSum
|
||||
End If
|
||||
|
||||
' If Checksum does not exist in History entries, just return the MD5 Checksum
|
||||
If oTable.Rows.Count = 0 Then
|
||||
_logger.Debug("File [{0}] was not found in History!", pFilePath)
|
||||
Return oMD5CheckSum
|
||||
End If
|
||||
|
||||
' ====================================================
|
||||
' Checksum exists in History entries, reject!
|
||||
' ====================================================
|
||||
|
||||
Dim oRejected As Boolean
|
||||
Dim oHistoryId As Integer
|
||||
|
||||
' Try to read Rejected Status and History Id
|
||||
Try
|
||||
Dim oRow As DataRow = oTable.Rows.Item(0)
|
||||
oRejected = DirectCast(oRow.Item("REJECTED"), Boolean)
|
||||
oHistoryId = oRow.Item("GUID")
|
||||
|
||||
Catch ex As Exception
|
||||
_logger.Warn("Error while converting REJECTED: " & ex.Message)
|
||||
oRejected = False
|
||||
|
||||
End Try
|
||||
|
||||
' If the file was already rejected, it is allowed to be processed again,
|
||||
' 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!
|
||||
'
|
||||
' This logic can be overwritten by the ForceRejection parameter.
|
||||
' If it is set to true, the file will be rejected if the file exists in the history entries,
|
||||
' regardless of the rejected parameter.
|
||||
If oRejected = False Or pIgnoreRejectionStatus = True Then
|
||||
Throw New MD5HashException($"There is already an identical invoice! - HistoryID [{oHistoryId}]")
|
||||
|
||||
Else
|
||||
_logger.Info("File has already been processed")
|
||||
_logger.Info("ZuGFeRDFile already has been processed, but formerly obviously was rejected!")
|
||||
|
||||
End If
|
||||
|
||||
Return oMD5CheckSum
|
||||
End Function
|
||||
End Class
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user