EDMIService: First completed version of UpdateFileMethod

This commit is contained in:
Jonathan Jenne 2022-02-07 14:10:52 +01:00
parent deef67b548
commit 533df59b1f
3 changed files with 77 additions and 197 deletions

View File

@ -2,12 +2,11 @@
Imports DigitalData.Modules.Database.MSSQLServer Imports DigitalData.Modules.Database.MSSQLServer
Imports DigitalData.Modules.Language Imports DigitalData.Modules.Language
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.EDMI.API.Client
Imports System.Data.SqlClient Imports System.Data.SqlClient
Imports System.IO
Imports DigitalData.Modules.ZooFlow.State Imports DigitalData.Modules.ZooFlow.State
Imports DigitalData.Modules.Patterns Imports DigitalData.Modules.Patterns
Imports DigitalData.Services.EDMIService.Methods.IDB Imports DigitalData.Services.EDMIService.Methods.IDB
Imports DigitalData.Modules.Filesystem
Namespace IDB Namespace IDB
Public Class Helpers Public Class Helpers
@ -15,15 +14,17 @@ Namespace IDB
Private ReadOnly Patterns As Patterns2 Private ReadOnly Patterns As Patterns2
Private ReadOnly Database As MSSQLServer Private ReadOnly Database As MSSQLServer
Private ReadOnly FileEx As File
Public Sub New(pLogConfig As LogConfig, pMSSQLServer As MSSQLServer) Public Sub New(pLogConfig As LogConfig, pMSSQLServer As MSSQLServer)
MyBase.New(pLogConfig) MyBase.New(pLogConfig)
Patterns = New Patterns2(pLogConfig) Patterns = New Patterns2(pLogConfig)
Database = pMSSQLServer Database = pMSSQLServer
FileEx = New File(pLogConfig)
End Sub End Sub
Public Function GetPlaceholderValue(pValue As String, Public Function GetPlaceholderValue(pValue As String,
pFileInfo As FileInfo, pFileInfo As IO.FileInfo,
pUserState As UserState, pUserState As UserState,
pUserAttributes As Dictionary(Of String, List(Of String)), pUserAttributes As Dictionary(Of String, List(Of String)),
pAutoAttributes As Dictionary(Of String, List(Of String))) As String pAutoAttributes As Dictionary(Of String, List(Of String))) As String
@ -135,6 +136,32 @@ Namespace IDB
End Try End Try
End Function End Function
Public Function GetFileObjectPath(pStore As GlobalState.ObjectStore, pDateImported As Date) As String
' Get directory by DateImported or, if not supplied, by current date
Dim oSubDirectory As String
If IsNothing(pDateImported) Then
oSubDirectory = FileEx.CreateDateDirectory(Now)
Else
oSubDirectory = FileEx.CreateDateDirectory(pDateImported)
End If
Logger.Debug("Subdirectory is [{0}]", oSubDirectory)
' Check and create final path, if necessary
Dim oFinalPath = IO.Path.Combine(pStore.Path, oSubDirectory)
If Not IO.Directory.Exists(oFinalPath) Then
Try
Logger.Debug("Path does not exist, creating: [{0}]", oFinalPath)
IO.Directory.CreateDirectory(oFinalPath)
Logger.Debug("Created folder [{0}]", oFinalPath)
Catch ex As Exception
LogAndThrow(ex, $"Store Directory [{oFinalPath}] could not be created!")
End Try
End If
Logger.Debug("Final Directory is [{0}]", oFinalPath)
Return oFinalPath
End Function
Public Function GetObject(pObjectId As Long) As IDBObject Public Function GetObject(pObjectId As Long) As IDBObject
Try Try
Dim oGetObjectSQL = $"SELECT * FROM VWIDB_OBJECT WHERE IDB_OBJ_ID = {pObjectId}" Dim oGetObjectSQL = $"SELECT * FROM VWIDB_OBJECT WHERE IDB_OBJ_ID = {pObjectId}"

View File

