Skip to content

Commit

Permalink
Fix properties migration in case of properties null
Browse files Browse the repository at this point in the history
Fix: Fix properties migration in case of properties null
  • Loading branch information
akfakmot committed Nov 12, 2024
1 parent a9cb03a commit 1de614c
Showing 1 changed file with 86 additions and 84 deletions.
170 changes: 86 additions & 84 deletions KVA/Migration.Tool.Source/Services/PageBuilderPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,119 +164,121 @@ private async Task<bool> WalkWidgets(int siteId, List<WidgetConfiguration> widge
private async Task<bool> MigrateProperties(int siteId, JObject properties, List<EditingFormControlModel>? formControlModels, IReadOnlyDictionary<string, IWidgetPropertyMigration> explicitMigrations)
{
bool needsDeferredPatch = false;
foreach ((string key, var value) in properties)
if (properties is not null)
{
logger.LogTrace("Migrating widget property {Name}|{Identifier}", key, value?.ToString());

var editingFcm = formControlModels?.FirstOrDefault(x => x.PropertyName.Equals(key, StringComparison.InvariantCultureIgnoreCase));

IWidgetPropertyMigration? propertyMigration = null;
WidgetPropertyMigrationContext? context = null;
bool customMigrationApplied = false;
if (explicitMigrations.ContainsKey(key))
{
context = new WidgetPropertyMigrationContext(siteId, null);
propertyMigration = explicitMigrations[key];
}
else if (editingFcm is not null)
foreach ((string key, var value) in properties)
{
context = new WidgetPropertyMigrationContext(siteId, editingFcm);
propertyMigration = widgetMigrationService.GetWidgetPropertyMigration(context, key);
}
logger.LogTrace("Migrating widget property {Name}|{Identifier}", key, value?.ToString());

bool allowDefaultMigrations = true;
if (propertyMigration is not null)
{
(var migratedValue, bool ndp, allowDefaultMigrations) = await propertyMigration.MigrateWidgetProperty(key, value, context!);
needsDeferredPatch = ndp || needsDeferredPatch;
properties[key] = migratedValue;
customMigrationApplied = true;
logger.LogTrace("Migration {Migration} applied to {Value}, resulting in {Result}", propertyMigration.GetType().FullName, value?.ToString() ?? "<null>", migratedValue?.ToString() ?? "<null>");
}
var editingFcm = formControlModels?.FirstOrDefault(x => x.PropertyName.Equals(key, StringComparison.InvariantCultureIgnoreCase));

if (allowDefaultMigrations && editingFcm is not null)
{
if (FieldMappingInstance.BuiltInModel.NotSupportedInKxpLegacyMode
.SingleOrDefault(x => x.OldFormComponent == editingFcm.FormComponentIdentifier) is var (oldFormComponent, newFormComponent))
IWidgetPropertyMigration? propertyMigration = null;
WidgetPropertyMigrationContext? context = null;
bool customMigrationApplied = false;
if (explicitMigrations.ContainsKey(key))
{
context = new WidgetPropertyMigrationContext(siteId, null);
propertyMigration = explicitMigrations[key];
}
else if (editingFcm is not null)
{
logger.LogTrace("Editing form component found {FormComponentName} => no longer supported {Replacement}", editingFcm.FormComponentIdentifier, newFormComponent);
context = new WidgetPropertyMigrationContext(siteId, editingFcm);
propertyMigration = widgetMigrationService.GetWidgetPropertyMigration(context, key);
}

bool allowDefaultMigrations = true;
if (propertyMigration is not null)
{
(var migratedValue, bool ndp, allowDefaultMigrations) = await propertyMigration.MigrateWidgetProperty(key, value, context!);
needsDeferredPatch = ndp || needsDeferredPatch;
properties[key] = migratedValue;
customMigrationApplied = true;
logger.LogTrace("Migration {Migration} applied to {Value}, resulting in {Result}", propertyMigration.GetType().FullName, value?.ToString() ?? "<null>", migratedValue?.ToString() ?? "<null>");
}

switch (oldFormComponent)
if (allowDefaultMigrations && editingFcm is not null)
{
if (FieldMappingInstance.BuiltInModel.NotSupportedInKxpLegacyMode
.SingleOrDefault(x => x.OldFormComponent == editingFcm.FormComponentIdentifier) is var (oldFormComponent, newFormComponent))
{
case Kx13FormComponents.Kentico_AttachmentSelector when newFormComponent == FormComponents.AdminAssetSelectorComponent:
logger.LogTrace("Editing form component found {FormComponentName} => no longer supported {Replacement}", editingFcm.FormComponentIdentifier, newFormComponent);

switch (oldFormComponent)
{
if (value?.ToObject<List<AttachmentSelectorItem>>() is { Count: > 0 } items)
case Kx13FormComponents.Kentico_AttachmentSelector when newFormComponent == FormComponents.AdminAssetSelectorComponent:
{
var nv = new List<object>();
foreach (var asi in items)
if (value?.ToObject<List<AttachmentSelectorItem>>() is { Count: > 0 } items)
{
var attachment = modelFacade.SelectWhere<ICmsAttachment>("AttachmentSiteID = @attachmentSiteId AND AttachmentGUID = @attachmentGUID",
new SqlParameter("attachmentSiteID", siteId),
new SqlParameter("attachmentGUID", asi.FileGuid)
)
.FirstOrDefault();
if (attachment != null)
var nv = new List<object>();
foreach (var asi in items)
{
switch (attachmentMigrator.MigrateAttachment(attachment).GetAwaiter().GetResult())
var attachment = modelFacade.SelectWhere<ICmsAttachment>("AttachmentSiteID = @attachmentSiteId AND AttachmentGUID = @attachmentGUID",
new SqlParameter("attachmentSiteID", siteId),
new SqlParameter("attachmentGUID", asi.FileGuid)
)
.FirstOrDefault();
if (attachment != null)
{
case MigrateAttachmentResultMediaFile { Success: true, MediaFileInfo: { } x }:
switch (attachmentMigrator.MigrateAttachment(attachment).GetAwaiter().GetResult())
{
nv.Add(new AssetRelatedItem { Identifier = x.FileGUID, Dimensions = new AssetDimensions { Height = x.FileImageHeight, Width = x.FileImageWidth }, Name = x.FileName, Size = x.FileSize });
break;
}
case MigrateAttachmentResultContentItem { Success: true, ContentItemGuid: { } contentItemGuid }:
{
nv.Add(new ContentItemReference { Identifier = contentItemGuid });
break;
}
default:
{
logger.LogWarning("Attachment '{AttachmentGUID}' failed to migrate", asi.FileGuid);
break;
case MigrateAttachmentResultMediaFile { Success: true, MediaFileInfo: { } x }:
{
nv.Add(new AssetRelatedItem { Identifier = x.FileGUID, Dimensions = new AssetDimensions { Height = x.FileImageHeight, Width = x.FileImageWidth }, Name = x.FileName, Size = x.FileSize });
break;
}
case MigrateAttachmentResultContentItem { Success: true, ContentItemGuid: { } contentItemGuid }:
{
nv.Add(new ContentItemReference { Identifier = contentItemGuid });
break;
}
default:
{
logger.LogWarning("Attachment '{AttachmentGUID}' failed to migrate", asi.FileGuid);
break;
}
}
}
else
{
logger.LogWarning("Attachment '{AttachmentGUID}' not found", asi.FileGuid);
}
}
else
{
logger.LogWarning("Attachment '{AttachmentGUID}' not found", asi.FileGuid);
}

properties[key] = JToken.FromObject(nv);
}

properties[key] = JToken.FromObject(nv);
logger.LogTrace("Value migrated from {Old} model to {New} model", oldFormComponent, newFormComponent);
break;
}

logger.LogTrace("Value migrated from {Old} model to {New} model", oldFormComponent, newFormComponent);
break;
default:
break;
}

default:
break;
}
}
else if (!customMigrationApplied)
{
if (FieldMappingInstance.BuiltInModel.SupportedInKxpLegacyMode.Contains(editingFcm.FormComponentIdentifier))
{
// OK
logger.LogTrace("Editing form component found {FormComponentName} => supported in legacy mode", editingFcm.FormComponentIdentifier);
}
else
else if (!customMigrationApplied)
{
// unknown control, probably custom
logger.LogTrace("Editing form component found {FormComponentName} => custom or inlined component, don't forget to migrate code accordingly", editingFcm.FormComponentIdentifier);
if (FieldMappingInstance.BuiltInModel.SupportedInKxpLegacyMode.Contains(editingFcm.FormComponentIdentifier))
{
// OK
logger.LogTrace("Editing form component found {FormComponentName} => supported in legacy mode", editingFcm.FormComponentIdentifier);
}
else
{
// unknown control, probably custom
logger.LogTrace("Editing form component found {FormComponentName} => custom or inlined component, don't forget to migrate code accordingly", editingFcm.FormComponentIdentifier);
}
}
}


if ("NodeAliasPath".Equals(key, StringComparison.InvariantCultureIgnoreCase))
{
needsDeferredPatch = true;
properties["TreePath"] = value;
properties.Remove(key);
if ("NodeAliasPath".Equals(key, StringComparison.InvariantCultureIgnoreCase))
{
needsDeferredPatch = true;
properties["TreePath"] = value;
properties.Remove(key);
}
}
}
}

return needsDeferredPatch;
}

Expand Down

0 comments on commit 1de614c

Please sign in to comment.