diff --git a/KVA/Migration.Tool.Source/Services/PageBuilderPatcher.cs b/KVA/Migration.Tool.Source/Services/PageBuilderPatcher.cs index 5f918e91..868af093 100644 --- a/KVA/Migration.Tool.Source/Services/PageBuilderPatcher.cs +++ b/KVA/Migration.Tool.Source/Services/PageBuilderPatcher.cs @@ -164,119 +164,121 @@ private async Task WalkWidgets(int siteId, List widge private async Task MigrateProperties(int siteId, JObject properties, List? formControlModels, IReadOnlyDictionary 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() ?? "", migratedValue?.ToString() ?? ""); - } + 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() ?? "", migratedValue?.ToString() ?? ""); + } - 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>() is { Count: > 0 } items) + case Kx13FormComponents.Kentico_AttachmentSelector when newFormComponent == FormComponents.AdminAssetSelectorComponent: { - var nv = new List(); - foreach (var asi in items) + if (value?.ToObject>() is { Count: > 0 } items) { - var attachment = modelFacade.SelectWhere("AttachmentSiteID = @attachmentSiteId AND AttachmentGUID = @attachmentGUID", - new SqlParameter("attachmentSiteID", siteId), - new SqlParameter("attachmentGUID", asi.FileGuid) - ) - .FirstOrDefault(); - if (attachment != null) + var nv = new List(); + foreach (var asi in items) { - switch (attachmentMigrator.MigrateAttachment(attachment).GetAwaiter().GetResult()) + var attachment = modelFacade.SelectWhere("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; }