Functions/BusinessLayer/Membership.ps1

<#
Add-/Remove- functions for the many-to-many link tables between Person, the three
organizational-unit tables (Department/Locality/ProfitCenter), and ESet.
Each -Person/-Department/-Locality/-ProfitCenter/-ESet parameter accepts either a UID
string or an object with the matching UID_* property, e.g. as returned by Get-OIMPerson.
#>


function Add-OIMPersonInDepartment {
    <#
    .SYNOPSIS
    Assigns a Person to a Department (PersonInDepartment).

    .EXAMPLE
    Add-OIMPersonInDepartment -Person $person -Department $department
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $Person,

        [Parameter(Mandatory = $true)]
        $Department
    )

    Process {
        $personUID = ConvertTo-OIMUID -InputObject $Person -PropertyName UID_Person
        $departmentUID = ConvertTo-OIMUID -InputObject $Department -PropertyName UID_Department

        if ($PSCmdlet.ShouldProcess("$personUID -> $departmentUID", 'Add PersonInDepartment')) {
            New-OIMObject -ObjectName PersonInDepartment -Properties @{UID_Person = $personUID; UID_Department = $departmentUID } -Confirm:$false
        }
    }
}

function Remove-OIMPersonInDepartment {
    <#
    .SYNOPSIS
    Removes a Person's assignment to a Department (PersonInDepartment).

    .EXAMPLE
    Remove-OIMPersonInDepartment -Person $person -Department $department
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $Person,

        [Parameter(Mandatory = $true)]
        $Department
    )

    Process {
        $personUID = ConvertTo-OIMUID -InputObject $Person -PropertyName UID_Person
        $departmentUID = ConvertTo-OIMUID -InputObject $Department -PropertyName UID_Department

        if ($PSCmdlet.ShouldProcess("$personUID -> $departmentUID", 'Remove PersonInDepartment')) {
            $safePerson = $personUID | ConvertTo-SQLLiteral
            $safeDepartment = $departmentUID | ConvertTo-SQLLiteral
            Get-OIMObject -ObjectName PersonInDepartment -Where "UID_Person = '$safePerson' AND UID_Department = '$safeDepartment'" |
                Remove-OIMObject -Confirm:$false
        }
    }
}

function Add-OIMPersonInLocality {
    <#
    .SYNOPSIS
    Assigns a Person to a Locality (PersonInLocality).

    .EXAMPLE
    Add-OIMPersonInLocality -Person $person -Locality $locality
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $Person,

        [Parameter(Mandatory = $true)]
        $Locality
    )

    Process {
        $personUID = ConvertTo-OIMUID -InputObject $Person -PropertyName UID_Person
        $localityUID = ConvertTo-OIMUID -InputObject $Locality -PropertyName UID_Locality

        if ($PSCmdlet.ShouldProcess("$personUID -> $localityUID", 'Add PersonInLocality')) {
            New-OIMObject -ObjectName PersonInLocality -Properties @{UID_Person = $personUID; UID_Locality = $localityUID } -Confirm:$false
        }
    }
}

function Remove-OIMPersonInLocality {
    <#
    .SYNOPSIS
    Removes a Person's assignment to a Locality (PersonInLocality).

    .EXAMPLE
    Remove-OIMPersonInLocality -Person $person -Locality $locality
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $Person,

        [Parameter(Mandatory = $true)]
        $Locality
    )

    Process {
        $personUID = ConvertTo-OIMUID -InputObject $Person -PropertyName UID_Person
        $localityUID = ConvertTo-OIMUID -InputObject $Locality -PropertyName UID_Locality

        if ($PSCmdlet.ShouldProcess("$personUID -> $localityUID", 'Remove PersonInLocality')) {
            $safePerson = $personUID | ConvertTo-SQLLiteral
            $safeLocality = $localityUID | ConvertTo-SQLLiteral
            Get-OIMObject -ObjectName PersonInLocality -Where "UID_Person = '$safePerson' AND UID_Locality = '$safeLocality'" |
                Remove-OIMObject -Confirm:$false
        }
    }
}

