Modules/EDMI_FILE_OPs/Document.vb
2019-03-01 15:52:00 +01:00

145 lines
5.5 KiB
VB.net

Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.EDMIFileOps.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, EDMI_ServiceAdress As String)
_logger = LogConfig.GetLogger()
_logConfig = LogConfig
Try
Dim binding As New NetTcpBinding()
binding.Security.Mode = SecurityMode.Transport
binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows
binding.MaxReceivedMessageSize = 2147483647
binding.MaxBufferSize = 2147483647
binding.MaxBufferPoolSize = 2147483647
binding.MaxConnections = 10000
binding.ReaderQuotas.MaxArrayLength = 2147483647
binding.ReaderQuotas.MaxStringContentLength = 2147483647
Dim endpointAddress = New EndpointAddress(EDMI_ServiceAdress)
_channelFactory = New ChannelFactory(Of IEDMServiceChannel)(binding, endpointAddress)
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
''' <summary>
''' Imports a file by filename
''' </summary>
''' <param name="FilePath">The filename to import</param>
''' <returns>A document object</returns>
Public Async Function ImportFileAsync(FilePath As String) As Task(Of DocumentResult)
Try
Return Await CreateDocument(FilePath)
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Public Async Function SetFileIndex(DocObject As DocumentObject, Syskey As String, Value As String) As Task
Try
Dim oResult As DocumentResult = _channel
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Private Async Function CreateDocument(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.CreateFileAsync(oName, oContents)
Return oDocObject
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