Imports System.IO Imports System.Threading.Tasks 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 Const DIVIDER_TEXT = "-------------------------------------" Public Enum LogLevel Info Warn [Error] End Enum 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 Tuple(Of String, LogLevel)) Implements ISync.OnLogEntry Public Event OnFileProcessed As EventHandler(Of String) Implements ISync.OnFileProcessed Public Event OnFileError As EventHandler(Of String) Implements ISync.OnFileError Private DirectoryGuid As String = Guid.NewGuid.ToString() Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer, pConfig As Config) MyBase.New(pLogConfig) Database = pDatabase Config = pConfig End Sub Friend Sub RefreshDirectoryGuid() DirectoryGuid = Guid.NewGuid.ToString() 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 Dim oFinalDirectoryPath = Config.OutputDirectory If Config.AddDateSubDirectory = True Then Dim oSubPath = FileEx.CreateDateDirectory(oFinalDirectoryPath) If oSubPath Is Nothing Then Throw New ApplicationException("Output sub path could not be created!") End If Logger.Debug("Date Subdirectory [{0}] created.", oSubPath) oFinalDirectoryPath = oSubPath End If If Config.AddImportSubDirectory = True Then Dim oSubPath = Path.Combine(oFinalDirectoryPath, DirectoryGuid) Try Directory.CreateDirectory(oSubPath) Catch ex As Exception Throw New ApplicationException("Output subpath could not be created!") End Try Logger.Debug("Import Subdirectory [{0}] created", oSubPath) oFinalDirectoryPath = oSubPath End If Return Path.Combine(oFinalDirectoryPath, pFileName) 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, New Tuple(Of String, LogLevel)(String.Format(pMessage, pArgs), LogLevel.Info)) End Sub Friend Sub AddWarnEntry(pMessage As String, ParamArray pArgs As Object()) RaiseEvent OnLogEntry(Me, New Tuple(Of String, LogLevel)(String.Format(pMessage, pArgs), LogLevel.Warn)) End Sub Friend Sub AddErrorEntry(pMessage As String, ParamArray pArgs As Object()) RaiseEvent OnLogEntry(Me, New Tuple(Of String, LogLevel)(String.Format(pMessage, pArgs), LogLevel.Error)) End Sub Friend Sub AddDivider() RaiseEvent OnLogEntry(Me, New Tuple(Of String, LogLevel)(DIVIDER_TEXT, LogLevel.Info)) End Sub Friend Sub RaiseFileProcessed(pFilePath As String) RaiseEvent OnFileProcessed(Me, pFilePath) End Sub Friend Sub RaiseFileError(pFilePath As String) RaiseEvent OnFileError(Me, pFilePath) End Sub End Class