This commit is contained in:
Jonathan Jenne
2021-11-10 16:53:04 +01:00
parent 1d74f2909f
commit e732ce7099
23 changed files with 1513 additions and 634 deletions

View File

@@ -5,7 +5,7 @@ Imports MultiTool.Shared.Winline
Namespace Documents
Public Class Document
Public File As FileInfo
Public Type As DocumentType
Public Property Type As DocumentType
Public Schema As Schema
Public Mandator As Mandator
@@ -48,29 +48,6 @@ Namespace Documents
Public Overrides Function Equals(obj As Object) As Boolean
Return FullName = DirectCast(obj, Document).FullName
End Function
' Public Type As DocumentType
' Public Data As Object
' Public DataOriginal As Object
' Public DataOutput As Object
' Public DataString As String
' Public Selected As Boolean = False
' Public Shared Function GetDocumentTypeFromTemplateName(pTemplateName As String) As DocumentType
' Return DocumentMatch.TypeMatchingTable.
' Where(Function(kv) pTemplateName.Contains(kv.Key)).
' Select(Function(kv) kv.Value).
' FirstOrDefault()
' End Function
' Public Shared Function GetDocumentSchemaFromDocumentType(pDocumentType As DocumentType) As Type
' Return DocumentMatch.SchemaMatchingTable.
' Where(Function(kv) pDocumentType = kv.Key).
' Select(Function(kv) kv.Value).
' FirstOrDefault()
' End Function
End Class
End Namespace

129
MultiTool.Shared/Mapper.vb Normal file
View File

@@ -0,0 +1,129 @@
Imports System.Globalization
Imports System.Text.RegularExpressions
Imports AutoMapper
Imports AutoMapper.Configuration
Imports DigitalData.Modules.Logging
Imports MultiTool.Shared.DocumentRow
Public Class Mapper
Private MapperConfig As MapperConfiguration
Private LogConfig As LogConfig
Private Logger As Logger
Public Sub New(pLogConfig As LogConfig)
LogConfig = pLogConfig
Logger = pLogConfig.GetLogger
End Sub
Public Function GetMapper(Of T)(pPropertyMap As Dictionary(Of String, String)) As IMapper
MapperConfig = New MapperConfiguration(CreateMapperConfig(Of T)(pPropertyMap))
MapperConfig.AssertConfigurationIsValid()
Return MapperConfig.CreateMapper()
End Function
Private Function CreateMapperConfig(Of T)(pPropertyMap As Dictionary(Of String, String)) As MapperConfigurationExpression
Logger.Info("Creating mapper config for type [{0}]", GetType(T).Name)
Dim oConfig As New MapperConfigurationExpression()
oConfig.AddProfile(New MappingProfile(Of T)(LogConfig, pPropertyMap))
Return oConfig
End Function
Public Class MappingProfile(Of T)
Inherits Profile
Private LogConfig As LogConfig
Public Overrides ReadOnly Property ProfileName As String
Get
Return "MappingProfile"
End Get
End Property
Public Sub New(pLogConfig As LogConfig, pPropertyMap As Dictionary(Of String, String))
LogConfig = pLogConfig
CreateMap(Of Dictionary(Of String, FieldValue), T).ConvertUsing(New ReportTypeConverter(Of T)(LogConfig, pPropertyMap))
End Sub
End Class
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("(?<Key>[\w\s-]+)(?:\[(?<Subkey>[\w]+)\])?")
Private LogConfig As LogConfig
Private 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
Public Function Convert(source As Dictionary(Of String, FieldValue), destination As TDestination, context As ResolutionContext) As TDestination Implements ITypeConverter(Of Dictionary(Of String, FieldValue), TDestination).Convert
If source Is Nothing Then
Return Nothing
End If
Dim oResult = Activator.CreateInstance(Of TDestination)
Logger.Info("Mapping object of Type [{0}]", GetType(TDestination).Name)
For Each oMapItem As KeyValuePair(Of String, String) In PropertyMap
Try
' SourceKey will be something like 'Fakt_Kontonummer[Final]'
' DestinationKey will be something like 'Text1'
Dim oSourceKeyCombined As String = oMapItem.Key
Dim oDestinationKey As String = oMapItem.Value
' Resolve SourceKey into Key and Subkey
Dim oMatch As Match = KeyWithSubkey.Match(oSourceKeyCombined)
Dim oSourceKey As String = oMatch.Groups("Key")?.Value
Dim oSourceSubkey As String = oMatch.Groups("Subkey")?.Value
' Set property value if property exists in source
If source.ContainsKey(oSourceKey) Then
' Try to get the value from 'source'
Dim oFieldValue As FieldValue = source.Item(oSourceKey)
' Get the destination property by DestinationKey
Dim oProperty = GetType(TDestination).
GetProperties().
SingleOrDefault(Function(p) p.Name = oDestinationKey)
' Set the property if it exists
If oProperty IsNot Nothing Then
Dim oValue = GetFieldValue(oFieldValue, oSourceKey, oSourceSubkey)
Logger.Info("Transferring value [{0}] from [{1}] -> [{2}]", oValue, oSourceKeyCombined, oDestinationKey)
oProperty.SetValue(oResult, oValue)
Else
Logger.Warn("Property [{0}] does not exist in destination object. Possible error in configuration.", oDestinationKey)
End If
End If
Catch ex As Exception
Logger.Warn("Could not transfer key [{0}] to destination object", oMapItem.Key)
Logger.Error(ex)
End Try
Next
Return oResult
End Function
Private Function GetFieldValue(pValue As FieldValue, pKey As String, pSubKey As String) As String
If pSubKey = "Original" Then
Return pValue.Original
ElseIf pSubKey = "External" Then
Return pValue.External
Else
Return pValue.Final
End If
End Function
End Class
End Class

