Compare commits

...

2 Commits

Author SHA1 Message Date
Jonathan Jenne
623e1717d6 clean up 2022-04-27 10:48:33 +02:00
Jonathan Jenne
fa4ac39074 Rework Patterns to allow for row based and final sql replacement when importing 2022-04-26 16:19:17 +02:00
7 changed files with 106 additions and 67 deletions

View File

@@ -10,6 +10,9 @@
Public Const FUNCTION_FIELD = "FIELD" Public Const FUNCTION_FIELD = "FIELD"
Public Const FUNCTION_RUNNINGNUMBER = "RUNNINGNUMBER" Public Const FUNCTION_RUNNINGNUMBER = "RUNNINGNUMBER"
Public Const PLACEHOLDER_CONST = "CONST"
Public Const PLACEHOLDER_FIELD = "FIELD"
Public Const TEMPLATE_TYPE_DATE = "xs:date" Public Const TEMPLATE_TYPE_DATE = "xs:date"
Public Const TEMPLATE_TYPE_INTEGER = "xs:integer" Public Const TEMPLATE_TYPE_INTEGER = "xs:integer"
Public Const TEMPLATE_TYPE_DECIMAL = "xs:decimal" Public Const TEMPLATE_TYPE_DECIMAL = "xs:decimal"

View File

@@ -43,6 +43,7 @@ Namespace Documents
Dim oDocumentErrors As List(Of String) = DocumentErrors. Dim oDocumentErrors As List(Of String) = DocumentErrors.
Select(Function(err) err.ToString()). Select(Function(err) err.ToString()).
ToList() ToList()
Return oDocumentErrors. Return oDocumentErrors.
Concat(oRowErrors). Concat(oRowErrors).
ToList() ToList()

View File

