From 720618f29b015ac3bcc14367dd1693c8a508bee1 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Fri, 5 Apr 2019 15:51:42 +0200 Subject: [PATCH] Add MSSQL Functions to ZUGFeRD Service --- DDZUGFeRDService/App.config | 6 ++ .../My Project/Settings.Designer.vb | 18 +++++ DDZUGFeRDService/My Project/Settings.settings | 6 ++ DDZUGFeRDService/ThreadRunner.vb | 13 +++- DDZUGFeRDService/ZUGFeRDService.vb | 17 +++-- Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb | 16 ++++- Jobs/EDMI/ZUGFeRD/WorkerArgs.vb | 2 + Modules.Database/MSSQLServer.vb | 65 +++++++++---------- ZUGFeRDTest/Form1.Designer.vb | 12 ++++ ZUGFeRDTest/Form1.vb | 10 +++ 10 files changed, 122 insertions(+), 43 deletions(-) diff --git a/DDZUGFeRDService/App.config b/DDZUGFeRDService/App.config index 44d74781..c60c3482 100644 --- a/DDZUGFeRDService/App.config +++ b/DDZUGFeRDService/App.config @@ -31,6 +31,12 @@ 10 + + + + + False + \ No newline at end of file diff --git a/DDZUGFeRDService/My Project/Settings.Designer.vb b/DDZUGFeRDService/My Project/Settings.Designer.vb index 37e6b49b..66da4ee7 100644 --- a/DDZUGFeRDService/My Project/Settings.Designer.vb +++ b/DDZUGFeRDService/My Project/Settings.Designer.vb @@ -116,6 +116,24 @@ Namespace My Return CType(Me("JOB_INTERVAL"),Integer) End Get End Property + + _ + Public ReadOnly Property MSSQL_CONNECTIONSTRING() As String + Get + Return CType(Me("MSSQL_CONNECTIONSTRING"),String) + End Get + End Property + + _ + Public ReadOnly Property MSSQL_ENABLED() As Boolean + Get + Return CType(Me("MSSQL_ENABLED"),Boolean) + End Get + End Property End Class End Namespace diff --git a/DDZUGFeRDService/My Project/Settings.settings b/DDZUGFeRDService/My Project/Settings.settings index f340b8c1..05b0c648 100644 --- a/DDZUGFeRDService/My Project/Settings.settings +++ b/DDZUGFeRDService/My Project/Settings.settings @@ -23,5 +23,11 @@ 10 + + + + + False + \ No newline at end of file diff --git a/DDZUGFeRDService/ThreadRunner.vb b/DDZUGFeRDService/ThreadRunner.vb index 4e8db193..3d2601b4 100644 --- a/DDZUGFeRDService/ThreadRunner.vb +++ b/DDZUGFeRDService/ThreadRunner.vb @@ -22,18 +22,27 @@ Public Class ThreadRunner Private _originalEmailDirectory As String Private _zugferd As ZUGFeRDInterface Private _jobArguments As WorkerArgs + Private _mssql As MSSQLServer Private Const TIMER_INTERVAL_MS = 10_000 - Public Sub New(LogConfig As LogConfig, Firebird As Firebird) + Public Sub New(LogConfig As LogConfig, Firebird As Firebird, Optional MSSQL As MSSQLServer = Nothing) _logConfig = LogConfig _logger = _logConfig.GetLogger() _firebird = Firebird _zugferd = New ZUGFeRDInterface(_logConfig) + _mssql = MSSQL Dim args As New WorkerArgs() args = LoadFolderConfig(args) args = LoadPropertyMapFor(args, "DEFAULT") + + ' Use MSSQL Server if available + If _mssql IsNot Nothing Then + _logger.Debug("Data will be inserted into MSSQL Server.") + args.InsertIntoSQLServer = True + End If + _jobArguments = args _logger.Debug("Checking SuccessDirectory {0}", args.SuccessDirectory) @@ -104,7 +113,7 @@ Public Class ThreadRunner _logger.Debug("Background worker running..") - Dim job As New ImportZUGFeRDFiles(_logConfig, _firebird) + Dim job As New ImportZUGFeRDFiles(_logConfig, _firebird, _mssql) job.Start(args) Catch ex As Exception _logger.Warn("Background worker failed!") diff --git a/DDZUGFeRDService/ZUGFeRDService.vb b/DDZUGFeRDService/ZUGFeRDService.vb index ab68a822..eb7070ca 100644 --- a/DDZUGFeRDService/ZUGFeRDService.vb +++ b/DDZUGFeRDService/ZUGFeRDService.vb @@ -7,6 +7,7 @@ Public Class ZUGFeRDService Private _logConfig As LogConfig Private _logger As Logger Private _firebird As Firebird + Private _mssql As MSSQLServer = Nothing Private _threadRunner As ThreadRunner @@ -21,14 +22,22 @@ Public Class ZUGFeRDService Dim oUser As String = My.Settings.DB_USER Dim oPassword As String = My.Settings.DB_PASSWORD Dim oJobInterval As Integer = My.Settings.JOB_INTERVAL - - _logger.Debug("Datasource: {0}", oDataSource) - _logger.Debug("Database: {0}", oDatabase) + Dim oMSSQLConnectionString As String = My.Settings.MSSQL_CONNECTIONSTRING + Dim oMSSQLEnabled As Boolean = My.Settings.MSSQL_ENABLED _firebird = New Firebird(_logConfig, oDataSource, oDatabase, oUser, oPassword) + If oMSSQLEnabled = True Then + _mssql = New MSSQLServer(_logConfig, oMSSQLConnectionString) + + If _mssql.DBInitialized = False Then + _logger.Warn("MSSQL Connection could not be initialized. Disabling MSSQL.") + _mssql = Nothing + End If + End If + Try - _threadRunner = New ThreadRunner(_logConfig, _firebird) + _threadRunner = New ThreadRunner(_logConfig, _firebird, _mssql) _threadRunner.Start(oJobInterval) Catch ex As Exception _logger.Error(ex) diff --git a/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb b/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb index a4e36fbe..554a9523 100644 --- a/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb +++ b/Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb @@ -24,13 +24,15 @@ Public Class ImportZUGFeRDFiles Private _zugferd As ZUGFeRDInterface Private _firebird As Firebird Private _filesystem As Filesystem.File + Private _mssql As MSSQLServer - Public Sub New(LogConfig As LogConfig, Firebird As Firebird) + Public Sub New(LogConfig As LogConfig, Firebird As Firebird, Optional MSSQL As MSSQLServer = Nothing) _logConfig = LogConfig _logger = LogConfig.GetLogger() _firebird = Firebird _filesystem = New Filesystem.File(_logConfig) _zugferd = New ZUGFeRDInterface(_logConfig) + _mssql = MSSQL End Sub Private Function RandomValue(lowerBound As Integer, upperBound As Integer) As Integer @@ -182,7 +184,7 @@ Public Class ImportZUGFeRDFiles Public Sub Start(Arguments As Object) Implements IJob.Start Dim args As WorkerArgs = Arguments - _logger.Info("Starting Job {0}", Me.GetType.Name) + _logger.Info("Starting Job {0}", [GetType].Name) Try For Each oPath As String In args.WatchDirectories @@ -276,9 +278,17 @@ Public Class ImportZUGFeRDFiles Dim oTableName = Item.Value.TableName Dim oCommand = $"INSERT INTO {oTableName} (REFERENCE_GUID, ITEM_DESCRIPTION, ITEM_VALUE) VALUES ('{oFileGroupId}', '{propertyDescripton}', '{propertyValue}')" - _logger.Debug("Mapping Property {0} to value {1}. Will be inserted into table {2}", propertyDescripton, propertyValue, oTableName) + ' Insert into SQL Server + If args.InsertIntoSQLServer = True Then + Dim oResult = _mssql.NewExecutenonQuery(oCommand) + If oResult = False Then + _logger.Warn("SQL Command was not successful. Check the log.") + End If + End If + + ' Insert into Firebird _firebird.ExecuteNonQueryWithConnection(oCommand, oConnection, Firebird.TransactionMode.ExternalTransaction, oTransaction) Next diff --git a/Jobs/EDMI/ZUGFeRD/WorkerArgs.vb b/Jobs/EDMI/ZUGFeRD/WorkerArgs.vb index 5b9d4393..68c39324 100644 --- a/Jobs/EDMI/ZUGFeRD/WorkerArgs.vb +++ b/Jobs/EDMI/ZUGFeRD/WorkerArgs.vb @@ -7,6 +7,7 @@ Public Class WorkerArgs Public OriginalEmailDirectory As String Public RejectedEmailDirectory As String Public PropertyMap As Dictionary(Of String, XmlItemProperty) + Public InsertIntoSQLServer As Boolean Public Sub New() WatchDirectories = New List(Of String) @@ -15,5 +16,6 @@ Public Class WorkerArgs OriginalEmailDirectory = Nothing RejectedEmailDirectory = Nothing PropertyMap = New Dictionary(Of String, XmlItemProperty) + InsertIntoSQLServer = False End Sub End Class \ No newline at end of file diff --git a/Modules.Database/MSSQLServer.vb b/Modules.Database/MSSQLServer.vb index 45437826..cf1b1fbc 100644 --- a/Modules.Database/MSSQLServer.vb +++ b/Modules.Database/MSSQLServer.vb @@ -1,29 +1,27 @@ Imports System.Data.SqlClient +Imports DigitalData.Modules.Logging Public Class MSSQLServer - Private Shared Logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger Public DBInitialized As Boolean = False Public CurrentSQLConnectionString As String = "" - Private CurrentSQLConnection As SqlClient.SqlConnection - Public Sub New(CONSTRING As String) - Init(CONSTRING) - End Sub - Public Function Init(CONSTRING As String) + Private CurrentSQLConnection As SqlConnection + Private _Logger As Logger + + Public Sub New(LogConfig As LogConfig, ConnectionString As String) + _Logger = LogConfig.GetLogger() + Try - Dim oSQLconnect As New SqlClient.SqlConnection - oSQLconnect.ConnectionString = CONSTRING + Dim oSQLconnect As New SqlConnection + oSQLconnect.ConnectionString = ConnectionString oSQLconnect.Open() oSQLconnect.Close() - CurrentSQLConnectionString = CONSTRING + CurrentSQLConnectionString = ConnectionString DBInitialized = True - Return True Catch ex As Exception DBInitialized = False - Logger.Error(ex) - 'clsLogger.Add("Error in DatabaseInit: " & ex.Message, True) - Return False + _Logger.Error(ex) End Try - End Function + End Sub Private Function GetSQLConnection() Try @@ -32,22 +30,21 @@ Public Class MSSQLServer oSQLconnect.ConnectionString = CurrentSQLConnectionString CurrentSQLConnection = oSQLconnect CurrentSQLConnection.Open() - Else If CurrentSQLConnection.State <> ConnectionState.Open Then - Logger.Warn($"Actual ConnectionState is: '{CurrentSQLConnection.State.ToString}'") + _Logger.Warn($"Actual ConnectionState is: '{CurrentSQLConnection.State.ToString}'") Try CurrentSQLConnection.Open() Return True Catch ex As Exception - Logger.Warn("Could not reconnect to database!") + _Logger.Warn("Could not reconnect to database!") Return False End Try End If End If Return True Catch ex As Exception - Logger.Error(ex) + _Logger.Error(ex) Return False End Try End Function @@ -65,18 +62,18 @@ Public Class MSSQLServer Return Nothing End If - Dim oSQLCOmmand As SqlClient.SqlCommand + Dim oSQLCOmmand As SqlCommand oSQLCOmmand = CurrentSQLConnection.CreateCommand() oSQLCOmmand.CommandText = sqlcommand oSQLCOmmand.CommandTimeout = commandtimeout - Dim adapter1 As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(oSQLCOmmand) + Dim adapter1 As SqlDataAdapter = New SqlDataAdapter(oSQLCOmmand) adapter1.Fill(dt) Return dt Catch ex As Exception - Logger.Error(ex) - Logger.Debug("sqlcommand: " & sqlcommand) + _Logger.Error(ex) + _Logger.Debug("sqlcommand: " & sqlcommand) Return Nothing End Try End Function @@ -93,7 +90,7 @@ Public Class MSSQLServer Return Nothing End If 'Dim oSQLconnect As New SqlClient.SqlConnection - Dim oSQLCOmmand As SqlClient.SqlCommand + Dim oSQLCOmmand As SqlCommand oSQLCOmmand = CurrentSQLConnection.CreateCommand() oSQLCOmmand.CommandText = executeStatement @@ -102,8 +99,8 @@ Public Class MSSQLServer oSQLCOmmand.Dispose() Return True Catch ex As Exception - Logger.Error(ex) - Logger.Debug("executeStatement: " & executeStatement) + _Logger.Error(ex) + _Logger.Debug("executeStatement: " & executeStatement) Return False End Try End Function @@ -118,8 +115,8 @@ Public Class MSSQLServer Exit Sub End If - Dim oSQLCOmmand As SqlClient.SqlCommand - Dim callback As New AsyncCallback(AddressOf Execute_non_Query_Async_Callback) + Dim oSQLCOmmand As SqlCommand + Dim callback As New AsyncCallback(AddressOf NewExecuteNonQueryAsync_Callback) Try oSQLCOmmand = CurrentSQLConnection.CreateCommand() oSQLCOmmand.CommandText = executeStatement @@ -127,15 +124,15 @@ Public Class MSSQLServer oSQLCOmmand.BeginExecuteNonQuery(callback, oSQLCOmmand) oSQLCOmmand.Dispose() Catch ex As Exception - Logger.Error(ex) - Logger.Debug("executeStatement: " & executeStatement) + _Logger.Error(ex) + _Logger.Debug("executeStatement: " & executeStatement) End Try End Sub - Private Sub Execute_non_Query_Async_Callback(ByVal result As IAsyncResult) - Dim command As SqlClient.SqlCommand = CType(result.AsyncState, SqlClient.SqlCommand) + Private Sub NewExecuteNonQueryAsync_Callback(ByVal result As IAsyncResult) + Dim command As SqlCommand = CType(result.AsyncState, SqlCommand) Dim res = command.EndExecuteNonQuery(result) - Logger.Info(String.Format("Finished executing Async database operation: {0}", command.CommandText)) + _Logger.Info(String.Format("Finished executing Async database operation: {0}", command.CommandText)) End Sub ''' ''' Executes the passed sql-statement as Scalar @@ -159,8 +156,8 @@ Public Class MSSQLServer oSQLCOmmand.Dispose() Return result Catch ex As Exception - Logger.Error(ex) - Logger.Debug("executeStatement: " & executeStatement) + _Logger.Error(ex) + _Logger.Debug("executeStatement: " & executeStatement) Return Nothing End Try End Function diff --git a/ZUGFeRDTest/Form1.Designer.vb b/ZUGFeRDTest/Form1.Designer.vb index 83a12ffd..cdfa5cd6 100644 --- a/ZUGFeRDTest/Form1.Designer.vb +++ b/ZUGFeRDTest/Form1.Designer.vb @@ -26,6 +26,7 @@ Partial Class Form1 Me.Button1 = New System.Windows.Forms.Button() Me.ListBox1 = New System.Windows.Forms.ListBox() Me.Button2 = New System.Windows.Forms.Button() + Me.Button3 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'OpenFileDialog1 @@ -58,11 +59,21 @@ Partial Class Form1 Me.Button2.Text = "Validate Single File" Me.Button2.UseVisualStyleBackColor = True ' + 'Button3 + ' + Me.Button3.Location = New System.Drawing.Point(12, 70) + Me.Button3.Name = "Button3" + Me.Button3.Size = New System.Drawing.Size(221, 23) + Me.Button3.TabIndex = 3 + Me.Button3.Text = "Load Single File" + Me.Button3.UseVisualStyleBackColor = True + ' '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.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.ListBox1) Me.Controls.Add(Me.Button1) @@ -76,4 +87,5 @@ Partial Class Form1 Friend WithEvents Button1 As Button Friend WithEvents ListBox1 As ListBox Friend WithEvents Button2 As Button + Friend WithEvents Button3 As Button End Class diff --git a/ZUGFeRDTest/Form1.vb b/ZUGFeRDTest/Form1.vb index d1135a30..14df4684 100644 --- a/ZUGFeRDTest/Form1.vb +++ b/ZUGFeRDTest/Form1.vb @@ -90,4 +90,14 @@ Public Class Form1 _zugferd.ValidateZUGFeRDFile(OpenFileDialog1.FileName) End If End Sub + + Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click + Dim oResult = OpenFileDialog1.ShowDialog() + + If oResult = DialogResult.OK Then + Dim oDoc = _zugferd.ValidateZUGFeRDFile(OpenFileDialog1.FileName) + Dim oZUGFERD = _zugferd.SerializeZUGFeRDDocument(oDoc) + Console.WriteLine() + End If + End Sub End Class