EDMIService: Rework NewFile and SetAttributeValue

This commit is contained in:
Jonathan Jenne
2021-11-29 11:07:46 +01:00
parent 2bd54ccad0
commit 23aad7e9c0
28 changed files with 847 additions and 203 deletions

View File

@@ -11,6 +11,7 @@ Imports DigitalData.Modules.EDMI.API.Rights
Imports DigitalData.Services.EDMIService.Exceptions
Imports DigitalData.Services.EDMIService.GlobalState
Imports DigitalData.Services.EDMIService.FileStorage
Imports DigitalData.Services.EDMIService.FileStorage.SetAttributeValue
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerSession)>
Public Class EDMIService
@@ -33,6 +34,9 @@ Public Class EDMIService
Private ReadOnly _Debug As Boolean = False
Private ReadOnly _Username As String
Private _IDBHelpers As IDB.Helpers
Public Shared Sub Configure(Config As ServiceConfiguration)
Dim oBaseAddress = Config.BaseAddresses.Item(0)
Dim oBinding = EDMI.API.Channel.GetBinding()
@@ -53,111 +57,21 @@ Public Class EDMIService
_Username = oUsername
_Logger = LogConfig.GetLogger()
_Logger.Debug("New Request by User [{0}]", _Username)
_IDBHelpers = New IDB.Helpers(LogConfig, MSSQL_IDB)
End Sub
Public Function NewFile(Data As NewFile.NewFileRequest) As NewFile.NewFileResponse Implements IEDMIService.NewFile
_Logger.Debug("Start of Method [NewFile]")
Try
Dim oNewObjectIdSQL = $"DECLARE @NEW_IDB_OBJ_ID BIGINT
EXEC PRIDB_NEW_OBJECT '{Data.KindType}','{Data.Who}','{Data.BusinessEntity}',0, @IDB_OBJ_ID = @NEW_IDB_OBJ_ID OUTPUT;
SELECT @NEW_IDB_OBJ_ID"
Dim oObjectId As Long = MSSQL_IDB.GetScalarValue(oNewObjectIdSQL)
_Logger.Info("New Object with Id [{0}] created!", oObjectId)
If IsNothing(oObjectId) Then
LogAndThrow("Could not create new ObjectId!")
End If
'---------------------------------------------------------------------------
' Find ObjectStore by Title
_Logger.Debug("Checking for DataStore [{0}].", Data.StoreName)
Dim oStore = GlobalState.ObjectStores.
Where(Function(store) store.Path.Equals(Data.StoreName, StringComparison.OrdinalIgnoreCase)).
SingleOrDefault()
If oStore Is Nothing Then
LogAndThrow($"DataStore [{Data.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(Data.FileImportedAt) Then
oSubDirectory = GetDateSubDirectory(Now)
Else
oSubDirectory = GetDateSubDirectory(Data.FileImportedAt)
End If
_Logger.Debug("Subdirectory is [{0}]", oSubDirectory)
' Check and create final path, if necessary
Dim oFinalPath = Path.Combine(oBasePath, oSubDirectory)
If Not Directory.Exists(oFinalPath) Then
Try
_Logger.Debug("Path does not exist, creating: [{0}]", oFinalPath)
Directory.CreateDirectory(oFinalPath)
_Logger.Debug("Created folder [{0}]", oFinalPath)
Catch ex As Exception
Throw GetFault(ex)
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, Data.FileName, oKeepFileName)
_Logger.Debug("Filename is [{0}]", oFileName)
Dim oFileObjectPath As String = Path.Combine(oFinalPath, oFileName)
Dim oFileObjectInfo As FileInfo = New FileInfo(oFileObjectPath)
Dim oFileObjectSize As Long = Data.FileContents.Length
Dim oFileObjectExtension As String = oFileObjectInfo.Extension.Substring(1)
Dim oFileObjectName As String = oFileObjectInfo.Name
_Logger.Debug("File Information for [{0}]:", oFileObjectName)
_Logger.Debug("Size: [{0}]", oFileObjectSize)
_Logger.Debug("Extension: [{0}]", oFileObjectExtension)
_Logger.Debug("Checksum: [{0}]", Data.FileChecksum)
Try
Using oStream = New FileStream(oFileObjectPath, FileMode.Create, FileAccess.Write)
_Logger.Info("ImportFile: Saving file to path [{0}]", oFileObjectPath)
_Logger.Info("ImportFile: Content Length: {0}", oFileObjectSize)
oStream.Write(Data.FileContents, 0, oFileObjectSize)
oStream.Flush(True)
oStream.Close()
End Using
Catch ex As Exception
_Logger.Error(ex)
LogAndThrow("Could not write file to disk!")
End Try
' Insert into DB
Dim oSQL As String = $"EXEC PRIDB_NEW_IDBFO '{oFileObjectPath}', '{oFileObjectName}', '{oFileObjectExtension}',{oFileObjectSize},{Data.FileChecksum} ,'{Data.Who}','{oObjectId}',{oStore.Id}"
Dim oResult As Boolean = MSSQL_IDB.ExecuteNonQuery(oSQL)
If oResult = False Then
LogAndThrow("IDB FileObject could not be created!")
End If
Return New NewFile.NewFileResponse(oObjectId)
Catch ex As FaultException
Return New NewFile.NewFileResponse(ex)
End Try
Dim oNewFile As New NewFileMethod(LogConfig, MSSQL_IDB, GlobalState.ObjectStores)
Return oNewFile.Run(Data)
End Function
Public Function SetAttributeValue(Data As SetAttributeValue.SetAttributeValueRequest) As SetAttributeValue.SetAttributeValueResponse Implements IEDMIService.SetAttributeValue
_Logger.Debug("Start of Method [SetAttributeValue]")
Dim oSetAttributeValue As New SetAttributeValueMethod(LogConfig, MSSQL_IDB)
Return oSetAttributeValue.Run(Data)
End Function
#Region "=== Heartbeat ==="
Public Function Heartbeat() As Boolean Implements IEDMIService.Heartbeat
@@ -544,23 +458,17 @@ Public Class EDMIService
Public Function TestObjectIdExists(Data As TestObjectIdExistsRequest) As TestObjectIdExistsResponse Implements IEDMIService.TestObjectIdExists
Try
Dim oSQL As String = $"SELECT IDB_OBJ_ID, ACTIVE, DELETED FROM TBIDB_OBJECT WHERE IDB_OBJ_ID = {Data.ObjectId}"
Dim oTable As DataTable = MSSQL_IDB.GetDatatable(oSQL)
Dim oDeleted As Boolean = False
Dim oActive As Boolean = False
If IsNothing(oTable) OrElse oTable.Rows.Count = 0 Then
_Logger.Warn("ObjectId {0} does not exist")
If _IDBHelpers.TestObjectIdExists(Data.ObjectId, oDeleted, oActive) = False Then
Return New TestObjectIdExistsResponse(False)
End If
Dim oRow As DataRow = oTable.Rows.Item(0)
Dim oActive = Utils.NotNull(oRow.Item("ACTIVE"), False)
Dim oDeleted = Utils.NotNull(oRow.Item("DELETED"), False)
Return New TestObjectIdExistsResponse(True) With {
.Deleted = oDeleted,
.Inactive = Not oActive
}
Catch ex As Exception
_Logger.Error(ex)
Return New TestObjectIdExistsResponse(False)
@@ -574,17 +482,10 @@ Public Class EDMIService
#End Region
#Region "=== Private ==="
Private Function GetFileObjectFileName(IDB_OBJ_ID As Long, pFilename As String, pKeepFilename As Boolean) As String
If pKeepFilename Then
Return pFilename
Else
Return $"{IDB_OBJ_ID}.ddfo"
End If
End Function
Private Function GetDateSubDirectory(pDate As Date) As String
Return Path.Combine(pDate.ToString("yyyy"), pDate.ToString("MM"), pDate.ToString("dd"))
End Function
Private Function GetFault(Exception As Exception) As FaultException
Dim oFault As New UnexpectedErrorFault(Exception)
@@ -674,10 +575,7 @@ Public Class EDMIService
End If
End Function
Private Function LogAndThrow(pMessage As String)
_Logger.Warn(pMessage)
Throw New ApplicationException(pMessage)
End Function
#End Region