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 oGuid As String = Path.GetFileNameWithoutExtension(oFile.FullName)
Dim oConnection = _firebird.GetConnection()
Dim oTransaction = oConnection.BeginTransaction()
Try
oDocument = _zugferd.ExtractZUGFeRDFile(oFile.FullName)
For Each mapping In args.PropertyMap
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
If String.IsNullOrEmpty(propertyValue) Then
Throw New Exception($"Property {mapping.Value} not found or empty.")
End If
'If String.IsNullOrEmpty(propertyValue) Then
' Throw New Exception($"Property {mapping.Value} not found or empty.")
'End If
Console.WriteLine("{0} => {1}", mapping.Key, propertyValue)
oValues.Add(mapping.Key, propertyValue)
' 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
' TODO: Insert into scheise
oTransaction.Commit()
Catch ex As Exception
oTransaction.Rollback()
oMoveDirectory = args.ErrorDirectory
_logger.Error(ex)
_logger.Error(ex, "File {0} was not processesd. Transaction rolled back.")
Finally
_filesystem.MoveTo(oFile.FullName, oMoveDirectory)
oConnection.Close()
'_filesystem.MoveTo(oFile.FullName, oMoveDirectory)
_logger.Info("Finished processing file {0}", oFile.Name)
End Try
Next
End If

View File

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

View File

@ -84,6 +84,7 @@
<Compile Include="My Project\AssemblyInfo.vb" />
</ItemGroup>
<ItemGroup>
<Reference Include="FirebirdSql.Data.FirebirdClient, Version=6.4.0.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c" />
<Reference Include="Microsoft.CSharp" />
<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>

View File

@ -56,6 +56,7 @@ Public Class Firebird
Public Enum TransactionMode
NoTransaction
ExternalTransaction
WithTransaction
End Enum
@ -138,18 +139,18 @@ Public Class Firebird
}.ToString()
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
Return Nothing
ElseIf TransactionMode.WithTransaction Then
Return Connection.BeginTransaction()
ElseIf Mode = TransactionMode.ExternalTransaction Then
Return Transaction
Else
Return Connection.BeginTransaction()
End If
End Function
Private Function MaybeCommitTransaction(Transaction As FbTransaction)
If IsNothing(Transaction) Then
Private Function MaybeCommitTransaction(Transaction As FbTransaction, TransactionMode As TransactionMode)
If TransactionMode = TransactionMode.NoTransaction Then
Return True
Else
Try
@ -168,27 +169,31 @@ Public Class Firebird
''' <param name="SqlCommand">The command to execute</param>
''' <param name="Connection">The Firebird connection to use</param>
''' <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)
If Connection Is Nothing Then
Return Nothing
End If
Dim oTransaction As FbTransaction = Connection.BeginTransaction()
Dim oTransaction = MaybeGetTransaction(Connection, TransactionMode, Transaction)
Try
Dim oCommand As New FbCommand With {
.CommandText = SqlCommand,
.Connection = Connection,
.Transaction = oTransaction
.Connection = Connection
}
If Not IsNothing(oTransaction) Then
oCommand.Transaction = oTransaction
End If
oCommand.ExecuteNonQuery()
Catch ex As Exception
_logger.Error(ex, $"Error in ExecuteNonQuery while executing command: '{SqlCommand}'")
Throw ex
Finally
oTransaction.Commit()
MaybeCommitTransaction(oTransaction, TransactionMode)
End Try
Return True
@ -213,14 +218,14 @@ Public Class Firebird
''' <param name="SqlQuery">The query to execute</param>
''' <param name="Connection">The Firebird connection to use</param>
''' <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)
If Connection Is Nothing Then
Return Nothing
End If
Dim oTransaction As FbTransaction = Connection.BeginTransaction()
Dim oTransaction = MaybeGetTransaction(Connection, TransactionMode, Transaction)
Dim oResult As Object
Try
@ -234,7 +239,7 @@ Public Class Firebird
_logger.Error(ex, $"Error in ReturnScalar while executing command: '{SqlQuery}'")
Throw ex
Finally
oTransaction.Commit()
MaybeCommitTransaction(oTransaction, TransactionMode)
End Try
Return oResult
@ -259,14 +264,14 @@ Public Class Firebird
''' <param name="SqlQuery">The query to execute</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>
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)
If Connection Is Nothing Then
Return Nothing
End If
Dim oTransaction = MaybeGetTransaction(Connection, TransactionMode)
Dim oTransaction = MaybeGetTransaction(Connection, TransactionMode, Transaction)
Dim oDatatable As New DataTable() With {
.TableName = "DDRESULT"
}
@ -283,7 +288,7 @@ Public Class Firebird
_logger.Error(ex, $"Error in GetDatatableWithConnection while executing command: '{SqlQuery}'")
Throw ex
Finally
MaybeCommitTransaction(oTransaction)
MaybeCommitTransaction(oTransaction, TransactionMode)
End Try
Return oDatatable

View File

@ -24,6 +24,7 @@ Partial Class Form1
Private Sub InitializeComponent()
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
Me.Button1 = New System.Windows.Forms.Button()
Me.ListBox1 = New System.Windows.Forms.ListBox()
Me.SuspendLayout()
'
'OpenFileDialog1
@ -39,11 +40,20 @@ Partial Class Form1
Me.Button1.Text = "Button1"
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
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(916, 435)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
@ -53,4 +63,5 @@ Partial Class Form1
Friend WithEvents OpenFileDialog1 As OpenFileDialog
Friend WithEvents Button1 As Button
Friend WithEvents ListBox1 As ListBox
End Class

View File

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