EDMI: Small Changes

This commit is contained in:
Jonathan Jenne 2021-06-14 16:29:12 +02:00
parent 3a79781156
commit 8c966d8d5f
2 changed files with 147 additions and 71 deletions

View File

@ -78,38 +78,155 @@ Public Class Client
End Try
End Function
Public Class ImportFileOptions
Public KeepExtension As Boolean
End Class
''' <summary>
''' Imports a file by filename
''' </summary>
''' <returns>A document object</returns>
Public Async Function ImportFileAsync(FilePath As String, DocumentType As String, ObjectStoreId As Long, Optional RetentionDays As Integer = 0) As Task(Of Long)
Public Async Function ImportFileAsync(pFilePath As String, pWho As String, pWhen As Date, pObjectStoreType As String, pObjectStoreId As Long, pBusinessEntity As String, ImportOptions As ImportFileOptions) As Task(Of String)
'pObjectId As Long, pStoreType As String, pDate As String, pExtension As String, pKeepExtension As String
Dim oKindType = "DOC"
Try
Dim oFileInfo As New FileInfo(FilePath)
If oFileInfo.Exists = False Then
Throw New FileNotFoundException("Cannot import non-existing file.", FilePath)
If File.Exists(pFilePath) = False Then
Throw New ApplicationException("ImportFileAsync: Path does not exist")
End If
Using oStream As New FileStream(FilePath, FileMode.Open)
Dim oContents(oStream.Length) As Byte
Dim oBytesRead = Await oStream.ReadAsync(oContents, 0, oStream.Length)
Dim oData As New DocumentImportRequest() With {
.FileName = oFileInfo.Name,
.Contents = oContents,
.DocumentType = DocumentType,
.ObjectStoreId = ObjectStoreId,
.RetentionDays = RetentionDays
}
Dim oFileInfo As New FileInfo(pFilePath)
Dim oExtension As String = oFileInfo.Extension
Dim oResponse = Await _channel.ImportFileAsync(oData)
Dim oObjectIdResponse = Await _channel.NewObjectIdAsync(New NewObjectIdRequest With {
.BusinessEntity = pBusinessEntity,
.KindType = oKindType,
.Who = pWho
})
Return oResponse.ObjectId
If oObjectIdResponse.ObjectId = Nothing OrElse oObjectIdResponse.ObjectId = 0 Then
Throw New ApplicationException("ImportFileAsync: Could not get ObjectId")
End If
Dim oFilePathResponse = Await _channel.NewFileObjectAsync(New NewFileObjectRequest With {
.DateImported = pWhen,
.Extension = oExtension,
.KeepExtension = ImportOptions.KeepExtension,
.ObjectId = oObjectIdResponse.ObjectId,
.StoreType = pObjectStoreType
})
If oFilePathResponse.FileObjectPath = Nothing OrElse oFilePathResponse.FileObjectPath = "" Then
Throw New ApplicationException("ImportFileAsync: Could not get FileObject Path")
End If
Using oFileStream As New FileStream(pFilePath, FileMode.Open, FileAccess.Read)
Using oMemoryStream As New MemoryStream()
oFileStream.CopyTo(oMemoryStream)
Dim oContents = oMemoryStream.ToArray()
Dim oFileImportResponse = Await _channel.ImportFileIntoFileObjectAsync(New ImportFileIntoFileObjectRequest With {
.pIDBFilePath = oFilePathResponse.FileObjectPath,
.pIDB_OBJ_ID = oObjectIdResponse.ObjectId,
.pObjectStoreID = pObjectStoreId,
.pWho = pWho,
.Contents = oContents
})
If oFileImportResponse.Result = False Then
Throw New ApplicationException("ImportFileAsync: Could not Import File Contents")
End If
End Using
End Using
Return oFilePathResponse.FileObjectPath
Catch ex As Exception
_logger.Error(ex)
Return Nothing
End Try
End Function
'Public Async Function ImportFileAsync(FilePath As String, DocumentType As String, ObjectStoreId As Long, Optional RetentionDays As Integer = 0) As Task(Of Long)
' Try
' Dim oFileInfo As New FileInfo(FilePath)
' If oFileInfo.Exists = False Then
' Throw New FileNotFoundException("Cannot import non-existing file.", FilePath)
' End If
' Using oStream As New FileStream(FilePath, FileMode.Open)
' Dim oContents(oStream.Length) As Byte
' Dim oBytesRead = Await oStream.ReadAsync(oContents, 0, oStream.Length)
' Dim oData As New DocumentImportRequest() With {
' .FileName = oFileInfo.Name,
' .Contents = oContents,
' .DocumentType = DocumentType,
' .ObjectStoreId = ObjectStoreId,
' .RetentionDays = RetentionDays
' }
' Dim oResponse = Await _channel.ImportFileAsync(oData)
' Return oResponse.ObjectId
' End Using
' Catch ex As Exception
' _logger.Error(ex)
' Throw ex
' End Try
'End Function
Public Function CreateObjectId(pKindType As String, pWho As String, pBusinessEntity As String) As Long
Try
Dim oArgs As New NewObjectIdRequest With {
.KindType = pKindType,
.BusinessEntity = pBusinessEntity,
.Who = pWho
}
Dim oResponse = _channel.NewObjectId(oArgs)
Return oResponse.ObjectId
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Public Function CreateFileStoreObject(pObjectId As Long, pStoreType As String, pDate As String, pExtension As String, pKeepExtension As String) As String
Try
' Remove dot in Extension
If pExtension.StartsWith(".") Then
pExtension = pExtension.Substring(1)
End If
Dim oArgs As New NewFileObjectRequest With {
.ObjectId = pObjectId,
.StoreType = pStoreType,
.DateImported = pDate,
.Extension = pExtension,
.KeepExtension = pKeepExtension
}
Dim oResponse = _channel.NewFileObject(oArgs)
Return oResponse.FileObjectPath
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Public Async Function ImportFileObjectAsync(pContent As Byte(), pWho As String, pObjectId As Long, pObjectStoreID As Short, pFileObjectPath As String) As Task(Of Boolean)
Try
Dim oData As New ImportFileIntoFileObjectRequest() With {
.Contents = pContent,
.pWho = pWho,
.pIDBFilePath = pFileObjectPath,
.pIDB_OBJ_ID = pObjectId,
.pObjectStoreID = pObjectStoreID
}
Dim oResponse = Await _channel.ImportFileIntoFileObjectAsync(oData)
Return oResponse.Result
Catch ex As Exception
_logger.Error(ex)
Return False
End Try
End Function
Public Async Function GetFileByObjectIdAsync(ObjectId As Long) As Task(Of StreamedFile)
Try
Dim oData As New DocumentStreamRequest() With {.ObjectId = ObjectId}
@ -280,58 +397,5 @@ Public Class Client
End Try
End Function
Public Function CreateObjectId(pKindType As String, pWho As String, pBusinessEntity As String) As Long
Try
Dim oArgs As New NewObjectIdRequest With {
.KindType = pKindType,
.BusinessEntity = pBusinessEntity,
.Who = pWho
}
Dim oResponse = _channel.NewObjectId(oArgs)
Return oResponse.ObjectId
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Public Function CreateFileStoreObject(pObjectId As Long, pStoreType As String, pDate As String, pExtension As String, pKeepExtension As String) As String
Try
' Remove dot in Extension
If pExtension.StartsWith(".") Then
pExtension = pExtension.Substring(1)
End If
Dim oArgs As New NewFileObjectRequest With {
.ObjectId = pObjectId,
.StoreType = pStoreType,
.DateImported = pDate,
.Extension = pExtension,
.KeepExtension = pKeepExtension
}
Dim oResponse = _channel.NewFileObject(oargs)
Return oResponse.FileObjectPath
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Public Async Function ImportFileObjectAsync(pContent As Byte(), pWho As String, pObjectId As Long, pObjectStoreID As Short, pFileObjectPath As String) As Task(Of Boolean)
Try
Dim oData As New ImportFileIntoFileObjectRequest() With {
.Contents = pContent,
.pWho = pWho,
.pIDBFilePath = pFileObjectPath,
.pIDB_OBJ_ID = pObjectId,
.pObjectStoreID = pObjectStoreID
}
Dim oResponse = Await _channel.ImportFileIntoFileObjectAsync(oData)
Return oResponse.Result
Catch ex As Exception
_logger.Error(ex)
Return False
End Try
End Function
End Class

View File

@ -526,10 +526,22 @@ Public Class EDMIService
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 = MSSQL_IDB.GetScalarValue(oSQL)
If oObjectId Is Nothing Then
Dim oDetails As New UnexpectedErrorFault(New Exception("NewObjectId: Could not create new ObjectId!"))
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End If
Return New NewObjectIdResponse With {.ObjectId = oObjectId}
Catch ex As FaultException
_Logger.Error(ex)
Throw ex
Catch ex As Exception
_Logger.Error(ex)
Return New NewObjectIdResponse With {.ObjectId = 0}
Dim oDetails As New UnexpectedErrorFault(ex)
Throw New FaultException(Of UnexpectedErrorFault)(oDetails, oDetails.ErrorMessage)
End Try
End Function
@ -615,7 +627,7 @@ Public Class EDMIService
Dim oObjectStore
If pStoreType = ClassConstants.FileStoreArchive Then
oObjectStore = GlobalState.ObjectStores.Item(0)
Else 'pStoreType = ClassConstants.FileStoreWork
Else ' pStoreType = ClassConstants.FileStoreWork Then
oObjectStore = GlobalState.ObjectStores.Item(1)
End If
_Logger.Debug($"oObjectStore is [{oObjectStore.Path}]")