Imports EDSL.Entities
Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports System.Configuration
Imports System.Data.Common
Public Class dbMainUser
  Private Shared strMainConnString As String = "MainConnString"
  Public Function GetAll(ByVal strWHEREclause As String, ByVal strORDERBYclause As String) As List(Of entMainLoginDetails)
  Dim objLogins As New List(Of entMainLoginDetails)
  Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
  Try
    Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminLoginGetAll")
    db.AddInParameter(dbCommand, "WHEREclause", DbType.String, strWHEREclause)
    db.AddInParameter(dbCommand, "ORDERBYclause", DbType.String, strORDERBYclause)
    Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
    While oRecordset.Read()
      objLogins.Add(New entMainLoginDetails() With { _
      .LoginID = Convert.ToInt32(oRecordset("LoginID")), _
      .LoginName = Convert.ToString(oRecordset("LoginName")), _
      .Firstname = Convert.ToString(oRecordset("LoginFirstname")), _
      .Surname = Convert.ToString(oRecordset("LoginSurname")), _
      .Active = Convert.ToString(oRecordset("Active")), _
      .LoginType = Convert.ToString(oRecordset("LoginType")), _
      .CreatedDate = Convert.ToString(oRecordset("CreatedDate")), _
      .LastLogin = Convert.ToString(oRecordset("LoginLastLogin")), _
      .TextDisplay = Convert.ToString(oRecordset("TextDisplay")) _
      })
    End While
    oRecordset.Close()
    End Using
  Catch ex As Exception
    Throw ex
  End Try
  Return objLogins
  End Function
  Public Function GetItem(ByVal intLoginID As Integer) As entMainLoginDetails
  Dim objLogin As New entMainLoginDetails
  Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)

  Try
    Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminLoginGet")
    db.AddInParameter(dbCommand, "LoginID", DbType.Int16, intLoginID)
    Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
    While oRecordset.Read()
      With objLogin
      .LoginID = Convert.ToInt32(oRecordset("LoginID"))
      .LoginName = Convert.ToString(oRecordset("LoginName"))
      .Firstname = Convert.ToString(oRecordset("LoginFirstname"))
      .Surname = Convert.ToString(oRecordset("LoginSurname"))
      .Active = Convert.ToString(oRecordset("Active"))
      .LoginType = Convert.ToString(oRecordset("LoginType"))
      .CreatedDate = Convert.ToString(oRecordset("CreatedDate"))
      .LastLogin = Convert.ToString(oRecordset("LoginLastLogin"))
      End With
    End While
    oRecordset.Close()
    End Using
    Return objLogin
  Catch ex As Exception
    Throw ex
    Return Nothing
  End Try
  End Function
  Public Function DeleteItem(ByVal intLoginID As Integer, ByVal intUserID As Integer) As Boolean
  Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
  Try
    Using dbCommand As DbCommand = db.GetStoredProcCommand("MainAdminLoginDelete")
    db.AddInParameter(dbCommand, "LoginID", DbType.Int16, intLoginID)
    db.AddInParameter(dbCommand, "UserID", DbType.Int16, intUserID)
    db.ExecuteNonQuery(dbCommand)
    End Using
    Return True
  Catch ex As Exception
    Return False
  End Try
  End Function
  Public Function SaveItem(ByVal intLoginID As Integer, ByVal strLoginName As String, ByVal strLoginFirstname As String, ByVal strLoginSurname As String, ByVal intLoginType As Integer, ByVal intActive As Integer, ByVal intUserID As Integer) As Integer
    Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
    Dim strSPName As String = ""
    Try
      If intLoginID = 0 Then
      If SaveDoesLoginExist(strLoginName) = True Then
        Return 2
      End If
      strSPName = "MainAdminLoginINSERT"
      Else
      strSPName = "MainAdminLoginUPDATE"
      End If
      Using dbCommand As DbCommand = db.GetStoredProcCommand(strSPName)
      If intLoginID <> 0 Then
        db.AddInParameter(dbCommand, "LoginID", DbType.Int16, intLoginID)
      End If
      db.AddInParameter(dbCommand, "LoginName", DbType.String, strLoginName)
      db.AddInParameter(dbCommand, "LoginFirstname", DbType.String, strLoginFirstname)
      db.AddInParameter(dbCommand, "LoginSurname", DbType.String, strLoginSurname)
      db.AddInParameter(dbCommand, "LoginType", DbType.Int16, intLoginType)
      db.AddInParameter(dbCommand, "Active", DbType.Int16, intActive)
      db.AddInParameter(dbCommand, "UserID", DbType.Int16, intUserID)
      db.ExecuteNonQuery(dbCommand)
      End Using
      Return 0
    Catch ex As Exception
      Return 1
    End Try
  End Function
  Public Function SaveItemPassword(ByVal intLoginID As Integer, ByVal strLoginPWD As String) As Integer
    Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
    Dim strSPName As String = ""
    Try
      strSPName = "MainAdminLoginUPDATEPassword"
      Using dbCommand As DbCommand = db.GetStoredProcCommand(strSPName)
        db.AddInParameter(dbCommand, "LoginID", DbType.Int16, intLoginID)
        db.AddInParameter(dbCommand, "LoginPWD", DbType.String, strLoginPWD)
        db.ExecuteNonQuery(dbCommand)
      End Using
      Return 0
    Catch ex As Exception
      Return 1
    End Try
  End Function
  Private Function SaveDoesLoginExist(ByVal strLoginName As String) As Boolean
  Dim objLogins As New List(Of entMainLoginDetails)
  Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
  Try
    Using dbCommand As DbCommand = db.GetStoredProcCommand("LoginExists")
    db.AddInParameter(dbCommand, "LoginName", DbType.String, strLoginName)
    Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)

    Return DirectCast(oRecordset, System.Data.SqlClient.SqlDataReader).HasRows
    oRecordset.Close()
    End Using
  Catch ex As Exception
    Return True
  End Try
  End Function
  Public Function LoginCheck(ByVal strLoginName As String, ByVal strLoginPWD As String) As entMainLoginDetails
  Dim objLogin As New entMainLoginDetails
  Dim db As Database = DatabaseFactory.CreateDatabase(strMainConnString)
  Try
    Using dbCommand As DbCommand = db.GetStoredProcCommand("LoginCheck")
    db.AddInParameter(dbCommand, "LoginName", DbType.String, strLoginName)
    db.AddInParameter(dbCommand, "LoginPWD", DbType.String, strLoginPWD)
    Dim oRecordset As IDataReader = db.ExecuteReader(dbCommand)
    While oRecordset.Read()
      With objLogin
      .LoginID = Convert.ToInt32(oRecordset("LoginID"))
      .LoginName = Convert.ToString(oRecordset("LoginName"))
      .LoginType = Convert.ToString(oRecordset("LoginType"))
      .Firstname = Convert.ToString(oRecordset("LoginFirstname"))
      .Surname = Convert.ToString(oRecordset("LoginSurname"))
      End With
    End While
    oRecordset.Close()
    End Using
  Catch ex As Exception
    Throw ex
  End Try
  Return objLogin
  End Function
End Class
