From 9b17c1fbcb41dc7dd0b70a3aa81880ec94e68dd1 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Wed, 5 Jun 2019 12:23:21 +0200 Subject: [PATCH] improve logging of missing/incorrectly configured properties --- Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb | 14 ++++++-- Jobs/EDMI/ZUGFeRD/PropertyValues.vb | 43 +++++++++++++++++-------- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb b/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb index bcc54190..96da49a9 100644 --- a/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb +++ b/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb @@ -185,6 +185,7 @@ Public Class ImportZUGFeRDFiles Public Sub Start(Arguments As Object) Implements IJob.Start Dim args As WorkerArgs = Arguments + Dim oPropertyExtractor = New PropertyValues(_logConfig) _logger.Info("Starting Job {0}", [GetType].Name) @@ -308,7 +309,7 @@ Public Class ImportZUGFeRDFiles ' get properties as a nested object, see `oPropertyList` For Each oProperty As KeyValuePair(Of String, XmlItemProperty) In oGroup - Dim oPropertyValues As List(Of Object) = PropertyValues.GetPropValue(oDocument, oProperty.Key) + Dim oPropertyValues As List(Of Object) = oPropertyExtractor.GetPropValue(oDocument, oProperty.Key) oPropertyList.Add(oProperty.Value, oPropertyValues) @@ -328,7 +329,8 @@ Public Class ImportZUGFeRDFiles Dim oColumnIsEmpty = False For Each oRow In oPropertyList - Dim oValue As List(Of Object) = oRow.Value.Item(oRowIndex) + Dim oValue As List(Of Object) = oRow.Value + 'Dim oValue As List(Of Object) = oRow.Value.Item(oRowIndex) oColumnIsEmpty = oValue.Count = 0 Next @@ -416,7 +418,7 @@ Public Class ImportZUGFeRDFiles ' Iterate through default properties For Each Item As KeyValuePair(Of String, XmlItemProperty) In oDefaultProperties - Dim oPropertyValueList As List(Of Object) = PropertyValues.GetPropValue(oDocument, Item.Key) + Dim oPropertyValueList As List(Of Object) = oPropertyExtractor.GetPropValue(oDocument, Item.Key) Dim oPropertyDescription As String = Item.Value.Description Dim oPropertyValue As Object = Nothing @@ -428,6 +430,12 @@ Public Class ImportZUGFeRDFiles oPropertyValue = Nothing Case Else oPropertyValue = oPropertyValueList.First() + + ' This should hopefully show config errors + If TypeOf oPropertyValue Is List(Of Object) Then + _logger.Warn("Property with Description {0} may be configured incorrectly", oPropertyDescription) + oPropertyValue = Nothing + End If End Select End If diff --git a/Jobs/EDMI/ZUGFeRD/PropertyValues.vb b/Jobs/EDMI/ZUGFeRD/PropertyValues.vb index b90aec42..7baabe41 100644 --- a/Jobs/EDMI/ZUGFeRD/PropertyValues.vb +++ b/Jobs/EDMI/ZUGFeRD/PropertyValues.vb @@ -1,20 +1,36 @@ Imports System.Collections.Generic Imports System.Reflection Imports System.Text.RegularExpressions +Imports DigitalData.Modules.Logging Public Class PropertyValues - Private Shared _indexPattern = "\((\d+)\)" - Private Shared _indexRegex As New Regex(_indexPattern) + Private _indexPattern = "\((\d+)\)" + Private _indexRegex As New Regex(_indexPattern) + Private _Logger As Logger - Public Shared Function GetPropValue(Obj As Object, PropertyName As String) As List(Of Object) + Public Sub New(LogConfig As LogConfig) + _Logger = LogConfig.GetLogger() + End Sub + + Public Function GetPropValue(Obj As Object, PropertyName As String) As List(Of Object) Dim oNameParts As String() = PropertyName.Split("."c) If IsNothing(Obj) Then + _Logger.Debug("`Obj` is Nothing. Exiting.") Return New List(Of Object) End If + If oNameParts.Length = 1 Then - Return Obj.GetType().GetProperty(PropertyName).GetValue(Obj, Nothing) + Dim oPropInfo As PropertyInfo = Obj.GetType().GetProperty(PropertyName) + + If IsNothing(oPropInfo) Then + _Logger.Debug("Property {0} does not exist.", PropertyName) + Return New List(Of Object) + Else + Dim oPropValue = oPropInfo.GetValue(Obj, Nothing) + Return New List(Of Object) From {oPropValue} + End If End If For Each oPart As String In oNameParts @@ -30,11 +46,8 @@ Public Class PropertyValues Dim oInfo As PropertyInfo = oType.GetProperty(oPartName) - If IsNothing(oInfo) Then - Return New List(Of Object) - End If - - If IsNothing(oInfo.GetValue(Obj, Nothing)) Then + If IsNothing(oInfo) OrElse IsNothing(oInfo.GetValue(Obj, Nothing)) Then + _Logger.Debug("Property {0} does not exist.", oPartName) Return New List(Of Object) End If @@ -56,7 +69,11 @@ Public Class PropertyValues End If For Each oArrayItem In Obj - Dim oResult = GetPropValue(oArrayItem, oPathFragments(1)) + Dim oResult As List(Of Object) = GetPropValue(oArrayItem, oPathFragments(1)) + + 'If Not IsNothing(oResult) AndAlso oResult.Count > 0 Then + ' oResults.Add(oResult.Item(0)) + 'End If If Not IsNothing(oResult) Then oResults.Add(oResult) @@ -70,7 +87,7 @@ Public Class PropertyValues Return New List(Of Object) From {Obj} End Function - Private Shared Function GetIndex(Prop As String) As Integer + Private Function GetIndex(Prop As String) As Integer If Regex.IsMatch(Prop, _indexPattern) Then Dim oMatch = _indexRegex.Match(Prop) Dim oGroup = oMatch.Groups.Item(1) @@ -82,11 +99,11 @@ Public Class PropertyValues Return Nothing End Function - Private Shared Function StripIndex(Prop As String) As String + Private Function StripIndex(Prop As String) As String Return Regex.Replace(Prop, _indexPattern, "") End Function - Private Shared Function HasIndex(Prop As String) As Boolean + Private Function HasIndex(Prop As String) As Boolean Return Regex.IsMatch(Prop, _indexPattern) End Function End Class