EDMIService: First working version of ImportFile

This commit is contained in:
Jonathan Jenne
2021-12-08 13:09:25 +01:00
parent d75272a17f
commit 3e5918297c
9 changed files with 180 additions and 118 deletions

View File

@@ -10,7 +10,8 @@ Namespace Methods.GlobalIndexer.ImportFile
Private ReadOnly Patterns As Patterns2
Private ReadOnly GetDatatable As GetDatatableFromCacheMethod
Private ReadOnly Connection As SqlClient.SqlConnection
Private ReadOnly Transaction As SqlClient.SqlTransaction
Private Profile As DataRow
Private Const VIEW_PROFILE = "VWGI_DOCTYPE_IDB"
@@ -23,6 +24,8 @@ Namespace Methods.GlobalIndexer.ImportFile
Patterns = New Patterns2(pLogConfig)
GetDatatable = New GetDatatableFromCacheMethod(LogConfig, Database, GlobalState)
Connection = Database.GetConnection()
Transaction = Connection.BeginTransaction()
End Sub
''' <summary>
@@ -67,12 +70,52 @@ Namespace Methods.GlobalIndexer.ImportFile
If oResponse.OK Then
Logger.Info("Import of file [{0}] under ObjectId [{1}] successful!", pData.File.FileName, oResponse.ObjectId)
Return New ImportFileResponse(oResponse.ObjectId)
Else
Throw New ApplicationException(oResponse.ErrorMessage)
End If
Logger.Info("Writing Attributes for ObjectId [{0}]", oResponse.ObjectId)
Dim oAttributes As New Dictionary(Of String, Object)
For Each oFinalAttribute In oFinalAttributes
If oFinalAttribute.AttributeValues Is Nothing OrElse oFinalAttribute.AttributeValues.Count = 0 Then
Logger.Warn("Values for Attribute [{0}] are empty. Skipping.", oFinalAttribute.AttributeName)
Continue For
End If
oAttributes.Add(oFinalAttribute.AttributeName, oFinalAttribute.AttributeValues.First)
Next
For Each oAttribute As KeyValuePair(Of String, Object) In oAttributes
Try
' Dont write empty attributes
If oAttribute.Value Is Nothing Then
Continue For
End If
Dim oSuccess = Helpers.SetAttributeValue(Connection, Transaction, oResponse.ObjectId, oAttribute.Key, oAttribute.Value, pData.User.Language, pData.User.UserName)
If oSuccess Then
Logger.Info("Attribute [{0}] written with value [{1}]", oAttribute.Key, oAttribute.Value)
Else
Logger.Warn("Attribute value could not be written")
End If
Catch ex As Exception
LogAndThrow(ex, $"Attribute [{oAttribute.Key}] could not be written!")
End Try
Next
' Finally, commit the transaction
Transaction?.Commit()
Return New ImportFileResponse(oResponse.ObjectId)
Catch ex As Exception
Logger.Warn("Error occurred while importing file!")
Logger.Error(ex)
Logger.Info("Rolling back transaction.")
Transaction?.Rollback()
Return New ImportFileResponse(ex)
End Try
End Function
@@ -142,7 +185,7 @@ Namespace Methods.GlobalIndexer.ImportFile
oIndexes.Add(oAutomaticIndex)
Next
Logger.Info("[{0}] automatic indexes loaded.", oIndexes)
Logger.Info("Automatic indexes loaded: [{0}]", oIndexes.Count)
Return oIndexes
Catch ex As Exception
@@ -186,6 +229,8 @@ Namespace Methods.GlobalIndexer.ImportFile
oIndexes.Add(oManualIndex)
Next
Logger.Info("Manual indexes loaded: [{0}]", oIndexes.Count)
Return oIndexes
Catch ex As Exception

View File

@@ -33,24 +33,29 @@ Namespace Methods.GlobalIndexer.ImportFile.Steps
End If
Logger.Info("Processing [{0}] automatic indexes", AutomaticIndexes.Count)
Dim oAttributes As List(Of UserAttributeValue) = pUserAttributes
Dim oUserAttributes As List(Of UserAttributeValue) = pUserAttributes
Dim oAutoAttributes As New List(Of UserAttributeValue)
For Each oAutomaticIndex In AutomaticIndexes
' We add oAttributes from the previous run into the current run so it is in theory possible to reference
' automatic attributes which have been set just before.
Dim oAttribute = ApplyAutomaticIndex(oAutomaticIndex, pFileInfo, pUserState, oAttributes)
Dim oAttribute = ApplyAutomaticIndex(oAutomaticIndex, pFileInfo, pUserState, oUserAttributes, oAutoAttributes)
If oAttribute IsNot Nothing Then
Logger.Info("Adding Attribute [{0}]", oAttribute)
oAttributes.Add(oAttribute)
Logger.Debug("Adding Attribute [{0}]", oAttribute)
oAutoAttributes.Add(oAttribute)
End If
Next
Return oAttributes
oUserAttributes.AddRange(oAutoAttributes)
Return oUserAttributes
End Function
Private Function ApplyAutomaticIndex(pAutomaticIndex As AutomaticIndex, pFileInfo As FileInfo, pUserState As UserState, pAttributes As List(Of UserAttributeValue)) As UserAttributeValue
Private Function ApplyAutomaticIndex(pAutomaticIndex As AutomaticIndex,
pFileInfo As FileInfo,
pUserState As UserState,
pAttributes As List(Of UserAttributeValue),
pAutoAttributes As List(Of UserAttributeValue)) As UserAttributeValue
Try
Dim oAttributeDict = pAttributes.ToDictionary(
Function(attr) attr.AttributeName,
@@ -77,6 +82,8 @@ Namespace Methods.GlobalIndexer.ImportFile.Steps
Dim oConnectionString As String = GlobalState.GetConnectionString(pAutomaticIndex.SQLConnectionId)
Dim oFinalSQLCommand = pAutomaticIndex.SQLCommand
' TODO: Dont show the unmasked conn string
Logger.Debug("SQL Connection String is: [{0}]", oConnectionString)
Logger.Debug("SQL Command is: [{0}]", oFinalSQLCommand)
oFinalSQLCommand = GetPlaceholderValue(oFinalSQLCommand, pFileInfo, pUserState, oAttributeDict)
@@ -109,11 +116,12 @@ Namespace Methods.GlobalIndexer.ImportFile.Steps
Private Function GetPlaceholderValue(pValue As String, pFileInfo As FileInfo, pUserState As UserState, pAttributes As Dictionary(Of String, List(Of String))) As String
Dim oResult As String = pValue
oResult = Patterns.ReplaceInternalValues(oResult)
oResult = Patterns.ReplaceFileValues(oResult, pFileInfo)
oResult = Patterns.ReplaceUserValues(oResult, pUserState)
oResult = Patterns.ReplaceGlobixValues(oResult, pAttributes)
' TODO: Get the automatic indexes in here too
oResult = Patterns.ReplaceGlobixValues(oResult, New Dictionary(Of String, List(Of String)), pAttributes)
Return oResult
End Function

View File

@@ -127,13 +127,13 @@ Public Class NewFileMethod
'TODO: File dates in try catch
Dim oDefaultAttributes As New Dictionary(Of String, Object) From {
Dim oSystemAttributes As New Dictionary(Of String, Object) From {
{"OriginFileName", pData.File.FileName},
{"OriginCreationDatetime", pData.File.FileCreatedAt},
{"OriginChangedDatetime", pData.File.FileChangedAt}
}
For Each oAttribute As KeyValuePair(Of String, Object) In oDefaultAttributes
For Each oAttribute As KeyValuePair(Of String, Object) In oSystemAttributes
Try
' Dont write empty attributes
If oAttribute.Value Is Nothing Then
@@ -142,9 +142,9 @@ Public Class NewFileMethod
Dim oSuccess = Helpers.SetAttributeValue(Connection, Transaction, oObjectId, oAttribute.Key, oAttribute.Value, pData.User.Language, pData.User.UserName)
If oSuccess Then
Logger.Debug("Default Attribute [{0}] written with value [{1}]", oAttribute.Key, oAttribute.Value)
Logger.Debug("System Attribute [{0}] written with value [{1}]", oAttribute.Key, oAttribute.Value)
Else
Logger.Warn("Default attribute value could not be written")
Logger.Warn("System attribute value could not be written")
End If
Catch ex As Exception
LogAndThrow(ex, $"System attribute [{oAttribute.Key}] could not be written!")
@@ -158,7 +158,7 @@ Public Class NewFileMethod
Return New NewFile.NewFileResponse(oObjectId)
Catch ex As Exception
Logger.Warn("Error occurred while importing file!")
Logger.Warn("Error occurred while creating file!")
Logger.Error(ex)
Logger.Info("Cleaning up files.")