MediaValet.DAM.psm1


function Import-MvDamFilenameListFromCsv {
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true,Position=0)]
        [string]$Path
    )
    PROCESS {
        $csv = Import-Csv -Path $Path
        $filenameArray = $csv | Foreach {$_.Filename}
        return $filenameArray
    }
    
}

function Export-MvDamAssetInfo {
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true,Position=0,ParameterSetName="From Filename List")]
        [string[]]$FilenameList,
        [parameter(Mandatory=$true,Position=0,ParameterSetName="From Asset ID List")]
        [string[]]$AssetIdList,
        [parameter(Mandatory=$true, Position=0,ParameterSetName="All Assets")]
        [switch]$ListAllAssets,
        [parameter(Mandatory=$true,Position=1)]
        [string]$Path
    )
    PROCESS {
        "Starting Export"
        if ($ListAllAssets)
        {
            Get-MvDamAssetInfo -ListAllAssets -Path $Path
        }
        else
        {
            $assets = @()
            if ($FilenameList)
            {
                $assets = Get-MvDamAssetInfo -FilenameList $FilenameList
            }
            if ($AssetIdList)
            {
                $assets = Get-MvDamAssetInfo -AssetIds $AssetIdList
            }
            "Building file"
            $assets | Select -Property Id, FileName, Title, Description, Status,@{N='Keywords';E={$_.Keywords -join ","}},`
                      @{N='Categories';E={$_.Categories.Tree.Name -join ","}}, @{N='Attributes';E={$_.Attributes -join ","}},`
                      AverageRating, UserRating, FileType, SizeInBytes, MediaType, Stream, VersionNo, CurrentVersion, HeadVersion, ParentId,`
                      IsLatestestVersion, CreatedDate, ModifiedAt, ImageHeight, ImageWidth, Dimensions, FrameRate, Length, BitRate,`
                      Resolution, ColorMode | Export-Csv $Path -NoTypeInformation
        }
    
        "Export Complete"
    }
}

function Export-MvDamAssetAttribute {
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true,Position=0)]
        [object[]]$AssetInfoList,
        [parameter(Mandatory=$false,Position=1)]
        [string[]]$AttributeList,
        [parameter(Mandatory=$true,Position=2)]
        [string]$Path
    )
    PROCESS {
        if ($AssetInfoList.GetType().Name -ne "AssetInfoModel[]" -or $AssetInfoList.Count -eq 0)
        {
            Write-Error -Message "Please pass an AssetInfoModel array with one or more items to the `$AssetInfoList parameter"
        }
        #Get AttributeList for Library
        if ($AttributeList -eq $null)
        {
            $AttributeList = (Get-MvDamAttributeDef) | Foreach {if ($_.IsSystemProperty){"System."+$_.Name}else{$_.Name}}
        }
        else #validate the AttributeList against the AttributeDefs
        {
            $objAttrNames = (Get-MvDamAttributeDef) | Foreach {if ($_.IsSystemProperty){"System."+$_.Name}else{$_.Name}}
            $AttributeList | Foreach { if (-not $objAttrNames.Contains($_)) {Write-Error "Invalid attribute name $_ specified"} } 
        }
        $assets = @()
        Foreach ($assetInfo in $AssetInfoList)
        {
            $asset = New-Object -TypeName PSObject
            $asset | Add-Member -MemberType NoteProperty -Name 'System.Id' -Value $assetInfo.Id
            $asset | Add-Member -MemberType NoteProperty -Name 'System.FileName' -Value $assetInfo.FileName
            $asset | Add-Member -MemberType NoteProperty -Name 'System.Title' -Value $assetInfo.Title
            $asset | Add-Member -MemberType NoteProperty -Name 'System.Description' -Value $assetInfo.Description
            Foreach($attributeName in $AttributeList)
            {
                if ($attributeName -notlike "System.*")
                {
                    $attrValue = $assetInfo.Attributes | Where {$_.AttributeName -eq $attributeName} | Select -Property AttributeValue
                    $asset | Add-Member -MemberType NoteProperty -Name "$attributeName" -Value $attrValue.AttributeValue
                }
            }
            $assets += $asset  
        }
        $assets | Export-Csv -Path $Path -NoTypeInformation
        return $assets
    }
}

