EDMIService: WIP Relations
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
Imports DigitalData.Modules.Database
|
||||
Option Explicit On
|
||||
|
||||
Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Language.Utils
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Imports Quartz
|
||||
|
||||
Public Class DatatableJob
|
||||
@@ -9,16 +13,16 @@ Public Class DatatableJob
|
||||
Dim oJobData = context.MergedJobDataMap
|
||||
Dim oLogConfig As LogConfig = oJobData.Item("LogConfig")
|
||||
Dim oLogger As Logger = oLogConfig.GetLogger()
|
||||
Dim oDetailRow As DataRow = oJobData.Item("CronJobDetails")
|
||||
Dim oDatatableName As String = NotNull(oDetailRow.Item("DT_NAME"), String.Empty)
|
||||
|
||||
Try
|
||||
Dim oCronJobTitle As String = oJobData.Item("CronJobTitle")
|
||||
Dim oDetailRow As DataRow = oJobData.Item("CronJobDetails")
|
||||
Dim oMSSQL As MSSQLServer = oJobData.Item("MSSQL")
|
||||
|
||||
Dim oConnectionId As Integer = oDetailRow.Item("CON_ID")
|
||||
Dim oTitle As String = oDetailRow.Item("TITLE")
|
||||
Dim oDatatableName As String = oDetailRow.Item("DT_NAME")
|
||||
Dim oSQL As String = oDetailRow.Item("COMMAND")
|
||||
Dim oConnectionId As Integer = NotNull(oDetailRow.Item("CON_ID"), String.Empty)
|
||||
Dim oTitle As String = NotNull(oDetailRow.Item("TITLE"), String.Empty)
|
||||
Dim oSQL As String = NotNull(oDetailRow.Item("COMMAND"), String.Empty)
|
||||
|
||||
oLogger.Debug("Running Command-Job [{0}]", oTitle)
|
||||
oLogger.Debug("Datatable Name: {0}", oDatatableName)
|
||||
@@ -26,14 +30,35 @@ Public Class DatatableJob
|
||||
|
||||
Dim oConnectionString = oMSSQL.Get_ConnectionStringforID(oConnectionId)
|
||||
|
||||
Dim oResult = oMSSQL.GetDatatableWithConnection(oSQL, oConnectionString)
|
||||
oResult.TableName = oDatatableName
|
||||
oLogger.Debug("Result Datatable contains [{0}] rows", oResult.Rows.Count)
|
||||
Dim oTable = oMSSQL.GetDatatableWithConnection(oSQL, oConnectionString)
|
||||
oTable.TableName = oDatatableName
|
||||
oLogger.Debug("Result Datatable [{0}] contains [{1}] rows", oTable.TableName, oTable.Rows.Count)
|
||||
|
||||
Dim oResult = New JobResult() With {
|
||||
.Table = oTable
|
||||
}
|
||||
|
||||
Dim oChildTableNAme As String = NotNull(oDetailRow.Item("CHILD_DT_NAME"), String.Empty)
|
||||
|
||||
If oChildTableNAme <> String.Empty Then
|
||||
Dim oParentColumn As String = NotNull(oDetailRow.Item("DT_COLUMN"), String.Empty)
|
||||
Dim oChildColumn As String = NotNull(oDetailRow.Item("CHILD_DT_COLUMN"), String.Empty)
|
||||
oLogger.Debug("Child Datatable [{0}] defined, Relation: Parent [{1}] -> Child [{2}]", oChildTableName, oParentColumn, oChildColumn)
|
||||
|
||||
Dim oChildTable As DataTable = oMSSQL.GetDatatableWithConnection($"SELECT * FROM {oChildTableName}", oConnectionString)
|
||||
oChildTable.TableName = oChildTableName
|
||||
oLogger.Debug("Child Datatable [{0}] contains [{1}] rows", oChildTable.TableName, oChildTable.Rows.Count)
|
||||
|
||||
oResult.ChildTable = oChildTable
|
||||
oResult.ChildRelationColumn = oChildColumn
|
||||
oResult.TableRelationColumn = oParentColumn
|
||||
End If
|
||||
|
||||
' Das Ergebnis speichern
|
||||
context.Result = oResult
|
||||
Catch ex As Exception
|
||||
oLogger.Warn("Unhandled exception while executing SQL for Datatable {}")
|
||||
oLogger.Error(ex)
|
||||
oLogger.Warn("Unhandled exception while executing SQL for Datatable {0}", oDatatableName)
|
||||
End Try
|
||||
|
||||
Return Task.FromResult(True)
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
Imports System.Threading
|
||||
Option Explicit On
|
||||
|
||||
Imports System.Threading
|
||||
Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Language
|
||||
Imports DigitalData.Modules.Logging
|
||||
Imports Quartz
|
||||
Imports Quartz.Listener
|
||||
@@ -23,34 +26,120 @@ Public Class JobListener
|
||||
Dataset = ResultDataSet
|
||||
End Sub
|
||||
|
||||
Public Overrides Function JobWasExecuted(context As IJobExecutionContext, jobException As JobExecutionException, Optional cancellationToken As CancellationToken = Nothing) As Task
|
||||
Try
|
||||
Dim oDataTable As DataTable = context.Result
|
||||
Dim oDetailRow As DataRow = context.MergedJobDataMap.Item("CronJobDetails")
|
||||
Dim oDatatableName As String = oDetailRow.Item("DT_NAME")
|
||||
Dim oDetailId As Integer = oDetailRow.Item("GUID")
|
||||
Dim oDatatableNameTemp As String = oDatatableName & "-TEMP"
|
||||
Public Sub ReplaceExistingTable(Name As String, Table As DataTable, DataSet As DataSet)
|
||||
Dim oDatatableNameTemp As String = Name & "-TEMP"
|
||||
|
||||
If Dataset.Tables.Contains(oDatatableName) Then
|
||||
_Logger.Debug("DataTable [{0}] exists, renaming and replacing in DataSet", oDatatableName)
|
||||
' Rename the new table, add TEMP suffix
|
||||
oDataTable.TableName = oDatatableNameTemp
|
||||
' Add the new table to the dataset
|
||||
Dataset.Tables.Add(oDataTable)
|
||||
' Remove the old table
|
||||
Dataset.Tables.Remove(oDatatableName)
|
||||
' Rename the new table
|
||||
Dataset.Tables.Item(oDatatableNameTemp).TableName = oDatatableName
|
||||
_Logger.Debug("DataTable [{0}] exists, renaming and replacing in DataSet", Name)
|
||||
' Rename the new table, add TEMP suffix
|
||||
Table.TableName = oDatatableNameTemp
|
||||
' Add the new table to the dataset
|
||||
DataSet.Tables.Add(Table)
|
||||
' Remove the old table
|
||||
DataSet.Tables.Remove(Name)
|
||||
' Rename the new table
|
||||
DataSet.Tables.Item(oDatatableNameTemp).TableName = Name
|
||||
End Sub
|
||||
|
||||
Public Sub AddNewTable(Name As String, Table As DataTable, DataSet As DataSet)
|
||||
_Logger.Debug("DataTable [{0}] does not exist, adding to DataSet", Name)
|
||||
|
||||
DataSet.Tables.Add(Table)
|
||||
End Sub
|
||||
|
||||
Public Sub AddRelation(ParentTableName As String, ParentColumnName As String, ChildTableName As String, ChildColumnName As String)
|
||||
Dim oChild As DataTable = Dataset.Tables.Item(ChildTableName)
|
||||
Dim oParent As DataTable = Dataset.Tables.Item(ParentTableName)
|
||||
|
||||
Dim oRelationName As String = $"{ParentTableName}-{ChildTableName}"
|
||||
Dim oParentColumn As DataColumn = oParent.Columns.Item(ParentColumnName)
|
||||
Dim oChildColumn As DataColumn = oChild.Columns.Item(ChildColumnName)
|
||||
|
||||
_Logger.Debug("New Relation: {0}/{1} => {2}/{3}", ParentTableName, ParentColumnName, ChildTableName, ChildColumnName)
|
||||
Try
|
||||
_Logger.Debug("Creating Relation [{0}]", oRelationName)
|
||||
_Logger.Debug("ParentColumn exists: {0}", IsNothing(oParentColumn))
|
||||
_Logger.Debug("ChildColumn exists: {0}", IsNothing(oChildColumn))
|
||||
Dim oRelation As New DataRelation(oRelationName, oParentColumn, oChildColumn)
|
||||
|
||||
_Logger.Debug("Adding Relation [{0}]", oRelationName)
|
||||
Dataset.Relations.Add(oRelation)
|
||||
|
||||
Catch ex As ArgumentNullException
|
||||
_Logger.Error(ex)
|
||||
_Logger.Warn("ArgumentNullException in AddRelation: {0}", ex.Message)
|
||||
|
||||
Catch ex As DuplicateNameException
|
||||
_Logger.Error(ex)
|
||||
_Logger.Warn("DuplicateNameException in AddRelation: {0}", ex.Message)
|
||||
|
||||
Catch ex As InvalidConstraintException
|
||||
_Logger.Error(ex)
|
||||
_Logger.Warn("DuplicateNameException in AddRelation: {0}", ex.Message)
|
||||
|
||||
Catch ex As ArgumentException
|
||||
_Logger.Error(ex)
|
||||
_Logger.Warn("ArgumentException in AddRelation: {0}", ex.Message)
|
||||
|
||||
Catch ex As Exception
|
||||
_Logger.Error(ex)
|
||||
_Logger.Warn("Exception in AddRelation: {0}", ex.Message)
|
||||
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
Public Sub SaveDataTables(Result As JobResult, DetailRow As DataRow)
|
||||
Try
|
||||
Dim oTable As DataTable = Result.Table
|
||||
Dim oName As String = DetailRow.Item("DT_NAME")
|
||||
Dim oDetailId As Integer = DetailRow.Item("GUID")
|
||||
Dim oDatatableNameTemp As String = oName & "-TEMP"
|
||||
|
||||
If Dataset.Tables.Contains(oName) Then
|
||||
If Result.ChildTable IsNot Nothing Then
|
||||
ReplaceExistingTable(Result.ChildTable.TableName, Result.ChildTable, Dataset)
|
||||
End If
|
||||
ReplaceExistingTable(oName, oTable, Dataset)
|
||||
|
||||
If Result.ChildTable IsNot Nothing Then
|
||||
Dim oRelation = Dataset.Relations.Item($"{oTable.TableName}-{Result.ChildTable.TableName}")
|
||||
|
||||
If oRelation IsNot Nothing Then
|
||||
Dataset.Relations.Remove(oRelation)
|
||||
End If
|
||||
|
||||
AddRelation(oName, Result.TableRelationColumn, Result.ChildTable.TableName, Result.ChildRelationColumn)
|
||||
End If
|
||||
Else
|
||||
_Logger.Debug("DataTable [{0}] does not exist, adding to DataSet", oDatatableName)
|
||||
Dataset.Tables.Add(oDataTable)
|
||||
AddNewTable(oName, oTable, Dataset)
|
||||
|
||||
If Result.ChildTable IsNot Nothing Then
|
||||
AddNewTable(Result.ChildTable.TableName, Result.ChildTable, Dataset)
|
||||
|
||||
AddRelation(oName, Result.TableRelationColumn, Result.ChildTable.TableName, Result.ChildRelationColumn)
|
||||
End If
|
||||
End If
|
||||
|
||||
_Logger.Debug("Listing Tables in DataSet")
|
||||
For Each oDataTable As DataTable In Dataset.Tables
|
||||
_Logger.Debug(oDataTable.TableName)
|
||||
Next
|
||||
|
||||
_Logger.Debug("Listing Relations in Dataset")
|
||||
For Each oRelation As DataRelation In Dataset.Relations
|
||||
_Logger.Debug(oRelation.RelationName)
|
||||
Next
|
||||
|
||||
_MSSQL.ExecuteNonQuery($"INSERT INTO TBAPPSERV_CRON_DETAIL_HISTORY (DETAIL_ID) VALUES ({oDetailId})")
|
||||
Catch ex As Exception
|
||||
_Logger.Warn("Unexpected error in JobListener: {0}", ex.Message)
|
||||
_Logger.Error(ex)
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
|
||||
Public Overrides Function JobWasExecuted(context As IJobExecutionContext, jobException As JobExecutionException, Optional cancellationToken As CancellationToken = Nothing) As Task
|
||||
Dim oDetailRow As DataRow = context.MergedJobDataMap.Item("CronJobDetails")
|
||||
SaveDataTables(context.Result, oDetailRow)
|
||||
|
||||
Return MyBase.JobWasExecuted(context, jobException, cancellationToken)
|
||||
End Function
|
||||
|
||||
6
Service.EDMIService/Scheduler/JobResult.vb
Normal file
6
Service.EDMIService/Scheduler/JobResult.vb
Normal file
@@ -0,0 +1,6 @@
|
||||
Public Class JobResult
|
||||
Public Table As DataTable
|
||||
Public ChildTable As DataTable
|
||||
Public TableRelationColumn As String
|
||||
Public ChildRelationColumn As String
|
||||
End Class
|
||||
Reference in New Issue
Block a user