Option Strict Off
Option Explicit On
Imports System.Linq
Imports System.Collections.Generic
Namespace FMStocks_DB
   Public Module Helpers
   'TransactionType enum
   Public Enum FMStocks_TransactionType
      Pending_Buy = 1
      Pending_Sell = 2
      Bought = 3
      Sold = 4
      Insufficient_Funds = 5
      Not_Enough_Shares = 6
   End Enum
   'Debug String Messages
   Public Const cstrStarting_Method As String = "Starting Method call"
   Public Const cstrFinishing_Method As String = "Finishing Method call"
   Public Const cstrError_Method As String = "Error in Method call"
   'WIN32 API Functions used to write to EventLog and get the Computername
Public Declare Function RegisterEventSource Lib "advapi32.dll" Alias "RegisterEventSourceA" (ByVal lpUNCServerName As String,ByVal lpSourceName As String) As Integer
Public Declare Function DeregisterEventSource Lib "advapi32.dll" (ByVal hEventLog As Integer) As Integer
Public Declare Function ReportEvent Lib "advapi32.dll" Alias "ReportEventA" (ByVal hEventLog As Integer,ByVal wType As Integer,ByVal wCategory As Integer,ByVal dwEventID As Integer,ByVal lpUserSid As Object,ByVal wNumStrings As Integer,ByVal dwDataSize As Integer,ByRef plpStrings As Integer,ByVal lpRawData As Object) As Boolean
Public Declare Function GetLastError Lib "kernel32" As Integer
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal hpvDest As Object,ByVal hpvSource As Object,ByVal cbCopy As Integer)
Public Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Integer,ByVal dwBytes As Integer) As Integer
Public Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Integer) As Integer
Private Declare Function GetComputerNameAPI Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String,ByRef nSize As Integer) As Integer
   Public Sub LogNTEvent(ByVal sString As String,ByVal iLogType As Integer,ByVal iEventID As Integer,ByVal sEventTitle As String)
      gmRTL.Core.App.DefInstance.LogEvent(sString,iLogType,iEventID, sEventTitle)
   End Sub
   ' mp is short for MakeParameter - does typesafe array creation for use with Run* functions
   Public Function mp(ByVal PName As String,ByVal PType As System.Data.SqlDbType,ByVal PSize As Integer,ByVal PValue As Object) As Object()
      mp = New Object(){PName,PType,PSize,PValue}
   End Function
   'Converts a variant into a string. If the varaint is null, it is converted to the empty string.
   Public Function ConvertToString(ByVal v As Object) As String
      ConvertToString = ""
      If (Convert.IsDBNull(v)) Then
         ConvertToString = ""
      Else
         ConvertToString = CStr(v)
      End If
   End Function
   'Converts any Null variant to null, otherwise it returns the existing value.
   Function NullsToZero(ByVal v As Object) As Double '' UPGRADE_INFO: hand-coded
      If (TypeOf v Is gmRTL.DataLib.SqlClient.Field) Then
         If (IsDBNull(v.Value)) Then
            NullsToZero = 0
         Else
            NullsToZero = Convert.ToDouble(v.Value)
         End If
      Else
         If (v Is Nothing) Then
            NullsToZero = 0
         Else
            NullsToZero = Convert.ToDouble(v)
         End If
      End If
   End Function
   'Raise an error without changing the transaction state.
   Public Sub RaiseError(ByVal module_migname As String,ByVal functionName As String,Optional ByVal strErrDesc As String = "",Optional ByVal lngErrNum As Integer = 0,Optional ByVal bWriteToLog As Boolean = False)
      'log the error to the event for later use.
      Dim strSource As String = ""

      If (strErrDesc = "") Then
         strErrDesc = Err.Description
      End If
      If (lngErrNum = 0) Then
         lngErrNum = Err.Number
      End If

      strSource = CreateSourceStack(module_migname,functionName)

      'add custom logging here... for now we log to application event log
      If (bWriteToLog) Then
         logError(lngErrNum,strSource,strErrDesc)
      End If

      Err.Raise(lngErrNum,strSource,strErrDesc)
   End Sub
   Public Function GetComputerName() as String '' UPGRADE_INFO: hand-coded
      return System.Environment.MachineName
   End Function
   Private Function CreateSourceStack(ByVal objectName As String,ByVal procName As String) As String
      ' Returns an source stack like message like:  "[FMStocks_DB.Account] VerifyUser [on AHI version 5.21.176]"
      ' add tab to each line in the  new source stack

      CreateSourceStack = Strings.Replace(Err.Source & Constants.vbNewLine & "[" & objectName & "]  " & procName & " [" & GetComputerName() & " version " & GetVersionNumber() & "]",Constants.vbNewLine,vbTab & Constants.vbNewLine)
   End Function
   'returns the current DLL version number
   Public Function GetVersionNumber() As String
      GetVersionNumber = gmRTL.Core.App.DefInstance.Major & "." & gmRTL.Core.App.DefInstance.Minor & "." & gmRTL.Core.App.DefInstance.Revision
   End Function
   'function takes a string with an '=' in it and returns the left part
   'GetKey("test=1") return test
   Public Function GetKey(ByVal strItem As String) As String
      GetKey = ""
      Dim i As Integer = 0

      i = Strings.InStr(1,strItem,"=",CompareMethod.Text)
      If (i > 0) Then
         GetKey = Strings.Left(strItem,i - 1)
      Else
         GetKey = ""
      End If
   End Function
   'function takes a string with an '=' in it and returns the right part
   'GetValue("test=1") return 1
   Public Function GetValue(ByVal strItem As String) As String
      GetValue = ""
      Dim i As Integer = 0

      i = Strings.InStr(1,strItem,"=",CompareMethod.Text)
      If (i > 0) Then
         GetValue = Mid(strItem,1 + i)
      Else
         GetValue = ""
      End If
   End Function
   'this procedure logs an error to the system application log
   Private Sub logError(ByVal errnum As Integer,ByVal source As String,ByVal description As String,Optional ByVal notes As String = "")

      gmRTL.Core.App.DefInstance.LogEvent(Constants.vbNewLine & "An error has occured in an FMStocks 2000 component." & Constants.vbNewLine & "Error Number:" & errnum & Constants.vbNewLine & Constants.vbNewLine & "Source: " & Constants.vbNewLine & source & Constants.vbNewLine & Constants.vbNewLine & "Description:" & Constants.vbNewLine & description & Constants.vbNewLine & Constants.vbNewLine & "Notes: " & Constants.vbNewLine & notes,System.Diagnostics.EventLogEntryType.Error) ' FMC1: App.LogEvent Optional Parameter

   End Sub
   'this procedure logs an error to the system application log
   Private Sub logEvent(ByVal source As String,ByVal description As String,Optional ByVal notes As String = "")

      gmRTL.Core.App.DefInstance.LogEvent(Constants.vbNewLine & "FMStocks 2000 Debug event." & Constants.vbNewLine & "Source: " & Constants.vbNewLine & source & Constants.vbNewLine & Constants.vbNewLine & "Description:" & Constants.vbNewLine & description & Constants.vbNewLine & Constants.vbNewLine & "Notes: " & Constants.vbNewLine & notes,System.Diagnostics.EventLogEntryType.Error) ' FMC1: App.LogEvent Optional Parameter

   End Sub
   Public Sub logDebugMSG(ByVal bWriteMSG As Boolean,ByVal strEvent As String,ByVal strSource As String,Optional ByVal strMSG As String = "")

      If (bWriteMSG) Then
         logEvent(strEvent & " -- " & strSource,"Debug event from " & strSource,strMSG)
      End If

   End Sub
   End Module
End Namespace
