Click here to Skip to main content
15,881,852 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.7K   881   17  
An article on data validation with a flat file schema generated in Flat File Checker.
Option Strict On
Imports System.Threading
Imports System.Xml
''' <summary>
''' Defines the base class for data column, which is a field in a table of any data source.
''' </summary>
''' <remarks></remarks>
Public Class DataColumn
    Inherits TableColumn
    Implements Xml.IHasXmlNode

    '  Private _index As Integer = -1 ' order of the column in the matrix of FlatFile object
    <Xml.Serialization.XmlElement("Comment")> _
    Private _comment As String
    Private _checks As AndChecksCollection
    Private _data_source As DataTable
    Private _index_in_file As Integer = -1
   ''' <summary>
    ''' Format of acceptable value in the column
    ''' </summary>
    ''' <remarks>Currently is only used in Date Conditions</remarks>
    Private _format As String
    Public Sub New(ByVal dataSource As DataTable, ByVal index As Integer)
        MyBase.New(dataSource, index)
        Me._data_source = dataSource
        If Me.Checks Is Nothing Then Me.Checks = New AndChecksCollection(Me)

    End Sub
    Public Sub New(ByVal dataSource As DataTable, ByVal name As String)
        MyBase.New(dataSource, name)
        Me._data_source = dataSource
        If Me.Checks Is Nothing Then Me.Checks = New AndChecksCollection(Me)

    End Sub
  
    Public Function SetIndexes() As Boolean
        Dim check As GeneralChecker

        ' That will overwrite index from definition file if there is one. 
        If _index_in_file < 0 Then
            _index_in_file = Me.Datasource.ColumnIndex(Me.Name)
        End If
        If _index_in_file < 0 Then '  Error
            Throw New FileFormatException(Me.Name)
        End If

        For Each check In Me.Checks ' Loop through columns in where statements
            If Not check.SetIndexes() Then
                logger.LogError("Index is not set for column in checks for column " & Me.FullName & ".", "Column.SetIndexes")
                Return False
            End If
        Next check ' Validation condition

        Return True
    End Function

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        If TypeOf obj Is TableColumn Then
            Return Me = CType(obj, DataColumn)
        Else
            Return MyBase.Equals(obj)
        End If
    End Function
    Public Property IndexInFile() As Integer
        Get
            Return Me._index_in_file
        End Get
        Set(ByVal value As Integer)
            _index_in_file = value
        End Set
    End Property
    Public Sub RunChecks(ByVal wait As AutoResetEvent)
        Me.OnValidating(New EventArgs)
        Dim check As GeneralChecker
        Try
            For Each check In Me.Checks
                If Not check.RunChecks(wait) Then
                    Me.OnValidated(New ColumnValidatedEventArgs(False))
                End If
            Next
        Finally
            Me.OnValidated(New ColumnValidatedEventArgs(True))
        End Try
    End Sub
    Public Shared Shadows Operator <>(ByVal col1 As DataColumn, ByVal col2 As DataColumn) As Boolean
        Return Not col1 = col2
    End Operator
    Public Shared Shadows Operator =(ByVal col1 As DataColumn, ByVal col2 As DataColumn) As Boolean
        If col1.Datasource = col2.Datasource Then
            If Not (String.IsNullOrEmpty(col1.Name) OrElse String.IsNullOrEmpty(col2.Name)) Then
                Return UCase(col1.FullName) = UCase(col2.FullName)
            Else
                Return False
            End If
        Else : Return False
        End If
    End Operator


    Public Overrides Function GetHashCode() As Integer
        Return MyBase.GetHashCode()
    End Function

    ''' <summary>
    ''' Returns XML node of the Column
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetNode() As XmlNode Implements System.Xml.IHasXmlNode.GetNode
        Dim ColNode As XmlNode
        
        ColNode = Me.Document.CreateNode(XmlNodeType.Element, "mstns", "Column", "")

        XmlNodeAppendAttribute(ColNode, "Name", Name)
        If Not String.IsNullOrEmpty(Me.Format) Then
            XmlNodeAppendAttribute(ColNode, "Format", Format)
        End If


        If Not String.IsNullOrEmpty(_comment) Then
            Dim CommentNode As XmlNode = Me.Document.CreateNode(XmlNodeType.Element, "", "Comment", "")
            CommentNode.InnerText = _comment
            ColNode.AppendChild(CommentNode)
        End If

        If Me.Required Then
            Dim AttrRequired As XmlAttribute = Me.Document.CreateAttribute("Required")
            AttrRequired.Value = "True"
            ColNode.Attributes.Append(AttrRequired)
        End If

        Dim check As GeneralChecker
        For Each check In Me.Checks
            ColNode.AppendChild(check.GetNode())
        Next
        Return ColNode

    End Function

    ''' <summary>
    ''' Free text comment for the column
    ''' </summary>
    ''' <value>Text of the comment</value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Comment() As String
        Get
            Return _comment
        End Get
        Set(ByVal value As String)
            _comment = value
        End Set
    End Property
    Friend ReadOnly Property Document() As XmlDocument
        Get
            If Not Datasource Is Nothing Then
                Return Me.Datasource.Document
            Else : Return Nothing
            End If
        End Get
    End Property

    Public Shared Function GetColumnName(ByVal fullColumnName As String) As String
        If Not fullColumnName Is Nothing AndAlso fullColumnName Like "*.*" Then
            Dim c As String = Right(fullColumnName, Len(fullColumnName) - InStr(fullColumnName, "."))

            c = Replace(c, "[", "") ' To allow square brackets in full column name
            c = Replace(c, "]", "") ' ie [Contacts].[Date of birth]

            Return c
        Else : Return fullColumnName
        End If
    End Function


    ''' <summary>
    ''' File to which this column belongs
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Datasource() As DataTable
        Get
            Return Me._data_source
        End Get
        Set(ByVal value As DataTable)
            _data_source = value
        End Set
    End Property

    Public Property Format() As String
        Get
            Return _format
        End Get
        Set(ByVal value As String)
            _format = value
        End Set
    End Property
    ''' <summary>
    ''' List of Checks for Column
    ''' </summary>
    ''' <value>List of checks</value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Checks() As AndChecksCollection
        Get
            Checks = _checks
        End Get
        Set(ByVal value As AndChecksCollection)
            _checks = value
        End Set
    End Property

    Public Sub AddRule(ByVal check As GeneralChecker)
        Me._checks.Add(check)
        AddHandler check.Evaluated, AddressOf Me.Datasource.Datasources.OnRuleEvaluated
    End Sub

    Public Function InitiateInRules() As Boolean
        Dim check As GeneralChecker
        For Each check In _checks
            If Not check.InitiateMyColumns() Then Return False
        Next
        Return True
    End Function

    Public Overrides Function ToString() As String
        Return Me.Name
    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