Clean up, add DataRow Extension

This commit is contained in:
Jonathan Jenne 2021-11-19 14:23:17 +01:00
parent cc81a77f05
commit dec45ef493
7 changed files with 72 additions and 80 deletions

View File

@ -0,0 +1,13 @@
Imports System.Runtime.CompilerServices
Imports DigitalData.Modules.Language
Public Module DataRowEx
<Extension()>
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

View File

@ -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

View File

@ -7,8 +7,8 @@ Imports MultiTool.Shared.Documents.DocumentRow
Public Class Mapper Public Class Mapper
Private MapperConfig As MapperConfiguration Private MapperConfig As MapperConfiguration
Private LogConfig As LogConfig Private ReadOnly LogConfig As LogConfig
Private Logger As Logger Private ReadOnly Logger As Logger
Public Sub New(pLogConfig As LogConfig) Public Sub New(pLogConfig As LogConfig)
LogConfig = pLogConfig LogConfig = pLogConfig
@ -31,7 +31,7 @@ Public Class Mapper
Public Class MappingProfile(Of T) Public Class MappingProfile(Of T)
Inherits Profile Inherits Profile
Private LogConfig As LogConfig Private ReadOnly LogConfig As LogConfig
Public Overrides ReadOnly Property ProfileName As String Public Overrides ReadOnly Property ProfileName As String
Get Get
@ -48,15 +48,13 @@ Public Class Mapper
Private Class ReportTypeConverter(Of TDestination) Private Class ReportTypeConverter(Of TDestination)
Implements ITypeConverter(Of Dictionary(Of String, FieldValue), TDestination) Implements ITypeConverter(Of Dictionary(Of String, FieldValue), TDestination)
Private PropertyMap As Dictionary(Of String, String) Private ReadOnly PropertyMap As Dictionary(Of String, String)
Private KeyWithSubkey As New Regex("(?<Key>[\w\s-]+)(?:\[(?<Subkey>[\w]+)\])?") Private ReadOnly KeyWithSubkey As New Regex("(?<Key>[\w\s-]+)(?:\[(?<Subkey>[\w]+)\])?")
Private LogConfig As LogConfig Private ReadOnly Logger As Logger
Private Logger As Logger
Public Sub New(pLogConfig As LogConfig, pPropertyMap As Dictionary(Of String, String)) Public Sub New(pLogConfig As LogConfig, pPropertyMap As Dictionary(Of String, String))
MyBase.New() MyBase.New()
LogConfig = pLogConfig
Logger = pLogConfig.GetLogger() Logger = pLogConfig.GetLogger()
PropertyMap = pPropertyMap PropertyMap = pPropertyMap
End Sub End Sub
@ -94,7 +92,7 @@ Public Class Mapper
' Set the property if it exists ' Set the property if it exists
If oProperty IsNot Nothing Then 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) Logger.Info("Transferring value [{0}] from [{1}] -> [{2}]", oValue, oSourceKeyCombined, oDestinationKey)
oProperty.SetValue(oResult, oValue) oProperty.SetValue(oResult, oValue)
@ -113,11 +111,11 @@ Public Class Mapper
Return oResult Return oResult
End Function End Function
Private Function GetFieldValue(pValue As FieldValue, pKey As String, pSubKey As String) As String Private Function GetFieldValue(pValue As FieldValue, pKey As String) As String
If pSubKey = "Original" Then If pKey = "Original" Then
Return pValue.Original Return pValue.Original
ElseIf pSubKey = "External" Then ElseIf pKey = "External" Then
Return pValue.External Return pValue.External
Else Else

View File

@ -98,12 +98,12 @@
<Compile Include="BaseClass.vb" /> <Compile Include="BaseClass.vb" />
<Compile Include="Config.vb" /> <Compile Include="Config.vb" />
<Compile Include="Constants.vb" /> <Compile Include="Constants.vb" />
<Compile Include="DataRowEx.vb" />
<Compile Include="Documents\Document.vb" /> <Compile Include="Documents\Document.vb" />
<Compile Include="Documents\DocumentRow.vb" /> <Compile Include="Documents\DocumentRow.vb" />
<Compile Include="Documents\DocumentType.vb" /> <Compile Include="Documents\DocumentType.vb" />
<Compile Include="Documents\DocumentLoader.vb" /> <Compile Include="Documents\DocumentLoader.vb" />
<Compile Include="Exceptions.vb" /> <Compile Include="Exceptions.vb" />
<Compile Include="Helpers.vb" />
<Compile Include="IDictionaryEx.vb" /> <Compile Include="IDictionaryEx.vb" />
<Compile Include="IEnumerableEx.vb" /> <Compile Include="IEnumerableEx.vb" />
<Compile Include="Mapper.vb" /> <Compile Include="Mapper.vb" />

View File

@ -32,10 +32,10 @@ Namespace Templates
For Each oRow As DataRow In oTable.Rows For Each oRow As DataRow In oTable.Rows
Dim oTemplate As New Template With { Dim oTemplate As New Template With {
.Name = GetRowItem(oRow, "NAME", String.Empty), .Name = oRow.ItemEx("NAME", String.Empty),
.Description = GetRowItem(oRow, "DESCRIPTION", String.Empty), .Description = oRow.ItemEx("DESCRIPTION", String.Empty),
.FileName = GetRowItem(oRow, "FILE_NAME", String.Empty), .FileName = oRow.ItemEx("FILE_NAME", String.Empty),
.IsImport = GetRowItem(oRow, "IS_IMPORT", True) .IsImport = oRow.ItemEx("IS_IMPORT", True)
} }
oTemplates.Add(oTemplate) oTemplates.Add(oTemplate)
@ -59,13 +59,13 @@ Namespace Templates
For Each oRow As DataRow In oTable.Rows For Each oRow As DataRow In oTable.Rows
Dim oTemplate As New MappingConfig With { Dim oTemplate As New MappingConfig With {
.OrderKey = GetRowItem(oRow, "ORDER_KEY", String.Empty), .OrderKey = oRow.ItemEx("ORDER_KEY", String.Empty),
.SourceName = GetRowItem(oRow, "SOURCE_NAME", String.Empty), .SourceName = oRow.ItemEx("SOURCE_NAME", String.Empty),
.SourceItem = GetRowItem(oRow, "SOURCE_ITEM", String.Empty), .SourceItem = oRow.ItemEx("SOURCE_ITEM", String.Empty),
.SourceRegex = GetRowItem(oRow, "SOURCE_REGEX", String.Empty), .SourceRegex = oRow.ItemEx("SOURCE_REGEX", String.Empty),
.DestinationName = GetRowItem(oRow, "DESTINATION_NAME", String.Empty), .DestinationName = oRow.ItemEx("DESTINATION_NAME", String.Empty),
.DestinationItem = GetRowItem(oRow, "DESTINATION_ITEM", String.Empty), .DestinationItem = oRow.ItemEx("DESTINATION_ITEM", String.Empty),
.DestinationValue = GetRowItem(oRow, "DESTINATION_VALUE", String.Empty) .DestinationValue = oRow.ItemEx("DESTINATION_VALUE", String.Empty)
} }
oMappingConfigList.Add(oTemplate) oMappingConfigList.Add(oTemplate)
@ -89,19 +89,19 @@ Namespace Templates
For Each oRow As DataRow In oTable.Rows For Each oRow As DataRow In oTable.Rows
Dim oColumn As New ColumnConfig() With { Dim oColumn As New ColumnConfig() With {
.Template = GetRowItem(oRow, "TEMPLATE_NAME", String.Empty), .Template = oRow.ItemEx("TEMPLATE_NAME", String.Empty),
.Table = GetRowItem(oRow, "XML_TABLE", String.Empty), .Table = oRow.ItemEx("XML_TABLE", String.Empty),
.Name = GetRowItem(oRow, "XML_ITEM", String.Empty), .Name = oRow.ItemEx("XML_ITEM", String.Empty),
.Type = ColumnConfig.ConvertType(GetRowItem(oRow, "DATA_TYPE", String.Empty)), .Type = ColumnConfig.ConvertType(ItemEx(oRow, "DATA_TYPE", String.Empty)),
.OrderKey = GetRowItem(oRow, "ORDER_KEY", 0), .OrderKey = oRow.ItemEx("ORDER_KEY", 0),
.IsReadOnly = GetRowItem(oRow, "IS_READ_ONLY", False), .IsReadOnly = oRow.ItemEx("IS_READ_ONLY", False),
.IsVisible = GetRowItem(oRow, "IS_VISIBLE", True), .IsVisible = oRow.ItemEx("IS_VISIBLE", True),
.IsRequired = GetRowItem(oRow, "IS_REQUIRED", False), .IsRequired = oRow.ItemEx("IS_REQUIRED", False),
.IsHead = GetRowItem(oRow, "IS_HEAD", True), .IsHead = oRow.ItemEx("IS_HEAD", True),
.[Function] = New ColumnConfig.ColumnFunction With { .[Function] = New ColumnConfig.ColumnFunction With {
.Id = GetRowItem(oRow, "FUNCTION_ID", 0), .Id = oRow.ItemEx("FUNCTION_ID", 0),
.Name = GetRowItem(oRow, "FUNCTION_NAME", String.Empty), .Name = oRow.ItemEx("FUNCTION_NAME", String.Empty),
.Params = GetRowItem(oRow, "FUNCTION_PARAMETERS", String.Empty) .Params = oRow.ItemEx("FUNCTION_PARAMETERS", String.Empty)
} }
} }

