Imports System.Text.RegularExpressions

Partial Class RecipeHolderDatabaseDataSet



    Partial Class RecipeIngredientDataTable

        Private Sub RecipeIngredientDataTable_ColumnChanging(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanging
            If (e.Column.ColumnName = Me.IngredientAmountColumn.ColumnName) Then
                Dim proposedValue As Decimal
                If Decimal.TryParse(e.ProposedValue.ToString, proposedValue) Then
                    If Not proposedValue >= 1 AndAlso Not proposedValue <= 5000000000.0 Then
                        Throw New ArgumentOutOfRangeException(Me.IngredientAmountColumn.ColumnName.ToString & " is out of range.")
                    End If
                Else
                    Throw New ArgumentNullException(Me.IngredientAmountColumn.ColumnName.ToString & " is not in deciaml form.")
                End If

            End If

        End Sub

    End Class

    Partial Class AccountsDataTable

        Private Sub AccountsDataTable_ColumnChanging(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanging

            'check to see if we are working with first name column. 
            'If its not between 1 and 50 chars
            'throw an exception
            If (e.Column.ColumnName = Me.FirstNameColumn.ColumnName) Then
                Dim proposedValue As String = CStr(e.ProposedValue)
                If String.IsNullOrEmpty(proposedValue) Then
                    Throw New ArgumentNullException(Me.FirstNameColumn.ColumnName.ToString & " is out of range.")
                End If
                If Not proposedValue.Length >= 1 AndAlso Not proposedValue.Length <= 50 Then
                    Throw New ArgumentOutOfRangeException(Me.FirstNameColumn.ColumnName.ToString & " is out of range.")
                End If
                'check to see if we are working with last name column. 
                'If its not between 1 and 50 chars
                'throw an exception
            ElseIf (e.Column.ColumnName = Me.LastNameColumn.ColumnName) Then
                Dim proposedValue As String = CStr(e.ProposedValue)
                If String.IsNullOrEmpty(proposedValue) Then
                    Throw New ArgumentNullException(Me.LastNameColumn.ColumnName.ToString & " is out of range.")
                End If
                If Not proposedValue.Length >= 1 AndAlso Not proposedValue.Length <= 50 Then
                    Throw New ArgumentOutOfRangeException(Me.LastNameColumn.ColumnName.ToString & " is out of range.")
                End If
                'check to see if we are working with user name column. 
                'If its not between 0 and 50 chars
                'throw an exception
            ElseIf (e.Column.ColumnName = Me.UserNameColumn.ColumnName) Then
                Dim proposedValue As String = CStr(e.ProposedValue)
                If Not proposedValue.Length >= 0 AndAlso Not proposedValue.Length <= 50 Then
                    Throw New ArgumentOutOfRangeException(Me.UserNameColumn.ColumnName.ToString & " is out of range.")
                End If
                'check to see if we are working with password name column. 
                'If its not between 0 and 50 chars
                'throw an exception
            ElseIf (e.Column.ColumnName = Me.PasswordColumn.ColumnName) Then
                Dim proposedValue As String = CStr(e.ProposedValue)
                If Not proposedValue.Length >= 0 AndAlso Not proposedValue.Length <= 50 Then
                    Throw New ArgumentOutOfRangeException(Me.PasswordColumn.ColumnName.ToString & " is out of range.")
                End If

            ElseIf (e.Column.ColumnName = Me.isProtectedColumn.ColumnName) Then
                If Not CStr(e.ProposedValue).ToUpper = "TRUE" Or Not CStr(e.ProposedValue).ToUpper = "FALSE" Then
                    Throw New ArgumentOutOfRangeException(Me.isProtectedColumn.ColumnName.ToString & " is out of range.")
                End If

            ElseIf (e.Column.ColumnName = Me.EMailColumn.ColumnName) Then
                If Not validEMailAddress(CStr(e.ProposedValue)) Then
                    Throw New ArgumentOutOfRangeException(Me.isProtectedColumn.ColumnName.ToString & " is not in the correct format.")
                End If
            End If
        End Sub

        Public Shared Function validEMailAddress(ByVal emailAddress As String) As Boolean
            Const VALIDEMAIL As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"
            If Regex.IsMatch(emailAddress, VALIDEMAIL) Then
                validEMailAddress = True
            Else
                validEMailAddress = False
            End If
        End Function

    End Class

    Partial Class NutritionDetailsDataTable

        Private Sub NutritionDetailsDataTable_ColumnChanging(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanging
            If (e.Column.ColumnName = Me.CaloriesColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.CaloriesColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If

            ElseIf (e.Column.ColumnName = Me.CaloriesFromFatColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.CaloriesFromFatColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.TotalFatColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.TotalFatColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.SaturatedFatColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.SaturatedFatColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.TransFatColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.TransFatColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.CholesteralColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.CholesteralColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.SodiumColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.SodiumColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.TotalCarbohydratesColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.TotalCarbohydratesColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.DietaryFiberColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.DietaryFiberColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.SugarColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.SugarColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.ProteinColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.ProteinColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.VitaminAColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.VitaminAColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.VitaminCColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.VitaminCColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.CalciumColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.CalciumColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If
            ElseIf (e.Column.ColumnName = Me.IronColumn.ColumnName) Then
                If Not String.IsNullOrEmpty(CStr(e.ProposedValue)) Then
                    If Not IsNumeric(e.ProposedValue) Then
                        Throw New ArgumentException(Me.IronColumn.ColumnName.ToString & " has to be a numeric value.")
                    End If
                End If


            End If

        End Sub

    End Class

    Partial Class RecipesDataTable

        Private Sub RecipesDataTable_ColumnChanging(ByVal sender As System.Object, ByVal e As System.Data.DataColumnChangeEventArgs) Handles Me.ColumnChanging
            'Name
            If e.Column.ColumnName = Me.NameColumn.ColumnName Then
                Dim proposedValue As String = CStr(e.ProposedValue)

                If String.IsNullOrEmpty(proposedValue) Then
                    Throw New ArgumentNullException(Me.NameColumn.ColumnName.ToString & " is out of range.")
                End If
                If Not CStr(e.ProposedValue).Length >= 1 AndAlso Not CStr(e.ProposedValue).Length <= 50 Then
                    Throw New ArgumentOutOfRangeException(Me.NameColumn.ColumnName.ToString & " is out of range.")
                End If
            End If
            'Preparation Time
            If e.Column.ColumnName = Me.PreparationTimeColumn.ColumnName Then
                Dim proposedValue As Integer

                If Integer.TryParse(e.ProposedValue.ToString, proposedValue) Then

                    If Not proposedValue >= 0 AndAlso Not proposedValue <= 100000 Then
                        Throw New ArgumentOutOfRangeException(Me.PreparationTimeColumn.ColumnName.ToString & " is out of range.")
                    End If

                Else
                    Throw New ArgumentException(Me.PreparationTimeColumn.ColumnName.ToString & " has to be numeric.")
                End If
            End If
            'Directions
            If e.Column.ColumnName = Me.DirectionsColumn.ColumnName Then
                Dim proposedValue As String = CStr(e.ProposedValue)

                If Not proposedValue.Length >= 1 AndAlso Not proposedValue.Length <= 3000 Then
                    Throw New ArgumentOutOfRangeException(Me.DirectionsColumn.ColumnName.ToString & " is out of range.")
                End If
            End If
            'Cook Time
            If e.Column.ColumnName = Me.CookTimeColumn.ColumnName Then
                Dim proposedValue As Integer

                If Integer.TryParse(e.ProposedValue.ToString, proposedValue) Then

                    If Not proposedValue >= 0 AndAlso Not proposedValue <= 100000 Then
                        Throw New ArgumentOutOfRangeException(Me.CookTimeColumn.ColumnName.ToString & " is out of range.")
                    End If

                Else
                    Throw New ArgumentException(Me.CookTimeColumn.ColumnName.ToString & " has to be numeric.")
                End If
            End If

            If e.Column.ColumnName = Me.PhotoColumn.ColumnName Then

                'If Byte.TryParse(CStr(e.ProposedValue), Nothing) = Nothing Then

                'End If
            End If

            If e.Column.ColumnName = Me.TemperatureColumn.ColumnName Then
                Dim proposedValue As Integer

                If Integer.TryParse(e.ProposedValue.ToString, proposedValue) Then
                    If Not proposedValue >= 0 AndAlso Not proposedValue <= 2000 Then
                        Throw New ArgumentOutOfRangeException(Me.TemperatureColumn.ColumnName.ToString & " is out of range.")
                    End If
                Else
                    Throw New ArgumentException(Me.TemperatureColumn.ColumnName.ToString & " has to be numeric.")
                End If

            End If

        End Sub

    End Class

End Class
