Public/OData/Get-BDRelatedObject.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
function Get-BDRelatedObject {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true,ParameterSetName = "Identity", ValueFromPipeline=$true, Position=0)][String] $Identity,
        [Parameter(Mandatory = $false,ParameterSetName = "Object", ValueFromPipelineByPropertyName=$true)] [String] $Title, 
        [Parameter(Mandatory = $false,ParameterSetName = "Object", ValueFromPipelineByPropertyName=$true)] [String] $ID,
        
        [Parameter(Mandatory = $false)] [array] $Select,
        [Parameter(Mandatory = $false)] [int] $Top = $DefaultRecordLimit,
        [Alias("any")][switch] $AnyCondition
    )
    
    begin {
        $MaxRecordsInCall = 10 #Or it 'll glitch
    }
    
    process {     
        switch ($PsCmdlet.ParameterSetName) {
            "Identity" {
                $UsedIdentity = $Identity
            }
            "Object" {
                if($Title -and !$ID) {
                    $UsedIdentity =  (Get-BDObject -IsExactly @{Title = $Title}).ID 
                }
                else {
                    $UsedIdentity = $ID
                }
            }
        }
        if($UsedIdentity){
            $Relations = Get-BDRelation -IsExactly @{BlueDolphinObjectItemId = $UsedIdentity}
            $ThisObject = Get-BDObject -IsExactly @{ID = $UsedIdentity}
            $Retval = @()
            $isExactly = @{ID = @()}
            
            $Objects = @()

            #Get the Objects
            ForEach ($Relation in $Relations) {
                $RecordsInCalls +=1
                $isExactly.ID += $Relation.RelatedBlueDolphinObjectItemId  

                #If there are x object id in queue or this is the last object, get the objects
                if($RecordsInCalls -eq $MaxRecordsInCall -or $Relation -eq $Relations[-1]) {
                    $Objects += Get-BDObject -IsExactly $isExactly 
                    $RecordsInCalls = 0
                    $isExactly = @{ID = @()}
                }
            }


            #Attach the relation and object details
            ForEach ($Relation in $Relations) {
                $Object = [PSCustomObject]@{}
                $Object = ($Objects | ? ID -eq $Relation.RelatedBlueDolphinObjectItemId)[0].PSObject.Copy()
                $Object | Add-Member -Name "Direction" -MemberType NoteProperty -Value (IIf $Relation.IsRelationshipDirectionAlternative "=>" "<=")
                $Object | Add-Member -Name "Relation" -MemberType NoteProperty -Value $Relation  
                $Object | Add-Member -Name "RelatedObject" -MemberType NoteProperty -Value $ThisObject 
                [void]$Object.PSObject.TypeNames.Remove("BlueDolphin.Object")
                [void]$Object.PSObject.TypeNames.Insert(0,"BlueDolphin.RelatedObject")
                $Retval += $Object
            }

            Return $RetVal
        }
    }
    
    end {
        
    }
}