Skip to content

Commit

Permalink
Merge pull request #298 from Kentico/refactor/remove-kxp-project-3
Browse files Browse the repository at this point in the history
Refactor/remove kxp project 3
  • Loading branch information
akfakmot authored Nov 19, 2024
2 parents 1b23515 + 304046a commit 7548248
Show file tree
Hide file tree
Showing 181 changed files with 856 additions and 7,985 deletions.
43 changes: 12 additions & 31 deletions KVA/Migration.Tool.Source/Handlers/MigrateFormsCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,35 @@

using CMS.Base;
using CMS.DataEngine;

using CMS.OnlineForms;
using MediatR;

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

using Migration.Tool.Common;
using Migration.Tool.Common.Abstractions;
using Migration.Tool.Common.MigrationProtocol;
using Migration.Tool.Common.Services.BulkCopy;
using Migration.Tool.KXP.Api;
using Migration.Tool.KXP.Context;
using Migration.Tool.KXP.Models;
using Migration.Tool.Source.Contexts;
using Migration.Tool.Source.Model;

namespace Migration.Tool.Source.Handlers;

public class MigrateFormsCommandHandler(
ILogger<MigrateFormsCommandHandler> logger,
IDbContextFactory<KxpContext> kxpContextFactory,
IEntityMapper<ICmsClass, DataClassInfo> dataClassMapper,
IEntityMapper<ICmsForm, CmsForm> cmsFormMapper,
IEntityMapper<ICmsForm, BizFormInfo> cmsFormMapper,
KxpClassFacade kxpClassFacade,
BulkDataCopyService bulkDataCopyService,
PrimaryKeyMappingContext primaryKeyMappingContext,
IProtocol protocol,
ModelFacade modelFacade,
ToolConfiguration configuration
)
: IRequestHandler<MigrateFormsCommand, CommandResult>, IDisposable
: IRequestHandler<MigrateFormsCommand, CommandResult>
{
private KxpContext kxpContext = kxpContextFactory.CreateDbContext();

public void Dispose() => kxpContext.Dispose();

public async Task<CommandResult> Handle(MigrateFormsCommand request, CancellationToken cancellationToken)
{
var cmsClassForms = modelFacade.Select<ICmsClass>("ClassIsForm = 1", "ClassID");
Expand All @@ -63,7 +55,7 @@ public async Task<CommandResult> Handle(MigrateFormsCommand request, Cancellatio
{
protocol.FetchedSource(ksCmsForm);

var kxoCmsForm = kxpContext.CmsForms.FirstOrDefault(f => f.FormGuid == ksCmsForm.FormGUID);
var kxoCmsForm = BizFormInfo.Provider.Get(ksCmsForm.FormGUID);

protocol.FetchedTarget(kxoCmsForm);

Expand All @@ -75,33 +67,22 @@ public async Task<CommandResult> Handle(MigrateFormsCommand request, Cancellatio
(var cmsForm, bool newInstance) = result;
ArgumentNullException.ThrowIfNull(cmsForm, nameof(cmsForm));


try
{
if (newInstance)
{
kxpContext.CmsForms.Add(cmsForm);
}
else
{
kxpContext.CmsForms.Update(cmsForm);
}

await kxpContext.SaveChangesAsync(cancellationToken);
BizFormInfo.Provider.Set(cmsForm!);
logger.LogEntitySetAction(newInstance, cmsForm);

primaryKeyMappingContext.SetMapping<CmsForm>(
r => r.FormId,
primaryKeyMappingContext.SetMapping<BizFormInfo>(
r => r.FormID,
ksClass.ClassID,
cmsForm.FormId
cmsForm.FormID
);
}
catch (Exception ex)
{
await kxpContext.DisposeAsync(); // reset context errors
kxpContext = await kxpContextFactory.CreateDbContextAsync(cancellationToken);

protocol.Append(HandbookReferences
.ErrorCreatingTargetInstance<CmsForm>(ex)
.ErrorCreatingTargetInstance<BizFormInfo>(ex)
.NeedsManualAction()
.WithIdentityPrint(cmsForm)
);
Expand Down Expand Up @@ -168,8 +149,8 @@ private bool MapAndSaveUsingKxoApi(ICmsClass ksClass, DataClassInfo kxoDataClass
protocol.Success(ksClass, dataClassInfo, mapped);
logger.LogEntitySetAction(newInstance, dataClassInfo);

primaryKeyMappingContext.SetMapping<CmsClass>(
r => r.ClassId,
primaryKeyMappingContext.SetMapping<DataClassInfo>(
r => r.ClassID,
ksClass.ClassID,
dataClassInfo.ClassID
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Migration.Tool.Common.MigrationProtocol;
using Migration.Tool.KXP.Api;
using Migration.Tool.KXP.Api.Services.CmsClass;
using Migration.Tool.KXP.Models;
using Migration.Tool.Source.Contexts;
using Migration.Tool.Source.Helpers;
using Migration.Tool.Source.Mappers;
Expand Down Expand Up @@ -42,7 +41,7 @@ IEnumerable<IReusableSchemaBuilder> reusableSchemaBuilders

public async Task<CommandResult> Handle(MigratePageTypesCommand request, CancellationToken cancellationToken)
{
var entityConfiguration = toolConfiguration.EntityConfigurations.GetEntityConfiguration<CmsClass>();
var entityConfiguration = toolConfiguration.EntityConfigurations.GetEntityConfiguration<DataClassInfo>();

using var ksClasses = EnumerableHelper.CreateDeferrableItemWrapper(
modelFacade.Select<ICmsClass>("ClassIsDocumentType=1", "ClassID")
Expand Down Expand Up @@ -202,7 +201,7 @@ public async Task<CommandResult> Handle(MigratePageTypesCommand request, Cancell
{
logger.LogErrorMissingDependency(ksClass, nameof(ksClass.ClassInheritsFromClassID), ksClass.ClassInheritsFromClassID, typeof(DataClassInfo));
protocol.Append(HandbookReferences
.MissingRequiredDependency<CmsClass>(nameof(CmsClass.ClassId), classInheritsFromClassId)
.MissingRequiredDependency<DataClassInfo>(nameof(DataClassInfo.ClassID), classInheritsFromClassId)
.NeedsManualAction()
);
}
Expand Down Expand Up @@ -365,8 +364,8 @@ private async Task MigratePageTemplateConfigurations()
protocol.Success(ksClass, dataClassInfo, mapped);
logger.LogEntitySetAction(newInstance, dataClassInfo);

primaryKeyMappingContext.SetMapping<CmsClass>(
r => r.ClassId,
primaryKeyMappingContext.SetMapping<DataClassInfo>(
r => r.ClassID,
ksClass.ClassID,
dataClassInfo.ClassID
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using Migration.Tool.Common.Helpers;
using Migration.Tool.Common.MigrationProtocol;
using Migration.Tool.Common.Model;
using Migration.Tool.KXP.Models;
using Migration.Tool.Source.Contexts;
using Migration.Tool.Source.Helpers;
using Migration.Tool.Source.Mappers;
Expand Down Expand Up @@ -53,7 +52,7 @@ ClassMappingProvider classMappingProvider

public async Task<CommandResult> Handle(MigratePagesCommand request, CancellationToken cancellationToken)
{
var classEntityConfiguration = toolConfiguration.EntityConfigurations.GetEntityConfiguration<CmsClass>();
var classEntityConfiguration = toolConfiguration.EntityConfigurations.GetEntityConfiguration<DataClassInfo>();

var cultureCodeToLanguageGuid = modelFacade.SelectAll<ICmsCulture>()
.ToDictionary(c => c.CultureCode, c => c.CultureGUID, StringComparer.InvariantCultureIgnoreCase);
Expand Down
33 changes: 17 additions & 16 deletions KVA/Migration.Tool.Source/Helpers/Printer.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using CMS.ContactManagement;
using CMS.DataEngine;
using CMS.DataProtection;
using CMS.FormEngine;
using CMS.Globalization;
using CMS.MediaLibrary;
using CMS.Membership;
using CMS.Modules;
using CMS.OnlineForms;
using CMS.Websites;
using Migration.Tool.Common.Helpers;
using Migration.Tool.Common.Services;
using Migration.Tool.KXP.Models;
using Migration.Tool.Source.Model;

namespace Migration.Tool.Source.Helpers;
Expand All @@ -20,15 +22,15 @@ public static string PrintKxpModelInfo<T>(T model)

return model switch
{
MediaLibrary mediaLibrary => $"{currentTypeName}: {nameof(mediaLibrary.LibraryGuid)}={mediaLibrary.LibraryGuid}",
MediaFile mediaFile => $"{currentTypeName}: {nameof(mediaFile.FileGuid)}={mediaFile.FileGuid}",
CmsRole role => $"{currentTypeName}: {nameof(role.RoleGuid)}={role.RoleGuid}, {nameof(role.RoleName)}={role.RoleName}",
CmsUser user => $"{currentTypeName}: {nameof(user.UserGuid)}={user.UserGuid}, {nameof(user.UserName)}={user.UserName}",
CmsResource resource => $"{currentTypeName}: {nameof(resource.ResourceGuid)}={resource.ResourceGuid}, {nameof(resource.ResourceName)}={resource.ResourceName}",
CmsSettingsCategory settingsCategory => $"{currentTypeName}: {nameof(settingsCategory.CategoryName)}={settingsCategory.CategoryName}",
CmsSettingsKey settingsKey => $"{currentTypeName}: {nameof(settingsKey.KeyGuid)}={settingsKey.KeyGuid}, {nameof(settingsKey.KeyName)}={settingsKey.KeyName}",
CmsForm form => $"{currentTypeName}: {nameof(form.FormGuid)}={form.FormGuid}, {nameof(form.FormName)}={form.FormName}",
OmContactGroup omContactGroup => $"{currentTypeName}: {nameof(omContactGroup.ContactGroupGuid)}={omContactGroup.ContactGroupGuid}, {nameof(omContactGroup.ContactGroupName)}={omContactGroup.ContactGroupName}",
MediaLibraryInfo mediaLibrary => $"{currentTypeName}: {nameof(mediaLibrary.LibraryGUID)}={mediaLibrary.LibraryGUID}",
MediaFileInfo mediaFile => $"{currentTypeName}: {nameof(mediaFile.FileGUID)}={mediaFile.FileGUID}",
RoleInfo role => $"{currentTypeName}: {nameof(role.RoleGUID)}={role.RoleGUID}, {nameof(role.RoleName)}={role.RoleName}",
UserInfo user => $"{currentTypeName}: {nameof(user.UserGUID)}={user.UserGUID}, {nameof(user.UserName)}={user.UserName}",
ResourceInfo resource => $"{currentTypeName}: {nameof(resource.ResourceGUID)}={resource.ResourceGUID}, {nameof(resource.ResourceName)}={resource.ResourceName}",
SettingsCategoryInfo settingsCategory => $"{currentTypeName}: {nameof(settingsCategory.CategoryName)}={settingsCategory.CategoryName}",
SettingsKeyInfo settingsKey => $"{currentTypeName}: {nameof(settingsKey.KeyGUID)}={settingsKey.KeyGUID}, {nameof(settingsKey.KeyName)}={settingsKey.KeyName}",
BizFormInfo form => $"{currentTypeName}: {nameof(form.FormGUID)}={form.FormGUID}, {nameof(form.FormName)}={form.FormName}",
ContactGroupInfo omContactGroup => $"{currentTypeName}: {nameof(omContactGroup.ContactGroupGUID)}={omContactGroup.ContactGroupGUID}, {nameof(omContactGroup.ContactGroupName)}={omContactGroup.ContactGroupName}",

null => $"{currentTypeName}: <null>",
_ => $"TODO: {typeof(T).FullName}"
Expand Down Expand Up @@ -63,12 +65,11 @@ string FormatModel(string inner) => printType
MemberInfo item => FormatModel($"ID={item.MemberID}, Guid={item.MemberGuid} Name={item.MemberName}"),
WebPageFormerUrlPathInfo item => FormatModel($"ID={item.WebPageFormerUrlPathID}, Guid=N/A Name={item.WebPageFormerUrlPath}"),

CmsForm item => FormatModel($"ID={item.FormId}, GUID={item.FormGuid}, Name={item.FormName}"),
CmsUser item => FormatModel($"ID={item.UserId}, GUID={item.UserGuid}, Name={item.UserName}"),
CmsConsent item => FormatModel($"ID={item.ConsentId}, GUID={item.ConsentGuid}, Name={item.ConsentName}"),
CmsConsentArchive item => FormatModel($"ID={item.ConsentArchiveId}, GUID={item.ConsentArchiveGuid}"),
CmsConsentAgreement item => FormatModel($"ID={item.ConsentAgreementId}, GUID={item.ConsentAgreementGuid}"),
CmsSettingsKey item => FormatModel($"ID={item.KeyId}, GUID={item.KeyGuid}, Name={item.KeyName}"),
BizFormInfo item => FormatModel($"ID={item.FormID}, GUID={item.FormGUID}, Name={item.FormName}"),
ConsentInfo item => FormatModel($"ID={item.ConsentID}, GUID={item.ConsentGuid}, Name={item.ConsentName}"),
ConsentArchiveInfo item => FormatModel($"ID={item.ConsentArchiveID}, GUID={item.ConsentArchiveGuid}"),
ConsentAgreementInfo item => FormatModel($"ID={item.ConsentAgreementID}, GUID={item.ConsentAgreementGuid}"),
SettingsKeyInfo item => FormatModel($"ID={item.KeyID}, GUID={item.KeyGUID}, Name={item.KeyName}"),

CmsPageTemplateConfigurationK11 item => FormatModel($"Item not exists in K11 {item}"),
CmsPageTemplateConfigurationK12 item => FormatModel($"ID={item.PageTemplateConfigurationID}, GUID={item.PageTemplateConfigurationGUID}, Name={item.PageTemplateConfigurationName}, SiteId={item.PageTemplateConfigurationSiteID}"),
Expand Down
2 changes: 0 additions & 2 deletions KVA/Migration.Tool.Source/KsCoreDiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using Migration.Tool.Common.Services;
using Migration.Tool.Common.Services.BulkCopy;
using Migration.Tool.Common.Services.Ipc;
using Migration.Tool.KXP.Models;
using Migration.Tool.Source.Auxiliary;
using Migration.Tool.Source.Behaviors;
using Migration.Tool.Source.Contexts;
Expand Down Expand Up @@ -94,7 +93,6 @@ public static IServiceCollection UseKsToolCore(this IServiceCollection services,
services.AddTransient<IEntityMapper<CmsAttachmentMapperSource, MediaFileInfo>, CmsAttachmentMapper>();
services.AddTransient<IEntityMapper<ICmsClass, DataClassInfo>, CmsClassMapper>();
services.AddTransient<IEntityMapper<ICmsForm, BizFormInfo>, CmsFormMapper>();
services.AddTransient<IEntityMapper<ICmsForm, CmsForm>, CmsFormMapperEf>();
services.AddTransient<IEntityMapper<ICmsResource, ResourceInfo>, ResourceMapper>();
services.AddTransient<IEntityMapper<AlternativeFormMapperSource, AlternativeFormInfo>, AlternativeFormMapper>();
services.AddTransient<IEntityMapper<MemberInfoMapperSource, MemberInfo>, MemberInfoMapper>();
Expand Down
67 changes: 2 additions & 65 deletions KVA/Migration.Tool.Source/Mappers/CmsFormMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Migration.Tool.Common;
using Migration.Tool.Common.Abstractions;
using Migration.Tool.Common.MigrationProtocol;
using Migration.Tool.KXP.Models;
using Migration.Tool.Source.Contexts;
using Migration.Tool.Source.Model;

Expand Down Expand Up @@ -35,6 +34,8 @@ protected override BizFormInfo MapInternal(ICmsForm source, BizFormInfo target,
target.FormAccess = source.FormAccess.AsEnum<FormAccessEnum>();
target.FormSubmitButtonImage = source.FormSubmitButtonImage;
target.FormLastModified = source.FormLastModified;
target.FormGUID = source.FormGUID;

switch (source)
{
case CmsFormK11 s:
Expand Down Expand Up @@ -67,67 +68,3 @@ protected override BizFormInfo MapInternal(ICmsForm source, BizFormInfo target,
return target;
}
}

public class CmsFormMapperEf : EntityMapperBase<ICmsForm, CmsForm>
{
public CmsFormMapperEf(ILogger<CmsFormMapperEf> logger, PrimaryKeyMappingContext pkContext, IProtocol protocol) : base(logger, pkContext, protocol)
{
}

protected override CmsForm? CreateNewInstance(ICmsForm source, MappingHelper mappingHelper, AddFailure addFailure) => new();

protected override CmsForm MapInternal(ICmsForm source, CmsForm target, bool newInstance, MappingHelper mappingHelper, AddFailure addFailure)
{
target.FormDisplayName = source.FormDisplayName;
target.FormName = source.FormName;
// target.FormSendToEmail = source.FormSendToEmail;
// target.FormSendFromEmail = source.FormSendFromEmail;
// target.FormEmailSubject = source.FormEmailSubject;
// target.FormEmailTemplate = source.FormEmailTemplate;
// target.FormEmailAttachUploadedDocs = source.FormEmailAttachUploadedDocs;
target.FormItems = source.FormItems;
target.FormReportFields = source.FormReportFields;
target.FormSubmitButtonText = source.FormSubmitButtonText;
// target.FormConfirmationEmailField = source.FormConfirmationEmailField;
// target.FormConfirmationTemplate = source.FormConfirmationTemplate;
// target.FormConfirmationSendFromEmail = source.FormConfirmationSendFromEmail;
// target.FormConfirmationEmailSubject = source.FormConfirmationEmailSubject;
target.FormAccess = source.FormAccess;
target.FormSubmitButtonImage = source.FormSubmitButtonImage;
target.FormGuid = source.FormGUID;
target.FormLastModified = source.FormLastModified;
switch (source)
{
case CmsFormK11 s:
{
target.FormLogActivity = s.FormLogActivity.UseKenticoDefault();
break;
}
case CmsFormK12 s:
{
target.FormLogActivity = s.FormLogActivity.UseKenticoDefault();
target.FormBuilderLayout = s.FormBuilderLayout;
break;
}
case CmsFormK13 s:
{
target.FormLogActivity = s.FormLogActivity;
target.FormBuilderLayout = s.FormBuilderLayout;
break;
}

default:
break;
}

// TODO tk: 2022-05-20 new deduce: target.FormAfterSubmitMode = source.FormAfterSubmitMode;
// TODO tk: 2022-05-20 new deduce: target.FormAfterSubmitRelatedValue = source.FormAfterSubmitRelatedValue;

if (mappingHelper.TranslateRequiredId<ICmsClass>(c => c.ClassID, source.FormClassID, out int classId))
{
target.FormClassId = classId;
}

return target;
}
}
1 change: 0 additions & 1 deletion KVA/Migration.Tool.Source/Migration.Tool.Source.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<ItemGroup>
<ProjectReference Include="..\..\Migration.Tool.Common\Migration.Tool.Common.csproj" />
<ProjectReference Include="..\..\Migration.Tool.KXP.Api\Migration.Tool.KXP.Api.csproj" />
<ProjectReference Include="..\..\Migration.Tool.KXP\Migration.Tool.KXP.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit 7548248

Please sign in to comment.