Click here to Skip to main content
15,895,142 members
Articles / Programming Languages / XML

Validating data with Flat File Checker

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
30 Oct 2009GPL32 min read 43.8K   881   17  
An article on data validation with a flat file schema generated in Flat File Checker.
Public Class DateCondition
    Inherits Condition
    Private Shadows val1 As Date
    Private Shadows val2 As Date
    Private Shadows val2_arr() As Date
    Private _wrong_format_value As String
    Private _format_index As Integer
    Private _format As String
    Sub New(ByVal value As Date, ByVal formatIndex As Integer)
        SetSecondValue(value)
        _format_index = formatIndex
    End Sub
    Sub New(ByVal value As String, ByVal formatIndex As Integer)
        SetSecondValue(CDateArray(value, DateFormats(formatIndex)))
        _format_index = formatIndex
    End Sub
    Public Sub New(ByVal value As String, ByVal format As String, ByVal operatorValue As EnumOperator)
        SetSecondValue(DateTime.ParseExact(value, format, Globalization.CultureInfo.InvariantCulture))
        _format = format
        Me.Condition = operatorValue
    End Sub

    Protected Shadows Sub SetSecondValue(ByVal value As Date)
        val2 = value
    End Sub
    Protected Shadows Sub SetSecondValue(ByVal value() As Date)
        val2_arr = value
    End Sub
    Public Overrides Property SecondValue() As String
        Get
            Select Case BenchmarkType
                Case EnumBenchmarkType.DateType
                    Return CStr(val2)
                Case EnumBenchmarkType.DateArray
                    ' Convert array to string
                    Return ""
                Case Else
                    '  Throw 
                    Return ""
            End Select
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Overrides Property FirstValue() As String
        Get
            If String.IsNullOrEmpty(_format) Then
                If _wrong_format_value Is Nothing Or _wrong_format_value = "" Then
                    Return Format(val1, "dd/MM/yyyy")
                Else
                    Return _wrong_format_value
                End If
            Else
                Return Format(val1, _format)
            End If
        End Get
        Set(ByVal value As String)
            Me.IsEmptyValue = String.IsNullOrEmpty(value)
            If Me.IsEmptyValue Then Exit Property
            If String.IsNullOrEmpty(_format) Then
                If Not System.Text.RegularExpressions.Regex.IsMatch(value, RegExpDateFormats(_format_index)) Then
                    _wrong_format_value = value
                    val1 = Nothing
                    Throw New ArgumentException("Value has incorrect date format.")
                Else
                    _wrong_format_value = Nothing
                End If
                If Not DateTime.TryParseExact(value, DateFormats(_format_index), System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.AssumeUniversal, val1) Then
                    _wrong_format_value = value
                    Throw New ArgumentException("Value has incorrect values for parts of the string that represent specific part of the date.")
                End If
            Else
                If Not DateTime.TryParseExact(value, _format, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.AssumeUniversal, val1) Then
                    _wrong_format_value = value
                    Throw New ArgumentException("Value has incorrect date format.")
                End If
            End If
        End Set
    End Property


    ' **************************************************************
    '         Convert line into array of dates
    ' **************************************************************
    Private Shared Function CDateArray(ByVal value As String, _
    ByVal format As String, Optional ByVal delimeter As String = "," _
      ) As Date()

        Dim dArr() As Date
        Dim v() As String
        Dim i As Integer

        v = Split(value, delimeter)
        ReDim dArr(0 To UBound(v))
        For i = 0 To UBound(dArr)
            If Not DateTime.TryParseExact(v(i), format, System.Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.AssumeUniversal, dArr(i)) Then
                Throw New ArgumentException("Value is not a valid array of dates")
            End If
        Next i
        Return dArr

    End Function
    ' *********************************************************************************
    '                   Check if string is array of dates
    ' *********************************************************************************
    Public Shared Function ArrayOfDates(ByVal value As String, ByVal delimiter As String) As Boolean
        Dim v() As String

        v = Split(value, delimiter)
        Dim i As Integer
        For i = 0 To UBound(v)
            If Not IsDateFormat(v(i)) >= 0 Then
                Return False
            End If
        Next i
        Return True

    End Function



    ''' <summary>
    ''' Check if Strings are Equal
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Protected Overrides Function EvaluateEquals() As Boolean
        Return (val1 = val2)
    End Function

    ' ----------------------------------------------------------------------------
    '               CONDITION = NOT EQUALS
    ' ----------------------------------------------------------------------------
    ''' <summary>
    ''' Check if Numbers are not Equal
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Protected Overrides Function EvaluateNotEquals() As Boolean
        Return Not (val1 = val2)
    End Function

    Protected Overrides Function EvaluateBetween() As Boolean
        If UBound(val2_arr) = 1 Then ' Have 2 items only (array starts from Zero)
            Return val1 > val2_arr(0) AndAlso val1 < val2_arr(1)
        Else : Return False
        End If

    End Function

    ' ----------------------------------------------------------------------------
    '               CONDITION =  GREATER
    ' ----------------------------------------------------------------------------
    Protected Overrides Function EvaluateGreater() As Boolean
        Return (val1 > val2)
    End Function

    ' ----------------------------------------------------------------------------
    '               CONDITION =  GREATER OR EQUAL
    ' ----------------------------------------------------------------------------
    Protected Overrides Function EvaluateEqualGreater() As Boolean
        Return (val1 >= val2)
    End Function

    ' ----------------------------------------------------------------------------
    '               CONDITION =  LESS
    ' ----------------------------------------------------------------------------
    Protected Overrides Function EvaluateLess() As Boolean
        Return (val1 < val2)
    End Function

    ' ----------------------------------------------------------------------------
    '               CONDITION =  LESS OR EQUAL
    ' ----------------------------------------------------------------------------
    Protected Overrides Function EvaluateEqualLess() As Boolean
        Return (val1 <= val2)
    End Function

    ' ----------------------------------------------------------------------------
    '               CONDITION =  IN
    ' ----------------------------------------------------------------------------
    Protected Overrides Function EvaluateIN() As Boolean
        Dim k As Integer
        For k = 0 To val2_arr.Length - 1
            If val1 = val2_arr(k) Then Exit For
        Next
        Return (k <= val2_arr.Length - 1)
    End Function
    Protected Overrides Function EvaluateLike() As Boolean
        Return False
    End Function
    Protected Overrides Function EvaluateIsNull() As Boolean
        Return (_wrong_format_value Is Nothing OrElse _wrong_format_value = "")
    End Function
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Database Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions