This commit is contained in:
Jonathan Jenne 2018-12-17 14:46:02 +01:00
parent e30bc21cf2
commit 142d9e316c
6 changed files with 61 additions and 25 deletions

View File

@ -62,29 +62,44 @@ Public Class ImportZUGFeRDFiles
Dim oValues As New Dictionary(Of String, String) Dim oValues As New Dictionary(Of String, String)
Dim oGuid As String = Path.GetFileNameWithoutExtension(oFile.FullName) Dim oGuid As String = Path.GetFileNameWithoutExtension(oFile.FullName)
Dim oConnection = _firebird.GetConnection()
Dim oTransaction = oConnection.BeginTransaction()
Try Try
oDocument = _zugferd.ExtractZUGFeRDFile(oFile.FullName) oDocument = _zugferd.ExtractZUGFeRDFile(oFile.FullName)
For Each mapping In args.PropertyMap For Each mapping In args.PropertyMap
Dim propertyValue As String = PropertyValues.GetPropValue(oDocument, mapping.Value) Dim propertyValue As String = PropertyValues.GetPropValue(oDocument, mapping.Value)
_logger.Info("Mapping Property {0} to value {1}. Will be inserted into column {2}", mapping.Value, propertyValue, mapping.Key)
' TODO: Check for missing values ' TODO: Check for missing values
If String.IsNullOrEmpty(propertyValue) Then 'If String.IsNullOrEmpty(propertyValue) Then
Throw New Exception($"Property {mapping.Value} not found or empty.") ' Throw New Exception($"Property {mapping.Value} not found or empty.")
End If 'End If
Console.WriteLine("{0} => {1}", mapping.Key, propertyValue)
oValues.Add(mapping.Key, propertyValue) oValues.Add(mapping.Key, propertyValue)
Next
' TODO: Insert into scheise ' TODO: Insert into scheise
Dim oTableColumnArray = mapping.Key.Split(".")
Dim oTableName = oTableColumnArray.First()
Dim oColumnName = oTableColumnArray.Last()
_logger.Info("Mapping Property {0} to value {1}. Will be inserted into column {2} on table {3}", mapping.Value, propertyValue, oTableName, oColumnName)
Dim oCommand = $"INSERT INTO {oTableName} (FILEID, ""NAME"", ""VALUE"") VALUES ('{oGuid}', '{oColumnName}', '{propertyValue}')"
_firebird.ExecuteNonQueryWithConnection(oCommand, oConnection, Firebird.TransactionMode.ExternalTransaction, oTransaction)
Next
oTransaction.Commit()
Catch ex As Exception Catch ex As Exception
oTransaction.Rollback()
oMoveDirectory = args.ErrorDirectory oMoveDirectory = args.ErrorDirectory
_logger.Error(ex) _logger.Error(ex, "File {0} was not processesd. Transaction rolled back.")
Finally Finally
_filesystem.MoveTo(oFile.FullName, oMoveDirectory) oConnection.Close()
'_filesystem.MoveTo(oFile.FullName, oMoveDirectory)
_logger.Info("Finished processing file {0}", oFile.Name) _logger.Info("Finished processing file {0}", oFile.Name)
End Try End Try
Next Next
End If End If

View File

@ -36,6 +36,10 @@ Public Class PropertyValues
Obj = oInfo.GetValue(Obj, Nothing) Obj = oInfo.GetValue(Obj, Nothing)
If IsNothing(Obj) Then
Return Nothing
End If
If oHasIndex Then If oHasIndex Then
Obj = Obj(0) Obj = Obj(0)
End If End If

View File

@ -84,6 +84,7 @@
<Compile Include="My Project\AssemblyInfo.vb" /> <Compile Include="My Project\AssemblyInfo.vb" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Reference Include="FirebirdSql.Data.FirebirdClient, Version=6.4.0.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL"> <Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.5.11\lib\net45\NLog.dll</HintPath> <HintPath>..\packages\NLog.4.5.11\lib\net45\NLog.dll</HintPath>

View File

