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

Feat/migrate pages to content hub #281

Merged
merged 4 commits into from
Nov 8, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,30 @@ public async Task<CommandResult> Handle(MigratePageTypesCommand request, Cancell
bool hasFieldsAlready = true;
foreach (var cmml in classMapping.Mappings.Where(m => m.IsTemplate).ToLookup(x => x.SourceFieldName))
{
var cmm = cmml.FirstOrDefault() ?? throw new InvalidOperationException();
if (fieldInReusableSchemas.ContainsKey(cmm.TargetFieldName))
foreach (var cmm in cmml)
{
// part of reusable schema
continue;
}
if (fieldInReusableSchemas.ContainsKey(cmm.TargetFieldName))
{
// 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 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 { })
{
}
else
{
var src = fi.GetFormField(cmm.SourceFieldName);
src.Name = cmm.TargetFieldName;
nfi.AddFormItem(src);
hasFieldsAlready = false;
var fi = new FormInfo(sc.ClassFormDefinition);
if (nfi.GetFormField(cmm.TargetFieldName) is { })
{
}
else
{
var src = fi.GetFormField(cmm.SourceFieldName);
src.Name = cmm.TargetFieldName;
nfi.AddFormItem(src);
hasFieldsAlready = false;
}
}
//var cmm = cmml.FirstOrDefault() ?? throw new InvalidOperationException();
}

if (!hasFieldsAlready)
Expand Down Expand Up @@ -151,6 +154,11 @@ public async Task<CommandResult> Handle(MigratePageTypesCommand request, Cancell

foreach (string sourceClassName in classMapping.SourceClassNames)
{
if (newDt.ClassContentTypeType is ClassContentTypeType.REUSABLE)
{
continue;
}

var sourceClass = cmsClasses.First(c => c.ClassName.Equals(sourceClassName, StringComparison.InvariantCultureIgnoreCase));
foreach (var cmsClassSite in modelFacade.SelectWhere<ICmsClassSite>("ClassId = @classId", new SqlParameter("classId", sourceClass.ClassID)))
{
Expand Down Expand Up @@ -237,26 +245,29 @@ public async Task<CommandResult> Handle(MigratePageTypesCommand request, Cancell
var kxoDataClass = kxpClassFacade.GetClass(ksClass.ClassGUID);
protocol.FetchedTarget(kxoDataClass);

if (SaveUsingKxoApi(ksClass, kxoDataClass) is { } targetClassId)
if (SaveUsingKxoApi(ksClass, kxoDataClass) is { } targetClass)
{
foreach (var cmsClassSite in modelFacade.SelectWhere<ICmsClassSite>("ClassID = @classId", new SqlParameter("classId", ksClass.ClassID)))
if (targetClass.ClassContentTypeType is ClassContentTypeType.WEBSITE)
{
if (modelFacade.SelectById<ICmsSite>(cmsClassSite.SiteID) is { SiteGUID: var siteGuid })
foreach (var cmsClassSite in modelFacade.SelectWhere<ICmsClassSite>("ClassID = @classId", new SqlParameter("classId", ksClass.ClassID)))
{
if (ChannelInfoProvider.ProviderObject.Get(siteGuid) is { ChannelID: var channelId })
if (modelFacade.SelectById<ICmsSite>(cmsClassSite.SiteID) is { SiteGUID: var siteGuid })
{
var info = new ContentTypeChannelInfo { ContentTypeChannelChannelID = channelId, ContentTypeChannelContentTypeID = targetClassId };
ContentTypeChannelInfoProvider.ProviderObject.Set(info);
if (ChannelInfoProvider.ProviderObject.Get(siteGuid) is { ChannelID: var channelId })
{
var info = new ContentTypeChannelInfo { ContentTypeChannelChannelID = channelId, ContentTypeChannelContentTypeID = targetClass.ClassID };
ContentTypeChannelInfoProvider.ProviderObject.Set(info);
}
else
{
logger.LogWarning("Channel for site with SiteGUID '{SiteGuid}' not found", siteGuid);
}
}
else
{
logger.LogWarning("Channel for site with SiteGUID '{SiteGuid}' not found", siteGuid);
logger.LogWarning("Source site with SiteID '{SiteId}' not found", cmsClassSite.SiteID);
}
}
else
{
logger.LogWarning("Source site with SiteID '{SiteId}' not found", cmsClassSite.SiteID);
}
}
}
}
Expand Down Expand Up @@ -332,7 +343,7 @@ private async Task MigratePageTemplateConfigurations()
}
}

private int? SaveUsingKxoApi(ICmsClass ksClass, DataClassInfo kxoDataClass)
private DataClassInfo? SaveUsingKxoApi(ICmsClass ksClass, DataClassInfo kxoDataClass)
{
var mapped = dataClassMapper.Map(ksClass, kxoDataClass);
protocol.MappedTarget(mapped);
Expand Down Expand Up @@ -360,7 +371,7 @@ private async Task MigratePageTemplateConfigurations()
dataClassInfo.ClassID
);

return dataClassInfo.ClassID;
return dataClassInfo;
}
}
catch (Exception ex)
Expand Down
18 changes: 6 additions & 12 deletions KVA/Migration.Tool.Source/Handlers/MigratePagesCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Concurrent;
using System.Diagnostics;

using CMS.ContentEngine;
using CMS.ContentEngine.Internal;
using CMS.Core;
Expand All @@ -12,12 +11,9 @@
using CMS.Websites.Routing.Internal;
using Kentico.Xperience.UMT.Model;
using Kentico.Xperience.UMT.Services;

using MediatR;

using Microsoft.Data.SqlClient;
using Microsoft.Extensions.Logging;

using Migration.Tool.Common;
using Migration.Tool.Common.Abstractions;
using Migration.Tool.Common.Helpers;
Expand All @@ -30,7 +26,6 @@
using Migration.Tool.Source.Model;
using Migration.Tool.Source.Providers;
using Migration.Tool.Source.Services;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand Down Expand Up @@ -218,14 +213,13 @@ public async Task<CommandResult> Handle(MigratePagesCommand request, Cancellatio
var commonDataInfos = new List<ContentItemCommonDataInfo>();
foreach (var umtModel in results)
{
var result = await importer.ImportAsync(umtModel);
if (result is { Success: false })
{
logger.LogError("Failed to import: {Exception}, {ValidationResults}", result.Exception, JsonConvert.SerializeObject(result.ModelValidationResults));
}

switch (result)
switch (await importer.ImportAsync(umtModel))
{
case { Success: false } result:
{
logger.LogError("Failed to import: {Exception}, {ValidationResults}", result.Exception, JsonConvert.SerializeObject(result.ModelValidationResults));
break;
}
case { Success: true, Imported: ContentItemCommonDataInfo ccid }:
{
commonDataInfos.Add(ccid);
Expand Down
7 changes: 5 additions & 2 deletions KVA/Migration.Tool.Source/Mappers/CmsClassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class CmsClassMapper(
PrimaryKeyMappingContext primaryKeyMappingContext,
IProtocol protocol,
FieldMigrationService fieldMigrationService,
ModelFacade modelFacade
ModelFacade modelFacade,
ToolConfiguration configuration
)
: EntityMapperBase<ICmsClass, DataClassInfo>(logger,
primaryKeyMappingContext, protocol)
Expand Down Expand Up @@ -188,7 +189,9 @@ protected override DataClassInfo MapInternal(ICmsClass source, DataClassInfo tar
}:
{
target.ClassType = ClassType.CONTENT_TYPE;
target.ClassContentTypeType = ClassContentTypeType.WEBSITE;
target.ClassContentTypeType = configuration.ClassNamesConvertToContentHub.Contains(target.ClassName)
? ClassContentTypeType.REUSABLE
: ClassContentTypeType.WEBSITE;

target = PatchDataClassInfo(target, out string? oldPrimaryKeyName, out string? documentNameField);
break;
Expand Down
28 changes: 17 additions & 11 deletions KVA/Migration.Tool.Source/Mappers/ContentItemMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,22 @@ protected override IEnumerable<IUmtModel> MapInternal(CmsTreeMapperSource source
var sourceNodeClass = modelFacade.SelectById<ICmsClass>(cmsTree.NodeClassID) ?? throw new InvalidOperationException($"Fatal: node class is missing, class id '{cmsTree.NodeClassID}'");
var mapping = classMappingProvider.GetMapping(sourceNodeClass.ClassName);
var targetClassGuid = sourceNodeClass.ClassGUID;
DataClassInfo targetClassInfo = null;
if (mapping != null)
{
targetClassGuid = DataClassInfoProvider.ProviderObject.Get(mapping.TargetClassName)?.ClassGUID ?? throw new InvalidOperationException($"Unable to find target class '{mapping.TargetClassName}'");
targetClassInfo = DataClassInfoProvider.ProviderObject.Get(mapping.TargetClassName) ?? throw new InvalidOperationException($"Unable to find target class '{mapping.TargetClassName}'");
targetClassGuid = targetClassInfo.ClassGUID;
}

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

var contentItemGuid = spoiledGuidContext.EnsureNodeGuid(cmsTree.NodeGUID, cmsTree.NodeSiteID, cmsTree.NodeID);
bool isMappedTypeReusable = (targetClassInfo?.ClassContentTypeType is ClassContentTypeType.REUSABLE) || configuration.ClassNamesConvertToContentHub.Contains(sourceNodeClass.ClassName);
yield return new ContentItemModel
{
ContentItemGUID = contentItemGuid,
ContentItemName = safeNodeName,
ContentItemIsReusable = false, // page is not reusable
ContentItemIsReusable = isMappedTypeReusable,
ContentItemIsSecured = cmsTree.IsSecuredNode ?? false,
ContentItemDataClassGuid = migratedAsContentFolder ? null : targetClassGuid,
ContentItemChannelGuid = siteGuid
Expand Down Expand Up @@ -350,16 +353,19 @@ protected override IEnumerable<IUmtModel> MapInternal(CmsTreeMapperSource source
Debug.Assert(cmsTree.NodeLinkedNodeID == null, "cmsTree.NodeLinkedNodeId == null");
Debug.Assert(cmsTree.NodeLinkedNodeSiteID == null, "cmsTree.NodeLinkedNodeSiteId == null");

yield return new WebPageItemModel
if (!isMappedTypeReusable)
{
WebPageItemParentGuid = nodeParentGuid, // NULL => under root
WebPageItemGUID = contentItemGuid,
WebPageItemName = safeNodeName,
WebPageItemTreePath = treePath,
WebPageItemWebsiteChannelGuid = siteGuid,
WebPageItemContentItemGuid = contentItemGuid,
WebPageItemOrder = cmsTree.NodeOrder ?? 0 // 0 is nullish value
};
yield return new WebPageItemModel
{
WebPageItemParentGuid = nodeParentGuid, // NULL => under root
WebPageItemGUID = contentItemGuid,
WebPageItemName = safeNodeName,
WebPageItemTreePath = treePath,
WebPageItemWebsiteChannelGuid = siteGuid,
WebPageItemContentItemGuid = contentItemGuid,
WebPageItemOrder = cmsTree.NodeOrder ?? 0 // 0 is nullish value
};
}
}

private IEnumerable<IUmtModel> MigrateDraft(ICmsVersionHistory checkoutVersion, ICmsTree cmsTree, string sourceFormClassDefinition, string targetFormDefinition, Guid contentItemGuid,
Expand Down
9 changes: 9 additions & 0 deletions Migration.Tool.CLI/Migration.Tool.CLI.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup>
<ActiveDebugProfile>Migration</ActiveDebugProfile>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion Migration.Tool.CLI/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"MigrationProtocolPath": "C:\\Logs\\protocol.txt",
"KxConnectionString": "[TODO]",
"KxCmsDirPath": "[TODO]",
"XbKDirPath": "[TODO]",
"XbKDirPath": "[TODO]",
"XbKApiSettings": {
"ConnectionStrings": {
"CMSConnectionString": "[TODO]"
Expand Down
1 change: 1 addition & 0 deletions Migration.Tool.Common/ConfigurationNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ConfigurationNames
public const string UseDeprecatedFolderPageType = "UseDeprecatedFolderPageType";

public const string ExcludeCodeNames = "ExcludeCodeNames";
public const string ConvertClassesToContentHub = "ConvertClassesToContentHub";
public const string ExplicitPrimaryKeyMapping = "ExplicitPrimaryKeyMapping";

public const string SiteName = "SiteName";
Expand Down
8 changes: 8 additions & 0 deletions Migration.Tool.Common/ToolConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@
[ConfigurationKeyName(ConfigurationNames.CreateReusableFieldSchemaForClasses)]
public string? CreateReusableFieldSchemaForClasses { get; set; }

[ConfigurationKeyName(ConfigurationNames.ConvertClassesToContentHub)]
public string? ConvertClassesToContentHub { get; set; }

public IReadOnlySet<string> ClassNamesCreateReusableSchema => classNamesCreateReusableSchema ??= new HashSet<string>(
(CreateReusableFieldSchemaForClasses?.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) ?? []).Select(x => x.Trim()),
StringComparer.InvariantCultureIgnoreCase
);

public IReadOnlySet<string> ClassNamesConvertToContentHub => classNamesConvertToContentHub ??= new HashSet<string>(
(ConvertClassesToContentHub?.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) ?? []).Select(x => x.Trim()),
StringComparer.InvariantCultureIgnoreCase
);

#region Opt-in features

[ConfigurationKeyName(ConfigurationNames.OptInFeatures)]
Expand All @@ -77,7 +84,7 @@

#region Connection string of target instance

[ConfigurationKeyName(ConfigurationNames.XbKConnectionString)]

Check warning on line 87 in Migration.Tool.Common/ToolConfiguration.cs

View workflow job for this annotation

GitHub Actions / Build and Test

'ConfigurationNames.XbKConnectionString' is obsolete: 'not needed anymore, connection string from Kentico config section is used'
public string XbKConnectionString
{
get => xbKConnectionString!;
Expand All @@ -96,6 +103,7 @@

#region Path to root directory of target instance

private HashSet<string>? classNamesConvertToContentHub;
private HashSet<string>? classNamesCreateReusableSchema;
private string? xbKConnectionString;

Expand Down
61 changes: 61 additions & 0 deletions Migration.Tool.Extensions/ClassMappings/ClassMappingSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,67 @@ namespace Migration.Tool.Extensions.ClassMappings;

public static class ClassMappingSample
{
public static IServiceCollection AddReusableRemodelingSample(this IServiceCollection serviceCollection)
akfakmot marked this conversation as resolved.
Show resolved Hide resolved
{
const string targetClassName = "DancingGoatCore.CoffeeRemodeled";
// declare target class
var m = new MultiClassMapping(targetClassName, target =>
{
target.ClassName = targetClassName;
target.ClassTableName = "DancingGoatCore_CoffeeRemodeled";
target.ClassDisplayName = "Coffee remodeled";
target.ClassType = ClassType.CONTENT_TYPE;
target.ClassContentTypeType = ClassContentTypeType.REUSABLE;
target.ClassWebPageHasUrl = false;
});

// set new primary key
m.BuildField("CoffeeRemodeledID").AsPrimaryKey();

// change fields according to new requirements
const string sourceClassName = "DancingGoatCore.Coffee";
m
.BuildField("FarmRM")
.SetFrom(sourceClassName, "CoffeeFarm", true)
.WithFieldPatch(f => f.SetPropertyValue(FormFieldPropertyEnum.FieldCaption, "Farm RM"));

// field clone sample
m
.BuildField("FarmRM_Clone")
.SetFrom(sourceClassName, "CoffeeFarm", true)
.WithFieldPatch(f => f.SetPropertyValue(FormFieldPropertyEnum.FieldCaption, "Farm RM Clone"));

m
.BuildField("CoffeeCountryRM")
.WithFieldPatch(f => f.Caption = "Country RM")
.SetFrom(sourceClassName, "CoffeeCountry", true);

m
.BuildField("CoffeeVarietyRM")
.SetFrom(sourceClassName, "CoffeeVariety", true)
.WithFieldPatch(f => f.SetPropertyValue(FormFieldPropertyEnum.FieldCaption, "Variety RM"));

m
.BuildField("CoffeeProcessingRM")
.SetFrom(sourceClassName, "CoffeeProcessing", true)
.WithFieldPatch(f => f.SetPropertyValue(FormFieldPropertyEnum.FieldCaption, "Processing RM"));

m
.BuildField("CoffeeAltitudeRM")
.SetFrom(sourceClassName, "CoffeeAltitude", true)
.WithFieldPatch(f => f.SetPropertyValue(FormFieldPropertyEnum.FieldCaption, "Altitude RM"));

m
.BuildField("CoffeeIsDecafRM")
.SetFrom(sourceClassName, "CoffeeIsDecaf", true)
.WithFieldPatch(f => f.SetPropertyValue(FormFieldPropertyEnum.FieldCaption, "IsDecaf RM"));

// register class mapping
serviceCollection.AddSingleton<IClassMapping>(m);

return serviceCollection;
}

public static IServiceCollection AddSimpleRemodelingSample(this IServiceCollection serviceCollection)
{
const string targetClassName = "DancingGoatCore.CoffeeRemodeled";
Expand Down
4 changes: 4 additions & 0 deletions Migration.Tool.Extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ serviceCollection.AddSingleton<IClassMapping>(m);
demonstrated in method `AddReusableSchemaIntegrationSample`, goal is to take single data class and assign reusable schema.


### Convert page type to reusable content item (content hub)

demonstrated in method `AddReusableRemodelingSample`. Please note, that all information unique to page will be lost

## Custom widget property migrations

To create custom widget property migration:
Expand Down
2 changes: 2 additions & 0 deletions Migration.Tool.Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public static IServiceCollection UseCustomizations(this IServiceCollection servi
services.AddTransient<IWidgetPropertyMigration, WidgetPathSelectorMigration>();
services.AddTransient<IWidgetPropertyMigration, WidgetPageSelectorMigration>();


// services.AddClassMergeExample();
// services.AddSimpleRemodelingSample();
// services.AddReusableRemodelingSample();
// services.AddReusableSchemaIntegrationSample();
return services;
}
Expand Down
Loading