jj: oh my god /o\

This commit is contained in:
Jonathan Jenne
2018-12-14 16:18:12 +01:00
parent ee11d3cdc0
commit e30bc21cf2
22 changed files with 626 additions and 108 deletions

View File

@@ -0,0 +1,99 @@
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

View File

@@ -0,0 +1,68 @@
Imports System.Reflection
Imports System.Text.RegularExpressions
Public Class PropertyValues
Private Shared _indexPattern = "\((\d+)\)"
Private Shared _indexRegex As Regex = New Regex(_indexPattern)
Public Shared Function GetPropValue(Obj As Object, PropertyName As String)
Dim oNameParts As String() = PropertyName.Split("."c)
Dim oIndexReplaceRegex = "\(\d+\)"
If IsNothing(Obj) Then
Return Nothing
End If
If oNameParts.Length = 1 Then
Return Obj.GetType().GetProperty(PropertyName).GetValue(Obj, Nothing)
End If
For Each oPart As String In oNameParts
Dim oType As Type = Obj.GetType()
Dim oPartName = oPart
Dim oIndex As Integer = Nothing
Dim oHasIndex As Boolean = HasIndex(oPartName)
If oHasIndex Then
oPartName = StripIndex(oPart)
oIndex = GetIndex(oPart)
End If
Dim oInfo As PropertyInfo = oType.GetProperty(oPartName)
If IsNothing(oInfo) Then
Return Nothing
End If
Obj = oInfo.GetValue(Obj, Nothing)
If oHasIndex Then
Obj = Obj(0)
End If
Next
Return Obj
End Function
Private Shared Function GetIndex(Prop As String) As Integer
If Regex.IsMatch(Prop, _indexPattern) Then
Dim oMatch = _indexRegex.Match(Prop)
Dim oGroup = oMatch.Groups.Item(1)
Dim oValue = oGroup.Value
Return Integer.Parse(oValue)
End If
Return Nothing
End Function
Private Shared Function StripIndex(Prop As String) As String
Return Regex.Replace(Prop, _indexPattern, "")
End Function
Private Shared Function HasIndex(Prop As String) As Boolean
Return Regex.IsMatch(Prop, _indexPattern)
End Function
End Class