Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Namespace FMSTest
   Public Class frmFMSTest : Inherits System.Windows.Forms.Form
      Private Dim _disposed As Boolean = False
      <System.Diagnostics.DebuggerNonUserCode()> _
      Public Sub New()
         MyBase.New()
         'This call is required by the Windows Form Designer.
         InitializeComponent()
         'Add any initialization after the InitializeComponent() call
         If m_vb6FormDefInstance Is Nothing Then
            If m_InitializingDefInstance Then
               m_vb6FormDefInstance = Me
            Else
               Try
                  'For the start-up form, the first instance created is the default instance.
                  If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType Is Me.GetType Then
                     m_vb6FormDefInstance = Me
                  End If
               Catch
               End Try
            End If
         End If
         If (DesignMode) Then Exit Sub
         Form_Load(Nothing, Nothing)

      End Sub
      'Form overrides dispose to clean up the component list.
      <System.Diagnostics.DebuggerNonUserCode()> _
      Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
         If _disposed Then
            Return
         End If
         If Disposing Then
            If Not components Is Nothing Then
               components.Dispose()
            End If
         End If
         _disposed = True
         MyBase.Dispose(Disposing)
      End Sub
      Dim logPath As String = ""
      Private Const CLSTEXT As String = "<CLS>"
      Public Sub cmdTest_Click(ByVal sender As Object, ByVal e As EventArgs)
         RunTests()
      End Sub
      Public Sub RunTests()
         logPath = gLogFile

         If (System.IO.File.Exists(logPath)) Then
            System.IO.File.Delete(logPath)
         End If

         Dim o As FMStocks_DB.DBHelper = Nothing
         o = New FMStocks_DB.DBHelper
         msgText("Database = " & o.GetConnectionString())
         o = Nothing


         Test1()
         Test2()
         Test3()
         Test4()
         Test5()

      End Sub
      Private Sub Test1()
         gmRTL.Core.ErrorHandling.ResumeNext(Sub()

            Dim obj As FMStocks_Bus.Account = Nothing
            obj = New FMStocks_Bus.Account
            ' Common deployment error: this error handling block catches the common
            ' case where the FMStocks_Bus component isn't registered on the app server.
            If (Err.Number <> 0) Then
               msgText("Could not create an instance of the FMStocks_Bus.Account. Refer to the setup instructions for FMStocks." & Environment.NewLine & "Error: " & Err.Description & " (" & Err.Number & ")")
            End If

            Dim AccountID As Integer = 0
            Dim Email As String = ""
            Dim Password As String = ""

            Password = "pw"
            Email = "scott@vertigosoftware.com"

            Dim objAccount As FMStocks_Bus.Account = Nothing
            Dim FullName As String = ""
            Dim bIsValid As Boolean = False

            objAccount = New FMStocks_Bus.Account

            bIsValid = objAccount.VerifyLogin(Email,Password,FullName,AccountID)

            ' Common database error: if the DSN is incorrect, or the database is down or
            ' not reachable, this is the first place
            If (bIsValid) Then
               msgText("VerifyLogin call was successful")
            Else
               If (Err.Number <> 0) Then
                  msgText("VerifyLogin call failed." & Environment.NewLine & "Error: " & Err.Description & " (" & Err.Number & ")")
               Else
                  msgText("VerifyLogin call failed")
               End If

               AccountID = obj.Add("Scott","Stanfield",Password,Email)

               If (Err.Number <> 0 Or AccountID = 0) Then
                  msgText("Unable to add a new account. This usually means you tried to create an account using an email address that is already in use." & Environment.NewLine & "Error: " & Err.Description & " (" & Err.Number & ")")
               Else
                  msgText("A new account was successfully added with the ID of " & AccountID)
               End If
            End If

            objAccount = Nothing
            obj = Nothing

         End Sub)
      End Sub
      Private Sub Test2()
         Const FUNCNAME As String = "Test2"
         Dim obj As FMStocks_Bus.Account = Nothing
         Try
            gmRTL.Core.GlobalException.ClearErrorObject()

            obj = New FMStocks_Bus.Account

            Dim AccountID As Integer = 0
            Dim FullName As String = ""
            Dim Email As String = ""
            Dim Balance As Decimal = 0.00

            obj.VerifyLogin("ta1","ta",FullName,AccountID)

            obj.GetAccountInfo(AccountID,FullName,Email,Balance)

            msgText("AccountID =" & AccountID & Environment.NewLine & "FullName =" & FullName & Environment.NewLine & "Email =" & Email & Environment.NewLine & "Balance =" & Balance)

         Catch exc As System.Exception
            gmRTL.Core.GlobalException.SetErrorObject(exc)
         End Try
         obj = Nothing

         If (Err.Number <> 0) Then
            msgText(FUNCNAME & ": " & Err.Description)
         Else
            msgText(FUNCNAME & ": " & "Completed")
         End If

      End Sub
      Private Sub Test3()
         Const FUNCNAME As String = "Test3"
         Dim obj As FMStocks_DB.Account = Nothing
         Try
            gmRTL.Core.GlobalException.ClearErrorObject()

            obj = New FMStocks_DB.Account

            Dim AccountID As Integer = 0
            Dim FullName As String = ""
            Dim Email As String = ""
            Dim Balance As Decimal = 0.00

            obj.VerifyUser("ta1","ta",AccountID,FullName)

            msgText(TypeName(obj) & "VerifyUser" & Environment.NewLine & "AccountID =" & AccountID & Environment.NewLine & "FullName  =" & FullName & Environment.NewLine)

            obj.GetAccountInfo(CInt(AccountID),FullName,Email,Balance)

            msgText(TypeName(obj) & "GetAccountInfo" & Environment.NewLine & "AccountID =" & AccountID & Environment.NewLine & "FullName  =" & FullName & Environment.NewLine & "Email     =" & Email & Environment.NewLine & "Balance   =" & Balance)

            Dim rs As gmRTL.DataLib.SqlClient.Recordset = Nothing
            rs = obj.Summary(AccountID)
            msgText(TypeName(obj) & "Summary" & Environment.NewLine & utils.reportRecordSet(rs))

         Catch exc As System.Exception
            gmRTL.Core.GlobalException.SetErrorObject(exc)
         End Try
         obj = Nothing

         If (Err.Number <> 0) Then
            msgText(FUNCNAME & ": " & Err.Description)
         Else
            msgText(FUNCNAME & ": " & "Completed")
         End If

      End Sub
      Private Sub Test4()
         Const FUNCNAME As String = "Test4"
         Dim obj As FMSStore_Bus.Product = Nothing
         Try
            gmRTL.Core.GlobalException.ClearErrorObject()

            obj = New FMSStore_Bus.Product

            Dim rs As gmRTL.DataLib.SqlClient.Recordset = Nothing

            rs = obj.List()
            msgText(TypeName(obj) & "List" & Environment.NewLine & utils.reportRecordSet(rs))

            rs = obj.FindByDescription("Gorilla")
            msgText(TypeName(obj) & "FindByDescription" & Environment.NewLine & utils.reportRecordSet(rs))

            rs = obj.GetBySKU(1004007)
            msgText(TypeName(obj) & "GetBySKU" & Environment.NewLine & utils.reportRecordSet(rs))


         Catch exc As System.Exception
            gmRTL.Core.GlobalException.SetErrorObject(exc)
         End Try
         obj = Nothing

         If (Err.Number <> 0) Then
            msgText(FUNCNAME & ": " & Err.Description)
         Else
            msgText(FUNCNAME & ": " & "Completed")
         End If

      End Sub
      Private Sub Test5()
         Const FUNCNAME As String = "Test5"
         Dim obj As FMStocks_Bus.Ticker = Nothing
         Try
            gmRTL.Core.GlobalException.ClearErrorObject()

            obj = New FMStocks_Bus.Ticker

            Dim rs As gmRTL.DataLib.SqlClient.Recordset = Nothing

            Dim company As Object = Nothing
            Dim exchange As Object = Nothing

            rs = obj.GetFundamentals("GE",company,exchange)
            msgText(TypeName(obj) & "GetFundamentals" & Environment.NewLine & utils.reportRecordSet(rs))

            rs = obj.ListByCompany("General Electric")
            msgText(TypeName(obj) & "ListByCompany" & Environment.NewLine & utils.reportRecordSet(rs))

            rs = obj.ListByTicker("GEER")
            msgText(TypeName(obj) & "ListByTicker" & Environment.NewLine & utils.reportRecordSet(rs))


            Dim boolRC As Boolean = False
            boolRC = obj.VerifySymbol("GE")
            msgText(TypeName(obj) & "VerifySymbol" & Environment.NewLine & "bool = " & boolRC)

         Catch exc As System.Exception
            gmRTL.Core.GlobalException.SetErrorObject(exc)
         End Try
         obj = Nothing

         If (Err.Number <> 0) Then
            msgText(FUNCNAME & ": " & Err.Description)
         Else
            msgText(FUNCNAME & ": " & "Completed")
         End If

      End Sub
      Private Sub msgText(ByVal str As String)
         If (str = CLSTEXT) Then
            Text1.Text = ""
         Else
            Dim rpt As String = ""
            rpt = "-------------------------------------------" & Environment.NewLine & str & Environment.NewLine
            writeLog(rpt)
            Debug.WriteLine(rpt)
            Text1.Text = Text1.Text & Environment.NewLine & rpt
         End If
      End Sub
      Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs)
         If (Interaction.Command() Like "/batch*") Then
            RunTests()
            System.Windows.Forms.Application.DoEvents()
            'End
         End If
      End Sub
      Public Sub writeLog(ByVal rpt As String)
         Dim txt As System.IO.StreamWriter = Nothing
         Try
            gmRTL.Core.GlobalException.ClearErrorObject()


            txt = (new System.IO.FileInfo(logPath)).AppendText()
            txt.WriteLine(rpt)

         Catch exc As System.Exception
            gmRTL.Core.GlobalException.SetErrorObject(exc)
         End Try

         txt.Close()

         txt = Nothing

      End Sub
      Public Function readLog(ByVal logPath As String) As String
         readLog = ""
         Dim txt As System.IO.StreamReader = Nothing
         Try
            gmRTL.Core.GlobalException.ClearErrorObject()

            txt = new System.IO.StreamReader(new System.IO.FileStream(logPath,System.IO.FileMode.Open,System.IO.FileAccess.Read,System.IO.FileShare.ReadWrite))
            If ((txt.Peek() = -1)) Then
               readLog = logPath & "  is empty"
            Else
               readLog = txt.ReadToEnd()
            End If
         Catch exc As System.Exception
            gmRTL.Core.GlobalException.SetErrorObject(exc)
         End Try

         txt.Close()

         txt = Nothing
      End Function
   End Class
End Namespace
