jj
This commit is contained in:
@@ -47,6 +47,87 @@ Public Class ImportZUGFeRDFiles
|
||||
_zugferd = New ZUGFeRDInterface(_logConfig)
|
||||
End Sub
|
||||
|
||||
Private Function RandomValue(lowerBound As Integer, upperBound As Integer) As Integer
|
||||
Dim oRandomValue = CInt(Math.Floor((upperBound - lowerBound + 1) * Rnd())) + lowerBound
|
||||
Return oRandomValue
|
||||
End Function
|
||||
|
||||
Private Function GetEmailAddressForFileGUID(FileGuid As String) As String
|
||||
Dim oSQL = $"SELECT EMAIL_FROM FROM TBEDM_EMAIL_PROFILER_HISTORY WHERE EMAIL_MSGID = '{FileGuid}'"
|
||||
Try
|
||||
Dim emailAddress = _firebird.GetScalarValue(oSQL)
|
||||
|
||||
_logger.Debug("Got Email Address for FileId {0}: {1}", FileGuid, emailAddress)
|
||||
|
||||
Return emailAddress
|
||||
Catch ex As Exception
|
||||
_logger.Warn("Could not fetch Email Address for FileId {0}", FileGuid)
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function GetOriginalFileNameForFileGUID(FileGuid As String) As String
|
||||
Dim oSQL = $"SELECT EMAIL_ATTMT1 FROM TBEDM_EMAIL_PROFILER_HISTORY WHERE EMAIL_MSGID = '{FileGuid}'"
|
||||
Try
|
||||
Dim originalFilename = _firebird.GetScalarValue(oSQL)
|
||||
|
||||
_logger.Debug("Got Original Filename for FileId {0}: {1}", FileGuid, originalFilename)
|
||||
|
||||
Return originalFilename
|
||||
Catch ex As Exception
|
||||
_logger.Warn("Could not fetch Original Filename for FileId {0}", FileGuid)
|
||||
Return Nothing
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Sub AddToEmailQueue(FileGuid As String, OriginalFileName As String, MissingProperties As List(Of String))
|
||||
Try
|
||||
Dim oJobId = RandomValue(1, 10000)
|
||||
Dim oReference = FileGuid
|
||||
Dim oEmailTo = ""
|
||||
Dim oSubject = ""
|
||||
Dim oBody = ""
|
||||
Dim oAccountId = 1
|
||||
Dim oCreatedWho = "ZUGFeRD Service"
|
||||
Dim oEmailAddress = GetEmailAddressForFileGUID(FileGuid)
|
||||
Dim oOriginalFilename = GetOriginalFileNameForFileGUID(FileGuid)
|
||||
|
||||
oSubject = "File not ZUGFeRD-Compliant!"
|
||||
oBody = $"<p>The following file is not ZUGFeRD-compliant: {oOriginalFilename}</p>"
|
||||
|
||||
If IsNothing(oEmailAddress) OrElse String.IsNullOrWhiteSpace(oEmailAddress) Then
|
||||
oEmailTo = String.Empty
|
||||
Else
|
||||
oEmailTo = oEmailAddress
|
||||
End If
|
||||
|
||||
If MissingProperties.Count > 0 Then
|
||||
oBody &= $"{vbNewLine}{vbNewLine}"
|
||||
oBody &= $"The following Properties were marked as Required but were not found:"
|
||||
oBody &= $"{vbNewLine}{vbNewLine}"
|
||||
|
||||
For Each prop In MissingProperties
|
||||
oBody &= $"- {prop}"
|
||||
Next
|
||||
End If
|
||||
|
||||
_logger.Debug("Generated Email:")
|
||||
_logger.Debug("To: {0}", oEmailTo)
|
||||
_logger.Debug("Subject: {0}", oSubject)
|
||||
_logger.Debug("Body {0}", oBody)
|
||||
|
||||
Dim oSQLInsert = $"INSERT INTO TBEDM_EMAIL_QUEUE "
|
||||
oSQLInsert &= "(JOB_ID, REFERENCE1, EMAIL_ACCOUNT_ID, EMAIL_TO, EMAIL_SUBJ, EMAIL_BODY, CREATEDWHO) VALUES "
|
||||
oSQLInsert &= $"({oJobId}, '{oReference}', {oAccountId}, '{oEmailTo}', '{oSubject}', '{oBody}', '{oCreatedWho}')"
|
||||
|
||||
_firebird.ExecuteNonQuery(oSQLInsert)
|
||||
|
||||
_logger.Info("Email Queue updated for MessageId {0}.", FileGuid, oEmailTo)
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
Public Sub Start(Arguments As Object) Implements IJob.Start
|
||||
Dim args As WorkerArgs = Arguments
|
||||
|
||||
@@ -69,49 +150,54 @@ Public Class ImportZUGFeRDFiles
|
||||
|
||||
Dim oMoveDirectory As String = args.SuccessDirectory
|
||||
Dim oDocument As CrossIndustryDocumentType
|
||||
Dim oValues As New Dictionary(Of String, String)
|
||||
Dim oGuid As String = Path.GetFileNameWithoutExtension(oFile.FullName)
|
||||
|
||||
Dim oConnection = _firebird.GetConnection()
|
||||
Dim oTransaction = oConnection.BeginTransaction()
|
||||
Dim oMissingProperties As New List(Of String)
|
||||
|
||||
Try
|
||||
oDocument = _zugferd.ExtractZUGFeRDFile(oFile.FullName)
|
||||
|
||||
For Each mapping In args.PropertyMap
|
||||
Dim propertyValue As String = PropertyValues.GetPropValue(oDocument, mapping.Key)
|
||||
Dim propertyDescripton As String = mapping.Value.Description
|
||||
For Each Item As KeyValuePair(Of String, XmlItemProperty) In args.PropertyMap
|
||||
Dim propertyValue As String = PropertyValues.GetPropValue(oDocument, Item.Key)
|
||||
Dim propertyDescripton As String = Item.Value.Description
|
||||
|
||||
' TODO: Check for missing values
|
||||
If String.IsNullOrEmpty(propertyValue) Then
|
||||
_logger.Warn("Property {0} is empty or not found", propertyDescripton)
|
||||
If Item.Value.IsRequired Then
|
||||
_logger.Warn("Property {0} is empty but marked as required! Skipping.", propertyDescripton)
|
||||
oMissingProperties.Add(propertyDescripton)
|
||||
Continue For
|
||||
Else
|
||||
_logger.Debug("Property {0} is empty or not found. Skipping.", propertyDescripton)
|
||||
Continue For
|
||||
End If
|
||||
End If
|
||||
|
||||
If String.IsNullOrEmpty(propertyValue) And mapping.Value.IsRequired Then
|
||||
_logger.Error("Property {0} is empty but required!", propertyDescripton)
|
||||
Throw New ApplicationException($"Property {propertyDescripton} is empty but required!")
|
||||
End If
|
||||
|
||||
Console.WriteLine("{0} => {1}", propertyDescripton, propertyValue)
|
||||
oValues.Add(propertyDescripton, propertyValue)
|
||||
|
||||
Dim oTableName = mapping.Value.TableName
|
||||
Dim oTableName = Item.Value.TableName
|
||||
Dim oCommand = $"INSERT INTO {oTableName} (REFERENCE_GUID, ITEM_DESCRIPTION, ITEM_VALUE) VALUES ('{oGuid}', '{propertyDescripton}', '{propertyValue}')"
|
||||
|
||||
_logger.Info("Mapping Property {0} to value {1}. Will be inserted into table {2}", propertyDescripton, propertyValue, oTableName)
|
||||
_logger.Debug("Mapping Property {0} to value {1}. Will be inserted into table {2}", propertyDescripton, propertyValue, oTableName)
|
||||
|
||||
_firebird.ExecuteNonQueryWithConnection(oCommand, oConnection, Firebird.TransactionMode.ExternalTransaction, oTransaction)
|
||||
Next
|
||||
|
||||
If oMissingProperties.Count > 0 Then
|
||||
Throw New Exception($"Some properties were empty but marked as required required!")
|
||||
End If
|
||||
|
||||
oTransaction.Commit()
|
||||
Catch ex As Exception
|
||||
_logger.Warn("File {0} was not processed. Transaction rolled back.", oFile.Name)
|
||||
_logger.Error(ex)
|
||||
oTransaction.Rollback()
|
||||
oMoveDirectory = args.ErrorDirectory
|
||||
_logger.Error(ex, "File {0} was not processesd. Transaction rolled back.")
|
||||
|
||||
AddToEmailQueue(oGuid, oFile.Name, oMissingProperties)
|
||||
Finally
|
||||
oConnection.Close()
|
||||
_filesystem.MoveTo(oFile.FullName, oMoveDirectory)
|
||||
_logger.Info("Finished processing file {0}", oFile.Name)
|
||||
_logger.Info("File moved to {0}", oMoveDirectory)
|
||||
End Try
|
||||
Next
|
||||
Else
|
||||
@@ -121,8 +207,4 @@ Public Class ImportZUGFeRDFiles
|
||||
_logger.Info("Finished processing directory {0}", oPath)
|
||||
Next
|
||||
End Sub
|
||||
|
||||
|
||||
|
||||
|
||||
End Class
|
||||
|
||||
Reference in New Issue
Block a user