From da96e6ca43a5fbdf68be23dec1cf72f1c8b69b89 Mon Sep 17 00:00:00 2001 From: akfakmot Date: Tue, 12 Nov 2024 17:43:45 +0100 Subject: [PATCH] Remove Migration.Tool.KXP dependency from MigrateContactManagementCommandHandler Refactor: Remove Migration.Tool.KXP dependency from MigrateContactManagementCommandHandler --- .../MigrateContactManagementCommandHandler.cs | 143 +++++++++--------- 1 file changed, 70 insertions(+), 73 deletions(-) diff --git a/Migration.Tool.Core.K11/Handlers/MigrateContactManagementCommandHandler.cs b/Migration.Tool.Core.K11/Handlers/MigrateContactManagementCommandHandler.cs index b2c864a5..54d5cdf0 100644 --- a/Migration.Tool.Core.K11/Handlers/MigrateContactManagementCommandHandler.cs +++ b/Migration.Tool.Core.K11/Handlers/MigrateContactManagementCommandHandler.cs @@ -1,10 +1,11 @@ using CMS.Activities; using CMS.ContactManagement; using CMS.ContentEngine; - +using CMS.Globalization; +using CMS.Membership; +using CMS.Websites; using MediatR; -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Migration.Tool.Common; @@ -17,14 +18,11 @@ using Migration.Tool.Core.K11.Services; using Migration.Tool.K11.Models; using Migration.Tool.KXP.Api; -using Migration.Tool.KXP.Context; -using Migration.Tool.KXP.Models; namespace Migration.Tool.Core.K11.Handlers; public class MigrateContactManagementCommandHandler( ILogger logger, - IDbContextFactory kxpContextFactory, BulkDataCopyService bulkDataCopyService, ToolConfiguration toolConfiguration, PrimaryKeyMappingContext primaryKeyMappingContext, @@ -33,12 +31,8 @@ public class MigrateContactManagementCommandHandler( KxpClassFacade kxpClassFacade, ISpoiledGuidContext spoiledGuidContext, IProtocol protocol) - : IRequestHandler, IDisposable + : IRequestHandler { - private readonly KxpContext kxpContext = kxpContextFactory.CreateDbContext(); - - public void Dispose() => kxpContext.Dispose(); - public Task Handle(MigrateContactManagementCommand request, CancellationToken cancellationToken) { countryMigrator.MigrateCountriesAndStates(); @@ -58,39 +52,41 @@ public Task Handle(MigrateContactManagementCommand request, Cance #region "Migrate contacts" + private readonly ContactInfo kxpContactInfo; // used just for nameof(*) purposes + private CommandResult? MigrateContacts() { var requiredColumnsForContactMigration = new Dictionary { - { nameof(Tool.K11.Models.OmContact.ContactId), nameof(KXP.Models.OmContact.ContactId) }, - { nameof(Tool.K11.Models.OmContact.ContactFirstName), nameof(KXP.Models.OmContact.ContactFirstName) }, - { nameof(Tool.K11.Models.OmContact.ContactMiddleName), nameof(KXP.Models.OmContact.ContactMiddleName) }, - { nameof(Tool.K11.Models.OmContact.ContactLastName), nameof(KXP.Models.OmContact.ContactLastName) }, - { nameof(Tool.K11.Models.OmContact.ContactJobTitle), nameof(KXP.Models.OmContact.ContactJobTitle) }, - { nameof(Tool.K11.Models.OmContact.ContactAddress1), nameof(KXP.Models.OmContact.ContactAddress1) }, - { nameof(Tool.K11.Models.OmContact.ContactCity), nameof(KXP.Models.OmContact.ContactCity) }, - { nameof(Tool.K11.Models.OmContact.ContactZip), nameof(KXP.Models.OmContact.ContactZip) }, - { nameof(Tool.K11.Models.OmContact.ContactStateId), nameof(KXP.Models.OmContact.ContactStateId) }, - { nameof(Tool.K11.Models.OmContact.ContactCountryId), nameof(KXP.Models.OmContact.ContactCountryId) }, - { nameof(Tool.K11.Models.OmContact.ContactMobilePhone), nameof(KXP.Models.OmContact.ContactMobilePhone) }, - { nameof(Tool.K11.Models.OmContact.ContactBusinessPhone), nameof(KXP.Models.OmContact.ContactBusinessPhone) }, - { nameof(Tool.K11.Models.OmContact.ContactEmail), nameof(KXP.Models.OmContact.ContactEmail) }, + { nameof(OmContact.ContactId), nameof(kxpContactInfo.ContactID) }, + { nameof(OmContact.ContactFirstName), nameof(kxpContactInfo.ContactFirstName) }, + { nameof(OmContact.ContactMiddleName), nameof(kxpContactInfo.ContactMiddleName) }, + { nameof(OmContact.ContactLastName), nameof(kxpContactInfo.ContactLastName) }, + { nameof(OmContact.ContactJobTitle), nameof(kxpContactInfo.ContactJobTitle) }, + { nameof(OmContact.ContactAddress1), nameof(kxpContactInfo.ContactAddress1) }, + { nameof(OmContact.ContactCity), nameof(kxpContactInfo.ContactCity) }, + { nameof(OmContact.ContactZip), nameof(kxpContactInfo.ContactZIP) }, + { nameof(OmContact.ContactStateId), nameof(kxpContactInfo.ContactStateID) }, + { nameof(OmContact.ContactCountryId), nameof(kxpContactInfo.ContactCountryID) }, + { nameof(OmContact.ContactMobilePhone), nameof(kxpContactInfo.ContactMobilePhone) }, + { nameof(OmContact.ContactBusinessPhone), nameof(kxpContactInfo.ContactBusinessPhone) }, + { nameof(OmContact.ContactEmail), nameof(kxpContactInfo.ContactEmail) }, // No support 2022-07-07 { nameof(OmContact.ContactBirthday), nameof(KXO.Models.OmContact.ContactBirthday) }, - { nameof(Tool.K11.Models.OmContact.ContactGender), nameof(KXP.Models.OmContact.ContactGender) }, + { nameof(OmContact.ContactGender), nameof(kxpContactInfo.ContactGender) }, // { nameof(OmContact.ContactStatusId), nameof(KXO.Models.OmContact.ContactStatusId) }, // No support 2022-07-07 but needs to be mapped because of constraint - { nameof(Tool.K11.Models.OmContact.ContactNotes), nameof(KXP.Models.OmContact.ContactNotes) }, - { nameof(Tool.K11.Models.OmContact.ContactOwnerUserId), nameof(KXP.Models.OmContact.ContactOwnerUserId) }, + { nameof(OmContact.ContactNotes), nameof(kxpContactInfo.ContactNotes) }, + { nameof(OmContact.ContactOwnerUserId), nameof(kxpContactInfo.ContactOwnerUserID) }, // No support 2022-07-07 { nameof(OmContact.ContactMonitored), nameof(KXO.Models.OmContact.ContactMonitored) }, - { nameof(Tool.K11.Models.OmContact.ContactGuid), nameof(KXP.Models.OmContact.ContactGuid) }, - { nameof(Tool.K11.Models.OmContact.ContactLastModified), nameof(KXP.Models.OmContact.ContactLastModified) }, - { nameof(Tool.K11.Models.OmContact.ContactCreated), nameof(KXP.Models.OmContact.ContactCreated) }, + { nameof(OmContact.ContactGuid), nameof(kxpContactInfo.ContactGUID) }, + { nameof(OmContact.ContactLastModified), nameof(kxpContactInfo.ContactLastModified) }, + { nameof(OmContact.ContactCreated), nameof(kxpContactInfo.ContactCreated) }, // No support 2022-07-07 { nameof(OmContact.ContactBounces), nameof(KXO.Models.OmContact.ContactBounces) }, - { nameof(Tool.K11.Models.OmContact.ContactCampaign), nameof(KXP.Models.OmContact.ContactCampaign) }, + { nameof(OmContact.ContactCampaign), nameof(kxpContactInfo.ContactCampaign) }, // No support 2022-07-07 { nameof(OmContact.ContactSalesForceLeadId), nameof(KXO.Models.OmContact.ContactSalesForceLeadId) }, // No support 2022-07-07 { nameof(OmContact.ContactSalesForceLeadReplicationDisabled), nameof(KXO.Models.OmContact.ContactSalesForceLeadReplicationDisabled) }, // No support 2022-07-07 { nameof(OmContact.ContactSalesForceLeadReplicationDateTime), nameof(KXO.Models.OmContact.ContactSalesForceLeadReplicationDateTime) }, // No support 2022-07-07 { nameof(OmContact.ContactSalesForceLeadReplicationSuspensionDateTime), nameof(KXO.Models.OmContact.ContactSalesForceLeadReplicationSuspensionDateTime) }, - { nameof(Tool.K11.Models.OmContact.ContactCompanyName), nameof(KXP.Models.OmContact.ContactCompanyName) } + { nameof(OmContact.ContactCompanyName), nameof(kxpContactInfo.ContactCompanyName) } // No support 2022-07-07 { nameof(OmContact.ContactSalesForceLeadReplicationRequired), nameof(KXO.Models.OmContact.ContactSalesForceLeadReplicationRequired) }, }; @@ -119,9 +115,9 @@ public Task Handle(MigrateContactManagementCommand request, Cance } } - primaryKeyMappingContext.PreloadDependencies(u => u.UserId); - primaryKeyMappingContext.PreloadDependencies(u => u.StateId); - primaryKeyMappingContext.PreloadDependencies(u => u.CountryId); + primaryKeyMappingContext.PreloadDependencies(u => u.UserId); + primaryKeyMappingContext.PreloadDependencies(u => u.StateId); + primaryKeyMappingContext.PreloadDependencies(u => u.CountryId); var bulkCopyRequest = new BulkCopyRequest("OM_Contact", s => true, // s => s != "ContactID", @@ -149,7 +145,7 @@ public Task Handle(MigrateContactManagementCommand request, Cance private ValueInterceptorResult ContactValueInterceptor(int ordinal, string columnName, object value, Dictionary currentRow) { - if (columnName.Equals(nameof(KXP.Models.OmContact.ContactCompanyName), StringComparison.InvariantCultureIgnoreCase)) + if (columnName.Equals(nameof(kxpContactInfo.ContactCompanyName), StringComparison.InvariantCultureIgnoreCase)) { // autofix removed in favor of error report and data consistency // var truncatedValue = SqlDataTypeHelper.TruncateString(value, 100); @@ -171,27 +167,27 @@ private ValueInterceptorResult ContactValueInterceptor(int ordinal, string colum } } - if (columnName.Equals(nameof(KXP.Models.OmContact.ContactOwnerUserId), StringComparison.InvariantCultureIgnoreCase) && value is int sourceUserId) + if (columnName.Equals(nameof(kxpContactInfo.ContactOwnerUserID), StringComparison.InvariantCultureIgnoreCase) && value is int sourceUserId) { - switch (primaryKeyMappingContext.MapSourceId(u => u.UserId, sourceUserId)) + switch (primaryKeyMappingContext.MapSourceId(u => u.UserId, sourceUserId)) { case (true, var id): return ValueInterceptorResult.ReplaceValue(id); case { Success: false }: { // try search member - if (keyMappingContext.MapSourceKey( + if (keyMappingContext.MapSourceKey( s => s.UserId, s => s.UserGuid, sourceUserId, - t => t.MemberId, + t => t.MemberID, t => t.MemberGuid ) is { Success: true, Mapped: { } memberId }) { return ValueInterceptorResult.ReplaceValue(memberId); } - protocol.Append(HandbookReferences.MissingRequiredDependency(columnName, value) + protocol.Append(HandbookReferences.MissingRequiredDependency(columnName, value) .WithData(currentRow)); return ValueInterceptorResult.SkipRow; } @@ -201,15 +197,15 @@ private ValueInterceptorResult ContactValueInterceptor(int ordinal, string colum } } - if (columnName.Equals(nameof(KXP.Models.OmContact.ContactStateId), StringComparison.InvariantCultureIgnoreCase) && value is int sourceStateId) + if (columnName.Equals(nameof(kxpContactInfo.ContactStateID), StringComparison.InvariantCultureIgnoreCase) && value is int sourceStateId) { - switch (primaryKeyMappingContext.MapSourceId(u => u.StateId, sourceStateId.NullIfZero())) + switch (primaryKeyMappingContext.MapSourceId(u => u.StateId, sourceStateId.NullIfZero())) { case (true, var id): return ValueInterceptorResult.ReplaceValue(id); case { Success: false }: { - protocol.Append(HandbookReferences.MissingRequiredDependency(columnName, value) + protocol.Append(HandbookReferences.MissingRequiredDependency(columnName, value) .WithData(currentRow)); return ValueInterceptorResult.SkipRow; } @@ -219,15 +215,15 @@ private ValueInterceptorResult ContactValueInterceptor(int ordinal, string colum } } - if (columnName.Equals(nameof(KXP.Models.OmContact.ContactCountryId), StringComparison.InvariantCultureIgnoreCase) && value is int sourceCountryId) + if (columnName.Equals(nameof(kxpContactInfo.ContactCountryID), StringComparison.InvariantCultureIgnoreCase) && value is int sourceCountryId) { - switch (primaryKeyMappingContext.MapSourceId(u => u.CountryId, sourceCountryId.NullIfZero())) + switch (primaryKeyMappingContext.MapSourceId(u => u.CountryId, sourceCountryId.NullIfZero())) { case (true, var id): return ValueInterceptorResult.ReplaceValue(id); case { Success: false }: { - protocol.Append(HandbookReferences.MissingRequiredDependency(columnName, value) + protocol.Append(HandbookReferences.MissingRequiredDependency(columnName, value) .WithData(currentRow)); return ValueInterceptorResult.SkipRow; } @@ -245,29 +241,30 @@ private ValueInterceptorResult ContactValueInterceptor(int ordinal, string colum #region "Migrate contact activities" + private readonly ActivityInfo kxpActivityInfo; private CommandResult? MigrateContactActivities() { var requiredColumnsForContactMigration = new Dictionary { - { nameof(Tool.K11.Models.OmActivity.ActivityId), nameof(KXP.Models.OmActivity.ActivityId) }, - { nameof(Tool.K11.Models.OmActivity.ActivityContactId), nameof(KXP.Models.OmActivity.ActivityContactId) }, - { nameof(Tool.K11.Models.OmActivity.ActivityCreated), nameof(KXP.Models.OmActivity.ActivityCreated) }, - { nameof(Tool.K11.Models.OmActivity.ActivityType), nameof(KXP.Models.OmActivity.ActivityType) }, - // No support 2022-07-07 { nameof(OmActivity.ActivityItemId), nameof(KXO.Models.OmActivity.ActivityItemId) }, - // No support 2022-07-07 { nameof(OmActivity.ActivityItemDetailId), nameof(KXO.Models.OmActivity.ActivityItemDetailId) }, - { nameof(Tool.K11.Models.OmActivity.ActivityValue), nameof(KXP.Models.OmActivity.ActivityValue) }, - { nameof(Tool.K11.Models.OmActivity.ActivityUrl), nameof(KXP.Models.OmActivity.ActivityUrl) }, - { nameof(Tool.K11.Models.OmActivity.ActivityTitle), nameof(KXP.Models.OmActivity.ActivityTitle) }, - { nameof(Tool.K11.Models.OmActivity.ActivitySiteId), nameof(KXP.Models.OmActivity.ActivityChannelId) }, - { nameof(Tool.K11.Models.OmActivity.ActivityComment), nameof(KXP.Models.OmActivity.ActivityComment) }, - // { nameof(OmActivity.ActivityCampaign), nameof(KXP.Models.OmActivity.ActivityCampaign) }, // deprecated without replacement in v27 - { nameof(Tool.K11.Models.OmActivity.ActivityUrlreferrer), nameof(KXP.Models.OmActivity.ActivityUrlreferrer) }, - { nameof(Tool.K11.Models.OmActivity.ActivityCulture), nameof(KXP.Models.OmActivity.ActivityLanguageId) }, - { nameof(Tool.K11.Models.OmActivity.ActivityNodeId), nameof(KXP.Models.OmActivity.ActivityWebPageItemGuid) }, - { nameof(Tool.K11.Models.OmActivity.ActivityUtmsource), nameof(KXP.Models.OmActivity.ActivityUtmsource) }, + { nameof(OmActivity.ActivityId), nameof(kxpActivityInfo.ActivityID) }, + { nameof(OmActivity.ActivityContactId), nameof(kxpActivityInfo.ActivityContactID) }, + { nameof(OmActivity.ActivityCreated), nameof(kxpActivityInfo.ActivityCreated) }, + { nameof(OmActivity.ActivityType), nameof(kxpActivityInfo.ActivityType) }, + // No support 2022-07-07 { nameof(OmActivity.ActivityItemID), nameof(KXO.Models.OmActivity.ActivityItemID) }, + // No support 2022-07-07 { nameof(OmActivity.ActivityItemDetailID), nameof(KXO.Models.OmActivity.ActivityItemDetailID) }, + { nameof(OmActivity.ActivityValue), nameof(kxpActivityInfo.ActivityValue) }, + { nameof(OmActivity.ActivityUrl), nameof(kxpActivityInfo.ActivityURL) }, + { nameof(OmActivity.ActivityTitle), nameof(kxpActivityInfo.ActivityTitle) }, + { nameof(OmActivity.ActivitySiteId), nameof(kxpActivityInfo.ActivityChannelID) }, + { nameof(OmActivity.ActivityComment), nameof(kxpActivityInfo.ActivityComment) }, + // { nameof(OmActivity.ActivityCampaign), nameof(kxpActivityInfo.ActivityCampaign) }, // deprecated without replacement in v27 + { nameof(OmActivity.ActivityUrlreferrer), nameof(kxpActivityInfo.ActivityURLReferrer) }, + { nameof(OmActivity.ActivityCulture), nameof(kxpActivityInfo.ActivityLanguageID) }, + { nameof(OmActivity.ActivityNodeId), nameof(kxpActivityInfo.ActivityWebPageItemGUID) }, + { nameof(OmActivity.ActivityUtmsource), nameof(kxpActivityInfo.ActivityUTMSource) }, // No support 2022-07-07 { nameof(OmActivity.ActivityAbvariantName), nameof(KXO.Models.OmActivity.ActivityAbvariantName) }, - // OBSOLETE 26.0.0: { nameof(OmActivity.ActivityUrlhash), nameof(KXP.Models.OmActivity.ActivityUrlhash) }, - { nameof(Tool.K11.Models.OmActivity.ActivityUtmcontent), nameof(KXP.Models.OmActivity.ActivityUtmcontent) } + // OBSOLETE 26.0.0: { nameof(OmActivity.ActivityUrlhash), nameof(kxpActivityInfo.ActivityUrlhash) }, + { nameof(OmActivity.ActivityUtmcontent), nameof(kxpActivityInfo.ActivityUTMContent) } }; foreach (var cfi in kxpClassFacade.GetCustomizedFieldInfos(ActivityInfo.TYPEINFO.ObjectClassName)) @@ -309,15 +306,15 @@ private ValueInterceptorResult ContactValueInterceptor(int ordinal, string colum private ValueInterceptorResult ActivityValueInterceptor(int columnOrdinal, string columnName, object value, Dictionary currentRow) { - if (columnName.Equals(nameof(Tool.K11.Models.OmActivity.ActivitySiteId), StringComparison.InvariantCultureIgnoreCase) && + if (columnName.Equals(nameof(OmActivity.ActivitySiteId), StringComparison.InvariantCultureIgnoreCase) && value is int sourceActivitySiteId) { - var result = keyMappingContext.MapSourceKey( + var result = keyMappingContext.MapSourceKey( s => s.SiteId, s => s.SiteGuid, sourceActivitySiteId.NullIfZero(), - t => t.ChannelId, - t => t.ChannelGuid + t => t.ChannelID, + t => t.ChannelGUID ); switch (result) { @@ -336,7 +333,7 @@ private ValueInterceptorResult ActivityValueInterceptor(int columnOrdinal, strin case AutofixEnum.Error: default: //error protocol.Append(HandbookReferences - .MissingRequiredDependency(columnName, value) + .MissingRequiredDependency(columnName, value) .WithData(currentRow) ); return ValueInterceptorResult.SkipRow; @@ -348,9 +345,9 @@ private ValueInterceptorResult ActivityValueInterceptor(int columnOrdinal, strin } } - if (columnName.Equals(nameof(Tool.K11.Models.OmActivity.ActivityNodeId), StringComparison.InvariantCultureIgnoreCase) && value is int activityNodeId) + if (columnName.Equals(nameof(OmActivity.ActivityNodeId), StringComparison.InvariantCultureIgnoreCase) && value is int activityNodeId) { - if (currentRow.TryGetValue(nameof(Tool.K11.Models.OmActivity.ActivitySiteId), out object? mSiteId) && mSiteId is int siteId) + if (currentRow.TryGetValue(nameof(OmActivity.ActivitySiteId), out object? mSiteId) && mSiteId is int siteId) { if (spoiledGuidContext.GetNodeGuid(siteId, activityNodeId) is { } nodeGuid) { @@ -369,14 +366,14 @@ private ValueInterceptorResult ActivityValueInterceptor(int columnOrdinal, strin case AutofixEnum.Error: default: //error protocol.Append(HandbookReferences - .MissingRequiredDependency(columnName, value) + .MissingRequiredDependency(columnName, value) .WithData(currentRow) ); return ValueInterceptorResult.SkipRow; } } - if (columnName.Equals(nameof(KXP.Models.OmActivity.ActivityLanguageId), StringComparison.InvariantCultureIgnoreCase) && value is string cultureCode) + if (columnName.Equals(nameof(kxpActivityInfo.ActivityLanguageID), StringComparison.InvariantCultureIgnoreCase) && value is string cultureCode) { return ValueInterceptorResult.ReplaceValue(ContentLanguageInfoProvider.ProviderObject.Get(cultureCode)?.ContentLanguageID); }