private/AdfObjectName.class.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
class AdfObjectName {
    [string] $Name
    [string] $Type
    [string] $Folder

    AdfObjectName ([string] $Name, [string] $Type) 
    {
        $this.Name = $Name
        $this.Type = $Type
    }

    AdfObjectName ([string] $Name, [string] $Type, [string] $Folder) 
    {
        $this.Name = $Name
        $this.Type = $Type
        $this.Folder = $Folder
    }

    AdfObjectName ([string] $FullName) 
    {
        $m = [regex]::matches($FullName, '([a-zA-Z]+)\.([a-zA-Z 0-9\-_]+)@?(.*)')
        if ($m.Success -eq $false) {
            throw "ADFT0028: Expected format of name for 'FullName' input parameter is: objectType.objectName[@folderName]"
        }
        if ($m.Groups[1].Value -notin [AdfObject]::allowedTypes ) { 
            throw "ADFT0029: Unknown object type: $($m.Groups[1].Value)."
        }
        $this.Type = $m.Groups[1].Value
        $this.Name = $m.Groups[2].Value
        $this.Folder = $m.Groups[3].Value
    }

    [String] FullName ([boolean] $quoted)
    {
        if ($quoted) {
            return "[$($this.Type)].[$($this.Name)]"
        } else {
            return "$($this.Type).$($this.Name)"
        }
    }

    [String] FullNameWithFolder ()
    {
        if ($this.Folder.Length -gt 0) {
            return "$($this.Type).$($this.Name)@$($this.Folder)"
        } else {
            return "$($this.Type).$($this.Name)"
        }
    }

    [String] FullName ()
    {
        return $this.FullName($false)
    }

    [String] FullNameQuoted ()
    {
        return $this.FullName($true)
    }

    [Boolean] IsNameMatch ([array]$wildcardPatterns)
    {
        $fullname = $this.FullName()
        $r = $wildcardPatterns | Where-Object { $fullname -like $_ }
        return $null -ne $r
    }

    [Boolean] IsNameExcluded ([AdfPublishOption] $opt)
    {
        $fullname = $this.FullNameWithFolder()

        # One can exclude objects by listing them explicitly in Excludes collection, ...
        $excPatterns = $opt.Excludes.Keys
        $r = $excPatterns | Where-Object { $fullname -like $_ }
        if ($null -ne $r) 
        { 
            # Means: object is excluded if matches any item in (Excludes) collection
            return $true    
        }

        # ... or by listing them implicitly in Includes collection:
        $incPatterns = $opt.Includes.Keys
        if ($incPatterns.Count -eq 0) 
        { 
            # If no items = all objects match => object is not excluded
            return $false 
        }
        $r = $incPatterns | Where-Object { $fullname -like $_ }
        # Means: object is excluded if not match any item in (Includes) collection
        return ($null -eq $r)   
    }

}