@ -56,6 +56,7 @@ Public Class Firebird
Public Enum TransactionMode Public Enum TransactionMode
NoTransaction NoTransaction
ExternalTransaction
WithTransaction WithTransaction
End Enum End Enum
@ -138,18 +139,18 @@ Public Class Firebird
}.ToString() }.ToString()
End Function End Function
Private Function MaybeGetTransaction(Connection As FbConnection, Mode As TransactionMode) Private Function MaybeGetTransaction(Connection As FbConnection, Mode As TransactionMode, Transaction As FbTransaction)
If Mode = TransactionMode.NoTransaction Then If Mode = TransactionMode.NoTransaction Then
Return Nothing Return Nothing
ElseIf TransactionMode.WithTransaction Then ElseIf Mode = TransactionMode.ExternalTransaction Then
Return Connection.BeginTransaction() Return Transaction
Else Else
Return Connection.BeginTransaction() Return Connection.BeginTransaction()
End If End If
End Function End Function
Private Function MaybeCommitTransaction(Transaction As FbTransaction) Private Function MaybeCommitTransaction(Transaction As FbTransaction, TransactionMode As TransactionMode)
If IsNothing(Transaction) Then If TransactionMode = TransactionMode.NoTransaction Then
Return True Return True
Else Else
Try Try
@ -168,27 +169,31 @@ Public Class Firebird
''' <param name="SqlCommand">The command to execute</param> ''' <param name="SqlCommand">The command to execute</param>
''' <param name="Connection">The Firebird connection to use</param> ''' <param name="Connection">The Firebird connection to use</param>
''' <returns>True, if command was executed sucessfully. Otherwise false.</returns> ''' <returns>True, if command was executed sucessfully. Otherwise false.</returns>
Public Function ExecuteNonQueryWithConnection(SqlCommand As String, Connection As FbConnection) As Boolean Public Function ExecuteNonQueryWithConnection(SqlCommand As String, Connection As FbConnection, Optional TransactionMode As TransactionMode = TransactionMode.WithTransaction, Optional Transaction As FbTransaction = Nothing) As Boolean
_logger.Debug("Executing Non-Query: {0}", SqlCommand) _logger.Debug("Executing Non-Query: {0}", SqlCommand)
If Connection Is Nothing Then If Connection Is Nothing Then
Return Nothing Return Nothing
End If End If
Dim oTransaction As FbTransaction = Connection.BeginTransaction() Dim oTransaction = MaybeGetTransaction(Connection, TransactionMode, Transaction)
Try Try
Dim oCommand As New FbCommand With { Dim oCommand As New FbCommand With {
.CommandText = SqlCommand, .CommandText = SqlCommand,
.Connection = Connection, .Connection = Connection
.Transaction = oTransaction
} }
If Not IsNothing(oTransaction) Then
oCommand.Transaction = oTransaction
End If
oCommand.ExecuteNonQuery() oCommand.ExecuteNonQuery()
Catch ex As Exception Catch ex As Exception
_logger.Error(ex, $"Error in ExecuteNonQuery while executing command: '{SqlCommand}'") _logger.Error(ex, $"Error in ExecuteNonQuery while executing command: '{SqlCommand}'")
Throw ex Throw ex
Finally Finally
oTransaction.Commit() MaybeCommitTransaction(oTransaction, TransactionMode)
End Try End Try
Return True Return True
@ -213,14 +218,14 @@ Public Class Firebird
''' <param name="SqlQuery">The query to execute</param> ''' <param name="SqlQuery">The query to execute</param>
''' <param name="Connection">The Firebird connection to use</param> ''' <param name="Connection">The Firebird connection to use</param>
''' <returns>The scalar value if the command was executed successfully. Nothing otherwise.</returns> ''' <returns>The scalar value if the command was executed successfully. Nothing otherwise.</returns>
Public Function GetScalarValueWithConnection(SqlQuery As String, Connection As FbConnection) As Object Public Function GetScalarValueWithConnection(SqlQuery As String, Connection As FbConnection, Optional TransactionMode As TransactionMode = TransactionMode.WithTransaction, Optional Transaction As FbTransaction = Nothing) As Object
_logger.Debug("Fetching Scalar-Value: {0}", SqlQuery) _logger.Debug("Fetching Scalar-Value: {0}", SqlQuery)
If Connection Is Nothing Then If Connection Is Nothing Then
Return Nothing Return Nothing
End If End If
Dim oTransaction As FbTransaction = Connection.BeginTransaction() Dim oTransaction = MaybeGetTransaction(Connection, TransactionMode, Transaction)
Dim oResult As Object Dim oResult As Object
Try Try
@ -234,7 +239,7 @@ Public Class Firebird
_logger.Error(ex, $"Error in ReturnScalar while executing command: '{SqlQuery}'") _logger.Error(ex, $"Error in ReturnScalar while executing command: '{SqlQuery}'")
Throw ex Throw ex
Finally Finally
oTransaction.Commit() MaybeCommitTransaction(oTransaction, TransactionMode)
End Try End Try
Return oResult Return oResult
@ -259,14 +264,14 @@ Public Class Firebird
''' <param name="SqlQuery">The query to execute</param> ''' <param name="SqlQuery">The query to execute</param>
''' <param name="Connection">The Firebird connection to use</param> ''' <param name="Connection">The Firebird connection to use</param>
''' <returns>A datatable containing the results if the command was executed successfully. Nothing otherwise.</returns> ''' <returns>A datatable containing the results if the command was executed successfully. Nothing otherwise.</returns>
Public Function GetDatatableWithConnection(SqlQuery As String, Connection As FbConnection, Optional TransactionMode As TransactionMode = TransactionMode.NoTransaction) As DataTable Public Function GetDatatableWithConnection(SqlQuery As String, Connection As FbConnection, Optional TransactionMode As TransactionMode = TransactionMode.NoTransaction, Optional Transaction As FbTransaction = Nothing) As DataTable
_logger.Debug("Fetching Datatable: {0}", SqlQuery) _logger.Debug("Fetching Datatable: {0}", SqlQuery)
If Connection Is Nothing Then If Connection Is Nothing Then
Return Nothing Return Nothing
End If End If
Dim oTransaction = MaybeGetTransaction(Connection, TransactionMode) Dim oTransaction = MaybeGetTransaction(Connection, TransactionMode, Transaction)
Dim oDatatable As New DataTable() With { Dim oDatatable As New DataTable() With {
.TableName = "DDRESULT" .TableName = "DDRESULT"
} }
@ -283,7 +288,7 @@ Public Class Firebird
_logger.Error(ex, $"Error in GetDatatableWithConnection while executing command: '{SqlQuery}'") _logger.Error(ex, $"Error in GetDatatableWithConnection while executing command: '{SqlQuery}'")
Throw ex Throw ex
Finally Finally
MaybeCommitTransaction(oTransaction) MaybeCommitTransaction(oTransaction, TransactionMode)
End Try End Try
Return oDatatable Return oDatatable

