Imports System.Collections.Generic Imports System.IO Imports System.Linq Imports DigitalData.Modules.Database Imports DigitalData.Modules.Interfaces Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Filesystem Public Class ImportZUGFeRDFiles Implements IJob Private _logger As Logger Private _logConfig As LogConfig Private _zugferd As ZUGFeRDInterface Private _firebird As Firebird Private _filesystem As Filesystem.File Public Class WorkerArgs Public WatchDirectories As List(Of String) Public SuccessDirectory As String Public ErrorDirectory As String Public PropertyMap As Dictionary(Of String, String) Public Sub New() WatchDirectories = New List(Of String) SuccessDirectory = Nothing ErrorDirectory = Nothing PropertyMap = New Dictionary(Of String, String) End Sub End Class Public Sub New(LogConfig As LogConfig, Firebird As Firebird) _logConfig = LogConfig _logger = LogConfig.GetLogger() _firebird = Firebird _filesystem = New Filesystem.File(_logConfig) _zugferd = New ZUGFeRDInterface(_logConfig) End Sub Public Sub Start(Arguments As Object) Implements IJob.Start Dim args As WorkerArgs = Arguments _logger.Info("Starting Job {0}", Me.GetType.Name) For Each oPath As String In args.WatchDirectories Dim oDirInfo As New DirectoryInfo(oPath) _logger.Info($"Start processing directory {oDirInfo.FullName}") If oDirInfo.Exists Then Dim oFiles As List(Of FileInfo) = oDirInfo.GetFiles().ToList() Dim oFileCount = oFiles.Count Dim oCurrentFileCount = 0 _logger.Info("Found {0} files", oFileCount) For Each oFile In oFiles oCurrentFileCount += 1 _logger.Info($"({oCurrentFileCount}/{oFileCount}) Start processing file {oFile.Name}") 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() Try oDocument = _zugferd.ExtractZUGFeRDFile(oFile.FullName) For Each mapping In args.PropertyMap Dim propertyValue As String = PropertyValues.GetPropValue(oDocument, mapping.Value) ' TODO: Check for missing values 'If String.IsNullOrEmpty(propertyValue) Then ' Throw New Exception($"Property {mapping.Value} not found or empty.") 'End If Console.WriteLine("{0} => {1}", mapping.Key, propertyValue) oValues.Add(mapping.Key, propertyValue) ' TODO: Insert into scheise Dim oTableColumnArray = mapping.Key.Split(".") Dim oTableName = oTableColumnArray.First() Dim oColumnName = oTableColumnArray.Last() _logger.Info("Mapping Property {0} to value {1}. Will be inserted into column {2} on table {3}", mapping.Value, propertyValue, oTableName, oColumnName) Dim oCommand = $"INSERT INTO {oTableName} (FILEID, ""NAME"", ""VALUE"") VALUES ('{oGuid}', '{oColumnName}', '{propertyValue}')" _firebird.ExecuteNonQueryWithConnection(oCommand, oConnection, Firebird.TransactionMode.ExternalTransaction, oTransaction) Next oTransaction.Commit() Catch ex As Exception oTransaction.Rollback() oMoveDirectory = args.ErrorDirectory _logger.Error(ex, "File {0} was not processesd. Transaction rolled back.") Finally oConnection.Close() '_filesystem.MoveTo(oFile.FullName, oMoveDirectory) _logger.Info("Finished processing file {0}", oFile.Name) End Try Next End If _logger.Info("Finished processing directory {0}", oPath) Next End Sub End Class