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

Fix/reusable schema duplicity #330

Merged
merged 2 commits into from
Jan 9, 2025
Merged
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
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
Loading