View File

@ -82,9 +82,9 @@ Namespace Winline
Dim oArticles As New List(Of Article) Dim oArticles As New List(Of Article)
For Each oRow As DataRow In oTable.Rows For Each oRow As DataRow In oTable.Rows
Dim oArticleId As String = GetRowItem(oRow, V21_ARTICLENUMBER, String.Empty) Dim oArticleId As String = ItemEx(oRow, V21_ARTICLENUMBER, String.Empty)
Dim oArticleDescription As String = GetRowItem(oRow, V21_ARTICLEDESCRIPTION, String.Empty) Dim oArticleDescription As String = ItemEx(oRow, V21_ARTICLEDESCRIPTION, String.Empty)
Dim oEAN As String = GetRowItem(oRow, V21_EAN, String.Empty) Dim oEAN As String = ItemEx(oRow, V21_EAN, String.Empty)
oArticles.Add(New Article With { oArticles.Add(New Article With {
.Id = oArticleId, .Id = oArticleId,
@ -124,12 +124,12 @@ Namespace Winline
Dim oAccounts As New List(Of Account) Dim oAccounts As New List(Of Account)
For Each oRow As DataRow In oTable.Rows For Each oRow As DataRow In oTable.Rows
Dim oAccountNumber As String = GetRowItem(oRow, V50_ACCOUNTNUMBER, String.Empty) Dim oAccountNumber As String = ItemEx(oRow, V50_ACCOUNTNUMBER, String.Empty)
Dim oAccountName As String = GetRowItem(oRow, V50_ACCOUNTNAME, String.Empty) Dim oAccountName As String = ItemEx(oRow, V50_ACCOUNTNAME, String.Empty)
Dim oStreetName As String = GetRowItem(oRow, V50_STREETNAME, String.Empty) Dim oStreetName As String = ItemEx(oRow, V50_STREETNAME, String.Empty)
Dim oZipCode As String = GetRowItem(oRow, V50_ZIPCODE, String.Empty) Dim oZipCode As String = ItemEx(oRow, V50_ZIPCODE, String.Empty)
Dim oCityName As String = GetRowItem(oRow, V50_CITYNAME, String.Empty) Dim oCityName As String = ItemEx(oRow, V50_CITYNAME, String.Empty)
Dim oGLN As String = GetRowItem(oRow, V50_GLN, String.Empty) Dim oGLN As String = ItemEx(oRow, V50_GLN, String.Empty)
oAccounts.Add(New Account With { oAccounts.Add(New Account With {
.Id = oAccountNumber, .Id = oAccountNumber,
@ -159,8 +159,8 @@ Namespace Winline
For Each oRow As DataRow In oTable.Rows For Each oRow As DataRow In oTable.Rows
Dim oDbInfo = SplitConnectionInfo(oRow) Dim oDbInfo = SplitConnectionInfo(oRow)
Dim oMandator = New Mandator With { Dim oMandator = New Mandator With {
.Id = GetRowItem(oRow, T01_MANDATORID, String.Empty), .Id = ItemEx(oRow, T01_MANDATORID, String.Empty),
.Name = GetRowItem(oRow, T01_MANDATORNAME, String.Empty), .Name = ItemEx(oRow, T01_MANDATORNAME, String.Empty),
.Database = oDbInfo.Item1, .Database = oDbInfo.Item1,
.Server = oDbInfo.Item2 .Server = oDbInfo.Item2
} }
@ -214,8 +214,8 @@ Namespace Winline
For Each oRow As DataRow In oTable.Rows For Each oRow As DataRow In oTable.Rows
oKinds.Add(New DocumentKind With { oKinds.Add(New DocumentKind With {
.Id = GetRowItem(oRow, T357_KINDID, String.Empty), .Id = ItemEx(oRow, T357_KINDID, String.Empty),
.Name = GetRowItem(oRow, T357_KINDNAME, String.Empty), .Name = ItemEx(oRow, T357_KINDNAME, String.Empty),
.Mandator = pMandator .Mandator = pMandator
}) })
Next Next
@ -264,11 +264,11 @@ Namespace Winline
End If End If
Dim oRow As DataRow = oTable.Rows.Item(0) Dim oRow As DataRow = oTable.Rows.Item(0)
Dim oAccountNumber As String = GetRowItem(oRow, V50_ACCOUNTNUMBER, String.Empty) Dim oAccountNumber As String = ItemEx(oRow, V50_ACCOUNTNUMBER, String.Empty)
Dim oAccountName As String = GetRowItem(oRow, V50_ACCOUNTNAME, String.Empty) Dim oAccountName As String = ItemEx(oRow, V50_ACCOUNTNAME, String.Empty)
Dim oStreetName As String = GetRowItem(oRow, V50_STREETNAME, String.Empty) Dim oStreetName As String = ItemEx(oRow, V50_STREETNAME, String.Empty)
Dim oZipCode As String = GetRowItem(oRow, V50_ZIPCODE, String.Empty) Dim oZipCode As String = ItemEx(oRow, V50_ZIPCODE, String.Empty)
Dim oCityName As String = GetRowItem(oRow, V50_CITYNAME, String.Empty) Dim oCityName As String = ItemEx(oRow, V50_CITYNAME, String.Empty)
Return New Account With { Return New Account With {
.Id = oAccountNumber, .Id = oAccountNumber,
@ -314,7 +314,7 @@ Namespace Winline
End If End If
Dim oRow As DataRow = oTable.Rows.Item(0) 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 Return oArticleNumber
Catch ex As Exception Catch ex As Exception
@ -347,9 +347,9 @@ Namespace Winline
For Each oRow In oTable.Rows For Each oRow In oTable.Rows
oContacts.Add(New Contact With { oContacts.Add(New Contact With {
.Id = GetRowItem(Of String)(oRow, T45_KEY, Nothing), .Id = ItemEx(Of String)(oRow, T45_KEY, Nothing),
.Name = GetRowItem(Of String)(oRow, T45_NAME, Nothing), .Name = ItemEx(Of String)(oRow, T45_NAME, Nothing),
.Number = GetRowItem(Of String)(oRow, T45_CONTACTNUMBER, Nothing) .Number = ItemEx(Of String)(oRow, T45_CONTACTNUMBER, Nothing)
}) })
Next Next
@ -389,7 +389,7 @@ Namespace Winline
End If End If
Dim oRow As DataRow = oTable.Rows.Item(0) 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 If oReplacementArticleNumber Is Nothing Then
Return pArticleNumber Return pArticleNumber
@ -450,7 +450,7 @@ Namespace Winline
End If End If
Dim oRow As DataRow = oTable.Rows.Item(0) 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 ' 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 If oMandator.Regex <> String.Empty Then

View File

@ -2,7 +2,7 @@
Public Class XmlData Public Class XmlData
'<DebuggerStepThrough> <DebuggerStepThrough>
Public Shared Function GetElementAttribute(pElement As XElement, pName As String) As String Public Shared Function GetElementAttribute(pElement As XElement, pName As String) As String
Try Try
Dim oAttribute As XAttribute = pElement.Attribute(pName) Dim oAttribute As XAttribute = pElement.Attribute(pName)