@ -34,37 +34,17 @@ Namespace Methods.IDB.NewFile
' Find ObjectStore by Title ' Find ObjectStore by Title
Logger.Debug("Checking for DataStore [{0}].", pData.StoreName) Logger.Debug("Checking for DataStore [{0}].", pData.StoreName)
Dim oStore = GlobalState.ObjectStores.Where(Function(store) store.Title.Equals(pData.StoreName, StringComparison.OrdinalIgnoreCase)).SingleOrDefault() Dim oStore = GlobalState.ObjectStores.
Where(Function(store) store.Title = pData.StoreName).
SingleOrDefault()
If oStore Is Nothing Then If oStore Is Nothing Then
LogAndThrow($"DataStore [{pData.StoreName}] does not exist. Exiting.") LogAndThrow($"DataStore [{pData.StoreName}] does not exist. Exiting.")
End If End If
' Get Store base path ' Get Store base and final path
Dim oBasePath As String = oStore.Path Logger.Debug("Store BasePath is [{0}]", oStore.Path)
Logger.Debug("Store BasePath is [{0}]", oBasePath) Dim oFinalPath = Helpers.GetFileObjectPath(oStore, pData.File.FileImportedAt)
' Get directory by DateImported or, if not supplied, by current date
Dim oSubDirectory As String
If IsNothing(pData.File.FileImportedAt) Then
oSubDirectory = GetDateSubDirectory(Now)
Else
oSubDirectory = GetDateSubDirectory(pData.File.FileImportedAt)
End If
Logger.Debug("Subdirectory is [{0}]", oSubDirectory)
' Check and create final path, if necessary
Dim oFinalPath = IO.Path.Combine(oBasePath, oSubDirectory)
If Not IO.Directory.Exists(oFinalPath) Then
Try
Logger.Debug("Path does not exist, creating: [{0}]", oFinalPath)
IO.Directory.CreateDirectory(oFinalPath)
Logger.Debug("Created folder [{0}]", oFinalPath)
Catch ex As Exception
LogAndThrow(ex, $"Store Directory [{oFinalPath}] could not be created!")
End Try
End If
Logger.Debug("Final Directory is [{0}]", oFinalPath)
' Get filename ' Get filename
Dim oKeepFileName As Boolean = False Dim oKeepFileName As Boolean = False
@ -182,10 +162,6 @@ Namespace Methods.IDB.NewFile
Return $"{IDB_OBJ_ID}.ddfo" Return $"{IDB_OBJ_ID}.ddfo"
End If End If
End Function End Function
Private Function GetDateSubDirectory(pDate As Date) As String
Return IO.Path.Combine(pDate.ToString("yyyy"), pDate.ToString("MM"), pDate.ToString("dd"))
End Function
End Class End Class
End Namespace End Namespace

View File

