Imports System.IO Imports System.Threading.Tasks Imports DevExpress.DocumentView Imports DigitalData.Modules.Base Imports DigitalData.Modules.Database Imports DigitalData.Modules.Logging Public MustInherit Class BaseModule Inherits BaseClass Implements ISync Friend ReadOnly Config As Config Friend ReadOnly Database As MSSQLServer Public MustOverride Property Name As String Implements ISync.Name Public MustOverride Property IsLoggedIn As Boolean Implements ISync.IsLoggedIn Public MustOverride Async Function Run() As Task Implements ISync.Run Public MustOverride Function Cleanup() As Task Implements ISync.Cleanup Public MustOverride Function TestConfigIsComplete() As Boolean Implements ISync.TestConfigIsComplete Public Event OnLogEntry As EventHandler(Of String) Implements ISync.OnLogEntry Public Event OnFileProcessed As EventHandler(Of String) Implements ISync.OnFileProcessed Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer, pConfig As Config) MyBase.New(pLogConfig) Database = pDatabase Config = pConfig End Sub Friend Async Function FetchDocIds() As Task(Of List(Of String)) If Config.SQLQueryFetch = "" Then Logger.Warn("Fetch Query is not configured. Exiting.") Return Nothing End If Dim oTable As DataTable = Await Database.GetDatatableAsync(Config.SQLQueryFetch) If oTable Is Nothing Then Logger.Warn("Error while fetching DocIds. Exiting") Return Nothing End If Dim oDocIds = oTable.Rows. Cast(Of DataRow). Select(Function(r) r.Item(0).ToString()). ToList() AddInfoEntry("Found [{0}] files.", oDocIds.Count) Return oDocIds End Function Friend Sub EnsureOutputDirectoryExists() Dim oOutputDirectory As String = Config.OutputDirectory If Directory.Exists(oOutputDirectory) = False Then Throw New DirectoryNotFoundException($"Directory '{oOutputDirectory}' does not exist.") End If End Sub Friend Function ConvertFilenameToSlug(pFileName As String) As String Dim oName = Path.GetFileNameWithoutExtension(pFileName) Dim oExtension = Path.GetExtension(pFileName) Return StringEx.ConvertTextToSlug(oName) & oExtension End Function Friend Function GetFinalFileName(pFileName As String, pId As String) As String Dim oExtension = Path.GetExtension(pFileName) Return pId & oExtension End Function Friend Function GetFinalFilePath(pFileName As String) As String If Config.AddDateSubDirectory Then Dim oSubPath = FileEx.CreateDateDirectory(Config.OutputDirectory) If oSubPath Is Nothing Then Throw New ApplicationException("Output sub path could not be created!") End If Logger.Debug("Subdirectory [{0}] created.", oSubPath) Return Path.Combine(oSubPath, pFileName) Else Return Path.Combine(Config.OutputDirectory, pFileName) End If End Function Friend Function CopyFileToOutputPath(pFileContents As Byte(), pFilePath As String) As Boolean Try Using oStream As New MemoryStream(pFileContents) Using oWriter As New FileStream(pFilePath, FileMode.Create) oStream.CopyTo(oWriter) Logger.Debug("File copied to document path [{0}]", pFilePath) End Using End Using Return True Catch ex As Exception Logger.Error(ex) Return False End Try End Function Public Function TestConfigIsCompleteBase() As Boolean Dim oComplete = True If Config.ConnectionString = String.Empty Then AddWarnEntry("Configuration for 'ConnectionString' is empty.") oComplete = False End If If Config.SQLQueryFetch = String.Empty Then AddWarnEntry("Configuration for 'SQLQueryFetch' is empty.") oComplete = False End If If Config.SQLQueryExport = String.Empty Then AddWarnEntry("Configuration for 'SQLQueryExport' is empty.") oComplete = False End If If Config.OutputDirectory = String.Empty Then AddWarnEntry("Configuration for 'OutputDirectory' is empty.") oComplete = False End If Return oComplete End Function Friend Sub AddInfoEntry(pMessage As String, ParamArray pArgs As Object()) RaiseEvent OnLogEntry(Me, String.Format(pMessage, pArgs)) End Sub Friend Sub AddWarnEntry(pMessage As String, ParamArray pArgs As Object()) RaiseEvent OnLogEntry(Me, String.Format(pMessage, pArgs)) End Sub Friend Sub AddDivider() RaiseEvent OnLogEntry(Me, "---") End Sub Friend Sub RaiseFileProcessed(pFilePath As String) RaiseEvent OnFileProcessed(Me, pFilePath) End Sub End Class