From 617315667c58552729fab3099fd2d7b6b43ca730 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Tue, 18 Jun 2019 13:55:52 +0200 Subject: [PATCH] flatten grouped results, add try..catch blocks at crititcal places --- Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb | 72 +++++++++++++++---------- Jobs/EDMI/ZUGFeRD/PropertyValues.vb | 33 ++++++++++++ 2 files changed, 77 insertions(+), 28 deletions(-) diff --git a/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb b/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb index cce1a012..cb7aabbd 100644 --- a/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb +++ b/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb @@ -301,7 +301,6 @@ Public Class ImportZUGFeRDFiles Function(Item) Item) _logger.Debug("Found {0} properties grouped in {1} group(s)", args.PropertyMap.Count - oDefaultProperties.Count, oGroupedProperties.Count) - ' Iterate through groups to get group scope and group items For Each oGroup In oGroupedProperties Dim oGroupScope As String = oGroup.Key @@ -312,8 +311,20 @@ 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) = oPropertyExtractor.GetPropValue(oDocument, oProperty.Key) + Dim oPropertyValues As List(Of Object) + + Try + oPropertyValues = oPropertyExtractor.GetPropValue(oDocument, oProperty.Key) + Catch ex As Exception + _logger.Warn("Unknown error occurred while fetching property {0} in group {1}:", oProperty.Value.Description, oGroupScope) + _logger.Error(ex) + oPropertyValues = New List(Of Object) + End Try + + ' Flatten result value + oPropertyValues = oPropertyExtractor.GetFinalPropValue(oPropertyValues) + ' Add to list oPropertyList.Add(oProperty.Value, oPropertyValues) ' check the first batch of values to determine the row count @@ -379,16 +390,6 @@ Public Class ImportZUGFeRDFiles _logger.Debug("Processing property {0}.", oPropertyDescription) - If TypeOf oPropertyValue Is List(Of Object) Then - Select Case oPropertyValue.Count - Case 0 - oPropertyValue = String.Empty - Case Else - Dim oList As List(Of Object) = DirectCast(oPropertyValue, List(Of Object)) - oPropertyValue = oList.Item(0) - End Select - End If - If IsNothing(oPropertyValue) OrElse String.IsNullOrEmpty(oPropertyValue) Then If oColumn.Key.IsRequired Then _logger.Warn("Property {0} is empty or not found but was required. Continuing with Empty String.", oPropertyDescription) @@ -423,26 +424,41 @@ Public Class ImportZUGFeRDFiles ' Iterate through default properties For Each Item As KeyValuePair(Of String, XmlItemProperty) In oDefaultProperties - Dim oPropertyValueList As List(Of Object) = oPropertyExtractor.GetPropValue(oDocument, Item.Key) + Dim oPropertyValueList As List(Of Object) Dim oPropertyDescription As String = Item.Value.Description Dim oPropertyValue As Object = Nothing - If IsNothing(oPropertyValueList) Then - oPropertyValue = Nothing - ElseIf TypeOf oPropertyValueList Is List(Of Object) Then - Select Case oPropertyValueList.Count - Case 0 - 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) + Try + oPropertyValueList = oPropertyExtractor.GetPropValue(oDocument, Item.Key) + Catch ex As Exception + _logger.Warn("Unknown error occurred while fetching property {0} in group {1}:", oPropertyDescription, Item.Value.GroupScope) + _logger.Error(ex) + oPropertyValueList = New List(Of Object) + End Try + + Try + If IsNothing(oPropertyValueList) Then + oPropertyValue = Nothing + ElseIf TypeOf oPropertyValueList Is List(Of Object) Then + Select Case oPropertyValueList.Count + Case 0 oPropertyValue = Nothing - End If - End Select - End If + Case Else + Dim oList As List(Of Object) = DirectCast(oPropertyValueList, List(Of Object)) + oPropertyValue = oList.Item(0) + + ' 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 + Catch ex As Exception + _logger.Warn("Unknown error occurred while processing property {0}:", oPropertyDescription) + _logger.Error(ex) + oPropertyValue = Nothing + End Try If IsNothing(oPropertyValue) OrElse String.IsNullOrEmpty(oPropertyValue) Then If Item.Value.IsRequired Then diff --git a/Jobs/EDMI/ZUGFeRD/PropertyValues.vb b/Jobs/EDMI/ZUGFeRD/PropertyValues.vb index 7baabe41..f8065f3c 100644 --- a/Jobs/EDMI/ZUGFeRD/PropertyValues.vb +++ b/Jobs/EDMI/ZUGFeRD/PropertyValues.vb @@ -1,4 +1,5 @@ Imports System.Collections.Generic +Imports System.Linq Imports System.Reflection Imports System.Text.RegularExpressions Imports DigitalData.Modules.Logging @@ -87,6 +88,38 @@ Public Class PropertyValues Return New List(Of Object) From {Obj} End Function + Public Function GetFinalPropValue(List As List(Of Object)) As List(Of Object) + Dim oResult As New List(Of Object) + + For Each Item In List + Dim oItemValue = DoGetFinalPropValue(Item) + + If Not IsNothing(oItemValue) Then + oResult.Add(oItemValue) + End If + Next + + Return oResult + End Function + + Private Function DoGetFinalPropValue(Value As Object) As String + If TypeOf Value Is List(Of Object) Then + Dim oList = DirectCast(Value, List(Of Object)) + Dim oCount = oList.Count + + Select Case oCount + Case 0 + Return Nothing + Case Else + Return DoGetFinalPropValue(oList.First()) + End Select + + Return DoGetFinalPropValue(Value) + Else + Return Value.ToString + End If + End Function + Private Function GetIndex(Prop As String) As Integer If Regex.IsMatch(Prop, _indexPattern) Then Dim oMatch = _indexRegex.Match(Prop)