2022-04-26 12:02:00 +02:00

163 lines
5.2 KiB
VB.net

Imports System.Text.RegularExpressions
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
Public Class FieldValue
Private _Final As String = ""
Private _External As String = ""
Private _Original As String = ""
Public Property DataType As ColumnType = ColumnType.String
Public Property Errors As New List(Of FieldError)
Public Property Original As String
Get
Return FormatValue(_Original, DataType)
End Get
Set(value As String)
_Original = value
End Set
End Property
Public Property External As String
Get
Return FormatValue(_External, DataType)
End Get
Set(value As String)
_External = value
End Set
End Property
Public Property Final As String
Get
Return FormatValue(_Final, DataType)
End Get
Set(value As String)
_Final = value
End Set
End Property
Public Property IsRequired As Boolean = False
Public Property IsVirtual As Boolean = False
Public Property SortKey As Integer = 0
Public Function GetValue(pValueType) 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 AddFieldError(pType As FieldErrorType, pMessage As String)
Errors.Add(New FieldError() With {
.Type = pType,
.Message = pMessage
})
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