function Add-OIMPersonInProfitCenter {
    <#
    .SYNOPSIS
    Assigns a Person to a ProfitCenter (PersonInProfitCenter).

    .EXAMPLE
    Add-OIMPersonInProfitCenter -Person $person -ProfitCenter $profitCenter
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $Person,

        [Parameter(Mandatory = $true)]
        $ProfitCenter
    )

    Process {
        $personUID = ConvertTo-OIMUID -InputObject $Person -PropertyName UID_Person
        $profitCenterUID = ConvertTo-OIMUID -InputObject $ProfitCenter -PropertyName UID_ProfitCenter

        if ($PSCmdlet.ShouldProcess("$personUID -> $profitCenterUID", 'Add PersonInProfitCenter')) {
            New-OIMObject -ObjectName PersonInProfitCenter -Properties @{UID_Person = $personUID; UID_ProfitCenter = $profitCenterUID } -Confirm:$false
        }
    }
}

function Remove-OIMPersonInProfitCenter {
    <#
    .SYNOPSIS
    Removes a Person's assignment to a ProfitCenter (PersonInProfitCenter).

    .EXAMPLE
    Remove-OIMPersonInProfitCenter -Person $person -ProfitCenter $profitCenter
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $Person,

        [Parameter(Mandatory = $true)]
        $ProfitCenter
    )

    Process {
        $personUID = ConvertTo-OIMUID -InputObject $Person -PropertyName UID_Person
        $profitCenterUID = ConvertTo-OIMUID -InputObject $ProfitCenter -PropertyName UID_ProfitCenter

        if ($PSCmdlet.ShouldProcess("$personUID -> $profitCenterUID", 'Remove PersonInProfitCenter')) {
            $safePerson = $personUID | ConvertTo-SQLLiteral
            $safeProfitCenter = $profitCenterUID | ConvertTo-SQLLiteral
            Get-OIMObject -ObjectName PersonInProfitCenter -Where "UID_Person = '$safePerson' AND UID_ProfitCenter = '$safeProfitCenter'" |
                Remove-OIMObject -Confirm:$false
        }
    }
}

function Add-OIMDepartmentESet {
    <#
    .SYNOPSIS
    Assigns an ESet (entitlement set) to a Department (DepartmentHasESet).

    .EXAMPLE
    Add-OIMDepartmentESet -Department $department -ESet $eset
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $Department,

        [Parameter(Mandatory = $true)]
        $ESet
    )

    Process {
        $departmentUID = ConvertTo-OIMUID -InputObject $Department -PropertyName UID_Department
        $esetUID = ConvertTo-OIMUID -InputObject $ESet -PropertyName UID_ESet

        if ($PSCmdlet.ShouldProcess("$departmentUID -> $esetUID", 'Add DepartmentHasESet')) {
            New-OIMObject -ObjectName DepartmentHasESet -Properties @{UID_Department = $departmentUID; UID_ESet = $esetUID } -Confirm:$false
        }
    }
}

function Remove-OIMDepartmentESet {
    <#
    .SYNOPSIS
    Removes an ESet assignment from a Department (DepartmentHasESet).

    .EXAMPLE
    Remove-OIMDepartmentESet -Department $department -ESet $eset
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $Department,

        [Parameter(Mandatory = $true)]
        $ESet
    )

    Process {
        $departmentUID = ConvertTo-OIMUID -InputObject $Department -PropertyName UID_Department
        $esetUID = ConvertTo-OIMUID -InputObject $ESet -PropertyName UID_ESet

        if ($PSCmdlet.ShouldProcess("$departmentUID -> $esetUID", 'Remove DepartmentHasESet')) {
            $safeDepartment = $departmentUID | ConvertTo-SQLLiteral
            $safeESet = $esetUID | ConvertTo-SQLLiteral
            Get-OIMObject -ObjectName DepartmentHasESet -Where "UID_Department = '$safeDepartment' AND UID_ESet = '$safeESet'" |
                Remove-OIMObject -Confirm:$false
        }
    }
}

