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, [PSCustomObject]$_Action, [Nullable[System.Int64]]$_Order = 0) {
        $this.Order = $_Order
        $this.ProjectId = $_Task.Project.Id
        $this.Task = [TMBrokerSubjectTask]::new(
            $_Task.Id,
            $_Task.Status,
            $_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) {
        Invoke-SubjectTaskAction -TMSession $TMSession -Subject $this
    }

    ## 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) {
        Invoke-SubjectTaskAction -TMSession $TMSession -Subject $this
    }

    #endregion Non-Static Methods
}


class TMBrokerSubjectAction {

    #region Non-Static Properties

    [System.Int64]$Id
    [System.String]$Script
    [System.Object]$Params
    [System.String]$Name
    [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.Script = $_Script
        $this.Params = $_Params
        $this.Name = $_Name
        $this.ExecutionStatus = 'Pending'
        $this.Errors = @()


    }

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

    #endregion Constructors

}


class TMBrokerSubjectTask {

    #region Non-Static Properties

    [System.Int64]$Id
    [System.String]$Status
    [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,
        [System.Int64]$_Score,
        [System.Int64]$_Number,
        [System.String]$_Title,
        [TMTaskDependency[]]$_Successors
    ) {
        $this.Id = $_Id
        $this.Status = $_Status
        $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.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

}