From 7b035bd3f7ebe6768d3a44fa9316beef0624823a Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Mon, 14 Dec 2020 16:48:15 +0100 Subject: [PATCH] EDMIService: first working version with relations --- Service.EDMIService/Scheduler/JobListener.vb | 98 +++++++++++++++----- 1 file changed, 75 insertions(+), 23 deletions(-) diff --git a/Service.EDMIService/Scheduler/JobListener.vb b/Service.EDMIService/Scheduler/JobListener.vb index 99cf4c40..d969cc47 100644 --- a/Service.EDMIService/Scheduler/JobListener.vb +++ b/Service.EDMIService/Scheduler/JobListener.vb @@ -27,36 +27,66 @@ Public Class JobListener End Sub Public Sub ReplaceExistingTable(Name As String, Table As DataTable, DataSet As DataSet, Optional ChildTable As DataTable = Nothing) - Dim oDatatableNameTemp As String = Name & "-TEMP" + Try + Dim oDatatableNameTemp As String = Name & "-TEMP" + + _Logger.Debug("DataTable [{0}] exists, renaming and replacing in DataSet", Name) - _Logger.Debug("DataTable [{0}] exists, renaming and replacing in DataSet", Name) - ' Rename the new table, add TEMP suffix - Table.TableName = oDatatableNameTemp + ' Rename the new table, add TEMP suffix + _Logger.Debug("Renaming new table [{0}] to [{1}]", Name, oDatatableNameTemp) + Table.TableName = oDatatableNameTemp - ' If child table exists, remove all connections to it - If ChildTable IsNot Nothing Then - ' Get name for relations and constraints - Dim oRelationName = GetRelationName(Name, ChildTable.TableName) + ' If child table exists, remove all connections to it + If ChildTable IsNot Nothing Then + _Logger.Debug("Removing relations/constraints on [{0}] and [{1}]", ChildTable.TableName, Name) - ' Remove the relation - Dim oRelation As DataRelation = DataSet.Relations.Item(oRelationName) - DataSet.Relations.Remove(oRelation) + ' Remove relations/constraints on child table + Dim oChildTable As DataTable = DataSet.Tables.Item(ChildTable.TableName) + If oChildTable IsNot Nothing Then + oChildTable.Constraints.Clear() + oChildTable.ChildRelations.Clear() + End If - ' Remove the constraint - Dim oConstraint As Constraint = ChildTable.Constraints.Item(oRelationName) - ChildTable.Constraints.Remove(oConstraint) - End If + ' Remove relations/constraints on parent table + Dim oParentTable As DataTable = DataSet.Tables.Item(Name) + If oParentTable IsNot Nothing Then + oParentTable.Constraints.Clear() + oParentTable.ChildRelations.Clear() + End If + End If - ' Remove the temp table if it exists - If DataSet.Tables.Contains(oDatatableNameTemp) Then - DataSet.Tables.Remove(Table) - End If + ' Remove the temp table if it exists + If DataSet.Tables.Contains(oDatatableNameTemp) Then + _Logger.Debug("Removing Table [{0}]", oDatatableNameTemp) - ' Add the new table to the dataset - DataSet.Tables.Add(Table) + DataSet.Tables.Item(oDatatableNameTemp).ChildRelations.Clear() + DataSet.Tables.Item(oDatatableNameTemp).Constraints.Clear() + DataSet.Tables.Remove(oDatatableNameTemp) + Else + _Logger.Debug("Table [{0}] does not exist, skipping removal.", oDatatableNameTemp) + End If + + ' Remove the current Table + If DataSet.Tables.Contains(Name) Then + _Logger.Debug("Removing Table [{0}]", Name) + + DataSet.Tables.Item(Name).Constraints.Clear() + DataSet.Tables.Remove(Name) + Else + _Logger.Debug("Table [{0}] does not exist, skipping removal.", Name) + End If - ' Rename the new table - DataSet.Tables.Item(oDatatableNameTemp).TableName = Name + ' Add the new table to the dataset + _Logger.Debug("Adding Table [{0}] to Dataset", Table.TableName) + DataSet.Tables.Add(Table) + + ' Rename the new table + _Logger.Debug("Renaming new table [{0}] to [{1}]", oDatatableNameTemp, Name) + DataSet.Tables.Item(oDatatableNameTemp).TableName = Name + Catch ex As Exception + _Logger.Error(ex) + Throw ex + End Try End Sub Public Sub AddNewTable(Name As String, Table As DataTable, DataSet As DataSet) @@ -118,6 +148,9 @@ Public Class JobListener Dim oDetailId As Integer = DetailRow.Item("GUID") Dim oDatatableNameTemp As String = oName & "-TEMP" + ' Used for debugging relations and constraints + 'ListTables(Dataset) + If Dataset.Tables.Contains(oName) Then ' Replace existing table If Result.ChildTable IsNot Nothing Then @@ -153,6 +186,25 @@ Public Class JobListener End Try End Sub + Private Sub ListTables(dataset As DataSet) + Dim oIndex As Integer = 1 + + For Each oTable As DataTable In dataset.Tables + _Logger.Debug("Table: [{0}] ({1})", oTable.TableName, oIndex) + + _Logger.Debug("-> Listing Constraints for [{0}]:", oTable.TableName) + For Each oConstraint As Constraint In oTable.Constraints + _Logger.Debug("---> Constraint: [{0}]", oConstraint.ConstraintName) + Next + + _Logger.Debug("-> Listing Child-Relations for [{0}]:", oTable.TableName) + For Each oRelation As DataRelation In oTable.ChildRelations + _Logger.Debug("---> Relation: [{0}] ", oRelation.RelationName) + Next + + oIndex += 1 + Next + 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")