@@ -2,21 +2,29 @@
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Imports MultiTool.Common.Documents Imports MultiTool.Common.Documents
Imports MultiTool.Common.Templates Imports MultiTool.Common.Templates
Imports MultiTool.Common.Constants
Imports MultiTool.Common.Winline.Entities Imports MultiTool.Common.Winline.Entities
Public Class Patterns Public Class Patterns
Inherits BaseClass Inherits BaseClass
Private ReadOnly GeneralConfig As GeneralConfig Private ReadOnly GeneralConfig As GeneralConfig
Private ReadOnly Regex As New Regex("{#(\w+)#([\w\s_-]+)}+")
Public Sub New(pLogConfig As LogConfig, pGeneralConfig As GeneralConfig) Public Sub New(pLogConfig As LogConfig, pGeneralConfig As GeneralConfig)
MyBase.New(pLogConfig) MyBase.New(pLogConfig)
GeneralConfig = pGeneralConfig GeneralConfig = pGeneralConfig
End Sub End Sub
Public Function ReplaceForImport(pDocument As Documents.Document, pRow As DocumentRow, pReportFileName As String, oString As String) ''' <summary>
Dim oRegex = New Regex("{#(\w+)#([\w\s_-]+)}+") ''' Replaces placeholders in an sql marked as final sql in the template.
Dim oMatches As MatchCollection = oRegex.Matches(oString) ''' </summary>
''' <remarks>
''' This SQL does not replace field-value placeholders!
''' This is a globally applied function!
''' </remarks>
Public Function ReplaceForImportFinalSQL(pDocument As Document, pReportFileName As String, pString As String) As String
Dim oMatches As MatchCollection = Regex.Matches(pString)
For Each oMatch As Match In oMatches For Each oMatch As Match In oMatches
Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value
@@ -24,56 +32,39 @@ Public Class Patterns
Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value
Select Case oPlaceholderType.ToUpper Select Case oPlaceholderType.ToUpper
Case "FIELD" Case PLACEHOLDER_CONST
Dim oFieldName = oPlaceholderValue
Dim oTargetField = pRow.Fields.
Where(Function(field) field.Key = oFieldName).
SingleOrDefault()
oString = oString.Replace(oPlaceholderString, oTargetField.Value.Final)
Case "CONST"
Dim oValue = "" Dim oValue = ""
Select Case oPlaceholderValue.ToUpper Select Case oPlaceholderValue.ToUpper
Case "MESOYEAR"
oValue = GeneralConfig.GetWinLineYear()
Case "MESOCOMP"
oValue = pDocument.Mandator.Id
Case "USERNAME"
oValue = Environment.UserName
Case "CURRENTDATE"
oValue = Now.ToString()
Case "FILENAME" Case "FILENAME"
oValue = pDocument.FileName oValue = pDocument.FileName
Case "FILENAME_REPORT" Case "FILENAME_REPORT"
oValue = pReportFileName oValue = pReportFileName
Case "MANDATORDB"
oValue = pDocument.Mandator.Database
Case Else Case Else
oValue = "" oValue = ReplaceBasicValues(oPlaceholderValue, pDocument.Mandator)
End Select End Select
If oValue <> "" Then If oValue <> "" Then
oString = oString.Replace(oPlaceholderString, oValue) pString = pString.Replace(oPlaceholderString, oValue)
End If End If
End Select End Select
Next Next
Return oString Return pString
End Function End Function
Public Function ReplaceForExport(pDocument As ExportDocument, pMandator As Mandator, oString As String) ''' <summary>
Dim oRegex = New Regex("{#(\w+)#([\w\s_-]+)}+") '''
Dim oMatches As MatchCollection = oRegex.Matches(oString) ''' </summary>
''' <param name="pDocument"></param>
''' <param name="pRow"></param>
''' <param name="pString"></param>
''' <returns></returns>
Public Function ReplaceForImport(pDocument As Document, pRow As DocumentRow, pString As String)
Dim oMatches As MatchCollection = Regex.Matches(pString)
For Each oMatch As Match In oMatches For Each oMatch As Match In oMatches
Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value
@@ -81,22 +72,48 @@ Public Class Patterns
Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value
Select Case oPlaceholderType.ToUpper Select Case oPlaceholderType.ToUpper
Case "CONST" Case PLACEHOLDER_FIELD
Dim oFieldName = oPlaceholderValue
Dim oTargetField = pRow.Fields.
Where(Function(field) field.Key = oFieldName).
SingleOrDefault()
pString = pString.Replace(oPlaceholderString, oTargetField.Value.Final)
Case PLACEHOLDER_CONST
Dim oValue = "" Dim oValue = ""
Select Case oPlaceholderValue.ToUpper Select Case oPlaceholderValue.ToUpper
Case "MESOYEAR" Case "FILENAME"
oValue = GeneralConfig.GetWinLineYear() oValue = pDocument.FileName
Case "MESOCOMP" Case Else
oValue = pMandator.Id oValue = ReplaceBasicValues(oPlaceholderValue, pDocument.Mandator)
Case "USERNAME" End Select
oValue = Environment.UserName
Case "CURRENTDATE" If oValue <> "" Then
oValue = Now.ToString() pString = pString.Replace(oPlaceholderString, oValue)
End If
End Select
Next
Return pString
End Function
Public Function ReplaceForExport(pDocument As ExportDocument, pMandator As Mandator, oString As String)
Dim oMatches As MatchCollection = Regex.Matches(oString)
For Each oMatch As Match In oMatches
Dim oPlaceholderString As String = oMatch.Groups.Item(0)?.Value
Dim oPlaceholderType As String = oMatch.Groups.Item(1)?.Value
Dim oPlaceholderValue As String = oMatch.Groups.Item(2)?.Value
Select Case oPlaceholderType.ToUpper
Case PLACEHOLDER_CONST
Dim oValue = ""
Select Case oPlaceholderValue.ToUpper
Case "FILENAME" Case "FILENAME"
oValue = pDocument.FilenameExport oValue = pDocument.FilenameExport
@@ -106,11 +123,9 @@ Public Class Patterns
Case "RUNNINGNUMBER" Case "RUNNINGNUMBER"
oValue = pDocument.RunningNumber oValue = pDocument.RunningNumber
Case "MANDATORDB"
oValue = pMandator.Database
Case Else Case Else
oValue = "" oValue = ReplaceBasicValues(oPlaceholderValue, pMandator)
End Select End Select
@@ -122,4 +137,31 @@ Public Class Patterns
Return oString Return oString
End Function End Function
''' <summary>
''' Replaces basic values in the CONST namespace that only depend on the current mandator
''' </summary>
''' <returns>Value that will be used to replaced the placeholder</returns>
Public Function ReplaceBasicValues(pPlaceholderValue As String, pMandator As Mandator) As String
Select Case pPlaceholderValue.ToUpper
Case "MESOYEAR"
Return GeneralConfig.GetWinLineYear()
Case "MESOCOMP"
Return pMandator.Id
Case "USERNAME"
Return Environment.UserName
Case "MANDATORDB"
Return pMandator.Database
Case "CURRENTDATE"
Return Now.ToString()
Case Else
Return String.Empty
End Select
End Function
End Class End Class

View File

