Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Jobs
Imports DigitalData.Modules.Logging
Imports Quartz

Public Class ADJob
    Implements Quartz.IJob

    Public Function Execute(context As IJobExecutionContext) As Task Implements Quartz.IJob.Execute
        Dim oJobData = context.MergedJobDataMap
        Dim oLogConfig As LogConfig = oJobData.Item("LogConfig")
        Dim oFirebird As Firebird = oJobData.Item("Firebird")
        Dim oArgs As Dictionary(Of String, String) = oJobData.Item("Args")
        Dim oADJobArgs = New ADSyncArgs()

        If oArgs.ContainsKey("RootPath") Then
            oADJobArgs.RootPath = oArgs.Item("RootPath")
        End If

        If oArgs.ContainsKey("UserFilter") Then
            oADJobArgs.UserFilter = oArgs.Item("UserFilter")
        End If

        If oArgs.ContainsKey("GroupFilter") Then
            oADJobArgs.GroupFilter = oArgs.Item("GroupFilter")
        End If

        If oArgs.ContainsKey("DisableFirebird") AndAlso oArgs.Item("DisableFirebird") = "True" Then
            oFirebird = Nothing
        End If

        Dim oMSSQL As MSSQLServer = oJobData.Item("MSSQL")
        If oArgs.ContainsKey("DisableMSSQL") AndAlso oArgs.Item("DisableMSSQL") = "True" Then
            oMSSQL = Nothing
        End If

        Dim oADSyncJob As New ADSyncJob(oLogConfig, oFirebird, oMSSQL)
        oADSyncJob.Start(oADJobArgs)

        Return Task.FromResult(True)
    End Function
End Class