@ -20,7 +20,6 @@ Namespace Methods.IDB.UpdateFile
End Sub End Sub
Public Function Run(pData As UpdateFileRequest) As UpdateFileResponse Public Function Run(pData As UpdateFileRequest) As UpdateFileResponse
Try Try
' TODO: Update file object ' TODO: Update file object
If Helpers.TestObjectIdExists(pData.ObjectId) = False Then If Helpers.TestObjectIdExists(pData.ObjectId) = False Then
@ -28,8 +27,13 @@ Namespace Methods.IDB.UpdateFile
End If End If
'TODO: Create a view that collects all information about an idb object 'TODO: Create a view that collects all information about an idb object
Dim oResultObjectId As Long
If pData.CreateNewVersion = False Then If pData.CreateNewVersion = False Then
' ----------------------------------------------
' -- Replace the existing file-object
' ----------------------------------------------
Dim oGetFileObject As New GetFileObject.GetFileObjectMethod(LogConfig, DatabaseIDB, DatabaseECM, GlobalState) Dim oGetFileObject As New GetFileObject.GetFileObjectMethod(LogConfig, DatabaseIDB, DatabaseECM, GlobalState)
Dim oArgs = New GetFileObject.GetFileObjectRequest With { Dim oArgs = New GetFileObject.GetFileObjectRequest With {
.ObjectId = pData.ObjectId, .ObjectId = pData.ObjectId,
@ -56,178 +60,51 @@ Namespace Methods.IDB.UpdateFile
LogAndThrow(ex, $"Could not write file [{oFilePath}] to disk!") LogAndThrow(ex, $"Could not write file [{oFilePath}] to disk!")
End Try End Try
Return New UpdateFileResponse(pData.ObjectId) oResultObjectId = pData.ObjectId
Else Else
Throw New ApplicationException("Not implemented!!") ' ----------------------------------------------
' -- Create a new object + file object
' ----------------------------------------------
Dim oObjectTable = DatabaseIDB.GetDatatable($"Select * FROM VWIDB_OBJECT WHERE IDB_OBJ_ID = {pData.ObjectId}")
Dim oObjectRow As DataRow = oObjectTable.Rows.Item(0)
Dim oKind As String = oObjectRow.Item("KIND_NAME")
Dim oBusinessEntity As String = oObjectRow.Item("BE_NAME")
Dim oStore As String = oObjectRow.ItemArray("STORE_NAME")
Dim oNewFileMethod As New NewFile.NewFileMethod(LogConfig, DatabaseIDB, DatabaseECM, GlobalState)
Dim oNewFileRequest As New NewFile.NewFileRequest With {
.File = pData.File,
.BusinessEntity = oBusinessEntity,
.KindType = oKind,
.StoreName = oStore,
.User = pData.User
}
Dim oNewFileResponse = oNewFileMethod.Run(oNewFileRequest)
Dim oNewObjectId = oNewFileResponse.ObjectId
Dim oSql As String = $"EXEC PRIDB_NEW_VERSION_OBJECT '{pData.ObjectId}', '{oNewObjectId}', '{pData.User.UserName}'"
DatabaseIDB.ExecuteNonQuery(oSql)
oResultObjectId = oNewObjectId
End If End If
' Finally, commit the transaction
Transaction?.Commit()
Return New UpdateFileResponse(oResultObjectId)
Catch ex As Exception Catch ex As Exception
Logger.Warn("Error occurred while creating file!")
Logger.Error(ex)
Logger.Info("Rolling back transaction.")
Transaction?.Rollback()
Return New UpdateFileResponse(ex) Return New UpdateFileResponse(ex)
End Try End Try
'Dim oFilePath As String = Nothing
'
'Dim oExistingObjectId = TestFileChecksumExists(pData.File.FileChecksum)
'If oExistingObjectId > 0 Then
' Return New UpdateFileResponse(oExistingObjectId)
'End If
'Try
' Dim oObjectId = NewObjectId(pData.KindType, pData.BusinessEntity, pData.User.UserName)
' If oObjectId = 0 Then
' LogAndThrow("Could not create new ObjectId!")
' End If
' ' Find ObjectStore by Title
' Logger.Debug("Checking for DataStore [{0}].", pData.StoreName)
' Dim oStore = GlobalState.ObjectStores.
' Where(Function(store) store.Title.Equals(pData.StoreName, StringComparison.OrdinalIgnoreCase)).
'SingleOrDefault()
' If oStore Is Nothing Then
' LogAndThrow($"DataStore [{pData.StoreName}] does not exist. Exiting.")
' End If
' ' Get Store base path
' Dim oBasePath As String = oStore.Path
' Logger.Debug("Store BasePath is [{0}]", oBasePath)
' ' Get directory by DateImported or, if not supplied, by current date
' Dim oSubDirectory As String
' If IsNothing(pData.File.FileImportedAt) Then
' oSubDirectory = GetDateSubDirectory(Now)
' Else
' oSubDirectory = GetDateSubDirectory(pData.File.FileImportedAt)
' End If
' Logger.Debug("Subdirectory is [{0}]", oSubDirectory)
' ' Check and create final path, if necessary
' Dim oFinalPath = IO.Path.Combine(oBasePath, oSubDirectory)
' If Not IO.Directory.Exists(oFinalPath) Then
' Try
' Logger.Debug("Path does not exist, creating: [{0}]", oFinalPath)
' IO.Directory.CreateDirectory(oFinalPath)
' Logger.Debug("Created folder [{0}]", oFinalPath)
' Catch ex As Exception
' LogAndThrow(ex, $"Store Directory [{oFinalPath}] could not be created!")
' End Try
' End If
' Logger.Debug("Final Directory is [{0}]", oFinalPath)
' ' Get filename
' Dim oKeepFileName As Boolean = False
' If oStore.IsArchive Then
' Logger.Debug("Object Store is an archive: [{0}]", oStore.IsArchive)
' oKeepFileName = True
' End If
' Dim oFileName As String = GetFileObjectFileName(oObjectId, pData.File.FileName, oKeepFileName)
' Logger.Debug("Filename is [{0}]", oFileName)
' oFilePath = IO.Path.Combine(oFinalPath, oFileName)
' Dim oFileObjectInfo As IO.FileInfo = New IO.FileInfo(oFilePath)
' Dim oFileObjectSize As Long = pData.File.FileContents.Length
' Dim oFileObjectName As String = oFileObjectInfo.Name
' Dim oOriginalExtension As String = pData.File.FileInfoRaw.Extension.Substring(1)
' Logger.Debug("File Information for [{0}]:", oFileObjectName)
' Logger.Debug("Size: [{0}]", oFileObjectSize)
' Logger.Debug("Original Extension: [{0}]", oOriginalExtension)
' Logger.Debug("Checksum: [{0}]", pData.File.FileChecksum)
' Try
' Using oStream = New IO.FileStream(oFilePath, IO.FileMode.Create, IO.FileAccess.Write)
' Logger.Info("Saving file to path [{0}]", oFilePath)
' oStream.Write(pData.File.FileContents, 0, oFileObjectSize)
' oStream.Flush(True)
' oStream.Close()
' End Using
' Catch ex As Exception
' LogAndThrow(ex, $"Could not write file [{oFilePath}] to disk!")
' End Try
' '---------------------------------------------------------------------------
' Logger.Info("Creating IDB FileObject for ObjectId [{0}].", oObjectId)
' ' Insert into DB
' Dim oSQL As String = $"EXEC PRIDB_NEW_IDBFO
' '{oFinalPath}',
' '{oFileObjectName}',
' '{oOriginalExtension}',
' {oFileObjectSize},
' '{pData.File.FileChecksum}' ,
' '{pData.User.UserName}',
' '{oObjectId}',
' {oStore.Id}"
' Dim oResult As Boolean = DatabaseIDB.ExecuteNonQueryWithConnectionObject(oSQL, Connection, ExternalTransaction, Transaction)
' If oResult = False Then
' LogAndThrow("IDB FileObject could not be created!")
' End If
' '---------------------------------------------------------------------------
' 'TODO: File dates in try catch
' 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 oSystemAttributes
' Try
' ' Dont write empty attributes
' If oAttribute.Value Is Nothing Then
' Continue For
' End If
' Dim oSuccess = Helpers.SetAttributeValue(Connection, Transaction, oObjectId, oAttribute.Key, oAttribute.Value, pData.User.Language, pData.User.UserName)
' If oSuccess Then
' Logger.Debug("System Attribute [{0}] written with value [{1}]", oAttribute.Key, oAttribute.Value)
' Else
' 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!")
' End Try
' Next
' '---------------------------------------------------------------------------
' ' Finally, commit the transaction
' Transaction?.Commit()
' Return New UpdateFileResponse(oObjectId)
'Catch ex As Exception
' Logger.Warn("Error occurred while creating file!")
' Logger.Error(ex)
' Logger.Info("Cleaning up files.")
' If Not IsNothing(oFilePath) AndAlso IO.File.Exists(oFilePath) Then
' Try
' IO.File.Delete(oFilePath)
' Catch exInner As Exception
' Logger.Warn("Error while cleaning up files.")
' Logger.Error(exInner)
' End Try
' End If
' Logger.Info("Rolling back transaction.")
' Transaction?.Rollback()
' Return New UpdateFileResponse(ex)
'End Try
End Function End Function
Private Function TestFileChecksumExists(pChecksum As String) As Long Private Function TestFileChecksumExists(pChecksum As String) As Long