@@ -965,7 +965,7 @@ Namespace Winline
End If End If
End Function End Function
Public Async Function ExecuteFinalSQL(pDocument As ExportDocument, pTemplate As Template, pMandator As Mandator) As Task(Of Boolean) Public Async Function ExecuteFinalSQLForExport(pDocument As ExportDocument, pTemplate As Template, pMandator As Mandator) As Task(Of Boolean)
Try Try
Dim oSql As String = Patterns.ReplaceForExport(pDocument, pMandator, pTemplate.FinalSQL) Dim oSql As String = Patterns.ReplaceForExport(pDocument, pMandator, pTemplate.FinalSQL)
Return Await Database.ExecuteNonQueryAsync(oSql) Return Await Database.ExecuteNonQueryAsync(oSql)
@@ -976,6 +976,17 @@ Namespace Winline
End Try End Try
End Function End Function
Public Async Function ExecuteFinalSQLForImport(pDocument As Document, pTemplate As Template, pReportFilename As String) As Task(Of Boolean)
Try
Dim oSql As String = Patterns.ReplaceForImportFinalSQL(pDocument, pReportFilename, pTemplate.FinalSQL)
Return Await Database.ExecuteNonQueryAsync(oSql)
Catch ex As Exception
Logger.Error(ex)
Return False
End Try
End Function
Private Function GetDocumentFromDataRow(pDataRow As DataRow) As ExportDocument Private Function GetDocumentFromDataRow(pDataRow As DataRow) As ExportDocument
Dim oAccountNumber = pDataRow.Item("ACCOUNT_NUMBER") Dim oAccountNumber = pDataRow.Item("ACCOUNT_NUMBER")
Dim oRunningNumber As String = pDataRow.Item("RUNNING_NUMBER") Dim oRunningNumber As String = pDataRow.Item("RUNNING_NUMBER")

View File

@@ -245,7 +245,7 @@ Public Class frmExportMain
SplashScreenManager.SetWaitFormDescription(oMessage) SplashScreenManager.SetWaitFormDescription(oMessage)
Await WebService.ExportDocumentFromWinline(oDocument, CurrentTemplate, oMandator) Await WebService.ExportDocumentFromWinline(oDocument, CurrentTemplate, oMandator)
Dim oFinalSqlResult = Await Winline.ExecuteFinalSQL(oDocument, CurrentTemplate, oMandator) Dim oFinalSqlResult = Await Winline.ExecuteFinalSQLForExport(oDocument, CurrentTemplate, oMandator)
If oFinalSqlResult = False Then If oFinalSqlResult = False Then
Throw New DatabaseException("FinalSQL was not executed successfully!") Throw New DatabaseException("FinalSQL was not executed successfully!")

View File

@@ -741,17 +741,12 @@ Public Class frmImportMain
Return True Return True
End If End If
WebService.RaiseWebServiceProgress("Bericht erzeugen")
' Generate the report ' Generate the report
WebService.RaiseWebServiceProgress("Bericht erzeugen")
' TODO: Call GetReportFilePath in GenerateReport to better control
' when filename is generated and use it as placeholder
Dim oReportResult = ReportGenerator.GenerateReport(pDocument, CurrentTemplate) Dim oReportResult = ReportGenerator.GenerateReport(pDocument, CurrentTemplate)
WebService.RaiseWebServiceProgress("Bericht exportieren")
' Export it to pdf ' Export it to pdf
WebService.RaiseWebServiceProgress("Bericht exportieren")
oReportResult.Report.ExportToPdf(oReportResult.FileName) oReportResult.Report.ExportToPdf(oReportResult.FileName)
' Mark Document as Imported, will be moved on Form Close or File Reload ' Mark Document as Imported, will be moved on Form Close or File Reload

View File

@@ -310,20 +310,7 @@ Public Class frmRowEditor
If oField.Value.HasError Then If oField.Value.HasError Then
e.Appearance.BackColor = Color.LightCoral e.Appearance.BackColor = Color.LightCoral
End If End If
' TODO: Remove this workaround if the conditions for errors are good now
'If e.DisplayText = String.Empty And (
' oField.Value.Error = FieldError.AccountNotFound Or
' oField.Value.Error = FieldError.ArticleNotFound) Then
' e.Appearance.BackColor = Color.LightCoral
'End If
End If End If
'If oField.Value.HasError Then
' e.Appearance.BackColor = Color.LightCoral
'End If
End Sub End Sub
Private Sub frmRowEditor_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown Private Sub frmRowEditor_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown