Skip to content

Commit

Permalink
dotnet format
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrch committed Oct 6, 2024
1 parent 16bbfd3 commit e040a2e
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ public async Task<CommandResult> Handle(MigratePageTypesCommand request, Cancell
modelFacade.Select<ICmsClass>("ClassIsDocumentType=1", "ClassID")
.OrderBy(x => x.ClassID)
);

ExecReusableSchemaBuilders();

var manualMappings = new Dictionary<string, DataClassInfo>();
foreach (var classMapping in classMappings)
{
var newDt = DataClassInfoProvider.GetDataClassInfo(classMapping.TargetClassName) ?? DataClassInfo.New();
classMapping.PatchTargetDataClass(newDt);

// might not need ClassGUID
// newDt.ClassGUID = GuidHelper.CreateDataClassGuid($"{newDt.ClassName}|{newDt.ClassTableName}");

var cmsClasses = new List<ICmsClass>();
foreach (string sourceClassName in classMapping.SourceClassNames)
{
Expand All @@ -68,7 +68,7 @@ public async Task<CommandResult> Handle(MigratePageTypesCommand request, Cancell

var nfi = string.IsNullOrWhiteSpace(newDt.ClassFormDefinition) ? new FormInfo() : new FormInfo(newDt.ClassFormDefinition);
bool hasPrimaryKey = false;
foreach (var formFieldInfo in nfi.GetFields(true, true, true, true, false))
foreach (var formFieldInfo in nfi.GetFields(true, true, true, true, false))
{
if (formFieldInfo.PrimaryKey)
{
Expand All @@ -86,7 +86,7 @@ public async Task<CommandResult> Handle(MigratePageTypesCommand request, Cancell
{
var prototype = FormHelper.GetBasicFormDefinition(classMapping.PrimaryKey);
nfi.AddFormItem(prototype.GetFormField(classMapping.PrimaryKey));
}
}
}

newDt.ClassFormDefinition = nfi.GetXmlDefinition();
Expand All @@ -109,12 +109,12 @@ public async Task<CommandResult> Handle(MigratePageTypesCommand request, Cancell
// part of reusable schema
continue;
}

var sc = cmsClasses.FirstOrDefault(sc => sc.ClassName.Equals(cmm.SourceClassName, StringComparison.InvariantCultureIgnoreCase))
?? throw new NullReferenceException($"The source class '{cmm.SourceClassName}' does not exist - wrong mapping {classMapping}");

var fi = new FormInfo(sc.ClassFormDefinition);
if(nfi.GetFormField(cmm.TargetFieldName) is {})
if (nfi.GetFormField(cmm.TargetFieldName) is { })
{
}
else
Expand All @@ -137,18 +137,18 @@ public async Task<CommandResult> Handle(MigratePageTypesCommand request, Cancell
nfi = new FormInfo(newDt.ClassFormDefinition);
foreach (string fieldName in classMapping.TargetFieldPatchers.Keys)
{
classMapping.TargetFieldPatchers[fieldName].Invoke(nfi.GetFormField(fieldName));
classMapping.TargetFieldPatchers[fieldName].Invoke(nfi.GetFormField(fieldName));
}

newDt.ClassFormDefinition = nfi.GetXmlDefinition();
}

DataClassInfoProvider.SetDataClassInfo(newDt);
foreach (var gByClass in classMapping.Mappings.GroupBy(x => x.SourceClassName))
{
manualMappings.TryAdd(gByClass.Key, newDt);
}

foreach (string sourceClassName in classMapping.SourceClassNames)
{
var sourceClass = cmsClasses.First(c => c.ClassName.Equals(sourceClassName, StringComparison.InvariantCultureIgnoreCase));
Expand Down Expand Up @@ -182,7 +182,7 @@ public async Task<CommandResult> Handle(MigratePageTypesCommand request, Cancell
{
continue;
}

if (ksClass.ClassInheritsFromClassID is { } classInheritsFromClassId && !primaryKeyMappingContext.HasMapping<ICmsClass>(c => c.ClassID, classInheritsFromClassId))
{
// defer migration to later stage
Expand Down Expand Up @@ -299,10 +299,10 @@ private void ExecReusableSchemaBuilders()
true,
false
);
patcher.PatchFields();
patcher.RemoveCategories();
var fi = new FormInfo(patcher.GetPatched());
return fi.GetFormField(fieldIdentifier.FieldName) switch
{
Expand All @@ -316,7 +316,7 @@ private void ExecReusableSchemaBuilders()
}
}
});

reusableSchemaService.EnsureReusableFieldSchema(reusableSchemaBuilder.SchemaName, reusableSchemaBuilder.SchemaDisplayName, reusableSchemaBuilder.SchemaDescription, fieldInfos.ToArray());
}
}
Expand Down
4 changes: 2 additions & 2 deletions KVA/Migration.Toolkit.Source/Helpers/FormDefinitionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static void MapFormDefinitionFields(ILogger logger, IFieldMigrationServic
target.ClassFormDefinition = new FormInfo().GetXmlDefinition();
}
}
public static void MapFormDefinitionFields(ILogger logger, IFieldMigrationService fieldMigrationService,

public static void MapFormDefinitionFields(ILogger logger, IFieldMigrationService fieldMigrationService,
string sourceClassDefinition, bool? classIsForm, bool classIsDocumentType,
DataClassInfo target, bool isCustomizableSystemClass, bool classIsCustom)
{
Expand Down
38 changes: 22 additions & 16 deletions KVA/Migration.Toolkit.Source/Mappers/ContentItemMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using CMS.Websites.Internal;
using Kentico.Xperience.UMT.Model;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Migration.Toolkit.Common;
using Migration.Toolkit.Common.Abstractions;
Expand Down Expand Up @@ -63,7 +62,7 @@ public class ContentItemMapper(
IAssetFacade assetFacade,
MediaLinkServiceFactory mediaLinkServiceFactory,
ToolkitConfiguration configuration,
ClassMappingProvider classMappingProvider
ClassMappingProvider classMappingProvider
) : UmtMapperBase<CmsTreeMapperSource>
{
private const string CLASS_FIELD_CONTROL_NAME = "controlname";
Expand All @@ -81,7 +80,7 @@ protected override IEnumerable<IUmtModel> MapInternal(CmsTreeMapperSource source
{
targetClassGuid = DataClassInfoProvider.ProviderObject.Get(mapping.TargetClassName)?.ClassGUID ?? throw new InvalidOperationException($"Unable to find target class '{mapping.TargetClassName}'");
}

bool migratedAsContentFolder = sourceNodeClass.ClassName.Equals("cms.folder", StringComparison.InvariantCultureIgnoreCase) && !configuration.UseDeprecatedFolderPageType.GetValueOrDefault(false);

var contentItemGuid = spoiledGuidContext.EnsureNodeGuid(cmsTree.NodeGUID, cmsTree.NodeSiteID, cmsTree.NodeID);
Expand Down Expand Up @@ -272,7 +271,7 @@ protected override IEnumerable<IUmtModel> MapInternal(CmsTreeMapperSource source
{
throw new Exception("Error, unable to find coupled data primary key");
}

var commonFields = UnpackReusableFieldSchemas(fi.GetFields<FormSchemaInfo>()).ToArray();
var targetColumns = commonFields
.Select(cf => ReusableSchemaService.RemoveClassPrefix(sourceNodeClass.ClassName, cf.Name))
Expand All @@ -285,8 +284,8 @@ protected override IEnumerable<IUmtModel> MapInternal(CmsTreeMapperSource source
MapCoupledDataFieldValues(dataModel.CustomProperties,
columnName => coupledDataRow?[columnName],
columnName => coupledDataRow?.ContainsKey(columnName) ?? false,
cmsTree, cmsDocument.DocumentID,
targetColumns, sfi, fi,
cmsTree, cmsDocument.DocumentID,
targetColumns, sfi, fi,
false, sourceNodeClass, sourceSite, mapping
).GetAwaiter().GetResult();

Expand Down Expand Up @@ -466,10 +465,11 @@ private IEnumerable<IUmtModel> MigrateDraft(ICmsVersionHistory checkoutVersion,
);
}

dataModel = new ContentItemDataModel {
ContentItemDataGUID = commonDataModel.ContentItemCommonDataGUID,
ContentItemDataCommonDataGuid = commonDataModel.ContentItemCommonDataGUID,
ContentItemContentTypeName = mapping?.TargetClassName ?? sourceNodeClass.ClassName
dataModel = new ContentItemDataModel
{
ContentItemDataGUID = commonDataModel.ContentItemCommonDataGUID,
ContentItemDataCommonDataGuid = commonDataModel.ContentItemCommonDataGUID,
ContentItemContentTypeName = mapping?.TargetClassName ?? sourceNodeClass.ClassName
};

if (sourceNodeClass.ClassIsCoupledClass)
Expand Down Expand Up @@ -560,7 +560,7 @@ IClassMapping mapping
)
{
Debug.Assert(sourceNodeClass.ClassTableName != null, "sourceNodeClass.ClassTableName != null");

foreach (string targetColumnName in newColumnNames)
{
string targetFieldName = null!;
Expand All @@ -585,8 +585,11 @@ IClassMapping mapping
valueConvertor = sourceValue => sourceValue;
break;
}

default:
break;
}

if (
targetFieldName.Equals("ContentItemDataID", StringComparison.InvariantCultureIgnoreCase) ||
targetFieldName.Equals("ContentItemDataCommonDataID", StringComparison.InvariantCultureIgnoreCase) ||
Expand All @@ -605,7 +608,7 @@ IClassMapping mapping
logger.LogTrace("Skipping '{FieldName}' - is external", targetFieldName);
continue;
}

string sourceFieldName = mapping?.GetSourceFieldName(targetColumnName, sourceNodeClass.ClassName) ?? targetColumnName;
if (!containsSourceValue(sourceFieldName))
{
Expand All @@ -624,7 +627,7 @@ IClassMapping mapping

var field = oldFormInfo.GetFormField(sourceFieldName);
string? controlName = field.Settings[CLASS_FIELD_CONTROL_NAME]?.ToString()?.ToLowerInvariant();

object? sourceValue = getSourceValue(sourceFieldName);
target[targetFieldName] = valueConvertor.Invoke(sourceValue);
var fvmc = new FieldMigrationContext(field.DataType, controlName, targetColumnName, new DocumentSourceObjectContext(cmsTree, sourceNodeClass, site, oldFormInfo, newFormInfo, documentId));
Expand Down Expand Up @@ -670,7 +673,7 @@ IClassMapping mapping
target[targetFieldName] = valueConvertor.Invoke(sourceValue);
}
}
else if(fmb != null)
else if (fmb != null)
{
switch (await fmb.MigrateValue(sourceValue, fvmc))
{
Expand All @@ -684,6 +687,9 @@ IClassMapping mapping
logger.LogError("Error while migrating field '{Field}' value {Value}", targetFieldName, sourceValue);
break;
}

default:
break;
}
}
else
Expand All @@ -695,7 +701,7 @@ IClassMapping mapping
var newField = newFormInfo.GetFormField(targetColumnName);
if (newField == null)
{

var commonFields = UnpackReusableFieldSchemas(newFormInfo.GetFields<FormSchemaInfo>()).ToArray();
newField = commonFields
.FirstOrDefault(cf => ReusableSchemaService.RemoveClassPrefix(mapping?.TargetClassName ?? sourceNodeClass.ClassName, cf.Name).Equals(targetColumnName, StringComparison.InvariantCultureIgnoreCase));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Migration.Toolkit.Common;
using Migration.Toolkit.Common.Builders;

namespace Migration.Toolkit.Source.Providers;
Expand All @@ -18,6 +17,6 @@ public class ClassMappingProvider(IEnumerable<IClassMapping> classMappings)
return current;
});

public IClassMapping? GetMapping(string className) => mappingsByClassName.GetValueOrDefault(className);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.Extensions.Logging;

using Migration.Toolkit.Common;
using Migration.Toolkit.Common.Builders;
using Migration.Toolkit.Common.Helpers;

namespace Migration.Toolkit.Source.Services;
Expand Down Expand Up @@ -84,7 +83,7 @@ public void AddReusableSchemaToDataClass(DataClassInfo dataClassInfo, Guid reusa
formInfo.AddFormItem(new FormSchemaInfo { Name = dataClassInfo.ClassName, Guid = reusableFieldSchemaGuid });
dataClassInfo.ClassFormDefinition = formInfo.GetXmlDefinition();
}

public void AddReusableSchemaToDataClass(DataClassInfo dataClassInfo, string reusableFieldSchemaName)
{
var formInfo = new FormInfo(dataClassInfo.ClassFormDefinition);
Expand Down
Loading

0 comments on commit e040a2e

Please sign in to comment.