This commit is contained in:
Jonathan Jenne
2021-10-21 16:19:35 +02:00
parent 5c58e05204
commit 8dfc659ef8
25 changed files with 856 additions and 440 deletions

View File

@@ -35,6 +35,9 @@ Namespace Documents
End Get
End Property
Public Overrides Function Equals(obj As Object) As Boolean
Return FullName = DirectCast(obj, Document).FullName
End Function
' Public Type As DocumentType
' Public Data As Object

View File

@@ -31,6 +31,7 @@ Namespace Documents
End If
Logger.Info("Loading files from directory [{0}]", pInputDirectory)
Files.Clear()
Try
Dim oDirectory As New DirectoryInfo(pInputDirectory)
@@ -38,21 +39,31 @@ Namespace Documents
Logger.Debug("Found [{0}] files in directory [{1}]", oFiles.Count, oDirectory)
Files = oFiles.
Select(AddressOf WrapFileInfo).
Select(AddressOf LoadDocumentData2).
Select(Function(d) MatchDataFromWinLine(d, Winline.Mandators)).
ToList()
For Each oFile In oFiles
Dim oDocument = LoadFile(oFile)
Files.Add(oDocument)
Next
Return True
Catch ex As Exception
Logger.Error(ex)
Throw New IO.IOException($"Could not load files from directory {pInputDirectory}", ex)
Throw New IOException($"Could not load files from directory {pInputDirectory}", ex)
End Try
End Function
Public Function LoadFile(pFileInfo As FileInfo) As Document
Dim oFileList As New List(Of FileInfo) From {pFileInfo}
Logger.Info("Loading file [{0}]", pFileInfo.Name)
Return oFileList.
Select(AddressOf WrapFileInfo).
Select(AddressOf LoadDocumentData2).
Select(Function(d) MatchDataFromWinLine(d, Winline.Mandators)).
SingleOrDefault()
End Function
Private Function LoadDocumentData2(pDocument As Document) As Document
Dim oText As String = IO.File.ReadAllText(pDocument.FullName)
Dim oDoc = XDocument.Parse(oText)
@@ -138,54 +149,22 @@ Namespace Documents
Dim oHead As DocumentRow = pDocument.Rows.
Where(Function(r) r.Name.ToUpper.EndsWith("T025")).
SetValue(Sub(r As DocumentRow)
Dim oAccountNumberItem = IIf(r.Fields.ContainsKey("Fakt_Kontonummer"), r.Fields.Item("Fakt_Kontonummer"), Nothing)
Dim oAccountNameItem = Nothing
If r.Fields.ContainsKey("Fakt_Name") Then
oAccountNameItem = r.Fields.Item("Fakt_Name")
End If
Dim oAccount = Winline.TryGetAccount(oAccountNumberItem.Original, pMandator)
If oAccount IsNot Nothing Then
oAccountNumberItem.External = oAccount.Id
If r.Fields.ContainsKey("Fakt_Name") Then
oAccountNameItem.External = oAccount.Name
End If
End If
End Sub).
SetValue(Sub(r As DocumentRow)
Dim oAccountNumberItem = Nothing
If r.Fields.ContainsKey("Lief_Kontonummer") Then
oAccountNumberItem = r.Fields.Item("Lief_Kontonummer")
Else
Exit Sub
End If
Dim oAccountNameItem = Nothing
If r.Fields.ContainsKey("Lief_Name") Then
oAccountNameItem = r.Fields.Item("Lief_Name")
End If
Dim oAccount = Winline.TryGetAccount(oAccountNumberItem.Original, pMandator)
If oAccount IsNot Nothing Then
oAccountNumberItem.External = oAccount.Id
If r.Fields.ContainsKey("Lief_Name") Then
oAccountNameItem.External = oAccount.Name
End If
End If
End Sub).
SetValue(Sub(r As DocumentRow) SetAccountByGLN(r, pMandator, "Fakt_Kontonummer", "Fakt_Name")).
SetValue(Sub(r As DocumentRow) SetAccountByGLN(r, pMandator, "Lief_Kontonummer", "Lief_Name")).
FirstOrDefault()
Dim oPositions As List(Of DocumentRow) = pDocument.Rows.
Where(Function(r) r.Name.ToUpper.EndsWith("T026")).
SetValue(Sub(p As DocumentRow)
Dim oArticleNumberItem = p.Fields.Item("Artikelnummer")
Dim oArticleNumber = Winline.TryGetArticleNumber(oArticleNumberItem.Original, pMandator)
SetValue(Sub(oRow As DocumentRow)
Dim oNumberItem As DocumentRow.FieldValue = oRow.Fields.GetOrDefault("Artikelnummer")
If oNumberItem Is Nothing Then
Exit Sub
End If
Dim oArticleNumber = Winline.TryGetArticleNumber(oNumberItem.Original, pMandator)
If oArticleNumber IsNot Nothing Then
oArticleNumberItem.External = oArticleNumber
oNumberItem.External = oArticleNumber
oNumberItem.Final = oArticleNumber
End If
End Sub).
ToList()
@@ -196,78 +175,47 @@ Namespace Documents
Return pDocument
End Function
Private Function WrapFileInfo(pFileInfo As FileInfo) As Document
Return New Document With {.File = pFileInfo}
Private Sub SetAccountByGLN(oRow As DocumentRow, pMandator As Winline.Mandator, pNumberField As String, pNameField As String)
' Try to read the Account number (which is a GLN really) and account Name
Dim oNumberItem As DocumentRow.FieldValue = oRow.Fields.GetOrDefault(pNumberField)
Dim oNameItem As DocumentRow.FieldValue = oRow.Fields.GetOrDefault(pNameField)
Dim oContainsAccountName As Boolean = Not IsNothing(oNameItem)
If oNumberItem Is Nothing Then
Exit Sub
End If
' Try to find an account that matches the GLN
Dim oAccount = Winline.TryGetAccount(oNumberItem.Original, pMandator)
' If an account was found, set it for External and Final value
If oAccount IsNot Nothing Then
oNumberItem.External = oAccount.Id
oNumberItem.Final = oAccount.Id
If oContainsAccountName Then
oNameItem.External = oAccount.Name
oNameItem.Final = oAccount.Name
Else
oRow.Fields.Add(pNameField, New DocumentRow.FieldValue() With {
.External = oAccount.Name,
.Final = oAccount.Name
})
End If
End If
End Sub
Private Function TryGetDictionaryItem(Of T)(pDictionary As IDictionary(Of String, T), pKey As String) As T
If pDictionary.ContainsKey(pKey) Then
Return pDictionary.Item(pKey)
Else
Return Nothing
End If
End Function
'Private Function LoadDocumentData(pDocument As Document) As Document
' Using oFileStream As New FileStream(pDocument.FullName, FileMode.Open, FileAccess.Read, FileShare.Read)
' Try
' Dim oXmlDocument = New XPathDocument(oFileStream)
' Dim oDocument = oXmlDocument.CreateNavigator()
' Dim oTemplateName = GetTemplateName(oDocument)
' Dim oDocumentType = DocumentMatch.GetDocumentTypeFromTemplateName(oTemplateName)
' Dim oSchemaType As Type = DocumentMatch.GetDocumentSchemaFromDocumentType(oDocumentType)
' ' Read data the first time, working copy
' 'Using oReader = oNavigator.ReadSubtree()
' ' Dim oSerializer = Serializer.GetSerializer(oSchemaType)
' ' pDocument.Data = oSerializer.Deserialize(oReader)
' 'End Using
' ' Read data the second time, archive copy
' 'Using oReader = oNavigator.ReadSubtree()
' ' Dim oSerializer = Serializer.GetSerializer(oSchemaType)
' ' pDocument.DataOriginal = oSerializer.Deserialize(oReader)
' 'End Using
' 'Dim oInstance As Object = Activator.CreateInstance(oSchemaType)
' 'Dim oProps = oSchemaType.GetProperties()
' 'Dim oHead = oProps.
' ' Where(Function(p) p.Name = "Head").
' ' SingleOrDefault()
' 'Dim oDescriptionHead As CustomAttributeData = oHead.CustomAttributes.
' ' Where(Function(d) d.AttributeType.Equals(GetType(DescriptionAttribute))).
' ' SingleOrDefault()
' 'Dim oDescriptionHeadValue = oDescriptionHead.ConstructorArguments.First().Value
' 'Dim oHeadXml = oDocument.Select($"//MESOWebService/{oDescriptionHeadValue}")
' 'Dim oPositions As PropertyInfo = oProps.
' ' Where(Function(p) p.Name = "Positions").
' ' SingleOrDefault()
' 'Dim oDescriptionPos As CustomAttributeData = oPositions.CustomAttributes.
' ' Where(Function(d) d.AttributeType.Equals(GetType(DescriptionAttribute))).
' ' SingleOrDefault()
' 'Dim oDescriptionPosValue = oDescriptionPos.ConstructorArguments.First().Value
' 'Dim oPosXml = oDocument.Select($"//MESOWebService/{oDescriptionPosValue}")
' pDocument.Type = oDocumentType
' Return pDocument
' Catch ex As Exception
' Logger.Error(ex)
' Dim oException As Exception = ex
' If ex.InnerException IsNot Nothing Then
' oException = ex.InnerException
' End If
' Throw oException
' End Try
' End Using
'End Function
Private Function GetTemplateName(pDocument As XPathNavigator) As String
Dim oTemplateName = pDocument.
SelectSingleNode("//MESOWebService").
GetAttribute("Template", "")
Return oTemplateName
Private Function WrapFileInfo(pFileInfo As FileInfo) As Document
Return New Document With {.File = pFileInfo}
End Function
End Class

View File

@@ -0,0 +1,17 @@
Imports System.Runtime.CompilerServices
Module IDictionaryEx
<Extension()>
Function GetOrDefault(Of TKey, TValue)(pDictionary As Dictionary(Of TKey, TValue), pKey As TKey, Optional pOnMissing As TValue = Nothing) As TValue
Dim oValue As TValue
Return IIf(pDictionary.TryGetValue(pKey, oValue), oValue, pOnMissing)
End Function
<Extension()>
Function GetOrInsertNew(Of T, U As New)(dic As Dictionary(Of T, U), key As T) As U
If dic.ContainsKey(key) Then Return dic(key)
Dim newObj As U = New U()
dic(key) = newObj
Return newObj
End Function
End Module

View File

@@ -2,7 +2,7 @@
Module IEnumerableEx
<Extension()>
Function SetValue(Of T)(ByVal items As IEnumerable(Of T), ByVal updateMethod As Action(Of T)) As IEnumerable(Of T)
Function SetValue(Of T)(items As IEnumerable(Of T), updateMethod As Action(Of T)) As IEnumerable(Of T)
For Each item As T In items
updateMethod(item)
Next

View File

@@ -100,6 +100,7 @@
<Compile Include="Documents\DocumentRow.vb" />
<Compile Include="Documents\DocumentType.vb" />
<Compile Include="Documents\DocumentLoader.vb" />
<Compile Include="IDictionaryEx.vb" />
<Compile Include="IEnumerableEx.vb" />
<Compile Include="My Project\AssemblyInfo.vb" />
<Compile Include="My Project\Application.Designer.vb">

View File

@@ -28,6 +28,9 @@ Namespace Winline
Public Const V50_ACCOUNTNUMBER = "c002"
Public Const V50_ACCOUNTNAME = "c003"
Public Const V50_STREETNAME = "c050"
Public Const V50_ZIPCODE = "c051"
Public Const V50_CITYNAME = "c052"
Public Const T45_KEY = "c000"
Public Const T45_NAME = "c001"
@@ -66,9 +69,12 @@ Namespace Winline
Try
Dim oSQL = $"
SELECT DISTINCT
[c002],
[c003]
FROM [{pMandator.Server}].[{pMandator.Database}].[dbo].[v005]
[c002], -- Kundennummer
[c003], -- Kundenname
[c050], -- Straße
[c052], -- Ort
[c051] -- PLZ
FROM [{pMandator.Server}].[{pMandator.Database}].[dbo].[v050]
WHERE
c139 IS NULL
AND mesocomp = '{pMandator.Id}'
@@ -77,9 +83,18 @@ Namespace Winline
Dim oAccounts As New List(Of Account)
For Each oRow As DataRow In oTable.Rows
Dim oAccountNumber As String = Utils.NotNull(oRow.Item(V50_ACCOUNTNUMBER), String.Empty)
Dim oAccountName As String = Utils.NotNull(oRow.Item(V50_ACCOUNTNAME), String.Empty)
Dim oStreetName As String = Utils.NotNull(oRow.Item(V50_STREETNAME), String.Empty)
Dim oZipCode As String = Utils.NotNull(oRow.Item(V50_ZIPCODE), String.Empty)
Dim oCityName As String = Utils.NotNull(oRow.Item(V50_CITYNAME), String.Empty)
oAccounts.Add(New Account With {
.Id = oRow.Item(V05_ACCOUNTID),
.Name = oRow.Item(V05_ACCOUNTNAME),
.Id = oAccountNumber,
.Name = oAccountName,
.StreetName = oStreetName,
.ZipCode = oZipCode,
.CityName = oCityName,
.Mandator = pMandator.Id
})
Next
@@ -188,7 +203,10 @@ Namespace Winline
Dim oSQL = $"
SELECT
[c002], -- Kundennummer
[c003] -- Kundenname
[c003], -- Kundenname
[c050], -- Straße
[c052], -- Ort
[c051] -- PLZ
FROM [{pMandator.Database}].[dbo].[v050]
WHERE [c004] = 2 -- KontoTyp
AND [c260] = '{pGLN}'
@@ -212,10 +230,16 @@ Namespace Winline
Dim oRow As DataRow = oTable.Rows.Item(0)
Dim oAccountNumber As String = Utils.NotNull(oRow.Item(V50_ACCOUNTNUMBER), String.Empty)
Dim oAccountName As String = Utils.NotNull(oRow.Item(V50_ACCOUNTNAME), String.Empty)
Dim oStreetName As String = Utils.NotNull(oRow.Item(V50_STREETNAME), String.Empty)
Dim oZipCode As String = Utils.NotNull(oRow.Item(V50_ZIPCODE), String.Empty)
Dim oCityName As String = Utils.NotNull(oRow.Item(V50_CITYNAME), String.Empty)
Return New Account With {
.Id = oAccountNumber,
.Name = oAccountName,
.StreetName = oStreetName,
.CityName = oCityName,
.ZipCode = oZipCode,
.Mandator = pMandator.Id
}
Catch ex As Exception

View File

@@ -2,6 +2,11 @@
Public Class Account
Public Property Id As String
Public Property Name As String
Public Property StreetName As String
Public Property CityName As String
Public Property ZipCode As String
Public Property Mandator As String
Public Overrides Function GetHashCode() As Integer

View File

@@ -25,82 +25,75 @@ Namespace Winline
'Mapper = MapperFactory.GetMapper()
End Sub
'Public Async Function TransferDocumentToWinLine(pDocument As Document) As Task(Of Boolean)
' If TypeOf pDocument.Data Is Schemas.Orders.Input.MESOWebService Then
' Return Await TransferOrderToWinline(pDocument)
' Else
' Return False
' End If
'End Function
Public Async Function TransferDocumentToWinline(pDocument As Document) As Task(Of Boolean)
Dim oBytes As Byte() = GetBytesFromDocument(pDocument)
Dim oWS As Config.WebServiceConfig = Config.Webservice
'Private Async Function TransferOrderToWinline(pDocument As Document) As Task(Of Boolean)
' Dim oOrderOutput = TransformOrderToOutput(pDocument.Data)
' Dim oWS As Config.WebServiceConfig = Config.Webservice
' --- Get and create path for request/response files
' ' --- 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 oPath As String = GetBaseWebServicePath()
' If IO.Directory.Exists(oPath) = False Then
' IO.Directory.CreateDirectory(oPath)
' End If
' --- Build all teh filenamez and pathz
' ' --- Build all teh filenamez and pathz
Dim oBaseFileName As String = GetBaseFilenameForRequest()
Dim oFileName = GetXmlFilenameWithSuffix(oBaseFileName, "Request", "xml")
' Dim oBaseFileName As String = GetBaseFilenameForRequest()
' Dim oFileName = GetXmlFilenameWithSuffix(oBaseFileName, "Request", "xml")
' Relative Path for Webservice Call
Dim oImportRelativeFilePath = IO.Path.Combine(GetDateSubDirectoryPath(Config.Webservice.ImportRelativePath), oFileName)
' ' Relative Path for Webservice Call
' Dim oImportRelativeFilePath = IO.Path.Combine(GetDateSubDirectoryPath(Config.Webservice.ImportRelativePath), oFileName)
' Absolute Path to copy Request file
Dim oImportAbsolutePath = IO.Path.Combine(Config.Webservice.ImportBasePath, Config.Webservice.ImportRelativePath)
Dim oImportAbsoluteFilePath = IO.Path.Combine(GetDateSubDirectoryPath(oImportAbsolutePath), oFileName)
' ' Absolute Path to copy Request file
' Dim oImportAbsolutePath = IO.Path.Combine(Config.Webservice.ImportBasePath, Config.Webservice.ImportRelativePath)
' Dim oImportAbsoluteFilePath = IO.Path.Combine(GetDateSubDirectoryPath(oImportAbsolutePath), oFileName)
' --- Serialize Data into XML string
' ' --- Serialize Data into XML string
Dim oOutputFilePath = IO.Path.Combine(GetBaseWebServicePath(), oFileName)
IO.File.WriteAllBytes(oOutputFilePath, oBytes)
' Dim oOutputFilePath = SerializeOrder(oOrderOutput, oFileName)
' --- Copy file to Winline Import Directory
' ' --- Copy file to Winline Import Directory
Try
IO.File.Copy(oOutputFilePath, oImportAbsoluteFilePath, True)
Catch ex As Exception
Logger.Error(ex)
Throw ex
End Try
' Try
' IO.File.Copy(oOutputFilePath, oImportAbsoluteFilePath, True)
' Catch ex As Exception
' Logger.Error(ex)
' Throw ex
' End Try
' --- Prepare URL and HTTP Client
Dim oTemplateType = pDocument.TemplateType
Dim oTemplateName = pDocument.TemplateName
' ' --- Prepare URL and HTTP Client
' Dim oTemplateType = 30
' Dim oTemplateName = "EXIM-VRG_orders"
' ActionCode: Should this be a test or not?
' 0 = Testcall
' 1 = Real call
Dim oActionCode = 1
' ' ActionCode: Should this be a test or not?
' ' 0 = Testcall
' ' 1 = Real call
' Dim oActionCode = 1
' Byref: Should data be supplied as file or as string?
' 0 = As String
' 1 = As File (relative to Winline Server directory)
Dim oByref = 1
' ' Byref: Should data be supplied as file or as string?
' ' 0 = As String
' ' 1 = As File (relative to Winline Server directory)
' Dim oByref = 1
Dim oURL As String = $"{oWS.BaseUrl}/ewlservice/import?User={oWS.Username}&Password={oWS.Password}&Company={pDocument.Mandator}&Type={oTemplateType}&Vorlage={oTemplateName}&ActionCode={oActionCode}&Byref={oByref}&Data={oImportRelativeFilePath}"
Dim oClient As New HttpClient()
' Dim oURL As String = $"{oWS.BaseUrl}/ewlservice/import?User={oWS.Username}&Password={oWS.Password}&Company={pDocument.Mandator}&Type={oTemplateType}&Vorlage={oTemplateName}&ActionCode={oActionCode}&Byref={oByref}&Data={oImportRelativeFilePath}"
' Dim oClient As New HttpClient()
Logger.Info("Creating HTTP Request to [{0}]", oWS.BaseUrl)
' Logger.Info("Creating HTTP Request to [{0}]", oWS.BaseUrl)
' --- Bring the action!
Try
Dim oResponse As HttpResponseMessage = Await oClient.GetAsync(oURL)
Await HandleResponse(oResponse, oPath, oBaseFileName)
' ' --- Bring the action!
' Try
' Dim oResponse As HttpResponseMessage = Await oClient.GetAsync(oURL)
' Await HandleResponse(oResponse, oPath, oBaseFileName)
' Return True
' Catch ex As Exception
' Logger.Error(ex)
' Throw ex
' Finally
' oClient.Dispose()
' End Try
'End Function
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()
@@ -115,6 +108,7 @@ Namespace Winline
Dim oBytes As Byte() = Encoding.UTF8.GetBytes(oResponseBody)
Using oStream As New IO.MemoryStream(oBytes)
Dim oResponseObject As Schemas.MESOWebServiceResult = oSerializer.Deserialize(oStream)
Dim oErrorStrings As New List(Of String)
For Each oDetails As Schemas.MESOWebServiceResultResultDetails In oResponseObject.ResultDetails
@@ -124,11 +118,14 @@ Namespace Winline
Else
Logger.Warn("ErrorCode: [{0}]", oDetails.ErrorCode)
Logger.Warn("ErrorText: [{0}]", oDetails.ErrorText)
oErrorStrings.Add($"[{oDetails.ErrorCode}] {oDetails.ErrorText}")
End If
Next
If oResponseObject.OverallSuccess = False Then
Throw New ApplicationException("Request to Webservice was unsuccessful. Please check the logs.")
Dim oMessage = $"Request to Webservice was unsuccessful:{vbNewLine}{vbNewLine}{String.Join(vbNewLine, oErrorStrings.ToArray)}"
Throw New ApplicationException(oMessage)
End If
End Using
@@ -155,6 +152,49 @@ Namespace Winline
End Try
End Function
Private Function GetBytesFromDocument(pDocument As Document) As Byte()
Dim oFilteredFields As New List(Of String) From {
"Fakt_Name",
"Lief_Name"
}
Using oStream As New IO.MemoryStream()
Dim w = XmlWriter.Create(oStream)
w.WriteStartDocument()
w.WriteStartElement("MESOWebService")
w.WriteAttributeString("Template", pDocument.TemplateName)
w.WriteAttributeString("TemplateType", pDocument.TemplateType)
w.WriteAttributeString("option", pDocument.Option)
w.WriteAttributeString("printVoucher", pDocument.PrintVoucher)
For Each oRow In pDocument.Rows
w.WriteStartElement(oRow.Name)
For Each oField As KeyValuePair(Of String, DocumentRow.FieldValue) In oRow.Fields
If oField.Value.Final = String.Empty Then
Continue For
End If
If oFilteredFields.Contains(oField.Key) Then
Continue For
End If
w.WriteStartElement(oField.Key)
w.WriteValue(oField.Value.Final)
w.WriteEndElement() ' Field
Next
w.WriteEndElement() ' Row
Next
w.WriteEndElement() ' MESOWebService
w.WriteEndDocument() ' Document
w.Close()
Return oStream.ToArray()
End Using
End Function
'Private Function TransformOrderToOutput(pData As Schemas.Orders.Input.MESOWebService) As Schemas.Orders.Output.MESOWebService
' Dim oData As Schemas.Orders.Input.MESOWebService = pData
' Dim oResult As Schemas.Orders.Output.MESOWebService = Mapper.Map(Of Schemas.Orders.Output.MESOWebService)(oData)