function Import-MvDamAssetAttributesFromCsv {
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true,Position=0)]
        [string]$SourcePath,
        [parameter(Position=1)]
        [string]$LogPath
    )
    PROCESS {
        $csv = @()
        $import = Import-Csv -Path $SourcePath
        if ($import.GetType().ToString() -eq "System.Object[]")
        {
            $csv = $import
        }
        else
        {
            $csv = @($import)
        }
        for($i=0;$i -lt $csv.Length; $i++)
        {
            $line = $csv[$i]
            $assetId = $line.('System.Id')
            $percentComplete = ($i/$csv.Length)*100
            Write-Progress -Activity "Importing Asset Attributes" -Status "Updating attributes for Asset Id $assetId" -PercentComplete $percentComplete 
            $txnInfo = New-MvDamTxnInfo -StartTime (Get-Date) -Id $assetId  -Status "InProgress" -TxnType "Update-MvDamAssetAttribute"
            $attrKvps = @{}
            $line | Get-Member | Where {$_.MemberType -eq "NoteProperty"} | Foreach {if ($line.($_.Name) -ne "") {$attrKvps.Add($_.Name, $line.($_.Name))}}
            $errorInfo = $null
            $txnResult = Update-MvDamAssetAttribute -AssetId $assetId -Attributes $attrKvps -ErrorAction Continue -ErrorVariable $errorInfo
            $txnInfo.EndTime = Get-Date
            if ($errorInfo)
            {
                $txnInfo.Status = "Failed"
                $txnInfo.Notes = $errorInfo.Message
            }
            else
            {
                $txnInfo.Status = "Succeeded"
            }
            if ($LogPath)
            {
                $txnInfo | Export-Csv -Path $LogPath -Append
            }
        }
        Write-Progress -Activity "Importing Asset Attributes" -Completed 

        return 
    }
}

function Test-MvDamAssetAttributeCsv {
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true,Position=0)]
        [string]$SourcePath,
        [parameter(Mandatory=$true,Position=1)]
        [string]$ResultPath
    )
    PROCESS {
        $csv = @()
        $import = Import-Csv -Path $SourcePath
        if ($import.GetType().ToString() -eq "System.Object[]")
        {
            $csv = $import
        }
        else
        {
            $csv = @($import)
        }
        $assetsWithErrors = 0
        $totalErrors = 0
        for($i=0;$i -lt $csv.Length; $i++)
        {
            $line = $csv[$i]
            $assetId = $line.('System.Id')
            $percentComplete = ($i/$csv.Length)*100
            Write-Progress -Activity "Validating Asset Attributes" -Status "Testing attributes for Asset Id $assetId" -PercentComplete $percentComplete 
            $attrKvps = @{}
            $line | Get-Member | Where {$_.MemberType -eq "NoteProperty"} | Foreach {if ($line.($_.Name) -ne "") {$attrKvps.Add($_.Name, $line.($_.Name))}}
            $errorInfo = $null
            $txnResult = Test-MvDamAssetAttribute -AssetId $assetId -Attributes $attrKvps -ErrorAction Continue -ErrorVariable $errorInfo
            if ($txnResult.Errors -eq 0)
            {
                $line | Add-Member -MemberType NoteProperty -Name 'Errors' -Value ""
            }
            else
            {
                $assetsWithErrors++
                $totalErrors += $txnResult.Errors
                $errorMsgs = ($txnResult.AttributeValidatonResults | Foreach ValidationResult) -join "; "
                $line | Add-Member -MemberType NoteProperty -Name 'Errors' -Value $errorMsgs
            }
            if ($i -eq 0)
            {
                $line | Export-Csv -Path $ResultPath -NoTypeInformation
            }
            else
            {
                $line | Export-Csv -Path $ResultPath -Append -NoTypeInformation
            }
        }
        Write-Progress -Activity "Validating Asset Attributes" -Completed 
        Write-Host "Total Assets Validated:"$csv.Length
        Write-Host "Assets with Error(s):"$assetsWithErrors
        Write-Host "Total Errors Detected"$totalErrors
        if ($totalErrors -eq 0)
        {
            Write-Host "`r`nCONGRATULATIONS! Your asset attribute CSV file is ready to import."
        }
        return 
    }
}


