15-12-2022

This commit is contained in:
Jonathan Jenne
2022-12-15 11:53:59 +01:00
parent 8d6d81f488
commit e4c5658c13
31 changed files with 1020 additions and 274 deletions

View File

@@ -30,9 +30,15 @@ Public Class UpdateProfile
Return New UpdateProfileResponse With {.OK = False}
End If
Logger.Debug("Executing Action [{0}]", pData.Action)
Select Case pData.Action
Case UpdateProfileRequest.UpdateProfileAction.Update
oResponse = DoUpdateImportProfile(pData)
If DoUpdateImportProfile(pData) Then
oResponse = UpdateImportProfileSteps(pData)
Else
oResponse = False
End If
Case UpdateProfileRequest.UpdateProfileAction.Create
oResponse = DoCreateImportProfile(pData)
@@ -42,6 +48,8 @@ Public Class UpdateProfile
End Select
Logger.Debug("Action successful: [{0}]", oResponse)
If oResponse Then
Scheduler.Reload()
Else
@@ -133,8 +141,153 @@ Public Class UpdateProfile
Return Database.ExecuteNonQuery(oCommand)
End Function
Private Function UpdateImportProfileSteps(pData As UpdateProfileRequest) As Boolean
Dim oProfile = pData.ImportProfile
Dim oSQL As String = "UPDATE"
' Get the existing steps from the database
Dim oExistingStepsSQL = $"SELECT GUID FROM TBECM_JR_FIW_STEP WHERE PROFILE_ID = {oProfile.Id}"
Dim oExistingSteps = Database.GetDatatable(oExistingStepsSQL)
Dim oExistingStepsIds = oExistingSteps.Rows.Cast(Of DataRow).Select(Of Integer)(Function(r) r.Item("GUID")).ToList()
Logger.Debug("Existing steps: [{0}]", oExistingStepsIds.Count)
' Get the updated steps from the sent data
Dim oUpdatedSteps = oProfile.Steps
Dim oUpdatedStepsIds = oUpdatedSteps.Select(Function(s) s.Id).ToList()
Logger.Debug("Updated steps: [{0}]", oUpdatedStepsIds.Count)
' Calculate differences
' =======================
' These ids did not exist in the database but in the sent data,
' they will be created.
Dim oNewIds = oUpdatedStepsIds.Except(oExistingStepsIds).ToList()
Logger.Debug("Steps that will be created: [{0}]", oNewIds.Count)
Dim oCreateSuccessful = DoCreateImportProfileSteps(pData, oNewIds)
Logger.Debug("Create Successful: [{0}]", oCreateSuccessful)
' These ids existed in the database but not in the sent data,
' they will be deleted.
Dim oDeletedIds = oExistingStepsIds.Except(oUpdatedStepsIds).ToList()
Logger.Debug("Steps that will be deleted: [{0}]", oDeletedIds.Count)
Dim oDeleteSuccessful = DoDeleteImportProfileSteps(pData, oDeletedIds)
Logger.Debug("Delete Successful: [{0}]", oDeleteSuccessful)
' These ids exist in both datasets, so they were not deleted or created,
' they will be updated in case their values changed.
Dim oUnchangedIds = oExistingStepsIds.Union(oUpdatedStepsIds).
Except(oNewIds).
Except(oDeletedIds).
Distinct().
ToList()
Logger.Debug("Steps that will be updated: [{0}]", oUnchangedIds.Count)
Dim oUpdateSuccessful = DoUpdateImportProfileSteps(pData, oUnchangedIds)
Logger.Debug("Update Successful: [{0}]", oUpdateSuccessful)
' If all operations were successful, only then we return true.
Return New List(Of Boolean) From {oDeleteSuccessful, oCreateSuccessful, oUpdateSuccessful}.All(Function(b) b)
End Function
Private Function DoUpdateImportProfileSteps(pData As UpdateProfileRequest, pIdList As List(Of Integer)) As Boolean
Dim oProfile = pData.ImportProfile
Dim oSQL As String = "UPDATE TBECM_JR_FIW_STEP
SET IDX_NAME = @IDX_NAME,
METHOD = @METHOD,
ARGUMENT1 = @ARGUMENT1,
ARGUMENT2 = @ARGUMENT2,
ARGUMENT3 = @ARGUMENT3,
SCOPE = @SCOPE,
ACTIVE = @ACTIVE
WHERE GUID = @GUID"
Dim oResults As New List(Of Boolean)
For Each oId In pIdList
Try
Dim oStep = pData.ImportProfile.Steps.Where(Function(s) s.Id = oId).SingleOrDefault()
Dim oCommand As New SqlClient.SqlCommand(oSQL)
oCommand.Parameters.Add("IDX_NAME", SqlDbType.NVarChar, 100).Value = oStep.IndexName
oCommand.Parameters.Add("METHOD", SqlDbType.NVarChar, 100).Value = oStep.Method
oCommand.Parameters.Add("ARGUMENT1", SqlDbType.NVarChar, 500).Value = oStep.Argument1
oCommand.Parameters.Add("ARGUMENT2", SqlDbType.NVarChar, 500).Value = oStep.Argument2
oCommand.Parameters.Add("ARGUMENT3", SqlDbType.NVarChar, 500).Value = oStep.Argument3
oCommand.Parameters.Add("SCOPE", SqlDbType.NVarChar, 50).Value = oStep.Scope
oCommand.Parameters.Add("ACTIVE", SqlDbType.Bit).Value = oStep.Active
oCommand.Parameters.Add("GUID", SqlDbType.Int).Value = oStep.Id
oResults.Add(Database.ExecuteNonQuery(oCommand))
Catch ex As Exception
oResults.Add(False)
Logger.Error(ex)
End Try
Next
Return oResults.All(Function(r) r)
End Function
Private Function DoCreateImportProfileSteps(pData As UpdateProfileRequest, pIdList As List(Of Integer)) As Boolean
Dim oProfile = pData.ImportProfile
Dim oSQL As String = "INSERT INTO TBECM_JR_FIW_STEP
(PROFILE_ID, IDX_NAME, METHOD, ARGUMENT1, ARGUMENT2, ARGUMENT3, SCOPE, ACTIVE) VALUES
(@PROFILE_ID, @IDX_NAME, @METHOD, @ARGUMENT1, @ARGUMENT2, @ARGUMENT3, @SCOPE, @ACTIVE)"
Dim oResults As New List(Of Boolean)
For Each oId In pIdList
Try
Dim oStep = pData.ImportProfile.Steps.Where(Function(s) s.Id = oId).SingleOrDefault()
Dim oCommand As New SqlClient.SqlCommand(oSQL)
oCommand.Parameters.Add("PROFILE_ID", SqlDbType.Int).Value = oStep.ProfileId
oCommand.Parameters.Add("IDX_NAME", SqlDbType.NVarChar, 100).Value = oStep.IndexName
oCommand.Parameters.Add("METHOD", SqlDbType.NVarChar, 100).Value = oStep.Method
oCommand.Parameters.Add("ARGUMENT1", SqlDbType.NVarChar, 500).Value = oStep.Argument1
oCommand.Parameters.Add("ARGUMENT2", SqlDbType.NVarChar, 500).Value = oStep.Argument2
oCommand.Parameters.Add("ARGUMENT3", SqlDbType.NVarChar, 500).Value = oStep.Argument3
oCommand.Parameters.Add("SCOPE", SqlDbType.NVarChar, 50).Value = oStep.Scope
oCommand.Parameters.Add("ACTIVE", SqlDbType.Bit).Value = oStep.Active
oResults.Add(Database.ExecuteNonQuery(oCommand))
Catch ex As Exception
oResults.Add(False)
Logger.Error(ex)
End Try
Next
Return oResults.All(Function(r) r)
End Function
Private Function DoDeleteImportProfileSteps(pData As UpdateProfileRequest, pIdList As List(Of Integer)) As Boolean
Dim oProfile = pData.ImportProfile
Dim oSQL As String = "DELETE FROM TBECM_JR_FIW_STEP WHERE GUID = @GUID"
Dim oResults As New List(Of Boolean)
For Each oId In pIdList
Try
Dim oStep = pData.ImportProfile.Steps.Where(Function(s) s.Id = oId).SingleOrDefault()
Dim oCommand As New SqlClient.SqlCommand(oSQL)
oCommand.Parameters.Add("GUID", SqlDbType.Int).Value = oStep.Id
oResults.Add(Database.ExecuteNonQuery(oCommand))
Catch ex As Exception
oResults.Add(False)
Logger.Error(ex)
End Try
Next
Return oResults.All(Function(r) r)
End Function
End Class
Public Class UpdateProfileRequest
Public Enum UpdateProfileAction
Create