Functions/BusinessLayer/Locality.ps1

function Get-OIMLocality {
    <#
    .SYNOPSIS
    Gets one or more Locality objects.

    .EXAMPLE
    Get-OIMLocality -Name 'Amsterdam'

    .EXAMPLE
    Get-OIMLocality -id $uid
    #>

    [CmdletBinding(DefaultParameterSetName = 'Search')]
    param(
        [Parameter(ParameterSetName = 'Single', Mandatory = $true)]
        [Alias('uid')]
        [string]$id,

        [Parameter(ParameterSetName = 'Search')]
        [Alias('Ident_locality')]
        [string]$Name,

        [Parameter(ParameterSetName = 'Search')]
        [string]$Where,

        [Parameter(ParameterSetName = 'Search')]
        [int]$First
    )

    if ($PSCmdlet.ParameterSetName -eq 'Single') {
        Get-OIMObject -ObjectName Locality -id $id
        return
    }

    $conditions = [Collections.Generic.List[string]]::new()
    if ($PSBoundParameters.ContainsKey('Name')) { $conditions.Add("Ident_locality = '$($Name | ConvertTo-SQLLiteral)'") }
    if ($PSBoundParameters.ContainsKey('Where')) { $conditions.Add("($Where)") }

    $getParams = @{ ObjectName = 'Locality' }
    if ($conditions.Count -gt 0) { $getParams['Where'] = $conditions -join ' AND ' }
    if ($PSBoundParameters.ContainsKey('First')) { $getParams['First'] = $First }

    Get-OIMObject @getParams
}

function New-OIMLocality {
    <#
    .SYNOPSIS
    Creates a Locality object.

    .PARAMETER ParentLocality
    A Locality object (as returned by Get-OIMLocality) or UID string to nest this locality under.

    .PARAMETER checkexists
    If a Locality with the given -Name already exists, return it instead of creating a duplicate.

    .EXAMPLE
    New-OIMLocality -Name 'Amsterdam' -ShortName AMS
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory = $true)]
        [string]$Name,

        [string]$ShortName,

        $ParentLocality,

        [hashtable]$Properties = @{},

        [switch]$checkexists
    )

    if ($checkexists) {
        $existing = Get-OIMLocality -Name $Name
        if ($null -ne $existing) { return $existing }
    }

    $allProperties = @{ Ident_locality = $Name }
    if ($PSBoundParameters.ContainsKey('ShortName')) { $allProperties['ShortName'] = $ShortName }
    if ($PSBoundParameters.ContainsKey('ParentLocality')) { $allProperties['UID_ParentLocality'] = ConvertTo-OIMUID -InputObject $ParentLocality -PropertyName UID_Locality }
    foreach ($key in $Properties.Keys) { $allProperties[$key] = $Properties[$key] }

    if ($PSCmdlet.ShouldProcess($Name, 'Create Locality')) {
        New-OIMObject -ObjectName Locality -Properties $allProperties -Confirm:$false
    }
}

function Set-OIMLocality {
    <#
    .SYNOPSIS
    Updates properties on one or more Locality objects.

    .EXAMPLE
    Get-OIMLocality -Name Amsterdam | Set-OIMLocality -Properties @{ShortName = 'AMS'}
    #>

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

        [Parameter(Mandatory = $true)]
        [hashtable]$Properties
    )

    Process {
        if ($PSCmdlet.ShouldProcess("$($Object.UID_Locality)", 'Update Locality')) {
            $Object | Set-OIMObject -Properties $Properties -Confirm:$false
        }
    }
}

function Remove-OIMLocality {
    <#
    .SYNOPSIS
    Removes one or more Locality objects.

    .EXAMPLE
    Get-OIMLocality -Name Amsterdam | Remove-OIMLocality
    #>

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

    Process {
        if ($PSCmdlet.ShouldProcess("$($Object.UID_Locality)", 'Remove Locality')) {
            $Object | Remove-OIMObject -Confirm:$false
        }
    }
}