310 lines
14 KiB
VB.net
310 lines
14 KiB
VB.net
Imports DigitalData.Modules.Database
|
|
Imports DigitalData.Modules.Logging
|
|
Imports DigitalData.Modules.Language
|
|
Imports ECM.JobRunner.Common
|
|
Imports ECM.JobRunner.Windows.Scheduler
|
|
|
|
Public Class UpdateProfile
|
|
Public Class UpdateProfileMethod
|
|
Inherits Base.BaseMethod
|
|
|
|
Private ReadOnly Scheduler As JobScheduler
|
|
|
|
Public Sub New(pLogConfig As LogConfig, pDatabase As MSSQLServer, pState As State, pScheduler As JobScheduler)
|
|
MyBase.New(pLogConfig, pDatabase, pState)
|
|
Scheduler = pScheduler
|
|
End Sub
|
|
|
|
Public Function Run(pData As UpdateProfileRequest) As UpdateProfileResponse
|
|
Dim oResponse As Boolean = False
|
|
Dim oUpdateJob As New UpdateJob.UpdateJobMethod(LogConfig, Database, State, Scheduler)
|
|
|
|
' Active on the profile sets active on the job as well
|
|
pData.ImportProfile.Job.Active = pData.ImportProfile.Active
|
|
|
|
Dim oJobResponse = oUpdateJob.Run(New UpdateJob.UpdateJobRequest With {
|
|
.Job = pData.ImportProfile.Job,
|
|
.Action = pData.Action
|
|
})
|
|
|
|
If oJobResponse.OK = False Then
|
|
Return New UpdateProfileResponse With {.OK = False}
|
|
End If
|
|
|
|
Logger.Debug("Executing Action [{0}]", pData.Action)
|
|
|
|
Select Case pData.Action
|
|
Case UpdateProfileRequest.UpdateProfileAction.Update
|
|
If DoUpdateImportProfile(pData) Then
|
|
oResponse = UpdateImportProfileSteps(pData)
|
|
Else
|
|
oResponse = False
|
|
End If
|
|
|
|
Case UpdateProfileRequest.UpdateProfileAction.Create
|
|
oResponse = DoCreateImportProfile(pData)
|
|
|
|
Case UpdateProfileRequest.UpdateProfileAction.Delete
|
|
oResponse = DoDeleteImportProfile(pData)
|
|
|
|
End Select
|
|
|
|
Logger.Debug("Action successful: [{0}]", oResponse)
|
|
|
|
If oResponse Then
|
|
Scheduler.Reload()
|
|
Else
|
|
Logger.Warn("Error while updating Profile, data not reloaded!")
|
|
End If
|
|
|
|
Return New UpdateProfileResponse With {.OK = oResponse}
|
|
End Function
|
|
|
|
Private Function DoUpdateImportProfile(pData As UpdateProfileRequest) As Boolean
|
|
Dim oProfile = pData.ImportProfile
|
|
Dim oSQL As String = "UPDATE TBECM_JR_FIW_PROFILE
|
|
SET JR_JOB_ID = @JR_JOB_ID,
|
|
WM_OBJECTTYPE = @WM_OBJECTTYPE,
|
|
SOURCE_FOLDER = @SOURCE_FOLDER,
|
|
TARGET_FOLDER = @TARGET_FOLDER,
|
|
BACKUP_FOLDER = @BACKUP_FOLDER,
|
|
SF_DATE_FORMAT = @SF_DATE_FORMAT,
|
|
DEL_FILE_SUCCESS = @DEL_FILE_SUCCESS,
|
|
INCL_SUBFOLDER = @INCL_SUBFOLDER,
|
|
EXCLUDE_REGEX = @EXCLUDE_REGEX,
|
|
ACTIVE = @ACTIVE
|
|
WHERE GUID = @GUID"
|
|
Dim oCommand As New SqlClient.SqlCommand(oSQL)
|
|
|
|
oCommand.Parameters.Add("JR_JOB_ID", SqlDbType.Int).Value = oProfile.Job.Id
|
|
oCommand.Parameters.Add("WM_OBJECTTYPE", SqlDbType.NVarChar, 100).Value = oProfile.ObjectTypeName
|
|
oCommand.Parameters.Add("SOURCE_FOLDER", SqlDbType.NVarChar, 500).Value = oProfile.SourceFolder
|
|
oCommand.Parameters.Add("TARGET_FOLDER", SqlDbType.NVarChar, 500).Value = oProfile.TargetFolder
|
|
oCommand.Parameters.Add("BACKUP_FOLDER", SqlDbType.NVarChar, 500).Value = oProfile.BackupFolder
|
|
oCommand.Parameters.Add("EXCLUDE_REGEX", SqlDbType.NVarChar, 5000).Value = oProfile.FileExcludeRegex
|
|
oCommand.Parameters.Add("SF_DATE_FORMAT", SqlDbType.NVarChar, 50).Value = oProfile.SubfolderDateFormat
|
|
oCommand.Parameters.Add("DEL_FILE_SUCCESS", SqlDbType.Bit).Value = oProfile.DeleteFiles
|
|
oCommand.Parameters.Add("INCL_SUBFOLDER", SqlDbType.Bit).Value = oProfile.IncludeSubfolders
|
|
oCommand.Parameters.Add("ACTIVE", SqlDbType.Bit).Value = oProfile.Active
|
|
oCommand.Parameters.Add("GUID", SqlDbType.Int).Value = oProfile.Id
|
|
|
|
Return Database.ExecuteNonQuery(oCommand)
|
|
End Function
|
|
|
|
Private Function DoCreateImportProfile(pData As UpdateProfileRequest) As Boolean
|
|
Dim oProfile = pData.ImportProfile
|
|
Dim oSQL As String = "INSERT INTO TBECM_JR_FIW_PROFILE (
|
|
JR_JOB_ID,
|
|
WM_OBJECTTYPE,
|
|
SOURCE_FOLDER,
|
|
TARGET_FOLDER,
|
|
BACKUP_FOLDER,
|
|
SF_DATE_FORMAT,
|
|
DEL_FILE_SUCCESS,
|
|
INCL_SUBFOLDER,
|
|
EXCLUDE_REGEX,
|
|
ACTIVE
|
|
) VALUES (
|
|
@JR_JOB_ID,
|
|
@WM_OBJECTTYPE,
|
|
@SOURCE_FOLDER,
|
|
@TARGET_FOLDER,
|
|
@BACKUP_FOLDER,
|
|
@SF_DATE_FORMAT,
|
|
@DEL_FILE_SUCCESS,
|
|
@INCL_SUBFOLDER,
|
|
@EXCLUDE_REGEX,
|
|
@ACTIVE
|
|
)"
|
|
Dim oCommand As New SqlClient.SqlCommand(oSQL)
|
|
|
|
oCommand.Parameters.Add("JR_JOB_ID", SqlDbType.Int).Value = oProfile.Job.Id
|
|
oCommand.Parameters.Add("WM_OBJECTTYPE", SqlDbType.NVarChar, 100).Value = oProfile.ObjectTypeName
|
|
oCommand.Parameters.Add("SOURCE_FOLDER", SqlDbType.NVarChar, 500).Value = oProfile.SourceFolder
|
|
oCommand.Parameters.Add("TARGET_FOLDER", SqlDbType.NVarChar, 500).Value = oProfile.TargetFolder
|
|
oCommand.Parameters.Add("BACKUP_FOLDER", SqlDbType.NVarChar, 500).Value = Utils.NotNull(oProfile.BackupFolder, String.Empty)
|
|
oCommand.Parameters.Add("EXCLUDE_REGEX", SqlDbType.NVarChar, 5000).Value = Utils.NotNull(oProfile.FileExcludeRegex, String.Empty)
|
|
oCommand.Parameters.Add("SF_DATE_FORMAT", SqlDbType.NVarChar, 50).Value = Utils.NotNull(oProfile.SubfolderDateFormat, String.Empty)
|
|
oCommand.Parameters.Add("DEL_FILE_SUCCESS", SqlDbType.Bit).Value = oProfile.DeleteFiles
|
|
oCommand.Parameters.Add("INCL_SUBFOLDER", SqlDbType.Bit).Value = oProfile.IncludeSubfolders
|
|
oCommand.Parameters.Add("ACTIVE", SqlDbType.Bit).Value = oProfile.Active
|
|
oCommand.Parameters.Add("GUID", SqlDbType.Int).Value = oProfile.Id
|
|
|
|
Return Database.ExecuteNonQuery(oCommand)
|
|
End Function
|
|
|
|
Private Function DoDeleteImportProfile(pData As UpdateProfileRequest) As Boolean
|
|
Dim oProfile = pData.ImportProfile
|
|
Dim oSQL As String = "DELETE FROM TBECM_JR_FIW_PROFILE WHERE GUID = @GUID"
|
|
Dim oCommand As New SqlClient.SqlCommand(oSQL)
|
|
|
|
oCommand.Parameters.Add("GUID", SqlDbType.Int).Value = oProfile.Id
|
|
|
|
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 = Utils.NotNull(oStep.Argument1, String.Empty)
|
|
oCommand.Parameters.Add("ARGUMENT2", SqlDbType.NVarChar, 500).Value = Utils.NotNull(oStep.Argument2, String.Empty)
|
|
oCommand.Parameters.Add("ARGUMENT3", SqlDbType.NVarChar, 500).Value = Utils.NotNull(oStep.Argument3, String.Empty)
|
|
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 = Utils.NotNull(oStep.Argument1, String.Empty)
|
|
oCommand.Parameters.Add("ARGUMENT2", SqlDbType.NVarChar, 500).Value = Utils.NotNull(oStep.Argument2, String.Empty)
|
|
oCommand.Parameters.Add("ARGUMENT3", SqlDbType.NVarChar, 500).Value = Utils.NotNull(oStep.Argument3, String.Empty)
|
|
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
|
|
Logger.Debug("Preparing to delete Step [{0}]", oId)
|
|
Dim oCommand As New SqlClient.SqlCommand(oSQL)
|
|
|
|
oCommand.Parameters.Add("GUID", SqlDbType.Int).Value = oId
|
|
|
|
Logger.Debug("Deleting Step [{0}]..", oId)
|
|
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
|
|
Update
|
|
Delete
|
|
End Enum
|
|
|
|
Public Action As UpdateProfileAction
|
|
Public ImportProfile As ImportProfile
|
|
End Class
|
|
|
|
Public Class UpdateProfileResponse
|
|
Inherits Base.BaseResponse
|
|
End Class
|
|
End Class
|