View File

@@ -44,6 +44,9 @@
<OptionInfer>On</OptionInfer>
</PropertyGroup>
<ItemGroup>
<Reference Include="AutoMapper, Version=10.0.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
<HintPath>..\packages\AutoMapper.10.1.1\lib\net461\AutoMapper.dll</HintPath>
</Reference>
<Reference Include="DevExpress.DataAccess.v19.2, Version=19.2.3.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a, processorArchitecture=MSIL" />
<Reference Include="DigitalData.Modules.Config">
<HintPath>..\..\DDMonorepo\Modules.Config\bin\Debug\DigitalData.Modules.Config.dll</HintPath>
@@ -103,6 +106,7 @@
<Compile Include="Exceptions.vb" />
<Compile Include="IDictionaryEx.vb" />
<Compile Include="IEnumerableEx.vb" />
<Compile Include="Mapper.vb" />
<Compile Include="My Project\AssemblyInfo.vb" />
<Compile Include="My Project\Application.Designer.vb">
<AutoGen>True</AutoGen>
@@ -123,7 +127,9 @@
<Compile Include="Schemas\Orders\Helpers.vb" />
<Compile Include="Schemas\Orders\Input.vb" />
<Compile Include="Schemas\Orders\OrderSchema.vb" />
<Compile Include="Schemas\Orders\ReportSource.vb" />
<Compile Include="Schemas\Report\ReportHead.vb" />
<Compile Include="Schemas\Report\ReportPosition.vb" />
<Compile Include="Schemas\Report\ReportSource.vb" />
<Compile Include="Schemas\Response.vb" />
<Compile Include="Schemas\Schema.vb" />
<Compile Include="Schemas\SchemaLoader.vb" />

View File

@@ -1,21 +0,0 @@
Imports DevExpress.DataAccess.ObjectBinding
Imports System.Collections.Generic
Imports System.ComponentModel
Namespace Schemas.Orders
<DisplayName("OrdersReport"), HighlightedClass>
Public Class ReportSource
<HighlightedMember>
Public Property Head As Orders.Input.MESOWebServiceEXIMVRG_ordersT025
<HighlightedMember>
Public Property Positions As IEnumerable(Of Orders.Input.MESOWebServiceEXIMVRG_ordersT026)
<HighlightedMember>
Public Iterator Function GetPositionList() As IEnumerable(Of Orders.Input.MESOWebServiceEXIMVRG_ordersT026)
For Each oPosition In Positions
Yield oPosition
Next
End Function
End Class
End Namespace

View File

@@ -0,0 +1,16 @@
Public Class ReportHead
Public Property Title As String
Public Property DateCreated As Date
Public Property Id As String
Public Property Text1 As String
Public Property Text2 As String
Public Property Text3 As String
Public Property Text4 As String
Public Property Text5 As String
Public Property Text6 As String
Public Property Text7 As String
Public Property Text8 As String
Public Property Text9 As String
Public Property Text10 As String
End Class

View File

@@ -0,0 +1,14 @@
Public Class ReportPosition
Public Property Id As String
Public Property Text1 As String
Public Property Text2 As String
Public Property Text3 As String
Public Property Text4 As String
Public Property Text5 As String
Public Property Text6 As String
Public Property Text7 As String
Public Property Text8 As String
Public Property Text9 As String
Public Property Text10 As String
End Class

View File

@@ -0,0 +1,16 @@
Imports System.ComponentModel
Imports DevExpress.DataAccess.ObjectBinding
<HighlightedClass, DisplayName("ReportSource")>
Public Class ReportSource
<HighlightedMember>
Public Property Head As ReportHead
<HighlightedMember>
Public Property Positions As IEnumerable(Of ReportPosition)
<HighlightedMember>
Public Iterator Function GetPositionList() As IEnumerable(Of ReportPosition)
For Each oPosition In Positions
Yield oPosition
Next
End Function
End Class

View File

@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoMapper" version="10.1.1" targetFramework="net461" />
<package id="NLog" version="4.7.10" targetFramework="net461" />
</packages>