Skip to content

Commit

Permalink
Merge pull request #330 from Kentico/fix/reusable-schema-duplicity
Browse files Browse the repository at this point in the history
Fix/reusable schema duplicity
  • Loading branch information
akfakmot authored Jan 9, 2025
2 parents 5af03f4 + c82037d commit 1ab75e8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
20 changes: 15 additions & 5 deletions KVA/Migration.Tool.Source/Mappers/CmsClassMapper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Diagnostics;
using System.Xml;

using CMS.ContentEngine;
using CMS.Core;
using CMS.DataEngine;
Expand Down Expand Up @@ -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<FormFieldInfo>().ToDictionary(x => x.Name, x => x.Guid);

FormDefinitionHelper.MapFormDefinitionFields(logger, fieldMigrationService, source, target, isCustomizableSystemClass, classIsCustom);

target.ClassHasUnmanagedDbSchema = false;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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<string, Guid> existingFieldGUIDs, out string? oldPrimaryKeyName, out string? documentNameField)
{
oldPrimaryKeyName = null;
documentNameField = null;
Expand All @@ -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<IContentTypeManager>();

contentTypeManager.Initialize(dataClass);

if (!string.IsNullOrWhiteSpace(tableName))
{
dataClass.ClassTableName = tableName;
}

var nfi = new FormInfo(dataClass.ClassFormDefinition);
foreach (var item in nfi.ItemsList.OfType<FormFieldInfo>())
{
if (existingFieldGUIDs.TryGetValue(item.Name, out var existingGUID))
{
item.Guid = existingGUID;
}
}

foreach (var dataDefinitionItem in fi.GetFormElements(true, true) ?? [])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private Dictionary<string, IClassMapping> 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)
Expand Down
7 changes: 5 additions & 2 deletions KVA/Migration.Tool.Source/Services/ReusableSchemaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormFieldInfo> GetFieldsFromReusableSchema(DataClassInfo dataClassInfo)
Expand Down

0 comments on commit 1ab75e8

Please sign in to comment.