Imports DigitalData.Modules.Logging Imports DigitalData.Modules.EDMIAPI.EDMIServiceReference Imports System.ServiceModel Imports System.IO Public Class Document Private _logger As Logger Private _logConfig As LogConfig Private _channelFactory As ChannelFactory(Of IEDMServiceChannel) Private _channel As IEDMServiceChannel Public Sub New(LogConfig As LogConfig, ServiceAdress As String) _logger = LogConfig.GetLogger() _logConfig = LogConfig Try Dim oBinding = Channel.GetBinding() Dim oAddress = New EndpointAddress(ServiceAdress) _channelFactory = New ChannelFactory(Of IEDMServiceChannel)(oBinding, oAddress) Connect2NetService() Catch ex As Exception _logger.Error(ex) End Try End Sub Private Function Connect2NetService() Try _channel = Nothing _channel = _channelFactory.CreateChannel() _logger.Info("Successfully connected to EDM_Network Service") AddHandler _channel.Faulted, AddressOf Reconnect _channel.Open() Return True Catch ex As Exception _logger.Error(ex) Return True End Try End Function Private Sub Reconnect() _channel.Abort() Connect2NetService() End Sub ''' ''' Imports a file by filename ''' ''' The filename to import ''' A document object Public Function ImportFile(FilePath As String) As DocumentResult Try Dim oContents As Byte() = File.ReadAllBytes(FilePath) Dim oInfo As New FileInfo(FilePath) Dim oName As String = oInfo.Name Dim oExtension As String = oInfo.Extension.Substring(1) Dim oDocObject = _channel.NewFile(oName, oContents) Return oDocObject Catch ex As Exception _logger.Error(ex) Throw ex End Try End Function ''' ''' Imports a file by filename ''' ''' The filename to import ''' A document object Public Async Function ImportFileAsync(FilePath As String) As Task(Of DocumentResult) Try Dim oContents As Byte() = File.ReadAllBytes(FilePath) Dim oInfo As New FileInfo(FilePath) Dim oName As String = oInfo.Name Dim oExtension As String = oInfo.Extension.Substring(1) Dim oDocObject = Await _channel.NewFileAsync(oName, oContents) Return oDocObject Catch ex As Exception _logger.Error(ex) Throw ex End Try End Function Public Async Function NewFileIndexAsync(DocObject As DocumentObject, Syskey As String, LanguageCode As String, Value As String) As Task(Of IndexResult) Try Dim oResult As IndexResult = Await _channel.NewFileIndexAsync(DocObject, Syskey, LanguageCode, Value) Return oResult Catch ex As Exception _logger.Error(ex) Throw ex End Try End Function Public Function NewFileIndex(DocObject As DocumentObject, Syskey As String, LanguageCode As String, Value As String) As IndexResult Try Dim oResult As IndexResult = _channel.NewFileIndex(DocObject, Syskey, LanguageCode, Value) Return oResult Catch ex As Exception _logger.Error(ex) Throw ex End Try End Function Public Function GetDocumentByDocumentId(DocumentId As Int64) As DocumentResult Try Return _channel.GetDocumentByDocumentId(DocumentId) Catch ex As Exception _logger.Error(ex) Throw ex End Try End Function Public Function GetDocumentByContainerId(ContainerId As String) As DocumentResult Try Return _channel.GetDocumentByContainerId(ContainerId) Catch ex As Exception _logger.Error(ex) Throw ex End Try End Function 'Public Async Function New_EDMI_File(oFILENAME As String, oUserName As String) As Task(Of String) ' Try ' Dim oFileGUID As DocumentResult = Await CreateDocument(oFILENAME) ' Dim oFileRecordID = Nothing ' If Not IsNothing(oFileGUID) Then ' Dim oSQL = $"SELECT FNEDMI_SET_RECORD(""TBEDMI_ADRESSE"",""{oUserName}"",FALSE,NULL,"""",'{oFileGUID._ContainerId}') FROM rdb$database;" ' oFileRecordID = Await New_EDMIFile_CreateDB_Record(oSQL) ' End If ' Return oFileRecordID ' Catch ex As Exception ' _logger.Error(ex) ' Return Nothing ' End Try 'End Function 'Private Async Function New_EDMIFile_CreateDB_Record(FBCommand As String) As Task(Of String) ' Try ' Dim oTimeTotal As TimeSpan ' Dim oStopwatch As New Stopwatch() ' oStopwatch.Start() ' Dim oRecord_ID As String ' Dim oRequestName = Await _channel.CreateDatabaseRequestAsync("CreateEDMFileRecord", True) ' Dim oResult = Await _channel.ReturnScalarAsync(FBCommand) ' oTimeTotal = oStopwatch.Elapsed ' oStopwatch.Reset() ' Await _channel.CloseDatabaseRequestAsync() ' If Not oResult.OK Then ' _logger.Warn($"Unexpected error while executing command: {oResult.ErrorMessage}") ' Else ' oRecord_ID = oResult.Scalar ' _logger.Debug($"SCALAR (SERVICE) {FBCommand} - TIME: {(oTimeTotal.ToString)}") ' End If ' Return oRecord_ID ' Catch ex As Exception ' _logger.Error(ex) ' Return Nothing ' End Try 'End Function 'Public Async Function Load_EDMIFile_2TempPath(oEDMIFile_GUID As String) As Task(Of String) 'Try ' Dim oResult As EDMIServiceReference.ContainerResult = Await _channel.GetFileAsync(oEDMIFile_GUID) ' Dim oTempPath = Path.Combine(Path.GetTempPath(), "EDMI_FileContainer") ' Directory.CreateDirectory(oTempPath) ' Dim oFilePath = Path.Combine(oTempPath, $"{oResult.Container.FileId}.{oResult.Container.Extension}") ' File.WriteAllBytes(oFilePath, oResult.Container.Contents) ' ' Process.Start(oTempPath) ' Return oTempPath 'Catch ex As Exception ' _logger.Error(ex) ' Return Nothing 'End Try 'End Function End Class