Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Media Sets #273

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
page 58213 MSFTTenantMediaSetAPI
{
PageType = API;
ApplicationArea = All;
UsageCategory = Administration;
SourceTable = "Tenant Media Set";
EntityName = 'tenantMediaSet';
EntitySetName = 'tenantMediaSet';
APIPublisher = 'MSFT';
APIGroup = 'moveData';
APIVersion = 'v1.0';
DelayedInsert = true;
Permissions = tabledata "Tenant Media Set" = rmid;

layout
{
area(Content)
{
repeater(TenantMedia)
{
field(id; Rec.ID)
{
Caption = 'id';
}
field(mediaID; Rec."Media Index")
{
Caption = 'Media Index';
}

field(base64ContentTxt; Base64ContentTxt)
{
Caption = 'Base64ContentTxt';
}

field(companyName; Rec."Company Name")
{
Caption = 'Company Name';
TableRelation = Company.Name;
}

}
}
}

trigger OnInsertRecord(BelowxRec: Boolean): Boolean
begin
SetBase64Text();
Rec.Insert();
exit(false);
end;

trigger OnAfterGetRecord()
begin
GetBase64Text();
end;

local procedure GetBase64Text()
var
Base64Convert: Codeunit "Base64 Convert";
ContentInStream: InStream;
ContentOutStream: OutStream;
TempBlob: Codeunit "Temp Blob";
begin
Clear(Base64ContentTxt);
Rec.CalcFields("Media ID");
if not Rec."Media ID".HasValue() then
exit;

TempBlob.CreateOutStream(ContentOutStream);
Rec."Media ID".ExportStream(ContentOutStream);
TempBlob.CreateInStream(ContentInStream);
Base64ContentTxt := Base64Convert.ToBase64(ContentInStream);
end;

local procedure SetBase64Text()
var
Base64Convert: Codeunit "Base64 Convert";
ContentInStream: InStream;
ContentOutStream: OutStream;
TempBlob: Codeunit "Temp Blob";
begin
TempBlob.CreateOutStream(ContentOutStream);
Base64Convert.FromBase64(Base64ContentTxt, ContentOutStream);
TempBlob.CreateInStream(ContentInStream);
Rec."Media ID".ImportStream(ContentInStream, '');
end;

var
Base64ContentTxt: text;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
page 58214 MSFTTenantMediaSetIDsAPI
{
PageType = API;
ApplicationArea = All;
UsageCategory = Administration;
SourceTable = "Tenant Media Set";
EntityName = 'tenantMediaSetIds';
EntitySetName = 'tenantMediaSetIds';
APIPublisher = 'MSFT';
APIGroup = 'moveData';
APIVersion = 'v1.0';
DelayedInsert = true;
Permissions = tabledata "Tenant Media Set" = rmid;

layout
{
area(Content)
{
repeater(TenantMedia)
{
field(id; Rec.ID)
{
Caption = 'id';
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,39 @@ $script:TokenExpirationTime = (Get-Date)

function Copy-TenantMedia
(
[int] $startIndex = 0, # Use this parameters if you want to run the script in parallel. Start the first scripts by specifying e.g. $maxCount 10.000 and second script with $startIndex 10.000 and $maxCount 10.000
[int] $maxCount = 0
[int] $startIndex = 0, # Use this parameters if you want to run the script in parallel. Start the first scripts by specifying e.g. $maxCount 10.000 and second script with $startIndex 10.000 and $maxCount 10.000
[int] $maxCount = 0,
[bool] $transferMediaSets = $false
)
{
# Setup URLs
Write-Host "Connecting to the environments"
$sourceCompanyId = Get-SourceCompanyURL -EnvironmentUlr $script:SourceEnvironmentUlr
$sourceMediaAPIUrl = $script:SourceEnvironmentUlr + '(' + $sourceCompanyId + ')/tenantMedia'
$sourceMediaIDsAPIUrl = $script:SourceEnvironmentUlr + '(' + $sourceCompanyId + ')/tenantMediaIds'
$sourceMediaSetAPIUrl = $script:SourceEnvironmentUlr + '(' + $sourceCompanyId + ')/tenantMediaSet'
$sourceMediaSetIDsAPIUrl = $script:SourceEnvironmentUlr + '(' + $sourceCompanyId + ')/tenantMediaSetIds'
Write-Host "Using company with ID $sourceCompanyId as source"

$targetCompanyId = Get-SourceCompanyURL -EnvironmentUlr $script:TargetEnvironmentUlr
$targetMediaAPIUrl = $script:TargetEnvironmentUlr + '(' + $targetCompanyId + ')/tenantMedia'
$targetMediaIDsAPIUrl = $script:TargetEnvironmentUlr + '(' + $targetCompanyId + ')/tenantMediaIds'
$targetMediaIDsAPIUrl = $script:TargetEnvironmentUlr + '(' + $targetCompanyId + ')/tenantMediaIds'
$targetMediaSetAPIUrl = $script:TargetEnvironmentUlr + '(' + $targetCompanyId + ')/tenantMediaSet'
$targetMediaSetIDsAPIUrl = $script:TargetEnvironmentUlr + '(' + $targetCompanyId + ')/tenantMediaSetIds'
Write-Host "Using company with ID $targetCompanyId as target"

# Get Tenant Media Records to copy
Write-Host "Discovering tenant media records to move"
$sourceTenantMediaIDs = Get-TenantMediaIDs -MediaIDsAPIUrl $sourceMediaIDsAPIUrl
[System.Collections.ArrayList] $itemsToMove = $sourceTenantMediaIDs | Select-Object -Property id | Sort-Object -Property id

$MediaSetitemsToMove = [System.Collections.ArrayList]@()
if ($transferMediaSets)
{
$sourceTenantMediaSetIDs = Get-TenantMediaIDs -MediaIDsAPIUrl $sourceMediaSetIDsAPIUrl
$MediaSetitemsToMove = $sourceTenantMediaSetIDs | Select-Object -Property id | Sort-Object -Property id
}

$targetTenantMediaIDs = Get-TenantMediaIDs -MediaIDsAPIUrl $targetMediaIDsAPIUrl
$ExistingTenantMedia = New-Object System.Collections.Specialized.OrderedDictionary

Expand All @@ -57,6 +69,24 @@ function Copy-TenantMedia
}
}

$ExistingTenantMediaSets = New-Object System.Collections.Specialized.OrderedDictionary
if ($transferMediaSets)
{
$targetTenantMediaSetIDs = Get-TenantMediaIDs -MediaIDsAPIUrl $targetMediaSetIDsAPIUrl
if($targetTenantMediaSetIDs)
{
if($targetTenantMediaSetIDs.value.Count -gt 0)
{
[System.Collections.ArrayList] $targetTenantMediaSetIDs = $targetTenantMediaSetIDs | Select-Object -Property id | Sort-Object -Property id

for ($i = 0; $i -lt $targetTenantMediaSetIDs.Count; $i++)
{
$ExistingTenantMediaSets.Add($targetTenantMediaSetIDs[$i].id,'')
}
}
}
}

$endIndexOfItemsToMove = $itemsToMove.Count

if($maxCount)
Expand Down Expand Up @@ -93,6 +123,39 @@ function Copy-TenantMedia
$response = Invoke-PostMethod -Uri $targetMediaAPIUrl -Token $Token -Body $tenantMediaBody
}
}

if ($transferMediaSets)
{

$endIndexOfItemSetssToMove = $MediaSetitemsToMove.Count

if($maxCount)
{
$endIndexOfItemSetssToMove = $startIndex + $maxCount
}

# Move selected records
for($i = $startIndex; $i -le $endIndexOfItemSetssToMove; $i++)
{
$mediaIDToMove = $MediaSetitemsToMove[$i].id
if (-not $ExistingTenantMediaSets.Contains($MediaSetitemsToMove[$i].id))
{
$sourceURl = $sourceMediaSetAPIUrl + '(' + $mediaIDToMove + ')'
$Token = Get-AADToken
$sourceResponse = Invoke-GetMethod -Uri $sourceURl -Token $Token

$tenantMediaBody = @{
id="$($sourceResponse.id)";
companyName=$($sourceResponse.companyName);
mediaID=$($sourceResponse.mediaID);
base64ContentTxt=$($sourceResponse.base64ContentTxt);
}

Write-Host "Moving Media with ID " $tenantMediaBody.id " File Name " $tenantMediaBody.fileName
$response = Invoke-PostMethod -Uri $targetMediaSetAPIUrl -Token $Token -Body $tenantMediaBody
}
}
}
}

function Get-TenantMediaIDs
Expand Down