WIDIG/WIDigConsoleApp/ModuleMain.vb

396 lines
17 KiB
VB.net

Imports System
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Windream
Imports DigitalData.Modules.Config
Imports System.IO
Imports System.Text.RegularExpressions
Imports DigitalData.Modules.Database
Module ModuleMain
Private _ArgumentLength As Integer
Public _database As MSSQLServer
Public oRegExArg As String
Public oRegex As New Regex("([\s\S]+)\={([\s\S]+)}")
Public Const CODE_SUCCESS = 0
Public Const CODE_ERROR = 1
Public Const MODE_OVERWRITE = "IMPO"
Public Const MODE_VERSION = "IMPV"
Public Const MODE_NACHINDEXIERUNG = "NI"
Public Const PARAM_SOURCE = "-Source@"
Public Const PARAM_MODE = "-Mode@"
Public Const PARAM_TARGET = "-Target@"
Public Const PARAM_WMTO = "-WMOT@"
Public Const PARAM_INDEX = "-index@"
Public Function Main(CommandLineArguments As String()) As Integer
Dim oReturnResult As Integer
Try
oReturnResult = CODE_SUCCESS
' Console.WriteLine("Starting up WIDig...")
Dim opath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Dim oLogConfig As New LogConfig(LogConfig.PathType.AppData, Nothing, "Console", "Digital Data", "WIDig")
LOGCONFIG = oLogConfig
LOGGER = LOGCONFIG.GetLogger
InitUserConfig()
LOGCONFIG.Debug = CONFIG.Config.LOG_DEBUG
LOGGER = LOGCONFIG.GetLogger
Dim oUserPW = GetUserPWPlain()
System.Console.WriteLine($"Starting up WIDig...")
If Connect2Windream(oUserPW) = False Then
Throw New ApplicationException("Could not initialize windream")
End If
System.Console.WriteLine($"Windream initialized!")
If InitDatabase() = False Then
Throw New ApplicationException("Could not initialize DB")
End If
System.Console.WriteLine($"Database initialized!")
If Load_DB_DAta() = False Then
Throw New ApplicationException("Could not load Regex from Database")
End If
System.Console.WriteLine($"Regex loaded from Database!")
If ParseArgs(CommandLineArguments) = False Then
Throw New ApplicationException("Could not parse command line arguments")
End If
System.Console.WriteLine($"Command line arguments parsed!")
If StreamORIndexFile() = False Then
Throw New ApplicationException("Could not stream or index file")
End If
System.Console.WriteLine($"File indexed or streamed!")
' Brauchen Sie das überhaupt?
If oErrorParse = True Or oErrorImport = True Then
Throw New ApplicationException(oErrorMessage)
End If
Return CODE_SUCCESS
Catch ex As Exception
LOGGER.Warn("Could not process file because of an error: {0}", oErrorMessage)
LOGGER.Warn("Error at Parse Stage: [{0}]", oErrorParse)
LOGGER.Warn("Error at Import Stage: [{0}]", oErrorImport)
LOGGER.Error(ex)
Return CODE_ERROR
End Try
End Function
Public Sub InitUserConfig()
Dim oProgramDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Digital Data", "WIDig")
Dim oUserAppDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Digital Data", "WIDig")
CONFIG = New ConfigManager(Of ClassConfig)(LOGCONFIG, oUserAppDataPath, oProgramDataPath)
End Sub
Public Function InitDatabase() As Boolean
If CONFIG.Config.ConnectionString.Length = 0 Then
Return False
End If
Try
_database = New MSSQLServer(LOGCONFIG, CONFIG.Config.ConnectionString)
If _database.DBInitialized = True Then
Return True
Else
Return False
End If
Catch ex As Exception
LOGGER.Error(ex)
Return False
End Try
End Function
Public Function Load_DB_DAta()
Try
Dim oSql = "SELECT [REGEX] FROM [TBDD_FUNCTION_REGEX] WHERE FUNCTION_NAME = 'WM-INDEXER-INDEX_GROUP'"
oRegExArg = _database.GetScalarValue(oSql, 120)
If oRegExArg.Length = 0 Then
oRegExArg = 0
End If
Return True
Catch ex As Exception
LOGGER.Error(ex)
Return False
End Try
End Function
Private Function Connect2Windream(oPW As String)
Try
WINDREAM = New Windream(LOGCONFIG, False, CONFIG.Config.WMDrive, CONFIG.Config.WMRelPath, True, CONFIG.Config.WMServer, CONFIG.Config.WMUsername, oPW, CONFIG.Config.Domain)
If Not IsNothing(WINDREAM) Then
If WINDREAM.SessionLoggedin = True Then
LOGGER.Debug("windream initialisiert")
Return True
End If
End If
Return False
Catch ex As Exception
LOGGER.Warn("CHECKING WMConnectivity: " & ex.Message)
Return False
End Try
End Function
Private Function GetUserPWPlain()
Try
Dim oPassword As String
Dim oEncryption As New ClassEncryption("!35452didalog=")
If CONFIG.Config.WMUserPW = String.Empty Then
oPassword = ""
Else
oPassword = oEncryption.DecryptData(CONFIG.Config.WMUserPW)
End If
Return oPassword
Catch ex As Exception
LOGGER.Warn("Error in GetUserPWPlain - the password [" & CONFIG.Config.WMUserPW & "] could not be decrypted", False)
Return String.Empty
End Try
End Function
Public Function ParseArgs(pArguments As String(), Optional pTest As Boolean = False)
Dim oINDEXInfoStarted As Boolean = False
Dim oINDEXInfotemp As String = ""
Try
If pArguments.Length <= 3 Then
_ArgumentLength = pArguments.Length
LOGGER.Warn($"Insufficient number of arguments [{pArguments.Length}]!")
System.Console.WriteLine($"Insufficient number of arguments - {Now.ToString}")
oErrorParse = True
Return False
End If
Dim oCount As Integer = 0
For Each oArg As String In pArguments
LOGGER.Debug($"[{oCount}] {oArg}")
oArg = oArg.Replace("""", "")
If oArg.StartsWith(PARAM_SOURCE) Then
oSourceFile = oArg.Replace(PARAM_SOURCE, "")
If IsNumeric(oSourceFile) Then
LOGGER.Info($"SourceFile seems to be a DocID [{oSourceFile}]")
Dim oSQL = $"SELECT [dbo].[FNDD_GET_WINDREAM_FILE_PATH] ({oSourceFile})"
oSourceFile = _database.GetScalarValue(oSQL)
End If
If System.IO.File.Exists(oSourceFile) = False Then
LOGGER.Warn($"Parser@Sourcefile - File [{oSourceFile}] is not existing!")
oErrorMessage &= vbNewLine & $"Parser@Sourcefile - File [{oSourceFile}] is not existing!"
oErrorParse = True
Return False
End If
ElseIf oArg.StartsWith(PARAM_MODE) Then
oMode = oArg.Replace(PARAM_MODE, "").ToUpper
ElseIf oArg.StartsWith(PARAM_TARGET) Then
oTargetPath = oArg.Replace(PARAM_TARGET, "")
If IsNumeric(oTargetPath) Then
LOGGER.Info($"Target seems to be a DocID [{oTargetPath}]")
Dim oSQL = $"SELECT [dbo].[FNDD_GET_WINDREAM_FILE_PATH] ({oTargetPath})"
oSourceFile = _database.GetScalarValue(oSQL)
End If
Dim oWMFolder = System.IO.Path.GetDirectoryName(oTargetPath)
Dim oWindowsPath = oTargetPath
oExtension = Path.GetExtension(oWindowsPath)
Dim oNormalizePath = WINDREAM.GetNormalizedPath(oTargetPath)
If WINDREAM.TestFileExists(oTargetPath) = False Then
LOGGER.Info($"WMFile [{oTargetPath}] not existing!")
End If
If oMode = MODE_VERSION Then
Dim oWMCheckPath = WINDREAM.VersionWMFilename(oTargetPath, System.IO.Path.GetExtension(oTargetPath))
If oNormalizePath.ToUpper <> oWMCheckPath.ToString.ToUpper Then
LOGGER.Info($"Target [{oNormalizePath}] already existed!! - NewWMFilename [{oWMCheckPath}]")
oTargetPath = oWMCheckPath
End If
End If
'Checks and creates the path if necessary
WINDREAM.NewFolder(oTargetPath, oExtension)
ElseIf oArg.StartsWith(PARAM_WMTO) Then
oWMObjecttype = oArg.Replace(PARAM_WMTO, "")
Dim oexists As Boolean = False
Dim myWMOTypes = WINDREAM.ObjectTypes
For Each otype As String In myWMOTypes
If oWMObjecttype = otype Then
oexists = True
Exit For
End If
Next
If oexists = False Then
LOGGER.Info($"WMObjekttype [{oWMObjecttype}] not existing!!")
oErrorMessage &= vbNewLine & $"WMObjekttype [{oWMObjecttype}] not existing!!"
Return False
oErrorParse = True
Else
WMIndices = WINDREAM.GetIndiciesByObjecttype(oWMObjecttype)
End If
ElseIf oArg.StartsWith(PARAM_INDEX) Then
oINDEXInfotemp = oArg
oINDEXInfoStarted = True
oINDEXInfotemp = oINDEXInfotemp.Replace(PARAM_INDEX, "")
Else
If oINDEXInfoStarted Then
oINDEXInfotemp &= " " & oArg
End If
End If
oCount += 1
Next
LOGGER.Debug("INDEXInfoTemp: [{0}]", oINDEXInfotemp)
Dim oIndexparts As List(Of String) = oINDEXInfotemp.
Split(New String() {"#~#"}, StringSplitOptions.RemoveEmptyEntries).
ToList()
For Each oIndexPart As String In oIndexparts
LOGGER.Debug(oIndexPart)
Next
LOGGER.Info($" [{oIndexparts.Count}] Indices parsed")
oIndexArr = oIndexparts
Return True
Catch ex As Exception
LOGGER.Error(ex)
LOGGER.Warn("Error in ParseArgs:" & vbNewLine & ex.Message)
oErrorMessage &= vbNewLine & "Error in ParseArgs:" & vbNewLine & ex.Message
oErrorParse = True
System.Console.WriteLine($"Error in ParseArgs - {Now.ToString}")
Return False
End Try
End Function
Public Function StreamORIndexFile()
Try
Dim oResult As Boolean = False
If oMode = MODE_VERSION Then
oResult = WINDREAM.NewFileStream(oSourceFile, oTargetPath)
ElseIf oMode = MODE_OVERWRITE Then
Dim oDeleted = WINDREAM.RemoveFile(oTargetPath)
If oDeleted = True Then
oResult = WINDREAM.NewFileStream(oSourceFile, oTargetPath)
Else
LOGGER.Warn($"Mode ImportOverwrite is active - but WMFile could not be deleted!!")
End If
ElseIf oMode = MODE_NACHINDEXIERUNG Then
oResult = True
End If
If oResult = True Then
If oMode <> MODE_NACHINDEXIERUNG Then
LOGGER.Info($"File successfully streamed to windream [{oTargetPath}]! Now indexing...")
End If
For Each oIndex As String In oIndexArr
Dim oMatch As Match = oRegex.Match(oIndex)
If oMatch.Success Then
Dim oIndexName = oMatch.Groups(1)?.Value
Dim oIndexValues = oMatch.Groups.Item(2)?.Value
Dim oSplitValue = New String() {"~#~"}
Dim oIndexValueArray = oIndexValues.Split(oSplitValue, StringSplitOptions.RemoveEmptyEntries)
Dim oIndexResult = False
LOGGER.Info("Setting Index [{0}] to [{1}].", oIndexName, oIndexValues)
If WINDREAM.TestIndexNameIsVectorIndex(oIndexName) Then
Dim oCombinedIndexValues = WINDREAM.GetVectorData(oTargetPath, oIndexName, oIndexValueArray, False)
oIndexResult = WINDREAM.SetFileIndex(oTargetPath, oIndexName, oCombinedIndexValues.ToList, oWMObjecttype)
Else
oIndexResult = WINDREAM.SetFileIndex(oTargetPath, oIndexName, oIndexValueArray(0), oWMObjecttype)
End If
oResult = oIndexResult
Else
oResult = False
End If
If oResult = False Then
LOGGER.Warn("Indexing failed. Exiting.")
Exit For
End If
Next
End If
If oResult = True Then
LOGGER.Info("## All Tasks finished ##")
oErrorImport = False
End If
'If oResult = True Then
' If oMode <> MODE_NACHINDEXIERUNG Then
' LOGGER.Info($"File successfully streamed to windream [{oTargetPath}]! Now indexing...")
' End If
' For Each oIndex2 As String In oIndexArr
' Dim oIndexInfo() = oIndex2.Split("={")
' Dim oIndexName = oIndexInfo(0)
' Dim oIndexvalue
' Dim r As Regex = New Regex(oRegExArg, RegexOptions.IgnoreCase)
' ' ' Match the regular expression pattern against a text string.
' Dim m As Match = r.Match(oIndex2)
' Do While m.Success
' ' oClearedBodyText = oClearedBodyText.Replace(m.Value, "")
' 'Dim g As Group = m.Groups(1)
' Dim g1 As Group = m.Groups(2)
' Dim g2 As Group = m.Groups(3)
' If Not IsNothing(g2.Value) Then
' oIndexvalue = g2.Value
' Console.WriteLine($"Indexvalue: {oIndexvalue}")
' End If
' If Len(oIndexvalue) > 0 Then
' If WMIndices.Contains(oIndexName) Then
' LOGGER.Info($"Setting Index: oIndexName [{oIndexName}] - oIndexvalue [{oIndexvalue}]")
' 'DEBUG
' oIndexvalue = New List(Of String) From {"Wert 1", "Wert 2", "wert 3"}
' 'DEBUG
' If WINDREAM.SetFileIndex(oTargetPath, oIndexName, oIndexvalue, oWMObjecttype) = False Then
' LOGGER.Info($"Index could not be set...")
' If WINDREAM.RemoveFile(oTargetPath) = True Then
' LOGGER.Info($"File deleted after error!")
' End If
' oResult = False
' Exit For
' End If
' Else
' LOGGER.Warn($"Transmitted index with name [{oIndexName}] is not existing in WM Objecttype!")
' If WINDREAM.RemoveFile(oTargetPath) = True Then
' LOGGER.Info($"File deleted after error!")
' End If
' oResult = False
' Exit For
' End If
' End If
' m = m.NextMatch()
' Loop
' Next
' If oResult = True Then
' LOGGER.Info("## All Tasks finished ##")
' oErrorImport = False
' End If
'End If
Return oResult
Catch ex As Exception
LOGGER.Warn($"Unexpected Error in StreamORIndexFile: {ex.Message}")
LOGGER.Error(ex)
Return False
End Try
End Function
End Module