diff --git a/KVA/Migration.Tool.Source/Mappers/CmsClassMapper.cs b/KVA/Migration.Tool.Source/Mappers/CmsClassMapper.cs index 87ca1499..3692a248 100644 --- a/KVA/Migration.Tool.Source/Mappers/CmsClassMapper.cs +++ b/KVA/Migration.Tool.Source/Mappers/CmsClassMapper.cs @@ -1,6 +1,5 @@ using System.Diagnostics; using System.Xml; - using CMS.ContentEngine; using CMS.Core; using CMS.DataEngine; @@ -55,6 +54,8 @@ protected override DataClassInfo MapInternal(ICmsClass source, DataClassInfo tar logger.LogDebug("{ClassName} is {@Properties}", source.ClassName, new { isCustomizableSystemClass, classIsCustom, source.ClassResourceID, classResource?.ResourceName }); } + var existingFieldGUIDs = new FormInfo(target.ClassFormDefinition).ItemsList.OfType().ToDictionary(x => x.Name, x => x.Guid); + FormDefinitionHelper.MapFormDefinitionFields(logger, fieldMigrationService, source, target, isCustomizableSystemClass, classIsCustom); target.ClassHasUnmanagedDbSchema = false; @@ -141,7 +142,7 @@ protected override DataClassInfo MapInternal(ICmsClass source, DataClassInfo tar target.ClassType = ClassType.OTHER; target.ClassContentTypeType = null; - target = PatchDataClassInfo(target, out string? oldPrimaryKeyName, out string? documentNameField); + target = PatchDataClassInfo(target, existingFieldGUIDs, out string? oldPrimaryKeyName, out string? documentNameField); break; } @@ -175,7 +176,7 @@ protected override DataClassInfo MapInternal(ICmsClass source, DataClassInfo tar target.ClassType = ClassType.CONTENT_TYPE; target.ClassContentTypeType = ClassContentTypeType.REUSABLE; - target = PatchDataClassInfo(target, out string? oldPrimaryKeyName, out string? documentNameField); + target = PatchDataClassInfo(target, existingFieldGUIDs, out string? oldPrimaryKeyName, out string? documentNameField); break; } @@ -193,7 +194,7 @@ protected override DataClassInfo MapInternal(ICmsClass source, DataClassInfo tar ? ClassContentTypeType.REUSABLE : ClassContentTypeType.WEBSITE; - target = PatchDataClassInfo(target, out string? oldPrimaryKeyName, out string? documentNameField); + target = PatchDataClassInfo(target, existingFieldGUIDs, out string? oldPrimaryKeyName, out string? documentNameField); break; } @@ -204,7 +205,7 @@ protected override DataClassInfo MapInternal(ICmsClass source, DataClassInfo tar return target; } - public static DataClassInfo PatchDataClassInfo(DataClassInfo dataClass, out string? oldPrimaryKeyName, out string? documentNameField) + public static DataClassInfo PatchDataClassInfo(DataClassInfo dataClass, Dictionary existingFieldGUIDs, out string? oldPrimaryKeyName, out string? documentNameField) { oldPrimaryKeyName = null; documentNameField = null; @@ -213,13 +214,22 @@ public static DataClassInfo PatchDataClassInfo(DataClassInfo dataClass, out stri var fi = new FormInfo(dataClass.ClassFormDefinition); string tableName = dataClass.ClassTableName; var contentTypeManager = Service.Resolve(); + contentTypeManager.Initialize(dataClass); + if (!string.IsNullOrWhiteSpace(tableName)) { dataClass.ClassTableName = tableName; } var nfi = new FormInfo(dataClass.ClassFormDefinition); + foreach (var item in nfi.ItemsList.OfType()) + { + if (existingFieldGUIDs.TryGetValue(item.Name, out var existingGUID)) + { + item.Guid = existingGUID; + } + } foreach (var dataDefinitionItem in fi.GetFormElements(true, true) ?? []) { diff --git a/KVA/Migration.Tool.Source/Providers/ClassMappingProvider.cs b/KVA/Migration.Tool.Source/Providers/ClassMappingProvider.cs index 3630e76a..823a5963 100644 --- a/KVA/Migration.Tool.Source/Providers/ClassMappingProvider.cs +++ b/KVA/Migration.Tool.Source/Providers/ClassMappingProvider.cs @@ -147,7 +147,7 @@ private Dictionary MappingsByClassName if (!hasFieldsAlready) { FormDefinitionHelper.MapFormDefinitionFields(logger, fieldMigrationService, nfi.GetXmlDefinition(), false, true, newDt, false, false); - CmsClassMapper.PatchDataClassInfo(newDt, out _, out _); + CmsClassMapper.PatchDataClassInfo(newDt, [], out _, out _); } if (classMapping.TargetFieldPatchers.Count > 0) diff --git a/KVA/Migration.Tool.Source/Services/ReusableSchemaService.cs b/KVA/Migration.Tool.Source/Services/ReusableSchemaService.cs index 5e48c95f..dff3f196 100644 --- a/KVA/Migration.Tool.Source/Services/ReusableSchemaService.cs +++ b/KVA/Migration.Tool.Source/Services/ReusableSchemaService.cs @@ -88,8 +88,11 @@ public void AddReusableSchemaToDataClass(DataClassInfo dataClassInfo, string reu { var formInfo = new FormInfo(dataClassInfo.ClassFormDefinition); var schema = reusableFieldSchemaManager.Get(reusableFieldSchemaName); - formInfo.AddFormItem(new FormSchemaInfo { Name = dataClassInfo.ClassName, Guid = schema.Guid }); - dataClassInfo.ClassFormDefinition = formInfo.GetXmlDefinition(); + if (!formInfo.ItemsList.Any(x => x is FormSchemaInfo fsi && fsi.Guid == schema.Guid)) + { + formInfo.AddFormItem(new FormSchemaInfo { Name = dataClassInfo.ClassName, Guid = schema.Guid }); + dataClassInfo.ClassFormDefinition = formInfo.GetXmlDefinition(); + } } public IEnumerable GetFieldsFromReusableSchema(DataClassInfo dataClassInfo)