Jonathan Jenne 77621193f2 Zooflow: WIP
2021-12-01 16:22:51 +01:00

227 lines
8.8 KiB
VB.net

Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Patterns
Imports DigitalData.Modules.Language
Imports DigitalData.Services.EDMIService.Methods.GetDatatableFromCache
Namespace Methods.GlobalIndexer.ImportFile
Public Class ImportFileMethod
Inherits BaseMethod
Private ReadOnly TableStore As DataSet
Private ReadOnly Patterns As Patterns2
Private ReadOnly GetDatatable As GetDatatableFromCacheMethod
Private ManualIndexes As DataTable
Private AutomaticIndexes As DataTable
Private ManualIndexesPostProcessing As DataTable
Private Const VIEW_INDEX_MANUAL = "VWDDINDEX_MAN"
Private Const VIEW_INDEX_AUTOMATIC = "VWDDINDEX_AUTOM"
Private Const TABLE_POST_PROCESSING = "TBDD_INDEX_MAN_POSTPROCESSING"
Private Const TYPE_VBSPLIT = "VBSPLIT"
Private Const TYPE_VBREPLACE = "VBREPLACE"
Private Const TYPE_REGEXPRESSION = "REG. EXPRESSION"
Public Sub New(pLogConfig As LogConfig, pMSSQLServer As MSSQLServer, pTableStore As DataSet)
MyBase.New(pLogConfig, pMSSQLServer)
TableStore = pTableStore
Patterns = New Patterns2(pLogConfig)
GetDatatable = New GetDatatableFromCacheMethod(LogConfig, Database, TableStore)
End Sub
''' <summary>
'''
''' </summary>
''' <remarks>
'''
'''
'''
''' </remarks>
Public Function Run(pData As ImportFileRequest)
Try
LoadIndexes(pData.ProfileId)
Dim oFinalAttributes = pData.AttributeValues
' apply the post processing
oFinalAttributes = ApplyManualPostprocessing(oFinalAttributes, ManualIndexesPostProcessing)
' TODO: apply the manual attributes
oFinalAttributes = ApplyAutomaticeAttributes(oFinalAttributes)
Catch ex As Exception
Return New ImportFileResponse(ex)
End Try
End Function
Private Function ApplyManualPostprocessing(pManualAttributes As List(Of UserAttributeValue), pPostprocessingSteps As DataTable) As List(Of UserAttributeValue)
Logger.Debug("Start of Method [ApplyManualPostprocessing]")
Dim oAttributes = pManualAttributes
For Each oProcessingRow As DataRow In pPostprocessingSteps.Rows
Dim oIndexId = oProcessingRow.ItemEx(Of Integer)("IDXMAN_ID")
Dim oIndexRow As DataRow = ManualIndexes.Select($"GUID = {oIndexId}").FirstOrDefault()
Dim oIndex As UserAttributeValue = pManualAttributes.
Where(Function(attr) attr.AttributeId = oProcessingRow.ItemEx(Of Integer)("IDXMAN_ID")).
FirstOrDefault()
Dim oValue = GetPostprocessingValue(oIndex.AttributeValues, oIndexRow)
oAttributes.Add(New UserAttributeValue With {
.AttributeId = oIndexId,
.AttributeName = oIndex.AttributeName,
.AttributeValues = oIndex.AttributeValues,
.ControlName = oIndex.ControlName
})
Next
Return oAttributes
End Function
Private Function ApplyAutomaticeAttributes(pManualAttributes As List(Of UserAttributeValue)) As List(Of UserAttributeValue)
Logger.Debug("Start of Method [ApplyAutomaticeAttributes]")
Return pManualAttributes
End Function
Private Function GetPostprocessingValue(pValues As List(Of String), pRow As DataRow)
Logger.Debug("Start of Method [GetPostprocessingValue]")
Dim oType = pRow.Item("TYPE")
Dim oResult As New List(Of String)
Logger.Debug("Type of Postprocessing is [{0}]", oType)
Select Case oType
Case TYPE_VBREPLACE
Dim oFindString = pRow.Item("TEXT1")
Dim oReplaceString = pRow.Item("TEXT2")
Logger.Debug("Replacing [{0}] with [{1}]", oFindString, oReplaceString)
For Each oIndexValue In pValues
Dim oReplaceResult = oIndexValue.Replace(oFindString, oReplaceString)
If oReplaceResult.Equals(oIndexValue) Then
Logger.Debug("Replace did not succeed, ReplaceString was not found.")
Else
Logger.Debug("Replace successful for [{0}].", oIndexValue)
End If
oResult.Add(oReplaceResult)
Next
Case TYPE_VBSPLIT
Dim oSeparator As String = pRow.Item("TEXT1")
Dim oSplitIndex As Integer = 0
Integer.TryParse(pRow.Item("TEXT2"), oSplitIndex)
Logger.Debug("Splitting String at Separator [{0}] and Index [{1}]", oSeparator, oSplitIndex)
For Each oIndexValue In pValues
Dim oSplitted As List(Of String) = oIndexValue.Split(oSeparator).ToList()
Logger.Debug("Split succeeded, resulting list has [{0}] items.", oSplitted.Count)
If oSplitIndex < oSplitted.Count Then
Dim oValue = oSplitted.Item(oSplitIndex)
Logger.Debug("Saving value [{0}] from Index [{1}]", oValue, oSplitIndex)
oResult.Add(oValue)
Else
Logger.Debug("SplitIndex(TEXT2) was out of array bounds. Skipping.")
End If
Next
Case Else
LogAndThrow($"Postprocessing type [{oType}] is not supported!")
End Select
Return oResult
End Function
Private Sub LoadIndexes(pProfileId As Integer)
Logger.Debug("Start of Method [LoadIndexes]")
LoadManualIndexes(pProfileId)
LoadAutomaticIndexes(pProfileId)
LoadPostProcessingSteps()
End Sub
Private Sub LoadAutomaticIndexes(pProfileId As Integer)
Logger.Debug("Start of Method [LoadAutomaticIndexes]")
Try
' Load automatic Indexes for this Import
Dim oAutomaticIndexes = GetDatatable.Run(
New GetDatatableFromCacheRequest With {
.DataTable = VIEW_INDEX_MANUAL,
.FilterExpression = $"DOK_ID = {pProfileId}"
})
If oAutomaticIndexes.OK = False Then
LogAndThrow(oAutomaticIndexes.ErrorMessage)
End If
AutomaticIndexes = oAutomaticIndexes.Table
Catch ex As Exception
LogAndThrow(ex, "Error while automatic loading indexes!")
End Try
End Sub
Private Sub LoadManualIndexes(pProfileId As Integer)
Logger.Debug("Start of Method [LoadManualIndexes]")
Try
' Load manual Indexes for this Import
Dim oManualIndexes = GetDatatable.Run(
New GetDatatableFromCacheRequest With {
.DataTable = VIEW_INDEX_MANUAL,
.FilterExpression = $"DOK_ID = {pProfileId}"
})
If oManualIndexes.OK = False Then
LogAndThrow(oManualIndexes.ErrorMessage)
End If
ManualIndexes = oManualIndexes.Table
Catch ex As Exception
LogAndThrow(ex, "Error while loading indexes!")
End Try
End Sub
Private Sub LoadPostProcessingSteps()
Logger.Debug("Start of Method [LoadPostProcessingSteps]")
Try
' Generate a string containing all index ids joined into a string
Dim oIndexIdList As New List(Of Integer)
For Each oRow As DataRow In ManualIndexes.Rows
oIndexIdList.Add(oRow.ItemEx(Of Integer)("GUID"))
Next
Dim oIndexIds As String = String.Join(",", oIndexIdList)
' Load all relevant postprocessing steps
Dim oPostProcessingSteps = GetDatatable.Run(
New GetDatatableFromCacheRequest With {
.DataTable = TABLE_POST_PROCESSING,
.FilterExpression = $"IDXMAN_ID IN ({oIndexIds})"
})
If oPostProcessingSteps.OK = False Then
LogAndThrow(oPostProcessingSteps.ErrorMessage)
End If
ManualIndexesPostProcessing = oPostProcessingSteps.Table
Catch ex As Exception
LogAndThrow(ex, "Error while loading post processing steps!")
End Try
End Sub
End Class
End Namespace