MultiTool/EDIDocumentImport/frmImportMain.vb
Jonathan Jenne 8dfc659ef8 WIP
2021-10-21 16:19:35 +02:00

295 lines
11 KiB
VB.net

Imports System.ComponentModel
Imports System.IO
Imports System.Xml
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Grid
Imports DigitalData.Controls.SQLConfig
Imports DigitalData.GUIs.Common
Imports DigitalData.Modules.Config
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports ImporterShared
Imports ImporterShared.Documents
Imports ImporterShared.Schemas
Imports ImporterShared.Winline
Public Class frmImportMain
Private LogConfig As LogConfig
Private Logger As Logger
Private ConfigManager As ConfigManager(Of ImporterShared.Config)
Private Database As MSSQLServer
Private Winline As Data
Private FileEx As DigitalData.Modules.Filesystem.File
Private WebService As WebService
Private PositionData As PositionData
Private DocumentLoader As Documents.DocumentLoader
Private SchemaLoader As Schemas.SchemaLoader
Private Grids As List(Of GridControl)
Private GridLoader As GridLoader
Private GridBuilder As GridBuilder
Private CurrentSchema As String
Private CurrentDocument As Document
Private Sub frmImportMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtVersion.Caption = String.Format(txtVersion.Caption, Application.ProductVersion)
LogConfig = New LogConfig(LogConfig.PathType.AppData, Nothing, Nothing, "Digital Data", "EDI Document Importer")
Logger = LogConfig.GetLogger()
Logger.Info("EDI Document Importer, Version [{0}]", Application.ProductVersion)
ConfigManager = New ConfigManager(Of Config)(LogConfig,
Application.UserAppDataPath,
Application.CommonAppDataPath,
Application.StartupPath)
GridBuilder = New GridBuilder(GridViewFiles)
GridBuilder.
WithDefaults.
WithReadOnlyOptions.
WithClipboardHandler()
FileEx = New DigitalData.Modules.Filesystem.File(LogConfig)
' If ConnectionString does not exist, show SQL Config Form
If ConfigManager.Config.ConnectionString = String.Empty Then
Dim oForm As New frmSQLConfig(LogConfig) With {
.FormTitle = "EDI Document Importer"
}
Dim oResult = oForm.ShowDialog()
If oResult = DialogResult.OK Then
ConfigManager.Config.ConnectionString = oForm.ConnectionString
ConfigManager.Save()
End If
End If
' Initialize Database
Dim oConnectionString = MSSQLServer.DecryptConnectionString(ConfigManager.Config.ConnectionString)
Database = New MSSQLServer(LogConfig, oConnectionString)
Winline = New Data(LogConfig, Database, ConfigManager.Config)
WebService = New WebService(LogConfig, ConfigManager.Config)
PositionData = New PositionData(LogConfig, Winline)
' Load WinLine Data
Winline.Mandators.Clear()
Winline.LoadMandators()
Winline.LoadEconomicYears()
Winline.LoadDocumentKinds(Winline.Mandators)
Winline.LoadTemplateConfiguration()
For Each oMandator In Winline.Mandators
Winline.LoadAccounts(oMandator)
Next
DocumentLoader = New DocumentLoader(LogConfig, Winline)
SchemaLoader = New SchemaLoader(LogConfig)
GridLoader = New GridLoader(LogConfig)
SchemaLoader.LoadFiles(ConfigManager.Config.SchemaDirectory)
CurrentSchema = SchemaLoader.SchemaList.First().FullName
Dim oSchema = SchemaLoader.GetSchemaFromFile(CurrentSchema)
Grids = CreateGridsAndColumns(oSchema)
End Sub
Private Function CreateGridsAndColumns(pSchema As Schemas.Schema) As List(Of GridControl)
Dim oGrids As New List(Of GridControl)
Dim oTableCounter = 0
For Each oTable In pSchema.Tables
If oTableCounter = 0 Then
Dim oGrid = GridLoader.GetGridFromElement(oTable)
AddHandler oGrid.DoubleClick, AddressOf Grid_DoubleClick
SplitContainerGrids1.Panel1.Controls.Add(oGrid)
oGrids.Add(oGrid)
End If
If oTableCounter = 1 Then
Dim oGrid = GridLoader.GetGridFromElement(oTable)
AddHandler oGrid.DoubleClick, AddressOf Grid_DoubleClick
SplitContainerGrids1.Panel2.Controls.Add(oGrid)
oGrids.Add(oGrid)
End If
If oTableCounter = 2 Then
Dim oGrid = GridLoader.GetGridFromElement(oTable)
AddHandler oGrid.DoubleClick, AddressOf Grid_DoubleClick
SplitContainerGrids2.Panel1.Controls.Add(oGrid)
oGrids.Add(oGrid)
End If
If oTableCounter = 3 Then
Dim oGrid = GridLoader.GetGridFromElement(oTable)
AddHandler oGrid.DoubleClick, AddressOf Grid_DoubleClick
SplitContainerGrids2.Panel2.Controls.Add(oGrid)
oGrids.Add(oGrid)
End If
If oTableCounter > 3 Then
MsgBox("Only 4 Tables are allowed currently!", MsgBoxStyle.Exclamation, Text)
End If
oTableCounter += 1
Next
If oTableCounter < 3 Then
SplitContainerGrids.PanelVisibility = DevExpress.XtraEditors.SplitPanelVisibility.Panel1
End If
Return oGrids
End Function
Private Sub Grid_DoubleClick(sender As Object, e As EventArgs)
Dim oGrid As GridControl = DirectCast(sender, GridControl)
Dim oView As GridView = DirectCast(oGrid.FocusedView, GridView)
If Not oView.IsDataRow(oView.FocusedRowHandle) Then
Exit Sub
End If
Dim oRow As DataRow = oView.GetDataRow(oView.FocusedRowHandle)
Dim oColumns = oView.Columns.Select(Function(c) c.FieldName).ToList()
Dim oDocumentRow = CurrentDocument.Rows.
Where(Function(r) r.Id.ToString = oRow.Item("GUID")).
SingleOrDefault()
Dim oAccounts = Winline.Accounts.
Where(Function(a) a.Mandator = CurrentDocument.Mandator).
ToList()
Dim oForm As New frmRowEditor(oColumns, oDocumentRow, oAccounts)
If oForm.ShowDialog() = DialogResult.OK Then
'Dim oModifiedRow = oForm.DocumentRow
'For Each oField In oModifiedRow.Fields
' oRow.Item(oField.Key) = oField.Value.Final
'Next
'oRow.AcceptChanges()
End If
End Sub
Private Sub btnLoadFiles_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnLoadFiles.ItemClick
If DocumentLoader.LoadFiles(ConfigManager.Config.InputDirectory) Then
GridControlFiles.DataSource = DocumentLoader.Files
End If
End Sub
Private Sub GridViewFiles_FocusedRowChanged(sender As Object, e As Views.Base.FocusedRowChangedEventArgs) Handles GridViewFiles.FocusedRowChanged
Dim oDocument As Document = GridViewFiles.GetRow(e.FocusedRowHandle)
If LoadDocument(oDocument) Then
CurrentDocument = oDocument
Else
MsgBox("Das Laden des Dokuments ist fehlgeschlagen!", MsgBoxStyle.Critical, Text)
End If
End Sub
Private Function LoadDocument(pDocument As Document)
Try
Dim oDatasources As New Dictionary(Of String, DataTable)
' List of Root Elements in XML
For Each oRow In pDocument.Rows
Dim oGrid As GridControl = Grids.
Where(Function(g) g.Name = oRow.Name).
SingleOrDefault()
' Create initial Datatable if none exists for this Root Element
If Not oDatasources.ContainsKey(oRow.Name) Then
Dim oTable As New DataTable()
oTable.Columns.Add(New DataColumn("GUID"))
For Each oField In oRow.Fields
oTable.Columns.Add(New DataColumn(oField.Key))
Next
oDatasources.Add(oRow.Name, oTable)
oGrid.DataSource = oTable
End If
Dim oDataTable = oDatasources.Item(oRow.Name)
Dim oDataRow = oDataTable.NewRow()
oDataRow.Item("GUID") = oRow.Id.ToString
For Each oField In oRow.Fields
oDataRow.Item(oField.Key) = oField.Value
Next
oDataTable.Rows.Add(oDataRow)
oDataTable.AcceptChanges()
Next
Return True
Catch ex As Exception
Logger.Error(ex)
Return False
End Try
End Function
Private Async Sub btnTransferFile_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnTransferFile.ItemClick
Try
Dim oDocument As Document = GridViewFiles.GetRow(GridViewFiles.FocusedRowHandle)
GridViewFiles.ShowLoadingPanel()
SplitContainerGrids.Enabled = False
Await WebService.TransferDocumentToWinline(oDocument)
MsgBox("Document successfully transferred to WinLine!", MsgBoxStyle.Information, Text)
Catch ex As Exception
MsgBox("Error while transferring to WinLine: " & ex.Message, MsgBoxStyle.Critical, Text)
Logger.Error(ex)
Finally
SplitContainerGrids.Enabled = True
GridViewFiles.HideLoadingPanel()
End Try
End Sub
Private Sub btnOpenInputDirectory_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnOpenInputDirectory.ItemClick
TryOpenDirectory(ConfigManager.Config.InputDirectory)
End Sub
Private Sub btnOpenOutputDirectory_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnOpenOutputDirectory.ItemClick
TryOpenDirectory(IO.Path.Combine(FileEx.GetAppDataPath("Digital Data", "EDI Document Importer"), "WebService"))
End Sub
Private Sub btnOpenSchemaDirectory_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnOpenSchemaDirectory.ItemClick
TryOpenDirectory(ConfigManager.Config.SchemaDirectory)
End Sub
Private Sub TryOpenDirectory(pPath As String)
Try
Process.Start(pPath)
Catch ex As Exception
MsgBox($"Path {pPath} could not be found!", MsgBoxStyle.Exclamation, Text)
End Try
End Sub
Private Sub btnReloadFile_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnReloadFile.ItemClick
Dim oResult As DialogResult = MsgBox("Wollen Sie wirklich die Aktuelle Datei neu laden? Alle von Ihnen getätigte Änderungen werden dabei verworfen.", MsgBoxStyle.Question Or MsgBoxStyle.YesNo, Text)
If oResult = DialogResult.Yes Then
Dim oDocument As Document = GridViewFiles.GetRow(GridViewFiles.FocusedRowHandle)
Dim oNewDocument = DocumentLoader.LoadFile(oDocument.File)
Dim oIndex = DocumentLoader.Files.IndexOf(oDocument)
DocumentLoader.Files.Item(oIndex) = oNewDocument
If LoadDocument(oNewDocument) Then
CurrentDocument = oNewDocument
Else
MsgBox("Das Laden des Dokuments ist fehlgeschlagen!", MsgBoxStyle.Critical, Text)
CurrentDocument = Nothing
End If
End If
End Sub
End Class