From 914ba9dc90b1b9ab8a048cabc09be33e024b8309 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Wed, 13 Feb 2019 16:23:02 +0100 Subject: [PATCH] config in client suite --- Config/Config.vbproj | 1 - Config/ConfigManager.vb | 44 +++++++++---- Config/OldConfig.vb | 78 ------------------------ EDMI_ClientSuite/ApplicationEvents.vb | 6 +- EDMI_ClientSuite/ClassConfig.vb | 3 + EDMI_ClientSuite/ClassInit.vb | 10 +-- EDMI_ClientSuite/EDMI_ClientSuite.vbproj | 5 ++ EDMI_ClientSuite/MyApplication.vb | 2 + EDMI_ClientSuite/frmConfigService.vb | 4 +- EDMI_ClientSuite/frmSplash.vb | 2 +- Filesystem/File.vb | 4 ++ 11 files changed, 60 insertions(+), 99 deletions(-) delete mode 100644 Config/OldConfig.vb create mode 100644 EDMI_ClientSuite/ClassConfig.vb diff --git a/Config/Config.vbproj b/Config/Config.vbproj index e1d2d6a4..4e569b1a 100644 --- a/Config/Config.vbproj +++ b/Config/Config.vbproj @@ -73,7 +73,6 @@ - diff --git a/Config/ConfigManager.vb b/Config/ConfigManager.vb index 08672da4..a1b894fb 100644 --- a/Config/ConfigManager.vb +++ b/Config/ConfigManager.vb @@ -12,7 +12,7 @@ Public Class ConfigManager(Of T) Private ReadOnly _File As Filesystem.File Private ReadOnly _UserPath As String Private ReadOnly _ComputerPath As String - Private ReadOnly _CurrentDataPath As String + Private _CurrentDataPath As String Private ReadOnly _Schema As T Private ReadOnly _Serializer As XmlSerializer @@ -53,33 +53,51 @@ Public Class ConfigManager(Of T) Throw New DirectoryNotFoundException($"Path {ComputerConfigPath} does not exist!") End If - If Not Filesystem.File Then Then - Throw New DirectoryNotFoundException() + If Not _File.TestPathIsDirectory(UserConfigPath) Then + Throw New ArgumentException($"Path {UserConfigPath} is not a directory!") End If + If Not _File.TestPathIsDirectory(ComputerConfigPath) Then + Throw New ArgumentException($"Path {ComputerConfigPath} is not a directory!") + End If + + _Config = LoadConfig() + End Sub + + ''' + ''' Save the current config object to `UserConfigPath` + ''' + Public Sub Save() + WriteToFile(_Config, _UserPath) + End Sub + + ''' + ''' First check if a user config exists and if it does, load it. + ''' If not, check if a systemwide config exists and and if it does, load it. + ''' Otherwise, create a user config using the default values from the supplied config class `T` + ''' + ''' + Private Function LoadConfig() As T + Dim oConfig As T + If IO.File.Exists(_UserPath) Then _Logger.Debug("Loading config from UserPath: {0}", _UserPath) _CurrentDataPath = _UserPath - _Config = ReadFromFile(_UserPath) + oConfig = ReadFromFile(_UserPath) ElseIf IO.File.Exists(_ComputerPath) Then _Logger.Debug("Loading config from ComputerPath: {0}", _ComputerPath) _CurrentDataPath = _ComputerPath - _Config = ReadFromFile(_ComputerPath) + oConfig = ReadFromFile(_ComputerPath) Else _Logger.Debug("Creating default config in UserPath: {0}", _UserPath) _CurrentDataPath = _UserPath - _Config = Activator.CreateInstance(_Schema.GetType) + oConfig = Activator.CreateInstance(_Schema.GetType) WriteToFile(_Config, _UserPath) End If - End Sub - ''' - ''' Save the current config object to `UserConfigPath` - ''' - Public Sub Save() - WriteToFile(_Config, _UserPath) - End Sub + Return oConfig + End Function ''' ''' Serialize a config object to byte array diff --git a/Config/OldConfig.vb b/Config/OldConfig.vb deleted file mode 100644 index 608cbd4d..00000000 --- a/Config/OldConfig.vb +++ /dev/null @@ -1,78 +0,0 @@ -Imports System.IO -Imports DigitalData.Modules.Logging - -Public MustInherit Class OldConfig - Private Const _userConfigFileName As String = "UserConfig.xml" - Private _logFactory As LogConfig - Private _logger As Logger - - Protected Const _configKey As String = "Key" - Protected Const _configValue As String = "Value" - Protected _dataTable As DataTable - - Protected ReadOnly Property PropertyNames As Dictionary(Of String, String) - Get - Return New Dictionary(Of String, String) - End Get - End Property - - Public Sub New(LogConfig As LogConfig) - _logFactory = LogConfig - _logger = LogConfig.GetLogger() - - - End Sub - - Private Function GetUserConfigPath() As String - Dim oAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) - Dim oCompanyName = My.Application.Info.CompanyName - Dim oProductName = My.Application.Info.ProductName - - Return Path.Combine(oAppData, oCompanyName, oProductName, _userConfigFileName) - End Function - - Protected Function LoadConfig() As DataTable - Dim oUserConfigPath As String = GetUserConfigPath() - Dim oDatatable As New DataTable() - - If Not File.Exists(oUserConfigPath) Then - _logger.Warn("Config file {0} does not exist", oUserConfigPath) - Return Nothing - End If - - Try - oDatatable.ReadXml(oUserConfigPath) - Return oDatatable - Catch ex As Exception - _logger.Error(ex) - Return Nothing - End Try - End Function - - Protected Sub SaveConfig(DataTable As DataTable) - Dim oUserConfigPath As String = GetUserConfigPath() - - Try - DataTable.WriteXml(oUserConfigPath) - Catch ex As Exception - _logger.Error(ex) - End Try - End Sub - - Protected Function ConvertToDataTable(dictionary As Dictionary(Of String, String)) As DataTable - Dim oNewDataTable As New DataTable("Config") - oNewDataTable.Columns.Add(New DataColumn("Key", GetType(String))) - oNewDataTable.Columns.Add(New DataColumn("Value", GetType(String))) - - For Each oProperty In dictionary - Dim oNewRow = oNewDataTable.NewRow() - oNewRow.Item("Key") = oProperty.Key - oNewRow.Item("Value") = oProperty.Value - oNewDataTable.Rows.Add(oNewRow) - Next - - oNewDataTable.AcceptChanges() - - Return oNewDataTable - End Function -End Class diff --git a/EDMI_ClientSuite/ApplicationEvents.vb b/EDMI_ClientSuite/ApplicationEvents.vb index 7feea862..6e4808af 100644 --- a/EDMI_ClientSuite/ApplicationEvents.vb +++ b/EDMI_ClientSuite/ApplicationEvents.vb @@ -1,4 +1,5 @@ -Imports DigitalData.Modules.Logging +Imports DigitalData.Modules.Config +Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging.LogConfig Namespace My @@ -13,7 +14,10 @@ Namespace My Public Sub App_Startup() Handles Me.Startup Dim oLogConfig As New LogConfig(PathType.AppData) + Dim oConfigManager As New ConfigManager(Of ClassConfig)(oLogConfig, Windows.Forms.Application.UserAppDataPath, Windows.Forms.Application.CommonAppDataPath) + LogConfig = oLogConfig + ConfigManager = oConfigManager _Logger = LogConfig.GetLogger() _Logger.Info("Starting Client Suite..") diff --git a/EDMI_ClientSuite/ClassConfig.vb b/EDMI_ClientSuite/ClassConfig.vb new file mode 100644 index 00000000..34f14285 --- /dev/null +++ b/EDMI_ClientSuite/ClassConfig.vb @@ -0,0 +1,3 @@ +Public Class ClassConfig + Public Property ServiceConnection As String = String.Empty +End Class diff --git a/EDMI_ClientSuite/ClassInit.vb b/EDMI_ClientSuite/ClassInit.vb index d309802a..bec7cdc4 100644 --- a/EDMI_ClientSuite/ClassInit.vb +++ b/EDMI_ClientSuite/ClassInit.vb @@ -1,5 +1,5 @@ Imports DigitalData.Modules.Logging -Imports DigitalData.Modules.Logging.LogConfig +Imports DigitalData.Modules.Config Imports System.ServiceModel Imports EDMI_ClientSuite.NetworkService_DDEDM Imports System.ServiceModel.Channels @@ -19,8 +19,9 @@ Public Class ClassInit _Logger = My.LogConfig.GetLogger() End Sub - Public Function TestConnectionURLExists() - Return My.Settings.ICMServiceAddress <> String.Empty + Public Function TestConnectionURLExists() As Boolean + Return My.ConfigManager.Config.ServiceConnection <> String.Empty + 'Return My.Settings.ICMServiceAddress <> String.Empty End Function Public Async Function TestConnectionAsync(EndpointURL As String) As Task(Of ConnectionTestResult) @@ -44,7 +45,8 @@ Public Class ClassInit End Function Public Function GetChannelFactory() As IChannelFactory(Of IEDMServiceChannel) - Return GetChannelFactory(My.Settings.ICMServiceAddress) + 'Return GetChannelFactory(My.Settings.ICMServiceAddress) + Return GetChannelFactory(My.ConfigManager.Config.ServiceConnection) End Function Public Function GetChannelFactory(EndpointURL As String) As ChannelFactory(Of IEDMServiceChannel) diff --git a/EDMI_ClientSuite/EDMI_ClientSuite.vbproj b/EDMI_ClientSuite/EDMI_ClientSuite.vbproj index 1cd69976..342ea652 100644 --- a/EDMI_ClientSuite/EDMI_ClientSuite.vbproj +++ b/EDMI_ClientSuite/EDMI_ClientSuite.vbproj @@ -115,6 +115,7 @@ + @@ -368,6 +369,10 @@ + + {44982F9B-6116-44E2-85D0-F39650B1EF99} + Config + {5b1171dc-fffe-4813-a20d-786aae47b320} EDMIFileOps diff --git a/EDMI_ClientSuite/MyApplication.vb b/EDMI_ClientSuite/MyApplication.vb index 228e18b1..f0442aed 100644 --- a/EDMI_ClientSuite/MyApplication.vb +++ b/EDMI_ClientSuite/MyApplication.vb @@ -1,5 +1,6 @@ Imports System.ServiceModel Imports System.Threading +Imports DigitalData.Modules.Config Imports DigitalData.Modules.Logging Imports EDMI_ClientSuite.NetworkService_DDEDM @@ -10,6 +11,7 @@ Namespace My ''' Module Extension + Property ConfigManager As ConfigManager(Of ClassConfig) Property LogConfig As LogConfig Property ChannelFactory As ChannelFactory(Of IEDMServiceChannel) Property Channel As IEDMServiceChannel diff --git a/EDMI_ClientSuite/frmConfigService.vb b/EDMI_ClientSuite/frmConfigService.vb index ad9b758e..3fbfd691 100644 --- a/EDMI_ClientSuite/frmConfigService.vb +++ b/EDMI_ClientSuite/frmConfigService.vb @@ -20,7 +20,9 @@ Public Class frmConfigService If oResult = ConnectionTestResult.Successful Then ' Save Endpoint URL - My.Settings.ICMServiceAddress = oEndpointURL + My.ConfigManager.Config.ServiceConnection = oEndpointURL + My.ConfigManager.Save() + 'My.Settings.ICMServiceAddress = oEndpointURL lblStatus.Text = "Verbindung hergestellt." Else Select Case oResult diff --git a/EDMI_ClientSuite/frmSplash.vb b/EDMI_ClientSuite/frmSplash.vb index 678e65b8..baeee23b 100644 --- a/EDMI_ClientSuite/frmSplash.vb +++ b/EDMI_ClientSuite/frmSplash.vb @@ -26,7 +26,7 @@ Public NotInheritable Class frmSplash BringToFront() Dim oInit As New ClassInit() - Dim oConnectionURLExists = oInit.TestConnectionURLExists + Dim oConnectionURLExists = oInit.TestConnectionURLExists() If Not oConnectionURLExists Then Dim oResult = frmConfigService.ShowDialog() diff --git a/Filesystem/File.vb b/Filesystem/File.vb index d11c27c8..e27264af 100644 --- a/Filesystem/File.vb +++ b/Filesystem/File.vb @@ -95,6 +95,10 @@ Public Class File End Sub Public Function TestPathIsDirectory(Path As String) As Boolean + If Not Directory.Exists(Path) Then + Return False + End If + Dim oIsDirectory As Boolean = (System.IO.File.GetAttributes(Path) And FileAttributes.Directory) = FileAttributes.Directory Return oIsDirectory End Function