From dec45ef4935fd2425e33aa86a0a27ee6c7e517ce Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Fri, 19 Nov 2021 14:23:17 +0100 Subject: [PATCH] Clean up, add DataRow Extension --- MultiTool.Shared/DataRowEx.vb | 13 ++++++ MultiTool.Shared/Helpers.vb | 19 -------- MultiTool.Shared/Mapper.vb | 22 ++++----- MultiTool.Shared/MultiTool.Shared.vbproj | 2 +- MultiTool.Shared/Templates/TemplateLoader.vb | 46 +++++++++---------- MultiTool.Shared/Winline/WinlineData.vb | 48 ++++++++++---------- MultiTool.Shared/XmlData.vb | 2 +- 7 files changed, 72 insertions(+), 80 deletions(-) create mode 100644 MultiTool.Shared/DataRowEx.vb delete mode 100644 MultiTool.Shared/Helpers.vb diff --git a/MultiTool.Shared/DataRowEx.vb b/MultiTool.Shared/DataRowEx.vb new file mode 100644 index 0000000..bebec62 --- /dev/null +++ b/MultiTool.Shared/DataRowEx.vb @@ -0,0 +1,13 @@ +Imports System.Runtime.CompilerServices +Imports DigitalData.Modules.Language + +Public Module DataRowEx + + Public Function ItemEx(Of T)(pRow As DataRow, pFieldName As String, Optional pDefaultValue As T = Nothing) As T + Try + Return Utils.NotNull(pRow.Item(pFieldName), pDefaultValue) + Catch ex As Exception + Return Nothing + End Try + End Function +End Module diff --git a/MultiTool.Shared/Helpers.vb b/MultiTool.Shared/Helpers.vb deleted file mode 100644 index 7ad7e6b..0000000 --- a/MultiTool.Shared/Helpers.vb +++ /dev/null @@ -1,19 +0,0 @@ -Imports DigitalData.Modules.Language - -Public Class Helpers - Public Shared Function GetRowItem(Of T)(pRow As DataRow, pFieldName As String, Optional pDefaultValue As T = Nothing) As T - Try - Return Utils.NotNull(pRow.Item(pFieldName), pDefaultValue) - Catch ex As Exception - Return Nothing - End Try - End Function - - Private Function TryGetDictionaryItem(Of T)(pDictionary As IDictionary(Of String, T), pKey As String) As T - If pDictionary.ContainsKey(pKey) Then - Return pDictionary.Item(pKey) - Else - Return Nothing - End If - End Function -End Class diff --git a/MultiTool.Shared/Mapper.vb b/MultiTool.Shared/Mapper.vb index c543397..c971527 100644 --- a/MultiTool.Shared/Mapper.vb +++ b/MultiTool.Shared/Mapper.vb @@ -7,8 +7,8 @@ Imports MultiTool.Shared.Documents.DocumentRow Public Class Mapper Private MapperConfig As MapperConfiguration - Private LogConfig As LogConfig - Private Logger As Logger + Private ReadOnly LogConfig As LogConfig + Private ReadOnly Logger As Logger Public Sub New(pLogConfig As LogConfig) LogConfig = pLogConfig @@ -31,7 +31,7 @@ Public Class Mapper Public Class MappingProfile(Of T) Inherits Profile - Private LogConfig As LogConfig + Private ReadOnly LogConfig As LogConfig Public Overrides ReadOnly Property ProfileName As String Get @@ -48,15 +48,13 @@ Public Class Mapper Private Class ReportTypeConverter(Of TDestination) Implements ITypeConverter(Of Dictionary(Of String, FieldValue), TDestination) - Private PropertyMap As Dictionary(Of String, String) - Private KeyWithSubkey As New Regex("(?[\w\s-]+)(?:\[(?[\w]+)\])?") + Private ReadOnly PropertyMap As Dictionary(Of String, String) + Private ReadOnly KeyWithSubkey As New Regex("(?[\w\s-]+)(?:\[(?[\w]+)\])?") - Private LogConfig As LogConfig - Private Logger As Logger + Private ReadOnly Logger As Logger Public Sub New(pLogConfig As LogConfig, pPropertyMap As Dictionary(Of String, String)) MyBase.New() - LogConfig = pLogConfig Logger = pLogConfig.GetLogger() PropertyMap = pPropertyMap End Sub @@ -94,7 +92,7 @@ Public Class Mapper ' Set the property if it exists If oProperty IsNot Nothing Then - Dim oValue = GetFieldValue(oFieldValue, oSourceKey, oSourceSubkey) + Dim oValue = GetFieldValue(oFieldValue, oSourceSubkey) Logger.Info("Transferring value [{0}] from [{1}] -> [{2}]", oValue, oSourceKeyCombined, oDestinationKey) oProperty.SetValue(oResult, oValue) @@ -113,11 +111,11 @@ Public Class Mapper Return oResult End Function - Private Function GetFieldValue(pValue As FieldValue, pKey As String, pSubKey As String) As String - If pSubKey = "Original" Then + Private Function GetFieldValue(pValue As FieldValue, pKey As String) As String + If pKey = "Original" Then Return pValue.Original - ElseIf pSubKey = "External" Then + ElseIf pKey = "External" Then Return pValue.External Else diff --git a/MultiTool.Shared/MultiTool.Shared.vbproj b/MultiTool.Shared/MultiTool.Shared.vbproj index bf28b0c..3ba37b9 100644 --- a/MultiTool.Shared/MultiTool.Shared.vbproj +++ b/MultiTool.Shared/MultiTool.Shared.vbproj @@ -98,12 +98,12 @@ + - diff --git a/MultiTool.Shared/Templates/TemplateLoader.vb b/MultiTool.Shared/Templates/TemplateLoader.vb index ad5eaa1..27afc8c 100644 --- a/MultiTool.Shared/Templates/TemplateLoader.vb +++ b/MultiTool.Shared/Templates/TemplateLoader.vb @@ -32,10 +32,10 @@ Namespace Templates For Each oRow As DataRow In oTable.Rows Dim oTemplate As New Template With { - .Name = GetRowItem(oRow, "NAME", String.Empty), - .Description = GetRowItem(oRow, "DESCRIPTION", String.Empty), - .FileName = GetRowItem(oRow, "FILE_NAME", String.Empty), - .IsImport = GetRowItem(oRow, "IS_IMPORT", True) + .Name = oRow.ItemEx("NAME", String.Empty), + .Description = oRow.ItemEx("DESCRIPTION", String.Empty), + .FileName = oRow.ItemEx("FILE_NAME", String.Empty), + .IsImport = oRow.ItemEx("IS_IMPORT", True) } oTemplates.Add(oTemplate) @@ -59,13 +59,13 @@ Namespace Templates For Each oRow As DataRow In oTable.Rows Dim oTemplate As New MappingConfig With { - .OrderKey = GetRowItem(oRow, "ORDER_KEY", String.Empty), - .SourceName = GetRowItem(oRow, "SOURCE_NAME", String.Empty), - .SourceItem = GetRowItem(oRow, "SOURCE_ITEM", String.Empty), - .SourceRegex = GetRowItem(oRow, "SOURCE_REGEX", String.Empty), - .DestinationName = GetRowItem(oRow, "DESTINATION_NAME", String.Empty), - .DestinationItem = GetRowItem(oRow, "DESTINATION_ITEM", String.Empty), - .DestinationValue = GetRowItem(oRow, "DESTINATION_VALUE", String.Empty) + .OrderKey = oRow.ItemEx("ORDER_KEY", String.Empty), + .SourceName = oRow.ItemEx("SOURCE_NAME", String.Empty), + .SourceItem = oRow.ItemEx("SOURCE_ITEM", String.Empty), + .SourceRegex = oRow.ItemEx("SOURCE_REGEX", String.Empty), + .DestinationName = oRow.ItemEx("DESTINATION_NAME", String.Empty), + .DestinationItem = oRow.ItemEx("DESTINATION_ITEM", String.Empty), + .DestinationValue = oRow.ItemEx("DESTINATION_VALUE", String.Empty) } oMappingConfigList.Add(oTemplate) @@ -89,19 +89,19 @@ Namespace Templates For Each oRow As DataRow In oTable.Rows Dim oColumn As New ColumnConfig() With { - .Template = GetRowItem(oRow, "TEMPLATE_NAME", String.Empty), - .Table = GetRowItem(oRow, "XML_TABLE", String.Empty), - .Name = GetRowItem(oRow, "XML_ITEM", String.Empty), - .Type = ColumnConfig.ConvertType(GetRowItem(oRow, "DATA_TYPE", String.Empty)), - .OrderKey = GetRowItem(oRow, "ORDER_KEY", 0), - .IsReadOnly = GetRowItem(oRow, "IS_READ_ONLY", False), - .IsVisible = GetRowItem(oRow, "IS_VISIBLE", True), - .IsRequired = GetRowItem(oRow, "IS_REQUIRED", False), - .IsHead = GetRowItem(oRow, "IS_HEAD", True), + .Template = oRow.ItemEx("TEMPLATE_NAME", String.Empty), + .Table = oRow.ItemEx("XML_TABLE", String.Empty), + .Name = oRow.ItemEx("XML_ITEM", String.Empty), + .Type = ColumnConfig.ConvertType(ItemEx(oRow, "DATA_TYPE", String.Empty)), + .OrderKey = oRow.ItemEx("ORDER_KEY", 0), + .IsReadOnly = oRow.ItemEx("IS_READ_ONLY", False), + .IsVisible = oRow.ItemEx("IS_VISIBLE", True), + .IsRequired = oRow.ItemEx("IS_REQUIRED", False), + .IsHead = oRow.ItemEx("IS_HEAD", True), .[Function] = New ColumnConfig.ColumnFunction With { - .Id = GetRowItem(oRow, "FUNCTION_ID", 0), - .Name = GetRowItem(oRow, "FUNCTION_NAME", String.Empty), - .Params = GetRowItem(oRow, "FUNCTION_PARAMETERS", String.Empty) + .Id = oRow.ItemEx("FUNCTION_ID", 0), + .Name = oRow.ItemEx("FUNCTION_NAME", String.Empty), + .Params = oRow.ItemEx("FUNCTION_PARAMETERS", String.Empty) } } diff --git a/MultiTool.Shared/Winline/WinlineData.vb b/MultiTool.Shared/Winline/WinlineData.vb index 2550ee1..39aafc3 100644 --- a/MultiTool.Shared/Winline/WinlineData.vb +++ b/MultiTool.Shared/Winline/WinlineData.vb @@ -82,9 +82,9 @@ Namespace Winline Dim oArticles As New List(Of Article) For Each oRow As DataRow In oTable.Rows - Dim oArticleId As String = GetRowItem(oRow, V21_ARTICLENUMBER, String.Empty) - Dim oArticleDescription As String = GetRowItem(oRow, V21_ARTICLEDESCRIPTION, String.Empty) - Dim oEAN As String = GetRowItem(oRow, V21_EAN, String.Empty) + Dim oArticleId As String = ItemEx(oRow, V21_ARTICLENUMBER, String.Empty) + Dim oArticleDescription As String = ItemEx(oRow, V21_ARTICLEDESCRIPTION, String.Empty) + Dim oEAN As String = ItemEx(oRow, V21_EAN, String.Empty) oArticles.Add(New Article With { .Id = oArticleId, @@ -124,12 +124,12 @@ Namespace Winline Dim oAccounts As New List(Of Account) For Each oRow As DataRow In oTable.Rows - Dim oAccountNumber As String = GetRowItem(oRow, V50_ACCOUNTNUMBER, String.Empty) - Dim oAccountName As String = GetRowItem(oRow, V50_ACCOUNTNAME, String.Empty) - Dim oStreetName As String = GetRowItem(oRow, V50_STREETNAME, String.Empty) - Dim oZipCode As String = GetRowItem(oRow, V50_ZIPCODE, String.Empty) - Dim oCityName As String = GetRowItem(oRow, V50_CITYNAME, String.Empty) - Dim oGLN As String = GetRowItem(oRow, V50_GLN, String.Empty) + Dim oAccountNumber As String = ItemEx(oRow, V50_ACCOUNTNUMBER, String.Empty) + Dim oAccountName As String = ItemEx(oRow, V50_ACCOUNTNAME, String.Empty) + Dim oStreetName As String = ItemEx(oRow, V50_STREETNAME, String.Empty) + Dim oZipCode As String = ItemEx(oRow, V50_ZIPCODE, String.Empty) + Dim oCityName As String = ItemEx(oRow, V50_CITYNAME, String.Empty) + Dim oGLN As String = ItemEx(oRow, V50_GLN, String.Empty) oAccounts.Add(New Account With { .Id = oAccountNumber, @@ -159,8 +159,8 @@ Namespace Winline For Each oRow As DataRow In oTable.Rows Dim oDbInfo = SplitConnectionInfo(oRow) Dim oMandator = New Mandator With { - .Id = GetRowItem(oRow, T01_MANDATORID, String.Empty), - .Name = GetRowItem(oRow, T01_MANDATORNAME, String.Empty), + .Id = ItemEx(oRow, T01_MANDATORID, String.Empty), + .Name = ItemEx(oRow, T01_MANDATORNAME, String.Empty), .Database = oDbInfo.Item1, .Server = oDbInfo.Item2 } @@ -214,8 +214,8 @@ Namespace Winline For Each oRow As DataRow In oTable.Rows oKinds.Add(New DocumentKind With { - .Id = GetRowItem(oRow, T357_KINDID, String.Empty), - .Name = GetRowItem(oRow, T357_KINDNAME, String.Empty), + .Id = ItemEx(oRow, T357_KINDID, String.Empty), + .Name = ItemEx(oRow, T357_KINDNAME, String.Empty), .Mandator = pMandator }) Next @@ -264,11 +264,11 @@ Namespace Winline End If Dim oRow As DataRow = oTable.Rows.Item(0) - Dim oAccountNumber As String = GetRowItem(oRow, V50_ACCOUNTNUMBER, String.Empty) - Dim oAccountName As String = GetRowItem(oRow, V50_ACCOUNTNAME, String.Empty) - Dim oStreetName As String = GetRowItem(oRow, V50_STREETNAME, String.Empty) - Dim oZipCode As String = GetRowItem(oRow, V50_ZIPCODE, String.Empty) - Dim oCityName As String = GetRowItem(oRow, V50_CITYNAME, String.Empty) + Dim oAccountNumber As String = ItemEx(oRow, V50_ACCOUNTNUMBER, String.Empty) + Dim oAccountName As String = ItemEx(oRow, V50_ACCOUNTNAME, String.Empty) + Dim oStreetName As String = ItemEx(oRow, V50_STREETNAME, String.Empty) + Dim oZipCode As String = ItemEx(oRow, V50_ZIPCODE, String.Empty) + Dim oCityName As String = ItemEx(oRow, V50_CITYNAME, String.Empty) Return New Account With { .Id = oAccountNumber, @@ -314,7 +314,7 @@ Namespace Winline End If Dim oRow As DataRow = oTable.Rows.Item(0) - Dim oArticleNumber As String = GetRowItem(oRow, V21_MAINARTICLENUMBER, String.Empty) + Dim oArticleNumber As String = ItemEx(oRow, V21_MAINARTICLENUMBER, String.Empty) Return oArticleNumber Catch ex As Exception @@ -347,9 +347,9 @@ Namespace Winline For Each oRow In oTable.Rows oContacts.Add(New Contact With { - .Id = GetRowItem(Of String)(oRow, T45_KEY, Nothing), - .Name = GetRowItem(Of String)(oRow, T45_NAME, Nothing), - .Number = GetRowItem(Of String)(oRow, T45_CONTACTNUMBER, Nothing) + .Id = ItemEx(Of String)(oRow, T45_KEY, Nothing), + .Name = ItemEx(Of String)(oRow, T45_NAME, Nothing), + .Number = ItemEx(Of String)(oRow, T45_CONTACTNUMBER, Nothing) }) Next @@ -389,7 +389,7 @@ Namespace Winline End If Dim oRow As DataRow = oTable.Rows.Item(0) - Dim oReplacementArticleNumber = GetRowItem(Of String)(oRow, V21_REPLACEMENTARTICLENUMBER, Nothing) + Dim oReplacementArticleNumber = ItemEx(Of String)(oRow, V21_REPLACEMENTARTICLENUMBER, Nothing) If oReplacementArticleNumber Is Nothing Then Return pArticleNumber @@ -450,7 +450,7 @@ Namespace Winline End If Dim oRow As DataRow = oTable.Rows.Item(0) - Dim oArticleNumber As String = GetRowItem(oRow, V21_MAINARTICLENUMBER, String.Empty) + Dim oArticleNumber As String = ItemEx(oRow, V21_MAINARTICLENUMBER, String.Empty) ' EAN was found, now we need to check it against the Regex of the current Mandantor, if one exists If oMandator.Regex <> String.Empty Then diff --git a/MultiTool.Shared/XmlData.vb b/MultiTool.Shared/XmlData.vb index c7900c4..a0d6509 100644 --- a/MultiTool.Shared/XmlData.vb +++ b/MultiTool.Shared/XmlData.vb @@ -2,7 +2,7 @@ Public Class XmlData - ' + Public Shared Function GetElementAttribute(pElement As XElement, pName As String) As String Try Dim oAttribute As XAttribute = pElement.Attribute(pName)