tools/classes.ps1

class MetaNullTechnologyReport {
    static [hashtable]$Commands = @{}

    static [System.Management.Automation.FunctionInfo] GetModuleCommand([string]$Module, [string]$CommandName) {
        if( -not ([MetaNullTechnologyReport]::Commands.Keys -contains $Module )) {
            [MetaNullTechnologyReport]::Commands += @{ $Module = @{}}
        }
        if( -not ([MetaNullTechnologyReport]::Commands.$Module.$CommandName )) {
            $Command = Get-Command -Module $Module ($CommandName -ireplace '^([a-z]+-)',"`$1$((Get-Module $Module).Prefix)")
            if($Command) {
                [MetaNullTechnologyReport]::Commands.$Module.$CommandName = $Command
            }
        }
        return [MetaNullTechnologyReport]::Commands.$Module.$CommandName
    }

    static [System.Management.Automation.FunctionInfo] Utils([string]$CommandName) {
        return [MetaNullTechnologyReport]::GetModuleCommand('MetaNullUtils',$CommandName)
    }
    static [System.Management.Automation.FunctionInfo] Wiki([string]$CommandName) {
        return [MetaNullTechnologyReport]::GetModuleCommand('MetaNullWiki',$CommandName)
    }
    static [System.Management.Automation.FunctionInfo] Portfolio([string]$CommandName) {
        return [MetaNullTechnologyReport]::GetModuleCommand('MetaNullTechnologyReport',$CommandName)
    }
    static [System.Management.Automation.FunctionInfo] ConfluencePS([string]$CommandName) {
        return [MetaNullTechnologyReport]::GetModuleCommand('ConfluencePS',$CommandName)
    }

    static [string] $RegistryKeyPath = 'HKCU:\Software\MetaNullTechnologyReport'
    static [string] $CredentialKeyName = 'ConfluenceApiUri'
    static [string] $ConfluenceApiUri = 'https://eesc-cor.atlassian.net/wiki/rest/api'
    static [System.Management.Automation.PSCredential] $ConfluenceCredential = ([System.Management.Automation.PSCredential]::Empty)
    static [string] $PortfolioPageId = 34636515
    static [string] $TechnologyReportPageId = 157057188
    static [string[]] $ArtifactSet = @('domain','product','container','technology','key-data-entity')
    static [string[]] $TechnologyReportArtifactSubset = @('container','technology')

    static [void] InitializeCredential() {
        if(-not([MetaNullTechnologyReport]::ConfluenceCredential) -or ([MetaNullTechnologyReport]::ConfluenceCredential -eq [PSCredential]::empty)) {
            [MetaNullTechnologyReport]::ConfluenceCredential = &([MetaNullTechnologyReport]::Utils('Get-CachedCredential')) -Name [MetaNullTechnologyReport]::CredentialKeyName
        }
        if(-not([MetaNullTechnologyReport]::ConfluenceCredential) -or ([MetaNullTechnologyReport]::ConfluenceCredential -eq [PSCredential]::empty)) {
            [MetaNullTechnologyReport]::ConfluenceCredential = Get-Credential -Message 'Please provide your Atlassian Credentials'
        }
        if(-not([MetaNullTechnologyReport]::ConfluenceCredential) -or ([MetaNullTechnologyReport]::ConfluenceCredential -eq [PSCredential]::empty)) {
            throw 'Credentials required'
        } else {
            if( &([MetaNullTechnologyReport]::Utils('Test-CachedCredential')) -Name ([MetaNullTechnologyReport]::CredentialKeyName) ) {
                &([MetaNullTechnologyReport]::Utils('Set-CachedCredential')) -Name ([MetaNullTechnologyReport]::CredentialKeyName) -InputCredential ([MetaNullTechnologyReport]::ConfluenceCredential) | Out-Null
            } else {
                &([MetaNullTechnologyReport]::Utils('New-CachedCredential')) -Name ([MetaNullTechnologyReport]::CredentialKeyName) -InputCredential ([MetaNullTechnologyReport]::ConfluenceCredential) | Out-Null
            }
        }
    }
}
class TechnologyReportItem {
    [string] $Product

    [WikiContainer] $Container
    [WikiTechnology] $Technology
    [WikiVersionLink] $Version

    [WikiVersionPriority] GetPriority() {
        return $this.Technology.AssessVersion($this.Version.Version)
    }
    [bool] GetHasVersion() {
        $Conditions = @(
            ($null -ne $this.Version.Version -and $this.Version.Version -ne '0.0.0.0')
            ($null -ne $this.Technology.xVersionAccepted -and $this.Technology.xVersionAccepted -ne '0.0.0.0')
            # ($null -ne $this.Technology.xVersionRecommended -and $this.Technology.xVersionRecommended -ne '0.0.0.0')
            ($null -ne $this.Technology.xVersionOutdated -and $this.Technology.xVersionOutdated -ne '0.0.0.0')
            ($null -ne $this.Technology.xVersionVulnerable -and $this.Technology.xVersionVulnerable -ne '0.0.0.0')
        )
        return ((($Conditions|Where-Object{$_})|Measure-Object).Count -gt 0)
    }

    static [hashtable[]] $StaticScriptPropertyDefinition = @(
        @{
            MemberName = 'Priority'
            MemberType = 'ScriptProperty'
            Value = {
                $this.GetPriority()
            }
        }
        @{
            MemberName = 'HasVersion'
            MemberType = 'ScriptProperty'
            Value = {
                $this.GetHasVersion()
            }
        }
        @{
            MemberName = 'ProductName'
            MemberType = 'ScriptProperty'
            Value = {
                $this.Product
            }
        }
        @{
            MemberName = 'ContainerName'
            MemberType = 'ScriptProperty'
            Value = {
                $this.Container.PageTitle
            }
        }
        @{
            MemberName = 'TechnologyName'
            MemberType = 'ScriptProperty'
            Value = {
                $this.Technology.PageTitle
            }
        }
    )
    static TechnologyReportItem() {
        $TypeName = [TechnologyReportItem].Name
        foreach ($Definition in [TechnologyReportItem]::StaticScriptPropertyDefinition) {
            Update-TypeData -TypeName $TypeName @Definition
        }
    }

    TechnologyReportItem([string]$Product, [WikiContainer]$Container, [WikiTechnology]$Technology, [WikiVersionLink]$Version) {
        $this.Product = $Product
        $this.Container = $Container.Clone()
        $this.Technology = $Technology.Clone()
        $this.Version = $Version.Clone()
    }

    [TechnologyReportItem] Clone() {
        return $this.PSObject.Copy()
    }

    #[string] ToString() {
    # return "[$($this.Priority)][$($this.ProductName), $($this.ContainerName)] $($this.TechnologyName), $($this.Version.Version)"
    #}
}