' ------------------------------------------------------------
' Code generated automatically by Code Architects' VB Migration Partner
' ------------------------------------------------------------

Option Strict Off      ' Code migrated from VB6 has Option Strict disabled by default

<System.Runtime.InteropServices.ProgID("FMStocks_DB.Account")> _ 
<Transaction(TransactionOption.Supported)> _
Public Class Account
	Inherits ServicedComponent

	#Region "Constructor"
	
	'A public default constructor
	Public Sub New()
		Class_Initialize_VB6()
		' Add initialization code here
	End Sub
	
	#End Region
	
	#Region "Static constructor"
	
	' This static constructor ensures that the VB6 support library be initialized before using current class.
	Shared Sub New()
		EnsureVB6LibraryInitialization()
	End Sub
	
	#End Region

	Private Const m_modName As String = "FMStocks_DB.Account"
	Private m_dbh As DBHelper
	
	Private Sub Class_Initialize_VB6()
		'must call CreateObject or COM+ will
		'not call IObjectConstructor inferface that DBHelper needs
		m_dbh = CreateObject6("FMStocks_DB.DBHelper")
	End Sub

	Private Sub Class_Terminate_VB6()
		m_dbh = Nothing
	End Sub

	Public Function Add(ByVal FirstName As String, ByVal LastName As String, ByVal password As String, ByVal Email As String, ByVal Balance As Decimal) As Integer
		
		On Error GoTo errorHandler 
		
		Add = m_dbh.RunSPReturnInteger("Account_Add", Array6(mp("@FirstName", ADODB.DataTypeEnum.adVarChar, 30, FirstName), mp("@LastName", ADODB.DataTypeEnum.adVarChar, 30, LastName), mp("@Password", ADODB.DataTypeEnum.adVarChar, 15, password), mp("@EMail", ADODB.DataTypeEnum.adVarChar, 50, Email), mp("@Balance", ADODB.DataTypeEnum.adCurrency, 8, Balance)))
		
		VB6MtsMethods.GetObjectContext().SetComplete
		Exit Function
		
errorHandler:
		VB6MtsMethods.GetObjectContext().SetAbort
		RaiseError(m_modName, "Add")
	End Function

	Public Function Summary(ByVal AccountID As Integer) As ADODB.Recordset
		On Error GoTo errorHandler 
		
		Summary = m_dbh.RunSPReturnRS("Account_Summary", Array6(mp("@AccountID", ADODB.DataTypeEnum.adInteger, 4, AccountID)))
		
		VB6MtsMethods.GetObjectContext().SetComplete
		Exit Function
		
errorHandler:
		VB6MtsMethods.GetObjectContext().SetAbort
		RaiseError(m_modName, "Summary(" & AccountID & ")")
	End Function

	Public Function VerifyUser(ByVal Email As String, ByVal password As String, ByRef AccountID As Object, ByRef FullName As Object) As Boolean
		
		On Error GoTo errorHandler 
		Dim strUserInfo As String = ""
		
		' We couldn't use the generic RunSP* functions because this stored proc has 2 output parameters
		' The following code is a modified version of RunSPWithString to support 2 outputs and 1 return value
		
		Dim cmd As ADODB.Command = New ADODB.Command()
		
		'Run the procedure
		With cmd
			.ActiveConnection.ConnectionString = m_dbh.GetConnectionString()
			.CommandText = "Account_VerifyLogin"
			.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
			
			'set Params
			.Parameters.Append(.CreateParameter("@retval", ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamOutput))
			.Parameters.Append(.CreateParameter("@EMail", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamInput, 50, Email))
			.Parameters.Append(.CreateParameter("@Password", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamInput, 50, password))
			.Parameters.Append(.CreateParameter("@AccountID", ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamOutput, 4))
			.Parameters.Append(.CreateParameter("@FullName", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamOutput, 50))
			
			'execute Stored Proc
			.Execute(, , ADODB.ExecuteOptionEnum.adExecuteNoRecords)
			
			If .Parameters("@retval").Value = 0 Then 
				VerifyUser = False
			Else
				AccountID = .Parameters(3).Value
				FullName = .Parameters(4).Value
				VerifyUser = True
			End If
		End With
		
		cmd = Nothing
		VB6MtsMethods.GetObjectContext().SetComplete
		
		Exit Function
		
errorHandler:
		cmd = Nothing
		VB6MtsMethods.GetObjectContext().SetAbort
		RaiseError(m_modName, "VerifyUser(" & Email & ",...)")
	End Function

	Public Sub GetAccountInfo(ByVal AccountID As Integer, ByRef FullName As Object, ByRef Email As Object, ByRef Balance As Object)
		On Error GoTo errorHandler 
		
		Dim rs As ADODB.Recordset = m_dbh.RunSPReturnRS("Account_GetInfo", Array6(mp("AccountID", ADODB.DataTypeEnum.adInteger, 4, AccountID)))

		FullName = rs.Fields("Firstname").Value & " " & rs.Fields("Lastname").Value
		Email = rs.Fields("email").Value
		Balance = rs.Fields("Balance").Value
		
		rs = Nothing
		Exit Sub
		
errorHandler:
		rs = Nothing
		VB6MtsMethods.GetObjectContext().SetAbort
		RaiseError(m_modName, "GetAccountInfo(" & AccountID & ")")
	End Sub

	Protected Overrides Sub Dispose(ByVal disposing As Boolean)
		Try
				Class_Terminate_VB6()
		Finally
			MyBase.Dispose(disposing)
		End Try
	End Sub

End Class
