EDMIService: sEcond working version

This commit is contained in:
Jonathan Jenne
2021-12-08 16:06:21 +01:00
parent 3e11385907
commit ebecda2506
8 changed files with 251 additions and 89 deletions

View File

@@ -3,6 +3,8 @@ Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Patterns
Imports DigitalData.Modules.Language
Imports DigitalData.Services.EDMIService.Methods.GetDatatableFromCache
Imports System.IO
Imports DigitalData.Modules.ZooFlow.State
Namespace Methods.GlobalIndexer.ImportFile
Public Class ImportFileMethod
@@ -12,6 +14,8 @@ Namespace Methods.GlobalIndexer.ImportFile
Private ReadOnly GetDatatable As GetDatatableFromCacheMethod
Private ReadOnly Connection As SqlClient.SqlConnection
Private ReadOnly Transaction As SqlClient.SqlTransaction
Private User As UserState
Private Profile As DataRow
Private Const VIEW_PROFILE = "VWGI_DOCTYPE_IDB"
@@ -38,6 +42,8 @@ Namespace Methods.GlobalIndexer.ImportFile
''' </remarks>
Public Function Run(pData As ImportFileRequest)
Try
User = pData.User
' TODO: Add missing user properties in UserState from TBDD_USER
'pData.User = ResolveUserFromUserName(pData.User.UserName)
@@ -46,17 +52,16 @@ Namespace Methods.GlobalIndexer.ImportFile
Dim oPostProcessingSteps As DataTable = LoadPostProcessingSteps(oManualIndexes)
LoadProfile(pData.ProfileId)
Dim oFinalAttributes = pData.AttributeValues
Dim oFileName As String = GetFilenameByNameconvention(pData.File.FileName, Profile.Item("NAMENKONVENTION"))
Dim oUserAttributes = pData.AttributeValues
Dim oAutoAttributes As List(Of UserAttributeValue) = Nothing
' Apply post processing
Dim oPostProcessing = New Steps.PostProcessing(LogConfig, oPostProcessingSteps)
oFinalAttributes = oPostProcessing.ApplyManualPostprocessing(oFinalAttributes)
oUserAttributes = oPostProcessing.ApplyManualPostprocessing(oUserAttributes)
' Apply automatic attributes
Dim oAutomaticIndexing = New Steps.AutomaticIndexing(LogConfig, Database, oAutomaticIndexes, GlobalState)
oFinalAttributes = oAutomaticIndexing.ApplyAutomaticeAttributes(oFinalAttributes, pData.File.FileInfoRaw, pData.User)
oAutoAttributes = oAutomaticIndexing.ApplyAutomaticeAttributes(oUserAttributes, pData.File.FileInfoRaw, User)
' Import the file
Dim oNewFile As New NewFileMethod(LogConfig, Database, GlobalState)
@@ -65,7 +70,7 @@ Namespace Methods.GlobalIndexer.ImportFile
.BusinessEntity = pData.BusinessEntity,
.KindType = pData.KindType,
.StoreName = pData.StoreName,
.User = pData.User
.User = User
})
If oResponse.OK Then
@@ -74,35 +79,23 @@ Namespace Methods.GlobalIndexer.ImportFile
Throw New ApplicationException(oResponse.ErrorMessage)
End If
Logger.Info("Writing Attributes for ObjectId [{0}]", oResponse.ObjectId)
Logger.Info("Generating display filename for file [{0}]", pData.File.FileName)
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
Dim oNameconvention As String = Profile.ItemEx(Of String)("NAMENKONVENTION")
Dim oDisplayFilename = GetFilenameByNameconvention(pData.File.FileInfoRaw, oNameconvention, User, oUserAttributes, oAutoAttributes)
oAttributes.Add(oFinalAttribute.AttributeName, oFinalAttribute.AttributeValues.First)
Next
Logger.Info("Collecting Attributes for ObjectId [{0}]", oResponse.ObjectId)
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 oFinalAttributes As New Dictionary(Of String, List(Of String)) From {
{"DisplayFileName", New List(Of String) From {oDisplayFilename}}
}
oFinalAttributes = oFinalAttributes.
Concat(Helpers.UserAttributesToDictionary(oUserAttributes)).
Concat(Helpers.UserAttributesToDictionary(oAutoAttributes)).
ToDictionary(Function(kv) kv.Key, Function(kv) kv.Value)
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
Logger.Info("Writing [{0}] Attributes for ObjectId [{0}] ", oResponse.ObjectId)
WriteAttributeValues(oResponse.ObjectId, oFinalAttributes)
' Finally, commit the transaction
Transaction?.Commit()
@@ -120,14 +113,44 @@ Namespace Methods.GlobalIndexer.ImportFile
End Try
End Function
Private Function GetFilenameByNameconvention(pFileInfo As FileInfo, pNameconvention As String, pUser As UserState, pAttributes As List(Of UserAttributeValue), pAutoAttributes As List(Of UserAttributeValue)) As String
Dim oAttributeDict = Helpers.UserAttributesToDictionary(pAttributes)
Dim oAutoAttributeDict = Helpers.UserAttributesToDictionary(pAutoAttributes)
If pNameconvention Is Nothing OrElse pNameconvention = String.Empty Then
Logger.Warn("Nameconvention for File [{0}] was empty. Returning original filename.", pFileInfo.Name)
Return pFileInfo.Name
End If
Private Function GetFilenameByNameconvention(pFileName As String, pNameconvention As String) As String
Return pFileName
Dim oFileName As String = Helpers.GetPlaceholderValue(pNameconvention, pFileInfo, pUser, oAttributeDict, oAutoAttributeDict)
Return oFileName & pFileInfo.Extension
End Function
Private Sub WriteAttributeValues(pObjectId As Long, pAttributes As Dictionary(Of String, List(Of String)))
For Each oAttribute As KeyValuePair(Of String, List(Of String)) In pAttributes
Try
' TODO: This works only for simple attributes for now!!!
Dim oValue = oAttribute.Value.FirstOrDefault()
' Dont write empty attributes
If oValue Is Nothing Then
Continue For
End If
' Now make the call to the database
Dim oSuccess = Helpers.SetAttributeValue(Connection, Transaction, pObjectId, oAttribute.Key, oValue, User.Language, 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
End Sub
''' <summary>
''' Load Profiles for this Import
''' </summary>