lib/Classes/Public/TMBrokerSubject.ps1


class TMBrokerSubject {

    #region Non-Static Properties

    [TMBrokerSubjectAction]$Action
    [System.Int64]$Order
    [System.Int64]$ProjectId
    [TMBrokerSubjectTask]$Task
    [TMBrokerSubjectAsset]$Asset
    [Object]$Job

    #endregion Non-Static Properties

    #region Constructors

    TMBrokerSubject() {}

    TMBrokerSubject([TMTask]$_Task) {
        $this.Order = 0
        $this.ProjectId = $_Task.Project.Id
        $this.Task = [TMBrokerSubjectTask]::new(
            $_Task.Id,
            $_Task.Status,
            $_Task.LastUpdated,
            $_Task.Slack,
            $_Task.TaskNumber,
            $_Task.Title,
            $_Task.Successors
        )
        $this.Asset = [TMBrokerSubjectAsset]::new($_Task.Asset)
        $this.Action = [TMBrokerSubjectAction]::new($_Task.Action)
    }

    TMBrokerSubject([TMTask]$_Task, [Nullable[System.Int64]]$_Order = 0) {
        $this.Order = $_Order
        $this.ProjectId = $_Task.Project.Id
        $this.Task = [TMBrokerSubjectTask]::new(
            $_Task.Id,
            $_Task.Status,
            $_Task.LastUpdated,
            $_Task.Slack,
            $_Task.TaskNumber,
            $_Task.Title,
            $_Task.Successors
        )
        $this.Asset = [TMBrokerSubjectAsset]::new($_Task.Asset)
        $this.Action = [TMBrokerSubjectAction]::new($_Task.Action)
    }

    TMBrokerSubject([TMTask]$_Task, [PSCustomObject]$_Action, [Nullable[System.Int64]]$_Order = 0) {
        $this.Order = $_Order
        $this.ProjectId = $_Task.Project.Id
        $this.Task = [TMBrokerSubjectTask]::new(
            $_Task.Id,
            $_Task.Status,
            $_Task.LastUpdated,
            $_Task.Slack,
            $_Task.TaskNumber,
            $_Task.Title,
            $_Task.Successors
        )
        $this.Asset = [TMBrokerSubjectAsset]::new($_Task.Asset)
        $this.Action = [TMBrokerSubjectAction]::new($_Action)
    }

    #endregion Constructors

    #region Non-Static Methods

    [void]Invoke([String]$TMSession, [Object]$Cache) {
        try {
            Invoke-SubjectTaskAction -TMSession $TMSession -Subject $this -Cache $Cache
        }
        catch {
            $ErrorString = $_.Exception.Message
            if ($ErrorString -match '"The argument "Started" does not belong') {
                Write-Host "This Subject Task has already been started elsewhere." -ForegroundColor Magenta
                return
            }
            else {
                Write-Host "Invoking the Task failed: [$($ErrorString)]" -ForegroundColor Magenta
                return
            }
        }
    }

    [void]Invoke([String]$TMSession) {
        try {
            Invoke-SubjectTaskAction -TMSession $TMSession -Subject $this
        } catch {
            $ErrorString = $_.Exception.Message
            if ($ErrorString -match '"The argument "Started" does not belong') {
                Write-Host "This Subject Task has already been started elsewhere." -ForegroundColor Magenta
                return
            } else {
                Write-Host "Invoking the Task failed: [$($ErrorString)]" -ForegroundColor Magenta
                return
            }
        }
    }

    ## Invoke Parallel execution tasks. This sends the ActionRequest to SessionManager for execution
    [void]InvokeParallel([String]$TMSession, [Object]$Cache) {
        try {
            Invoke-SubjectTaskActionParallel -TMSession $TMSession -Subject $this -Cache $Cache
        }
        catch {
            $ErrorString = $_.Exception.Message
            if ($ErrorString -match '"The argument "Started" does not belong') {
                Write-Host "This Subject Task has already been started elsewhere." -ForegroundColor Magenta
                return
            }
            else {
                Write-Host "Invoking the Task failed: [$($ErrorString)]" -ForegroundColor Magenta
                return
            }
        }
    }

    [void]InvokeParallel([String]$TMSession) {
        try {
            Invoke-SubjectTaskActionParallel -TMSession $TMSession -Subject $this
        } catch {
            $ErrorString = $_.Exception.Message
            if ($ErrorString -match '"The argument "Started" does not belong') {
                Write-Host "This Subject Task has already been started elsewhere." -ForegroundColor Magenta
                return
            } else {
                Write-Host "Invoking the Task failed: [$($ErrorString)]" -ForegroundColor Magenta
                return
            }
        }
    }

    #endregion Non-Static Methods
}


class TMBrokerSubjectAction {

    #region Non-Static Properties

    [System.Int64]$Id
    [System.String]$Script
    [System.Object]$Params
    [System.String]$Name
    [TMBrokerSubjectActionSetting]$Settings
    [ValidateSet('Started', 'Pending', 'Successful', 'Failed')]
    [System.String]$ExecutionStatus
    [System.Object[]]$Errors

