EDMIService: Small fixes and exception handling

This commit is contained in:
Jonathan Jenne
2020-12-23 14:19:47 +01:00
parent 62ac7860ef
commit 590407fbce
20 changed files with 638 additions and 201 deletions

View File

@@ -8,6 +8,7 @@ Imports System.Data.SqlClient
Imports System.ServiceModel.Description
Imports DigitalData.Services.EDMIService.Messages
Imports DigitalData.Modules.EDMI.API.Rights
Imports DigitalData.Services.EDMIService.Exceptions
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerSession)>
Public Class EDMIService
@@ -23,6 +24,9 @@ Public Class EDMIService
Public Shared GlobalState As GlobalState
Public Shared Scheduler As Scheduler
Public Const TBIDB_DOC_INFO = "TBIDB_DOC_INFO"
Public Const TBIDB_ACCESSRIGHT = "TBIDB_ACCESSRIGHT"
Private ReadOnly _Logger As Logger
Private ReadOnly _Debug As Boolean = False
Private ReadOnly _Username As String
@@ -100,11 +104,20 @@ Public Class EDMIService
Return New TableResult(oFilteredTable)
Else
Throw New ApplicationException($"DataTable {Name} does not exist")
_Logger.Warn($"Datatable {Name} does not exist")
Dim oDetails As New DataTableDoesNotExistFault(Name)
Throw New FaultException(Of DataTableDoesNotExistFault)(oDetails)
End If
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Throw New FaultException(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
#End Region
@@ -115,9 +128,16 @@ Public Class EDMIService
_Logger.Info($"ReturnDatatable_MSSQL_IDB, SQL: {SQL}")
Dim oResult As DataTable = MSSQL_IDB.GetDatatable(SQL)
Return New TableResult(oResult)
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Return New TableResult(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
@@ -126,9 +146,16 @@ Public Class EDMIService
_Logger.Info($"ReturnScalar_MSSQL_IDB, SQL: {SQL}")
Dim oResult As Object = MSSQL_IDB.GetScalarValue(SQL)
Return New ScalarResult(oResult)
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Return New ScalarResult(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
@@ -137,9 +164,16 @@ Public Class EDMIService
_Logger.Info($"ExecuteNonQuery_MSSQL_IDB, SQL: {SQL}")
Dim oResult As Boolean = MSSQL_IDB.ExecuteNonQuery(SQL)
Return New NonQueryResult()
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Return New NonQueryResult(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
#End Region
@@ -150,9 +184,16 @@ Public Class EDMIService
_Logger.Info($"ReturnDatatable_MSSQL_ECM, SQL: {SQL}")
Dim oResult As DataTable = MSSQL_ECM.GetDatatable(SQL)
Return New TableResult(oResult)
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Return New TableResult(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
@@ -161,9 +202,16 @@ Public Class EDMIService
_Logger.Info($"ReturnScalar_MSSQL_ECM, SQL: {SQL}")
Dim oResult As Object = MSSQL_ECM.GetScalarValue(SQL)
Return New ScalarResult(oResult)
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Return New ScalarResult(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
@@ -172,9 +220,16 @@ Public Class EDMIService
_Logger.Info($"ExecuteNonQuery_MSSQL_ECM, SQL: {SQL}")
Dim oResult As Boolean = MSSQL_ECM.ExecuteNonQuery(SQL)
Return New NonQueryResult()
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Return New NonQueryResult(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
#End Region
@@ -185,9 +240,16 @@ Public Class EDMIService
_Logger.Info($"ReturnDatatable, SQL: {SQL}")
Dim oResult As DataTable = Firebird.GetDatatable(SQL)
Return New TableResult(oResult)
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Return New TableResult(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
@@ -196,9 +258,16 @@ Public Class EDMIService
_Logger.Info($"ReturnScalar, SQL: {SQL}")
Dim oResult As Object = Firebird.GetScalarValue(SQL)
Return New ScalarResult(oResult)
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Return New ScalarResult(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
@@ -207,9 +276,16 @@ Public Class EDMIService
_Logger.Info($"ExecuteNonQuery, SQL: {SQL}")
Dim oResult As Boolean = Firebird.ExecuteNonQuery(SQL)
Return New NonQueryResult()
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Return New NonQueryResult(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
#End Region
@@ -220,14 +296,7 @@ Public Class EDMIService
''' </summary>
''' <returns></returns>
Public Function ImportFile(Data As DocumentImportRequest) As DocumentImportResponse Implements IEDMIService.ImportFile
Dim oObjectStore = GlobalState.ObjectStores.
Where(Function(s) s.Id = Data.ObjectStoreId).
FirstOrDefault()
If oObjectStore Is Nothing Then
Throw New FaultException($"Object Store with Id [{Data.ObjectStoreId}] does not exist!")
End If
Dim oObjectStore = GlobalState.ObjectStores.First()
Dim EDMIPath = New EDMI.File.Path(LogConfig, oObjectStore.Path)
' TODO:
@@ -244,12 +313,7 @@ Public Class EDMIService
Try
Directory.CreateDirectory(oDirectoryPath)
Catch ex As Exception
_Logger.Error(ex)
Throw New FaultException(ex.Message)
End Try
Try
Dim oVersionedFileName As String = Filesystem.GetVersionedFilename(oAbsolutePath)
_Logger.Info("ImportFile: Saving file [{0}] to path [{1}]", Data.FileName, oVersionedFileName)
@@ -270,37 +334,37 @@ Public Class EDMIService
Dim oObjectId = MSSQL_IDB.GetScalarValue(oCommand, "@IDB_OBJ_ID")
Return New DocumentImportResponse() With {.ObjectId = oObjectId}
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Throw New FaultException(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
Public Function GetFileByObjectId(Data As DocumentStreamRequest) As DocumentStreamResponse Implements IEDMIService.GetFileByObjectId
Try
Dim oSQL As String = $"SELECT ObjectStoreId FROM VWIDB_DOC_DATA WHERE IDB_OBJ_ID = {Data.ObjectId}"
Dim oObjectStoreId = MSSQL_IDB.GetScalarValue(oSQL)
Dim oFullPath = GetFullPathForObjectId(Data.ObjectId)
Dim oObjectStore = GlobalState.ObjectStores.
Where(Function(s) s.Id = oObjectStoreId).
FirstOrDefault()
Dim oSQL2 As String = $"SELECT DocRelativePath FROM VWIDB_DOC_DATA WHERE IDB_OBJ_ID = {Data.ObjectId}"
Dim oPath As String = MSSQL_IDB.GetScalarValue(oSQL2)
If IsNothing(oPath) Then
Throw New FaultException($"Object [{Data.ObjectId}] does not exist in database!")
If oFullPath = String.Empty Then
Dim oDetails As New ObjectDoesNotExistFault(Data.ObjectId)
_Logger.Warn("GetFileByObjectId: " & oDetails.ErrorMessage)
Throw New FaultException(Of ObjectDoesNotExistFault)(oDetails, oDetails.ErrorMessage)
End If
Dim EDMIPath As New EDMI.File.Path(LogConfig, oObjectStore.Path)
Dim oFullPath = EDMIPath.GetFullPathFromRelativePath(oPath)
_Logger.Debug("GetFileByObjectId: Loading file [{0}]", oFullPath)
Dim oFileInfo As New FileInfo(oFullPath)
If Not oFileInfo.Exists Then
Throw New FaultException($"Object [{Data.ObjectId}] does not exist on filesystem!")
Dim oDetails As New ObjectDoesNotExistFault(Data.ObjectId)
_Logger.Warn("GetFileByObjectId: " & oDetails.ErrorMessage)
Throw New FaultException(Of ObjectDoesNotExistFault)(oDetails, oDetails.ErrorMessage)
End If
Dim oDestination As New MemoryStream()
@@ -309,109 +373,145 @@ Public Class EDMIService
End Using
oDestination.Seek(0, SeekOrigin.Begin)
Dim oMessage As New Messages.DocumentStreamResponse() With {
Dim oMessage As New DocumentStreamResponse() With {
.FileName = oFileInfo.Name,
.FileContents = oDestination
}
Return oMessage
Catch ex As IOException
Catch ex As FaultException
_Logger.Error(ex)
Throw New FaultException($"Object [{Data.ObjectId}] could not be streamed!")
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Throw New FaultException(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
Public Function ListFilesForUser() As DocumentListResponse Implements IEDMIService.ListFilesForUser
Try
Dim oSQL = $"SELECT * FROM VWIDB_DOC_DATA"
Dim oDatatable As DataTable = MSSQL_IDB.GetDatatable(oSQL)
oDatatable.TableName = "DocumentList"
Dim oDatatable = GetFileList()
Return New DocumentListResponse() With {
.Datatable = oDatatable
}
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Throw New FaultException(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
Public Function GetFileInfoByObjectId(Data As DocumentInfoRequest) As DocumentInfoResponse Implements IEDMIService.GetFileInfoByObjectId
Try
Dim oAccessRight = GetAccessRight(Data.UserId, Data.ObjectId)
Dim oFullPath = GetFullPath(Data.ObjectId)
Dim oAccessRight = GetAccessRightForObjectId(Data.UserId, Data.ObjectId)
Dim oFullPath = GetFullPathForObjectId(Data.ObjectId)
If oFullPath = String.Empty Then
Dim oDetails As New ObjectDoesNotExistFault(Data.ObjectId)
Throw New FaultException(Of ObjectDoesNotExistFault)(oDetails)
End If
Return New DocumentInfoResponse With {
.FileRight = oAccessRight,
.FullPath = oFullPath
}
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Throw New FaultException(ex.Message)
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
Private Function GetFullPath(ObjectId As Long) As String
Dim oTableName As String = "TBIDB_DOC_INFO"
If Not GlobalState.TableStore.Tables.Contains(oTableName) Then
_Logger.Warn("GetFullPath: Document info table does not exist!")
Return String.Empty
End If
Dim oTable As DataTable = GlobalState.TableStore.Tables.Item(oTableName)
Dim oRows As List(Of DataRow) = oTable.Select($"IDB_OBJ_ID = {ObjectId}").ToList()
Dim oFullPath As String
If oRows.Count = 0 Then
_Logger.Warn("GetFullPath: Full path does not exist for on object [{1}]", ObjectId)
oFullPath = String.Empty
Else
Dim oRow As DataRow = oRows.First()
oFullPath = oRow.Item("FULL_PATH")
End If
Return oFullPath
End Function
Private Function GetAccessRight(UserId As Long, ObjectId As Long) As AccessRight
Dim oTableName As String = "TBIDB_ACCESSRIGHT"
If Not GlobalState.TableStore.Tables.Contains(oTableName) Then
_Logger.Warn("GetAccessRight: Access right table does not exist!")
Return AccessRight.VIEW_ONLY
End If
Dim oTable As DataTable = GlobalState.TableStore.Tables.Item(oTableName)
Dim oRows As List(Of DataRow) = oTable.Select($"IDB_OBJ_ID = {ObjectId} AND USR_ID = {UserId}").ToList()
Dim oRight As AccessRight = AccessRight.VIEW_ONLY
If oRows.Count = 0 Then
_Logger.Warn("GetAccessRight: Access right assignment does not exist for user [{0}] on object [{1}]", UserId, ObjectId)
Return AccessRight.VIEW_ONLY
Else
If oRows.Count > 1 Then
_Logger.Warn("GetAccessRight: More than one access right assignment found for user [{0}] on object [{1}]", UserId, ObjectId)
Public Function GetFileList() As DataTable
Try
If Not GlobalState.TableStore.Tables.Contains(TBIDB_DOC_INFO) Then
_Logger.Warn("GetFileList: Document info table does not exist!")
Return Nothing
End If
Dim oRow As DataRow = oRows.First()
Dim oRightAsInt = oRow.Item("ACCESSRIGHT")
Dim oTable As DataTable = GlobalState.TableStore.Tables.Item(TBIDB_DOC_INFO)
Return oTable
Catch ex As Exception
_Logger.Warn("GetFileList: Unexpected Error while getting file list.")
_Logger.Error(ex)
Return Nothing
End Try
End Function
Private Function GetFullPathForObjectId(ObjectId As Long) As String
Try
If Not GlobalState.TableStore.Tables.Contains(TBIDB_DOC_INFO) Then
_Logger.Warn("GetFullPathForObjectId: Document info table does not exist!")
Return String.Empty
End If
oRight = Utils.ToEnum(Of AccessRight)(oRightAsInt)
End If
Dim oTable As DataTable = GlobalState.TableStore.Tables.Item(TBIDB_DOC_INFO)
Dim oRows As List(Of DataRow) = oTable.Select($"IDB_OBJ_ID = {ObjectId}").ToList()
Return oRight
Dim oFullPath As String
If oRows.Count = 0 Then
_Logger.Warn("GetFullPathForObjectId: Full path does not exist for on object [{1}]", ObjectId)
oFullPath = String.Empty
Else
Dim oRow As DataRow = oRows.First()
oFullPath = oRow.Item("FULL_PATH")
End If
Return oFullPath
Catch ex As Exception
_Logger.Warn("GetFullPathForObjectId: Unexpected Error while getting full path for object [{0}].", ObjectId)
_Logger.Error(ex)
Return String.Empty
End Try
End Function
Private Function GetAccessRightForObjectId(UserId As Long, ObjectId As Long) As AccessRight
Try
If Not GlobalState.TableStore.Tables.Contains(TBIDB_ACCESSRIGHT) Then
_Logger.Warn("GetAccessRightForObjectId: Access right table does not exist!")
Return AccessRight.VIEW_ONLY
End If
Dim oTable As DataTable = GlobalState.TableStore.Tables.Item(TBIDB_ACCESSRIGHT)
Dim oRows As List(Of DataRow) = oTable.Select($"IDB_OBJ_ID = {ObjectId} AND USR_ID = {UserId}").ToList()
Dim oRight As AccessRight
If oRows.Count = 0 Then
_Logger.Warn("GetAccessRightForObjectId: Access right assignment does not exist for user [{0}] on object [{1}]", UserId, ObjectId)
Return AccessRight.VIEW_ONLY
Else
If oRows.Count > 1 Then
_Logger.Warn("GetAccessRightForObjectId: More than one access right assignment found for user [{0}] on object [{1}]", UserId, ObjectId)
End If
Dim oRow As DataRow = oRows.First()
Dim oRightAsInt = oRow.Item("ACCESSRIGHT")
oRight = Utils.ToEnum(Of AccessRight)(oRightAsInt)
End If
Return oRight
Catch ex As Exception
_Logger.Warn("GetAccessRightForObjectId: Unexpected Error while getting access right for object [{0}].", ObjectId)
_Logger.Error(ex)
Return AccessRight.VIEW_ONLY
End Try
End Function
#End Region
End Class