This commit is contained in:
Jonathan Jenne
2021-08-25 16:26:56 +02:00
parent 9e5c41a2bb
commit b99627359a
7 changed files with 230 additions and 34 deletions

View File

@@ -53,9 +53,18 @@ Namespace Winline
Public Sub LoadAccounts(pMandator As Mandator)
Logger.Info("Loading Accounts for Mandator [{0}]", pMandator)
Dim oYear = GetWinLineYear()
Try
Dim oSQL = $"SELECT DISTINCT [c002], [c003] FROM [{pMandator.Server}].[{pMandator.Database}].[dbo].[v005] WHERE c139 IS NULL"
Dim oSQL = $"
SELECT DISTINCT
[c002],
[c003]
FROM [{pMandator.Server}].[{pMandator.Database}].[dbo].[v005]
WHERE
c139 IS NULL
AND mesocomp = '{pMandator.Id}'
AND mesoyear = {oYear}"
Dim oTable = Database.GetDatatable(oSQL)
Dim oAccounts As New List(Of Account)

View File

@@ -1,10 +1,12 @@
Imports System.Xml
Imports System.Net
Imports System.Net.Http
Imports System.Globalization
Imports AutoMapper
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Filesystem
Imports ImporterShared.Documents
Imports System.Text
Namespace Winline
Public Class WebService
@@ -23,39 +25,90 @@ Namespace Winline
Mapper = MapperFactory.GetMapper()
End Sub
Public Function TransferDocumentToWinLine(pDocument As Document) As Boolean
Public Async Function TransferDocumentToWinLine(pDocument As Document) As Task(Of Boolean)
If TypeOf pDocument.Data Is Schemas.Orders.Input.MESOWebService Then
TransferOrderToWinline(pDocument)
Return Await TransferOrderToWinline(pDocument)
Else
Return False
End If
Return True
End Function
Private Function TransferOrderToWinline(pDocument As Document)
Private Async Function TransferOrderToWinline(pDocument As Document) As Task(Of Boolean)
Dim oOrderOutput = TransformOrderToOutput(pDocument.Data)
Dim oWS As Config.WebServiceConfig = Config.Webservice
Dim oFilePath = SerializeOrder(oOrderOutput)
Dim oXmlString = IO.File.ReadAllText(oFilePath)
pDocument.DataOutput = oOrderOutput
Dim oURL As String = $"{oWS.BaseUrl}/ewlservice/import?User={oWS.Username}&Password={oWS.Password}&Company={pDocument.Mandator}&Type=30&Vorlage=EXIM-VRG_orders&ActionCode=1&Byref=0Data={oXmlString}"
Dim oRequest = WebRequest.Create(oURL)
oRequest.Method = "POST"
' --- Get and create path for request/response files
Dim oBaseFileName As String = GetBaseFilenameForRequest()
Dim oPath As String = GetBaseWebServicePath()
If IO.Directory.Exists(oPath) = False Then
IO.Directory.CreateDirectory(oPath)
End If
' --- Serialize Data into XML string
Dim oXmlString = SerializeOrder(oOrderOutput, oBaseFileName)
Dim oReplacedXmlString = oXmlString.Replace("&", "").Replace("ß", "ss")
'Dim oEscapedString = Uri.EscapeUriString(oXmlString)
' --- Prepare URL and HTTP Client
Dim oURL As String = $"{oWS.BaseUrl}/ewlservice/import?User={oWS.Username}&Password={oWS.Password}&Company={pDocument.Mandator}&Type=30&Vorlage=EXIM-VRG_orders&ActionCode=1&Byref=0&Data={oXmlString}"
Dim oClient As New HttpClient()
Logger.Info("Creating HTTP Request to [{0}]", oWS.BaseUrl)
' TODO: Make better lol
' --- Bring the action!
Try
Dim oResponse As HttpResponseMessage = Await oClient.PostAsync(New Uri(oURL), New StringContent(String.Empty))
Await HandleResponse(oResponse, oPath, oBaseFileName)
Using oResponse = oRequest.GetResponse()
Using oStream = oResponse.GetResponseStream()
Using oReader As New IO.StreamReader(oStream)
Dim oData = oReader.ReadToEnd()
Return True
Catch ex As Exception
Logger.Error(ex)
Throw ex
Finally
oClient.Dispose()
End Try
End Function
Private Async Function HandleResponse(pResponse As HttpResponseMessage, pPath As String, pBaseFileNAme As String) As Task
pResponse.EnsureSuccessStatusCode()
Dim oResponseBody As String = Await pResponse.Content.ReadAsStringAsync()
Dim oContentType = pResponse.Content.Headers.ContentType.MediaType
Dim oSerializer = Serializer.GetSerializer(GetType(Schemas.MESOWebServiceResult))
Select Case oContentType
Case "text/xml"
WriteResponseFile(pPath, pBaseFileNAme, oResponseBody, "xml")
Dim oBytes As Byte() = Encoding.UTF8.GetBytes(oResponseBody)
Using oStream As New IO.MemoryStream(oBytes)
Dim oResponseObject As Schemas.MESOWebServiceResult = oSerializer.Deserialize(oStream)
If oResponseObject.OverallSuccess = False Then
Throw New ApplicationException(oResponseObject.ResultDetails.ErrorText)
End If
End Using
End Using
End Using
Return True
Case "text/plain"
WriteResponseFile(pPath, pBaseFileNAme, oResponseBody, "txt")
Throw New ApplicationException(oResponseBody)
Case Else
Throw New ApplicationException(oResponseBody)
End Select
End Function
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 oFilePath As String = IO.Path.Combine(pPath, oRequestFileName)
IO.File.WriteAllText(oFilePath, pResponseBody)
Return True
Catch ex As Exception
Logger.Error(ex)
Return False
End Try
End Function
Private Function TransformOrderToOutput(pData As Schemas.Orders.Input.MESOWebService) As Schemas.Orders.Output.MESOWebService
@@ -76,25 +129,36 @@ Namespace Winline
Return oResult
End Function
Private Function SerializeOrder(pData As Schemas.Orders.Output.MESOWebService) As String
Private Function SerializeOrder(pData As Schemas.Orders.Output.MESOWebService, pBaseFileName As String) As String
Dim oSerializer = Serializer.GetSerializer(GetType(Schemas.Orders.Output.MESOWebService))
Dim oSettings As New XmlWriterSettings With {.Indent = True}
Dim oPath As String = GetBaseWebServicePath()
Dim oPath As String = IO.Path.Combine(FileEx.GetAppDataPath("Digital Data", "EDI Document Importer"), "WebService")
Dim oFileName As String = $"{Now:yyyy-MM-dd-ffff}.xml"
Dim oFilePath As String = IO.Path.Combine(oPath, oFileName)
Dim oRequestFileName As String = GetXmlFilenameWithSuffix(pBaseFileName, "Request", "xml")
Dim oFilePath As String = IO.Path.Combine(oPath, oRequestFileName)
If IO.Directory.Exists(oPath) = False Then
IO.Directory.CreateDirectory(oPath)
End If
Using oWriter = XmlWriter.Create(oFilePath, oSettings)
Using oWriter = XmlWriter.Create(oFilePath, New XmlWriterSettings With {.Indent = True})
oSerializer.Serialize(oWriter, pData)
End Using
Return oFilePath
Using oStringWriter As New IO.StringWriter()
Using oXmlWriter = XmlWriter.Create(oStringWriter, New XmlWriterSettings With {.Indent = False})
oSerializer.Serialize(oXmlWriter, pData)
Return oStringWriter.ToString()
End Using
End Using
End Function
Private Function GetBaseWebServicePath() As String
Return IO.Path.Combine(FileEx.GetAppDataPath("Digital Data", "EDI Document Importer"), "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
End Class