Imports DigitalData.Modules.Logging

Namespace ClipboardWatcher
    Public Class ClassProfileLoader
        Private ReadOnly LogConfig As LogConfig
        Private ReadOnly Logger As Logger
        Private Database As ClassDataASorDB

        Public Sub New(LogConfig As LogConfig, AppServerOrDB As ClassDataASorDB)
            Me.LogConfig = LogConfig
            Me.Logger = LogConfig.GetLogger()
            Me.Database = AppServerOrDB
        End Sub

        Public Function LoadProfiles() As Boolean
            Try
                Dim oUserId = My.Application.User.UserId
                Dim oWhereClause = $"T1.USER_ID = {oUserId} OR GROUP_ID IN (SELECT DISTINCT GUID FROM TBDD_GROUPS WHERE GUID IN (SELECT GROUP_ID FROM TBDD_GROUPS_USER WHERE USER_ID = {oUserId}))"

                Dim oProfileSQL As String = $"SELECT DISTINCT GUID, NAME,REGEX_EXPRESSION,COMMENT,PROC_NAME,PROFILE_TYPE FROM VWCW_USER_PROFILE T1 WHERE {oWhereClause}"
                Dim oProcessSQL As String = $"SELECT DISTINCT T.GUID, T.PROFILE_ID,T.PROC_NAME FROM TBCW_PROFILE_PROCESS T, VWCW_USER_PROFILE T1 WHERE T.PROFILE_ID = T1.GUID AND ({oWhereClause})"
                Dim oWindowSQL As String = $"SELECT DISTINCT T.* FROM VWCW_PROFILE_REL_WINDOW T, VWCW_USER_PROFILE T1 WHERE T.PROFILE_ID = T1.GUID AND ({oWhereClause})"
                Dim oControlSQL As String = $"SELECT DISTINCT T.* FROM VWCW_PROFILE_REL_CONTROL T, VWCW_USER_PROFILE T1 WHERE T.PROFILE_ID = T1.GUID AND ({oWhereClause})"

                Dim oUserProfiles = Database.GetDatatable("DD_ECM", oProfileSQL, "VWCW_USER_PROFILE", "", "", "DB")
                Dim oProfileProcesses = Database.GetDatatable("DD_ECM", oProcessSQL, "TBCW_PROFILE_PROCESS", "", "", "DB")
                Dim oProfileWindows = Database.GetDatatable("DD_ECM", oWindowSQL, "VWCW_PROFILE_REL_WINDOW", "", "", "DB")
                Dim oProfileControls = Database.GetDatatable("DD_ECM", oControlSQL, "VWCW_PROFILE_REL_CONTROL", "", "", "DB")

                My.Application.ClipboardWatcher.UserProfiles = oUserProfiles
                My.Application.ClipboardWatcher.ProfileProcesses = oProfileProcesses
                My.Application.ClipboardWatcher.ProfileWindows = oProfileWindows
                My.Application.ClipboardWatcher.ProfileControls = oProfileControls
                My.Application.ClipboardWatcher.MonitoringActive = True

                Return True

            Catch ex As Exception
                Logger.Error(ex)

                Return False

            End Try
        End Function
    End Class

End Namespace