function Import-MvDamAssetKeywordFromCsv
{
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true,Position=0)]
        [string]$Path
    )
    PROCESS 
    {
        $Assets = Import-Csv -Path $Path | Select 'System.Id', Keywords

        "Validating Ids"
        $Count = 0
        foreach ($id in ($Assets | select Id))
        {
            if ($id -match '<<[a-z]*>>')
            {
                $Count += 1;
            }
        }

        if ($Count -gt 0)
        {
            throw "$Count issues were detected with the Asset Ids provided. Please resolve this before continuing."
        }

        "Ids are valid"
        "Starting keyword import"

        $err = @()
        $lastErrorCount = 0
        $errorLogPath = ".\UpdateKeywordErrors.csv"
        foreach ($asset in $Assets)
        {
            "AssetId: $asset.Id" 
           if(!$asset.Keywords.Equals(""))
           {
                "Keywords: $asset.Keywords"
                $keywords = $asset.Keywords.Split(",")
                Update-MvDamAssetKeyword -AssetId $asset.Id -Keywords $keywords -ErrorAction Continue -ErrorVariable +err

                if ($err.Count -gt $lastErrorCount)
                {
                    $updateKeywordError = New-Object -TypeName PSObject
                    $updateKeywordError | Add-Member -NotePropertyName "System.Id" -NotePropertyValue $asset.'System.Id'
                    $updateKeywordError | Add-Member -NotePropertyName "Keywords" -NotePropertyValue $asset.Keywords
                    $updateKeywordError | Add-Member -NotePropertyName "Error" -NotePropertyValue $err[$lastErrorCount]

                    $updateKeywordError | Export-Csv -Path $errorLogPath -NoTypeInformation -Append

                    $lastErrorCount += 1
                }
           }
        }
        "Import complete"
    }
}

function Import-MvDamAssetCategoryFromCsv
{
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true,Position=0)]
        [string]$Path,
        [parameter(Mandatory=$false)]
        [string]$ErrorLogPath = ".\ImportAssetCategoryErrorLog.csv" 
    )
    PROCESS 
    {
        $Assets = Import-Csv -Path $Path | Select 'System.Id', Path

        Test-MvDamSystemId -Ids ($Assets | Select -ExpandProperty System.Id)

        $err = @()
        $lastErrorCount = 0
        foreach ($asset in $Assets)
        {
            if(!$asset.Path.Equals(""))
            {
                $asset
                $category = Get-MvDamCategory -CategoryPath $asset.Path -ErrorAction Continue -ErrorVariable +err
                Update-MvDamAssetCategory -AssetId $asset.'System.Id' -CategoryIds $category.Id -ErrorAction Continue -ErrorVariable +err

                if ($err.Count -gt $lastErrorCount)
                {
                    $updateCategoryError = New-Object -TypeName PSObject
                    $updateCategoryError | Add-Member -NotePropertyName "System.Id" -NotePropertyValue $asset.'System.Id'
                    $updateCategoryError | Add-Member -NotePropertyName "Path" -NotePropertyValue $asset.Path
                    $updateCategoryError | Add-Member -NotePropertyName "Error" -NotePropertyValue $err[$lastErrorCount]

                    $updateCategoryError | Export-Csv -Path $ErrorLogPath -NoTypeInformation -Append

                    $lastErrorCount += 1
                }
            }
        }
    }
}

function Import-MvDamAssetStatusFromCsv {
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true,Position=0)]
        [string]$Path,
        [parameter(Mandatory=$false)]
        [string]$ErrorLogPath = ".\ImportAssetStatusErrorLog.csv" 
    )
    PROCESS
    {
        $Assets = Import-CSV -Path $Path | Select 'System.Id', Status
        
        Test-MvDamSystemId -Ids ($Assets | Select -ExpandProperty 'System.Id')
        
        $err = @()
        $lastErrorCount = 0
        foreach ($asset in $Assets)
        {
            if(!$asset.Status.Equals(""))
            {
                Update-MvDamAssetStatus -AssetId $asset.'System.Id' $asset.Status -ErrorAction Continue -ErrorVariable +err
            }
            
                if ($err.Count -gt $lastErrorCount)
                {
                    $updateCategoryError = New-Object -TypeName PSObject
                    $updateCategoryError | Add-Member -NotePropertyName "System.Id" -NotePropertyValue $asset.'System.Id'
                    $updateCategoryError | Add-Member -NotePropertyName "Status" -NotePropertyValue $asset.Status
                    $updateCategoryError | Add-Member -NotePropertyName "Error" -NotePropertyValue $err[$lastErrorCount]

                    $updateCategoryError | Export-Csv -Path $ErrorLogPath -NoTypeInformation -Append

                    $lastErrorCount += 1
                }
        }
    }
}

function Test-MvDamSystemId
{
    [CmdletBinding()]
    param (
        [parameter(Mandatory=$true,Position=0)]
        [string[]]$Ids
    )   

    PROCESS 
    {
        $Count = 0
        foreach ($id in $Ids)
        {
            try {
                [System.Guid]::Parse($id) | Out-Null

            } catch {
                $Count += 1
            }
        }

        if ($Count -gt 0)
        {
            throw "$Count issues were detected with the System.Ids provided. Please resolve this before continuing."
        } 
        else 
        {
            "Validation complete. No issues were detected."
        }
    }
}