This commit is contained in:
Jonathan Jenne
2020-04-27 14:16:42 +02:00
parent ed29e1b6a9
commit 4cde711955
12 changed files with 38 additions and 325 deletions

View File

@@ -47,7 +47,7 @@ Public Class EDMIService
End If
End Function
#Region "Auth"
#Region "=== Authorization ==="
Private Function TestUserAuth() As Boolean
Try
'Dim oSQL As String = $"SELECT FNIDB_AUTH_USER('{_username}') FROM RDB$DATABASE;"
@@ -60,12 +60,12 @@ Public Class EDMIService
End Try
End Function
#End Region
#Region "Heartbeat"
#Region "=== Heartbeat ==="
Public Function Heartbeat() As Boolean Implements IEDMIService.Heartbeat
Return True
End Function
#End Region
#Region "Request"
#Region "=== Database Request ==="
Public Sub CreateRequest(Name As String, Optional Debug As Boolean = False)
_request = New Request(Name, _username, Firebird, Debug)
_debug = Debug
@@ -90,7 +90,7 @@ Public Class EDMIService
End Sub
#End Region
#Region "Database"
#Region "=== Database (Firebird) ==="
Private Sub TestRequestCreated()
If IsNothing(_request) Then
Throw New Exceptions.NoRequestException()
@@ -144,174 +144,8 @@ Public Class EDMIService
Return New NonQueryResult(ex.Message)
End Try
End Function
#End Region
#Region "Document (with FileContainer)"
Public Function NewFile(FileName As String, Contents() As Byte) As DocumentResultOld Implements IEDMIService.NewFile
Try
Dim oContainer As FileContainer
Dim oContainerId As String
If Not TestUserAuth() Then
Throw New Exception($"User {_username} not authorized.")
End If
oContainer = FileContainer.Create(LogConfig, AppConfig.ContainerPassword)
oContainerId = oContainer.ContainerId
_logger.Debug("Container created with id {0}", oContainerId)
Dim oExtension As String = Path.GetExtension(FileName).Substring(1)
_logger.Debug("File extension of file {0} is {1}", FileName, oExtension)
Dim oSQL = $"SELECT FNICM_NEW_DOC('010', '{oContainerId}', '{GetContainerName(oContainerId)}', '{FileName}', '{oExtension}', '{_username}') FROM RDB$DATABASE;"
Dim oDocId As Int64 = Firebird.GetScalarValue(oSQL)
If oDocId = -1 Then
_logger.Warn("Database returned -1 while creating Document Entry. File was not saved!")
Return Nothing
End If
_logger.Debug("Database Entry created with DocId {0}", oDocId)
oContainer.SetFile(Contents, FileName)
oContainer.SaveAs(GetContainerPath(oContainerId))
_logger.Debug("File saved in Container!", FileName)
Dim oDocument = New DocumentObject(oContainerId, oDocId, FileName)
Return New DocumentResultOld(oDocument)
Catch ex As Exception
_logger.Error(ex)
Return New DocumentResultOld(ex.Message)
End Try
End Function
Public Function UpdateFile(DocObject As DocumentObject, Contents() As Byte) As DocumentResultOld Implements IEDMIService.UpdateFile
Try
TestFileExists(DocObject.ContainerId)
' TODO: update db
Dim oFilePath = GetContainerPath(DocObject.ContainerId)
Dim oFileContainer As FileContainer = FileContainer.Load(LogConfig, AppConfig.ContainerPassword, oFilePath)
oFileContainer.SetFile(Contents, oFileContainer.GetFile.FileName)
oFileContainer.Save()
Return New DocumentResultOld(DocObject)
Catch ex As Exception
_logger.Error(ex)
Return Nothing
End Try
End Function
Public Function GetFile(DocObject As DocumentObject) As DocumentResultOld Implements IEDMIService.GetFile
Try
TestFileExists(DocObject.ContainerId)
Dim oContainerPath = GetContainerPath(DocObject.ContainerId)
Dim oContainer As FileContainer = FileContainer.Load(LogConfig, AppConfig.ContainerPassword, oContainerPath)
Dim oContents As Byte() = oContainer.GetFile().Contents
Return New DocumentResultOld(DocObject, oContents)
Catch ex As Exception
_logger.Error(ex)
Return New DocumentResultOld(ex.Message)
End Try
End Function
Public Function DeleteFile(DocObject As DocumentObject) As Boolean Implements IEDMIService.DeleteFile
Try
TestFileExists(DocObject.ContainerId)
Dim oFilePath = GetContainerPath(DocObject.ContainerId)
IO.File.Delete(oFilePath)
'TODO: Delete doc from db
Return True
Catch ex As Exception
_logger.Error(ex)
Return False
End Try
End Function
Private Function GetContainerPath(ContainerId As String) As String
Return Path.Combine(AppConfig.ContainerPath, GetContainerName(ContainerId))
End Function
Private Function GetContainerName(ContainerId As String) As String
Return ContainerId & ".enc"
End Function
Private Sub TestFileExists(ContainerId)
Dim oContainerPath = GetContainerPath(ContainerId)
If Not IO.File.Exists(oContainerPath) Then
Throw New FileNotFoundException("Container existiert nicht", oContainerPath)
End If
End Sub
Public Function GetDocumentByDocumentId(DocumentId As Long) As DocumentResultOld Implements IEDMIService.GetDocumentByDocumentId
Try
Dim oSQL = $"SELECT GUID, CONTAINER_ID, ORIGINAL_FILENAME FROM TBIDB_DOCUMENT WHERE GUID = {DocumentId}"
Dim oTable = Firebird.GetDatatable(oSQL)
If oTable.Rows.Count = 0 Then
Return New DocumentResultOld("Document not found")
End If
Dim oRow As DataRow = oTable.Rows.Item(0)
Dim oDocument As New DocumentObject(
oRow.Item("CONTAINER_ID"),
oRow.Item("GUID"),
oRow.Item("ORIGINAL_FILENAME")
)
TestFileExists(oDocument.ContainerId)
Dim oContainerPath = GetContainerPath(oDocument.ContainerId)
Dim oContainer As FileContainer = FileContainer.Load(LogConfig, AppConfig.ContainerPassword, oContainerPath)
Dim oContents As Byte() = oContainer.GetFile().Contents
Return New DocumentResultOld(oDocument, oContents)
Catch ex As Exception
Return New DocumentResultOld(ex.Message)
End Try
End Function
Public Function GetDocumentByContainerId(ContainerId As String) As DocumentResultOld Implements IEDMIService.GetDocumentByContainerId
Try
Dim oSQL = $"SELECT GUID, CONTAINER_ID, ORIGINAL_FILENAME FROM TBIDB_DOCUMENT WHERE CONTAINER_ID = '{ContainerId}'"
Dim oTable = Firebird.GetDatatable(oSQL)
If oTable.Rows.Count = 0 Then
Return New DocumentResultOld("Document not found")
End If
Dim oRow As DataRow = oTable.Rows.Item(0)
Dim oDocument As New DocumentObject(
oRow.Item("CONTAINER_ID"),
oRow.Item("GUID"),
oRow.Item("ORIGINAL_FILENAME")
)
TestFileExists(oDocument.ContainerId)
Dim oContainerPath = GetContainerPath(oDocument.ContainerId)
Dim oContainer As FileContainer = FileContainer.Load(LogConfig, AppConfig.ContainerPassword, oContainerPath)
Dim oContents As Byte() = oContainer.GetFile().Contents
Return New DocumentResultOld(oDocument, oContents)
Catch ex As Exception
Return New DocumentResultOld(ex.Message)
End Try
End Function
#End Region
#Region "Document"
#Region "=== Document ==="
''' <summary>
''' Imports a file according to ObjectStoreId
''' </summary>
@@ -436,20 +270,5 @@ Public Class EDMIService
Throw New FaultException(ex.Message)
End Try
End Function
#End Region
#Region "Index"
Public Function NewFileIndex(DocObject As DocumentObject, Syskey As String, LanguageCode As String, Value As String) As IndexResult Implements IEDMIService.NewFileIndex
Try
Dim oSQL = $"SELECT FNIDB_NEW_DOC_VALUE({DocObject.DocumentId},'{Syskey}','{LanguageCode}','{Value}','{_username}') FROM RDB$DATABASE;"
Dim oIndexId As Int64 = Firebird.GetScalarValue(oSQL)
Return New IndexResult(oIndexId)
Catch ex As Exception
_logger.Error(ex)
Return New IndexResult(ex.Message)
End Try
End Function
#End Region
End Class