Imports DigitalData.Modules.Database
Imports DigitalData.Modules.EDMI.API
Imports DigitalData.Modules.EDMI.API.Constants
Imports DigitalData.Modules.EDMI.API.EDMIServiceReference
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Language.Utils

Public Class DatabaseWithFallback
    Private ReadOnly _Logger As Logger
    Private ReadOnly _Client As Client
    Private ReadOnly _DatabaseEDM As MSSQLServer
    Private ReadOnly _DatabaseIDB As MSSQLServer

    Public Enum TableType
        TBIDB_ATTRIBUTE
    End Enum

    Public Class Table
        Public TableName As String
        Public SQLCommand As String
        Public DatabaseType As Constants.DatabaseType
    End Class

    Public Property Tables As New List(Of Table) From {
        New Table With {.DatabaseType = DatabaseType.IDB, .TableName = "TBIDB_ATTRIBUTE", .SQLCommand = "SELECT * FROM TBIDB_ATTRIBUTE"}
    }


    Public Sub New(LogConfig As LogConfig, Client As Client, DatabaseECM As MSSQLServer, DatabaseIDB As MSSQLServer)
        _Logger = LogConfig.GetLogger()
        _Client = Client
        _DatabaseEDM = DatabaseECM
        _DatabaseIDB = DatabaseIDB
    End Sub

    Public Function GetDatatable(pDataTableName As String, pFallbackSQL As String, pFallbackType As Constants.DatabaseType, Optional pFilterExpression As String = "", Optional pSortByColumn As String = "", Optional pForceFallback As Boolean = False) As DataTable
        Try
            Dim oResult As DataTable = Nothing

            If pForceFallback = False Then
                Dim oTableResult As TableResult

                Try
                    oTableResult = _Client.GetDatatableByName(pDataTableName, pFilterExpression, pSortByColumn)
                Catch ex As Exception
                    _Logger.Error(ex)
                    oTableResult = Nothing
                End Try

                If oTableResult Is Nothing OrElse oTableResult.OK = False Then
                    _Logger.Warn("Datatable [{0}] could not be fetched from AppServer Cache. Falling back to direct Database Access.")
                    Return GetDatatableFromDatabase(pFallbackSQL, pFallbackType)
                End If

                Return oTableResult.Table
            Else
                Return GetDatatableFromDatabase(pFallbackSQL, pFallbackType)
            End If
        Catch ex As Exception
            _Logger.Error(ex)
            Return Nothing
        End Try
    End Function

    Private Function GetDatatableFromDatabase(SQLCommand As String, DatabaseType As DatabaseType)
        Try
            Select Case DatabaseType
                Case DatabaseType.ECM
                    Return _DatabaseEDM.GetDatatable(SQLCommand)

                Case DatabaseType.IDB
                    Return _DatabaseIDB.GetDatatable(SQLCommand)

                Case Else
                    Return Nothing

            End Select
        Catch ex As Exception
            _Logger.Error(ex)
            Return Nothing

        End Try
    End Function
End Class

