Imports System.ComponentModel Imports System.IO Imports DevExpress.Utils Imports DigitalData.Controls.SQLConfig Imports DigitalData.GUIs.Common Imports DigitalData.Modules.Config Imports DigitalData.Modules.Database Imports DigitalData.Modules.Logging Imports MultiTool.Common.Templates Imports MultiTool.Common.Winline Imports MultiTool.Common.Winline.Entities Public Class frmMain Private LogConfig As LogConfig Private Logger As Logger Private ConfigManager As ConfigManager(Of Common.Config) Private Database As MSSQLServer Private TemplateLoader As TemplateLoader Private GridBuilder As GridBuilder Private FormHelper As FormHelper Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load Try Text = String.Format(My.Resources.frmShared._0____WebService_Multitool_für_WinLine, My.Resources.frmShared.Auswahl_der_Vorlage) LogConfig = New LogConfig(LogConfig.PathType.AppData, Nothing, Nothing, Application.CompanyName, Application.ProductName) Logger = LogConfig.GetLogger() Logger.Info("Starting {0}, Version [{1}]", Application.ProductName, Application.ProductVersion) Catch ex As Exception FormHelper.ShowError(ex, My.Resources.frmShared.Laden_des_Formulars) End Try End Sub Private Async Sub frmMain_Shown(sender As Object, e As EventArgs) Handles Me.Shown SplashScreenManager.ShowWaitForm() Try SplashScreenManager.SetWaitFormDescription("Initialisierung der Grundfunktionen") FormHelper = New FormHelper(LogConfig, Me) ConfigManager = New ConfigManager(Of Common.Config)(LogConfig, Application.UserAppDataPath, Application.CommonAppDataPath, Application.StartupPath ) LogConfig.Debug = ConfigManager.Config.Debug GridBuilder = New GridBuilder(GridViewTemplates) GridBuilder.WithDefaults.WithReadOnlyOptions.WithClipboardHandler() GridViewTemplates.OptionsView.ShowAutoFilterRow = False ' If ConnectionString does not exist, show SQL Config Form If ConfigManager.Config.ConnectionString = String.Empty Then Dim oForm As New frmSQLConfig(LogConfig) With { .FormTitle = Application.ProductName } Dim oResult = oForm.ShowDialog() If oResult = DialogResult.OK Then ConfigManager.Config.ConnectionString = oForm.ConnectionString ConfigManager.Save() End If End If ' Initialize Database SplashScreenManager.SetWaitFormDescription("Initialisierung der Datenbankverbindung") Dim oConnectionString = MSSQLServer.DecryptConnectionString(ConfigManager.Config.ConnectionString) Database = New MSSQLServer(LogConfig, oConnectionString) SplashScreenManager.SetWaitFormDescription("Lade Vorlagen") GridControl1.DataSource = Await LoadTemplateData() SplashScreenManager.SetWaitFormDescription("Lade Winline Stammdaten") My.Winline = New WinlineData(LogConfig, Database, My.GeneralConfiguration, My.MappingConfiguration, My.MandatorConfiguration) Await LoadWinlineData(My.Winline) txtVersion.Caption = String.Format(My.Resources.frmMainExtra.Version___0_, My.Application.Info.Version.ToString) txtCulture.Caption = String.Format(My.Resources.frmMainExtra.Sprache___0_, My.Application.UICulture.ToString) Catch ex As Exception FormHelper.ShowError(ex, My.Resources.frmImportMainExtra.Laden_der_Winline_Daten) Finally SplashScreenManager.CloseWaitForm() End Try End Sub Private Async Function LoadWinlineData(Winline As WinlineData) As Task Winline.Mandators.Clear() Winline.LoadEconomicYears() Await Winline.LoadMandators() For Each oMandator As Mandator In Winline.Mandators SplashScreenManager.SetWaitFormDescription(String.Format(My.Resources.frmImportMainExtra.Lade__0__Konten, oMandator.Id)) Await Winline.LoadAccounts(oMandator) SplashScreenManager.SetWaitFormDescription(String.Format(My.Resources.frmImportMainExtra.Lade__0__Artikel, oMandator.Id)) Await Winline.LoadArticles(oMandator) SplashScreenManager.SetWaitFormDescription(String.Format(My.Resources.frmImportMainExtra.Lade__0__Belegarten, oMandator.Id)) Await Winline.LoadDocumentKinds(oMandator) SplashScreenManager.SetWaitFormDescription(String.Format("Lade {0}/Colli", oMandator.Id)) Await Winline.LoadPackingUnits(oMandator) Next My.Winline = Winline End Function Private Async Function LoadTemplateData() As Task(Of BindingList(Of Template)) ' Initialize Schemas TemplateLoader = New TemplateLoader(LogConfig, Database) Await TemplateLoader.LoadTemplates() Await TemplateLoader.LoadTemplateConfiguration() Await TemplateLoader.LoadGeneralConfiguration() Await TemplateLoader.LoadMappingConfiguration() Await TemplateLoader.LoadMandatorConfiguration() ' Save Schema data in 'My' Namespace My.MappingConfiguration = TemplateLoader.MappingConfiguration My.TemplateConfiguration = TemplateLoader.TemplateConfiguration My.MandatorConfiguration = TemplateLoader.MandatorConfiguration My.GeneralConfiguration = TemplateLoader.GeneralConfiguration Dim oBindingSource = New BindingList(Of Template) For Each oTemplate As Template In TemplateLoader.TemplateList oBindingSource.Add(oTemplate) Next Return oBindingSource End Function Private Sub btnOpenImportExportForm_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnOpenImportExportForm.ItemClick Dim oTemplate As Template = GridViewTemplates.GetRow(GridViewTemplates.FocusedRowHandle) OpenImportExportForm(oTemplate) End Sub Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown If e.KeyCode = Keys.F5 Then Dim oTemplate As Template = GridViewTemplates.GetRow(GridViewTemplates.FocusedRowHandle) OpenImportExportForm(oTemplate) End If End Sub Private Function TryCreateTemplateDirectories(pDirectories As List(Of String)) As Boolean For Each oDirectory In pDirectories If Directory.Exists(oDirectory) = False Then Dim oResult As MsgBoxResult = MsgBox($"Das Verzeichnis [{oDirectory}] existiert noch nicht, wird aber zum Benutzen des Programms benötigt. Wollen Sie es jetzt anlegen?", vbQuestion Or vbYesNo, Text) If oResult = MsgBoxResult.Yes Then Try Directory.CreateDirectory(oDirectory) Catch ex As Exception Logger.Error(ex) MsgBox($"Das Verzeichnis [{oDirectory}] konnte nicht angelegt werden. Grund: [{ex.Message}]") Return False End Try Else Return False End If End If Next Return True End Function Private Sub BarButtonItem4_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem4.ItemClick Dim oForm As New frmConfig(LogConfig, ConfigManager) oForm.ShowDialog() End Sub Private Sub BarButtonItem2_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem2.ItemClick Dim oUserConfigDirectory = New FileInfo(ConfigManager.UserConfigPath).Directory FormHelper.TryOpenDirectory(oUserConfigDirectory.FullName, My.Resources.frmImportMainExtra.Konfigurationsverzeichnis) End Sub Private Sub BarButtonItem3_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItem3.ItemClick FormHelper.TryOpenDirectory(LogConfig.LogDirectory, My.Resources.frmImportMainExtra.Logverzeichnis) End Sub Private Sub btnOpenSchemaDirectory_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnOpenSchemaDirectory.ItemClick FormHelper.TryOpenDirectory(My.GeneralConfiguration.TemplateDirectory, My.Resources.frmImportMainExtra.Vorlagenverzeichnis) End Sub Private Sub GridViewTemplates_DoubleClick(sender As Object, e As EventArgs) Handles GridViewTemplates.DoubleClick Dim oArgs As DXMouseEventArgs = TryCast(e, DXMouseEventArgs) Dim oHitInfo = GridViewTemplates.CalcHitInfo(oArgs.Location) If oHitInfo.InRow Then Dim oTemplate As Template = GridViewTemplates.GetRow(oHitInfo.RowHandle) OpenImportExportForm(oTemplate) End If End Sub Private Sub OpenImportExportForm(pTemplate As Template) Try If pTemplate Is Nothing Then MsgBox("Keine Vorlage ausgewählt! Bitte wählen Sie eine Vorlage aus!", MsgBoxStyle.Exclamation, Text) Exit Sub End If ' Initialize template tables pTemplate.Tables = New List(Of Template.Table) ' Fill tables pTemplate = TemplateLoader.UpdateTemplateFromFile(pTemplate, My.GeneralConfiguration.TemplateDirectory) pTemplate = TemplateLoader.UpdateTemplateFromDatabase(pTemplate) pTemplate = TemplateLoader.UpdateTemplateTablesFromDatabase(pTemplate, TemplateLoader.TemplateConfiguration) Dim oBaseDirectories As New List(Of String) From { pTemplate.InputDirectory, pTemplate.OutputDirectory, pTemplate.ArchiveDirectory } Dim oOutputDirectories As New List(Of String) From { pTemplate.OutputReportDirectory, pTemplate.OutputWebserviceDirectory, pTemplate.OutputXmlFileDirectory } If TryCreateTemplateDirectories(oBaseDirectories) Then If TryCreateTemplateDirectories(oOutputDirectories) Then If pTemplate.IsImport Then Dim oForm As New frmImportMain(LogConfig, ConfigManager, pTemplate) oForm.ShowDialog() Else Dim oForm As New frmExportMain(LogConfig, ConfigManager, pTemplate) oForm.ShowDialog() End If End If End If Catch ex As Exception FormHelper.ShowError(ex, My.Resources.frmMainExtra.Laden_der_Vorlage) End Try End Sub Private Async Sub btnReloadTemplates_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnReloadTemplates.ItemClick If SplashScreenManager.IsSplashFormVisible Then Exit Sub End If SplashScreenManager.ShowWaitForm() Try SplashScreenManager.SetWaitFormDescription("Lade Vorlagen") GridControl1.DataSource = Await LoadTemplateData() Catch ex As Exception FormHelper.ShowError(ex, "Laden der Vorlagen") Finally SplashScreenManager.CloseWaitForm() End Try End Sub Private Async Sub btnReloadWinlineData_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnReloadWinlineData.ItemClick If SplashScreenManager.IsSplashFormVisible Then Exit Sub End If SplashScreenManager.ShowWaitForm() Try SplashScreenManager.SetWaitFormDescription("Lade Winline Stammdaten") Await LoadWinlineData(My.Winline) Catch ex As Exception FormHelper.ShowError(ex, My.Resources.frmImportMainExtra.Laden_der_Winline_Daten) Finally SplashScreenManager.CloseWaitForm() End Try End Sub End Class