View File

@ -24,6 +24,7 @@ Partial Class Form1
Private Sub InitializeComponent() Private Sub InitializeComponent()
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog() Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
Me.Button1 = New System.Windows.Forms.Button() Me.Button1 = New System.Windows.Forms.Button()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.SuspendLayout() Me.SuspendLayout()
' '
'OpenFileDialog1 'OpenFileDialog1
@ -39,11 +40,20 @@ Partial Class Form1
Me.Button1.Text = "Button1" Me.Button1.Text = "Button1"
Me.Button1.UseVisualStyleBackColor = True Me.Button1.UseVisualStyleBackColor = True
' '
'ListBox1
'
Me.ListBox1.FormattingEnabled = True
Me.ListBox1.Location = New System.Drawing.Point(378, 12)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(526, 407)
Me.ListBox1.TabIndex = 1
'
'Form1 'Form1
' '
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(916, 435) Me.ClientSize = New System.Drawing.Size(916, 435)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.Button1)
Me.Name = "Form1" Me.Name = "Form1"
Me.Text = "Form1" Me.Text = "Form1"
@ -53,4 +63,5 @@ Partial Class Form1
Friend WithEvents OpenFileDialog1 As OpenFileDialog Friend WithEvents OpenFileDialog1 As OpenFileDialog
Friend WithEvents Button1 As Button Friend WithEvents Button1 As Button
Friend WithEvents ListBox1 As ListBox
End Class End Class

View File

@ -72,7 +72,7 @@ Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim args As New WorkerArgs() Dim args As New WorkerArgs()
args = LoadFolderConfig(args) args = LoadFolderConfig(args)
args = LoadPropertyMapFor(args, "ZUGFeRD-Invoice") args = LoadPropertyMapFor(args, "DEFAULT")
Dim job As New Jobs.ImportZUGFeRDFiles(_logConfig, _firebird) Dim job As New Jobs.ImportZUGFeRDFiles(_logConfig, _firebird)