    #endregion Non-Static Properties

    #region Constructors

    TMBrokerSubjectAction() {}

    TMBrokerSubjectAction([System.Int64]$_Id, [System.String]$_Script, [System.Object]$_Params, [System.String]$_Name) {
        $this.Id = $_Id
        $this.Params = $_Params
        $this.Name = $_Name
        $this.ExecutionStatus = 'Pending'
        $this.Errors = @()
        $this.ApplyActionSettings()
    }

    TMBrokerSubjectAction([Object]$_object) {
        $this.Id = $_object.Id
        $this.Script = $_object.Script
        $this.Params = $_object.methodParams
        $this.Name = $_object.Name
        $this.ExecutionStatus = 'Pending'
        $this.Errors = @()
        $this.ApplyActionSettings()
    }

    #endregion Constructors

    [void]ApplyActionSettings() {
        $ActionSettings = [PSCustomObject]@{
            RetryCount  = 0
            WaitSeconds = 30
        }
        $SettingParams = $this.Params | Where-Object {
            ($_.Context -eq 'USER_DEF') -and
            ($_.ParamName -match 'brokersetting_') -and
            ($_.Value -ne '')
        }

        foreach ($Param in $SettingParams) {
            switch (($Param.ParamName -split '_')[1]) {
                'RetryCount' {
                    $ActionSettings.RetryCount = [Int32]$Param.Value
                }

                'WaitSeconds' {
                    $ActionSettings.WaitSeconds = [Int32]$Param.Value
                }

                default { }
            }
        }

        $this.Settings = [TMBrokerSubjectActionSetting]::new($ActionSettings)
    }
}


class TMBrokerSubjectActionSetting {

    [Int32]$RetryCount
    [Int32]$WaitSeconds

    TMBrokerSubjectActionSetting() {
        $this.Retry = 0
        $this.WaitSeconds = 30
    }

    TMBrokerSubjectActionSetting([Int32]$_retryCount, [Int32]$_waitSeconds) {
        $this.RetryCount = $_retryCount
        $this.WaitSeconds = $_waitSeconds
    }

    TMBrokerSubjectActionSetting([Object]$_object) {
        $this.RetryCount = $_object.RetryCount
        $this.WaitSeconds = $_object.WaitSeconds
    }
}


class TMBrokerSubjectTask {

    #region Non-Static Properties

    [System.Int64]$Id
    [System.String]$Status
    [System.String]$PreviousStatus
    [Nullable[System.DateTime]]$LastUpdated
    [System.Int64]$Score
    [System.Int64]$Number
    [System.String]$Title
    [TMTaskDependency[]]$Successors

    #endregion Non-Static Properties

    #region Constructors

    TMBrokerSubjectTask() {}

    TMBrokerSubjectTask(
        [System.Int64]$_Id,
        [System.String]$_Status,
        [Nullable[System.DateTime]]$_LastUpdated,
        [System.Int64]$_Score,
        [System.Int64]$_Number,
        [System.String]$_Title,
        [TMTaskDependency[]]$_Successors
    ) {
        $this.Id = $_Id
        $this.Status = $_Status
        $this.PreviousStatus = $_Status
        $this.LastUpdated = $_LastUpdated
        $this.Score = $_Score
        $this.Number = $_Number
        $this.Title = $_Title
        $this.Successors = $_Successors | ForEach-Object { [TMTaskDependency]::new($_) }
    }

    TMBrokerSubjectTask([Object]$_object) {
        $this.Id = $_object.Id
        $this.Status = $_object.Status
        $this.PreviousStatus = $_object.Status
        $this.LastUpdated = $_object.LastUpdated
        $this.Score = $_object.Score
        $this.Number = $_object.Number
        $this.Title = $_object.Title
        $this.Successors = $_object.Successors | ForEach-Object { [TMTaskDependency]::new($_) }
    }

    #endregion Constructors

}


class TMBrokerSubjectAsset {

    #region Non-Static Properties

    [System.Int64]$Id
    [System.String]$Name
    [System.String]$Class
    [System.String]$Type
    [TMReference]$Bundle

    #endregion Non-Static Properties

    #region Constructors

    TMBrokerSubjectAsset() {}

    TMBrokerSubjectAsset([System.Int64]$_Id, [System.String]$_Name, [System.String]$_Class, [System.String]$_Type, [TMReference]$_Bundle) {
        $this.Id = $_Id
        $this.Name = $_Name
        $this.Class = $_Class
        $this.Type = $_Type
        $this.Bundle = [TMReference]::new($_Bundle)
    }

    TMBrokerSubjectAsset([Object]$_object) {
        $this.Id = $_object.Id
        $this.Name = $_object.Name
        $this.Class = $_object.Class
        $this.Type = $_object.Type
        $this.Bundle = [TMReference]::new($_object.Bundle)
    }

    #endregion Constructors

}