Modules/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb
2018-12-14 16:18:12 +01:00

100 lines
3.6 KiB
VB.net

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)
Try
oDocument = _zugferd.ExtractZUGFeRDFile(oFile.FullName)
For Each mapping In args.PropertyMap
Dim propertyValue As String = PropertyValues.GetPropValue(oDocument, mapping.Value)
_logger.Info("Mapping Property {0} to value {1}. Will be inserted into column {2}", mapping.Value, propertyValue, mapping.Key)
' TODO: Check for missing values
If String.IsNullOrEmpty(propertyValue) Then
Throw New Exception($"Property {mapping.Value} not found or empty.")
End If
oValues.Add(mapping.Key, propertyValue)
Next
' TODO: Insert into scheise
Catch ex As Exception
oMoveDirectory = args.ErrorDirectory
_logger.Error(ex)
Finally
_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