Jonathan Jenne 46d3dfbd47 Logging
2022-05-05 16:35:22 +02:00

209 lines
7.0 KiB
VB.net

Imports System.Text.RegularExpressions
Imports DigitalData.Modules.Logging
Imports MultiTool.Common.Constants
Namespace Documents
Public Class DocumentRow
Implements IComparable
''' <summary>
''' GUID to match DocumentRow with Row from Grid/DataTable
''' </summary>
Public Property Id As New Guid
''' <summary>
''' Counter to ensure consistency and order when writing XML
''' </summary>
Public Property SortKey As Integer
''' <summary>
''' Tabellen/Elementname aus XML
''' </summary>
Public Property TableName As String
''' <summary>
''' List of Row Values
''' </summary>
''' <returns></returns>
Public Property Fields As Dictionary(Of String, FieldValue)
Public ReadOnly Property HasErrors As Boolean
Get
If Errors.Count > 0 Then
Return True
Else
Return False
End If
End Get
End Property
Public ReadOnly Property Errors As List(Of FieldError)
Get
Return Fields.
Where(Function(f) f.Value.HasError).
SelectMany(Function(f) f.Value.Errors).ToList()
End Get
End Property
Public Sub New()
Id = Guid.NewGuid()
End Sub
Public Function CompareTo(other As Object) As Integer Implements IComparable.CompareTo
Return SortKey.CompareTo(DirectCast(other, DocumentRow).SortKey)
End Function
''' <summary>
''' TODO: Use this class to
''' </summary>
Public Class Field
Public ReadOnly Property Name
Public Property Value As FieldValue
Public Sub New(pName As String)
Name = pName
End Sub
End Class
Public Class FieldValue
Private _Final As String = ""
Private _External As String = ""
Private _Original As String = ""
Private Logger As Logger
Public Property DataType As ColumnType = ColumnType.String
Public Property Errors As New List(Of FieldError)
Public ReadOnly Property Original As String
Get
Return FormatValue(_Original, DataType)
End Get
End Property
Public ReadOnly Property External As String
Get
Return FormatValue(_External, DataType)
End Get
End Property
Public ReadOnly Property Final As String
Get
Return FormatValue(_Final, DataType)
End Get
End Property
Public Property IsRequired As Boolean = False
Public Property IsVirtual As Boolean = False
Public Property PreferExternalValue As Boolean = True
Public Property SortKey As Integer = 0
Public Sub New(pLogConfig As LogConfig)
Logger = pLogConfig.GetLogger()
End Sub
Public Function GetValue(pValueType As String) As String
Select Case pValueType
Case "Original"
Return Original
Case "External"
Return External
Case "Final"
Return Final
Case Else
Return Nothing
End Select
End Function
Public Sub SetValue(pValueType As String, pValue As String)
Select Case pValueType
Case "Original"
_Original = pValue
Case "External"
_External = pValue
Case "Final"
_Final = pValue
Case Else
' Noop
End Select
End Sub
Public Sub AddFieldError(pType As FieldErrorType, pMessage As String)
Errors.Add(New FieldError() With {
.Type = pType,
.Message = pMessage
})
End Sub
Public Sub SetExternalValue(pValue As String)
_External = pValue
' Set the external value as the final value, overriding the original / previous external value
' if the external value should be preferred
If PreferExternalValue = True Then
Logger.Debug("Setting Final value to [{0}] because PreferExternalValue is True", pValue)
_Final = pValue
End If
' If there is no Original value (because the field is virtual),
' set the external value as the final value regardless of the PreferExternalValue setting
If _Original = String.Empty Then
Logger.Debug("Setting Final value to [{0}] because Original value is empty", pValue)
_Final = pValue
End If
End Sub
Public Sub SetOriginalValue(pValue As String)
_Original = pValue
_Final = pValue
End Sub
Public ReadOnly Property HasError As Boolean
Get
' Required check was moved to DocumentLoader
Return Errors.Count > 0 'Or (IsRequired And Final = String.Empty)
End Get
End Property
Public Overrides Function ToString() As String
Return Final
End Function
Private Function FormatValue(pValue As String, pType As ColumnType) As String
Select Case pType
Case ColumnType.Decimal
Return FormatDecimalValue(pValue)
Case Else
Return pValue
End Select
End Function
''' <summary>
''' This function will capture values like below and format them according to winline format values
'''
''' 1000
''' 1.000
''' 1.000,00
''' </summary>
''' <param name="pValue">A string value that represents a number</param>
''' <returns>A string value which contains at dot (.) as the decimal divider, ex. 1000.00</returns>
Private Function FormatDecimalValue(pValue As String) As String
If Not pValue.Contains(","c) Then
Return pValue
End If
Dim oRegex = New Regex("(?<Value>\d+(?:\.\d+)*(?:,\d+)?)")
Dim oMatch = oRegex.Match(pValue)
If oMatch.Success Then
Dim oValue = oMatch.Groups.Item("Value").Value
Return oValue.Replace(","c, "."c)
Else
Return pValue
End If
End Function
End Class
End Class
End Namespace