Export report to Directory

This commit is contained in:
Jonathan Jenne
2021-11-30 12:17:26 +01:00
parent ad7e6a9c84
commit 436293cbb7
16 changed files with 166 additions and 161 deletions

View File

@@ -1,6 +1,6 @@
Imports System.Xml
Imports System.Net.Http
Imports System.Text
Imports System.Net.Http
Imports System.Xml
Imports DigitalData.Modules.Logging
Imports MultiTool.Shared.Documents
Imports MultiTool.Shared.Templates.GeneralConfig
@@ -12,15 +12,17 @@ Namespace Winline
Private ReadOnly Config As WebServiceConfig
Private ReadOnly Serializer As Serializer
Private ReadOnly AppDataPath As String
Private ReadOnly OutputDirectory As String
Private ReadOnly Helpers As Helpers
Public Event WebServiceProgress As EventHandler(Of String)
Public Sub New(pLogConfig As LogConfig, pWebserviceConfig As WebServiceConfig, pAppDataPath As String)
MyBase.New(pLogConfig, pLogConfig.GetLogger())
Public Sub New(pLogConfig As LogConfig, pWebserviceConfig As WebServiceConfig, pOutputDirectoryPath As String)
MyBase.New(pLogConfig)
Helpers = New Helpers(pLogConfig)
Serializer = New Serializer(pLogConfig)
Config = pWebserviceConfig
AppDataPath = pAppDataPath
OutputDirectory = pOutputDirectoryPath
End Sub
Public Async Function TransferDocumentToWinline(pDocument As Document, pMandator As Mandator, Optional pIsTest As Boolean = False) As Task(Of Boolean)
@@ -28,31 +30,27 @@ Namespace Winline
Dim oWS = Config
' --- Get and create path for request/response files
Dim oPath As String = GetBaseWebServicePath()
If IO.Directory.Exists(oPath) = False Then
IO.Directory.CreateDirectory(oPath)
End If
Dim oOutputDirectory = Helpers.GetDateDirectory(OutputDirectory)
RaiseEvent WebServiceProgress(Me, "Einstellungen laden")
' --- Build all teh filenamez and pathz
Dim oBaseFileName As String = GetBaseFilenameForRequest()
Dim oFileName = GetXmlFilenameWithSuffix(oBaseFileName, "Request", "xml")
Dim oBaseFileName As String = Helpers.GetDateTimeString()
Dim oFileName = Helpers.GetFilenameWithSuffix(oBaseFileName, "Request", "xml")
' Relative Path for Webservice Call
Dim oImportRelativeFilePath = IO.Path.Combine(GetDateSubDirectoryPath(oWS.ImportRelativePath), oFileName)
Dim oImportRelativeFilePath = IO.Path.Combine(Helpers.GetDateDirectory(oWS.ImportRelativePath), oFileName)
' Absolute Path to copy Request file
Dim oImportAbsolutePath = IO.Path.Combine(oWS.ImportBasePath, oWS.ImportRelativePath)
Dim oImportAbsoluteFilePath = IO.Path.Combine(GetDateSubDirectoryPath(oImportAbsolutePath), oFileName)
Dim oImportAbsoluteFilePath = IO.Path.Combine(Helpers.GetDateDirectory(oImportAbsolutePath), oFileName)
' --- Serialize Data into XML string
RaiseEvent WebServiceProgress(Me, "Dateien schreiben")
Dim oOutputFilePath = IO.Path.Combine(GetBaseWebServicePath(), oFileName)
Dim oOutputFilePath = IO.Path.Combine(oOutputDirectory, oFileName)
IO.File.WriteAllBytes(oOutputFilePath, oBytes)
' --- Copy file to Winline Import Directory
@@ -91,7 +89,7 @@ Namespace Winline
' --- Bring the action!
Try
Dim oResponse As HttpResponseMessage = Await oClient.GetAsync(oURL)
Await HandleResponse(oResponse, oPath, oBaseFileName)
Await HandleResponse(oResponse, oOutputDirectory, oBaseFileName)
Return True
Catch ex As Exception
@@ -102,7 +100,7 @@ Namespace Winline
End Try
End Function
Private Async Function HandleResponse(pResponse As HttpResponseMessage, pPath As String, pBaseFileNAme As String) As Task
Private Async Function HandleResponse(pResponse As HttpResponseMessage, pOutputPath As String, pBaseFileNAme As String) As Task
pResponse.EnsureSuccessStatusCode()
Dim oResponseBody As String = Await pResponse.Content.ReadAsStringAsync()
Dim oContentType = pResponse.Content.Headers.ContentType.MediaType
@@ -112,7 +110,7 @@ Namespace Winline
Select Case oContentType
Case "text/xml"
WriteResponseFile(pPath, pBaseFileNAme, oResponseBody, "xml")
WriteResponseFile(pOutputPath, pBaseFileNAme, oResponseBody, "xml")
Dim oBytes As Byte() = Encoding.UTF8.GetBytes(oResponseBody)
Using oStream As New IO.MemoryStream(oBytes)
@@ -139,7 +137,7 @@ Namespace Winline
End Using
Case "text/html"
WriteResponseFile(pPath, pBaseFileNAme, oResponseBody, "txt")
WriteResponseFile(pOutputPath, pBaseFileNAme, oResponseBody, "txt")
Throw New ApplicationException(oResponseBody)
@@ -150,7 +148,7 @@ Namespace Winline
Private Function WriteResponseFile(pPath As String, pBaseFileName As String, pResponseBody As String, pExtension As String)
Try
Dim oRequestFileName As String = GetXmlFilenameWithSuffix(pBaseFileName, "Response", pExtension)
Dim oRequestFileName As String = Helpers.GetFilenameWithSuffix(pBaseFileName, "Response", pExtension)
Dim oFilePath As String = IO.Path.Combine(pPath, oRequestFileName)
IO.File.WriteAllText(oFilePath, pResponseBody)
@@ -200,36 +198,6 @@ Namespace Winline
Return oStream.ToArray()
End Using
End Function
Private Function GetBaseWebServicePath() As String
Return IO.Path.Combine(AppDataPath, "WebService")
End Function
Private Function GetBaseFilenameForRequest() As String
Return $"{Now:yyyy-MM-dd_hh-mm-ffff}"
End Function
Private Function GetXmlFilenameWithSuffix(pBaseString As String, pSuffix As String, pExtension As String)
Return $"{pBaseString}-{pSuffix}.{pExtension}"
End Function
Private Function GetDateSubDirectoryPath(pBasePath As String)
Dim oDirectoryPath As String = Now.ToString("yyyy\\MM\\dd")
Dim oFullPath As String = IO.Path.Combine(pBasePath, oDirectoryPath)
If IO.Directory.Exists(oFullPath) = False Then
Try
IO.Directory.CreateDirectory(oFullPath)
Return oFullPath
Catch ex As Exception
Logger.Error(ex)
Return Nothing
End Try
Else
Return oFullPath
End If
End Function
End Class
End Namespace