function Add-OIMLocalityESet {
    <#
    .SYNOPSIS
    Assigns an ESet (entitlement set) to a Locality (LocalityHasESet).

    .EXAMPLE
    Add-OIMLocalityESet -Locality $locality -ESet $eset
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $Locality,

        [Parameter(Mandatory = $true)]
        $ESet
    )

    Process {
        $localityUID = ConvertTo-OIMUID -InputObject $Locality -PropertyName UID_Locality
        $esetUID = ConvertTo-OIMUID -InputObject $ESet -PropertyName UID_ESet

        if ($PSCmdlet.ShouldProcess("$localityUID -> $esetUID", 'Add LocalityHasESet')) {
            New-OIMObject -ObjectName LocalityHasESet -Properties @{UID_Locality = $localityUID; UID_ESet = $esetUID } -Confirm:$false
        }
    }
}

function Remove-OIMLocalityESet {
    <#
    .SYNOPSIS
    Removes an ESet assignment from a Locality (LocalityHasESet).

    .EXAMPLE
    Remove-OIMLocalityESet -Locality $locality -ESet $eset
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $Locality,

        [Parameter(Mandatory = $true)]
        $ESet
    )

    Process {
        $localityUID = ConvertTo-OIMUID -InputObject $Locality -PropertyName UID_Locality
        $esetUID = ConvertTo-OIMUID -InputObject $ESet -PropertyName UID_ESet

        if ($PSCmdlet.ShouldProcess("$localityUID -> $esetUID", 'Remove LocalityHasESet')) {
            $safeLocality = $localityUID | ConvertTo-SQLLiteral
            $safeESet = $esetUID | ConvertTo-SQLLiteral
            Get-OIMObject -ObjectName LocalityHasESet -Where "UID_Locality = '$safeLocality' AND UID_ESet = '$safeESet'" |
                Remove-OIMObject -Confirm:$false
        }
    }
}

function Add-OIMProfitCenterESet {
    <#
    .SYNOPSIS
    Assigns an ESet (entitlement set) to a ProfitCenter (ProfitCenterHasESet).

    .EXAMPLE
    Add-OIMProfitCenterESet -ProfitCenter $profitCenter -ESet $eset
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $ProfitCenter,

        [Parameter(Mandatory = $true)]
        $ESet
    )

    Process {
        $profitCenterUID = ConvertTo-OIMUID -InputObject $ProfitCenter -PropertyName UID_ProfitCenter
        $esetUID = ConvertTo-OIMUID -InputObject $ESet -PropertyName UID_ESet

        if ($PSCmdlet.ShouldProcess("$profitCenterUID -> $esetUID", 'Add ProfitCenterHasESet')) {
            New-OIMObject -ObjectName ProfitCenterHasESet -Properties @{UID_ProfitCenter = $profitCenterUID; UID_ESet = $esetUID } -Confirm:$false
        }
    }
}

function Remove-OIMProfitCenterESet {
    <#
    .SYNOPSIS
    Removes an ESet assignment from a ProfitCenter (ProfitCenterHasESet).

    .EXAMPLE
    Remove-OIMProfitCenterESet -ProfitCenter $profitCenter -ESet $eset
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
        $ProfitCenter,

        [Parameter(Mandatory = $true)]
        $ESet
    )

    Process {
        $profitCenterUID = ConvertTo-OIMUID -InputObject $ProfitCenter -PropertyName UID_ProfitCenter
        $esetUID = ConvertTo-OIMUID -InputObject $ESet -PropertyName UID_ESet

        if ($PSCmdlet.ShouldProcess("$profitCenterUID -> $esetUID", 'Remove ProfitCenterHasESet')) {
            $safeProfitCenter = $profitCenterUID | ConvertTo-SQLLiteral
            $safeESet = $esetUID | ConvertTo-SQLLiteral
            Get-OIMObject -ObjectName ProfitCenterHasESet -Where "UID_ProfitCenter = '$safeProfitCenter' AND UID_ESet = '$safeESet'" |
                Remove-OIMObject -Confirm:$false
        }
    }
}