Skip to content

Commit dd7ad40

Browse files
committed
Fix conversion of custom tables media selector field to content item selector
Fix: Fix conversion of custom tables media selector field to content item selector
1 parent 0885496 commit dd7ad40

File tree

6 files changed

+72
-17
lines changed

6 files changed

+72
-17
lines changed

KVA/Migration.Tool.Source/Providers/ClassMappingProvider.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
using Microsoft.Extensions.Logging;
66
using Migration.Tool.Common;
77
using Migration.Tool.Common.Builders;
8+
using Migration.Tool.Common.Enumerations;
89
using Migration.Tool.KXP.Api.Services.CmsClass;
10+
using Migration.Tool.Source.Contexts;
911
using Migration.Tool.Source.Helpers;
1012
using Migration.Tool.Source.Model;
1113
using Migration.Tool.Source.Services;
@@ -297,7 +299,23 @@ private void EnsureSettings()
297299
}
298300
else
299301
{
300-
m.BuildField(formFieldInfo.Name).SetFrom(mappedClass.ClassName, formFieldInfo.Name, true);
302+
if (formFieldInfo.Settings["controlname"] is string controlName
303+
&& string.Equals(controlName, Kx13FormControls.UserControlForText.MediaSelectionControl, StringComparison.InvariantCultureIgnoreCase))
304+
{
305+
var fieldMigration = fieldMigrationService.GetFieldMigration(new(string.Empty, controlName, null, new CustomTableSourceObjectContext()));
306+
if (fieldMigration is not null)
307+
{
308+
m.BuildField(formFieldInfo.Name).ConvertFrom(mappedClass.ClassName, formFieldInfo.Name, true, (x, _) => x);
309+
}
310+
else
311+
{
312+
throw new Exception($"No field migration found for {Kx13FormControls.UserControlForText.MediaSelectionControl} field {formFieldInfo.Name}");
313+
}
314+
}
315+
else
316+
{
317+
m.BuildField(formFieldInfo.Name).SetFrom(mappedClass.ClassName, formFieldInfo.Name, true);
318+
}
301319
}
302320
}
303321

Migration.Tool.CLI/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@
163163
services.AddSingleton<ICommandParser, CommandParser>();
164164
services.UseToolCommon();
165165

166+
var invokedCommands = new InvokedCommands();
167+
services.AddSingleton(invokedCommands);
168+
166169
await using var serviceProvider = services.BuildServiceProvider();
167170
KsCoreDiExtensions.InitServiceProvider(serviceProvider);
168171
using var scope = serviceProvider.CreateScope();
@@ -180,6 +183,8 @@
180183
// sort commands
181184
commands = commands.OrderBy(x => x.Rank).ToList();
182185

186+
invokedCommands.Commands.AddRange(commands);
187+
183188
var satisfiedDependencies = new HashSet<Type>();
184189
bool dependenciesSatisfied = true;
185190
if (!bypassDependencyCheck)

Migration.Tool.Common/Commands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public record MigrateCustomModulesCommand : IRequest<CommandResult>, ICommand
136136

137137
public record MigrateCustomTablesCommand : IRequest<CommandResult>, ICommand
138138
{
139-
public static readonly int Rank = 1 + MigrateSitesCommand.Rank + MigrateCustomModulesCommand.Rank;
139+
public static readonly int Rank = 1 + MigrateSitesCommand.Rank + MigrateCustomModulesCommand.Rank + MigrateMediaLibrariesCommand.Rank;
140140

141141
public static string Moniker => "custom-tables";
142142
public static string MonikerFriendly => "Custom tables";

Migration.Tool.Common/Helpers/MediaLinkService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class MediaLinkService(
2424
Dictionary<int, HashSet<string>> siteLibraryNames
2525
)
2626
{
27-
public MatchMediaLinkResult MatchMediaLink(string? linkStr, int currentSiteId)
27+
public MatchMediaLinkResult MatchMediaLink(string? linkStr, int? currentSiteId)
2828
{
2929
if (string.IsNullOrEmpty(linkStr))
3030
{
@@ -121,7 +121,7 @@ public MatchMediaLinkResult MatchMediaLink(string? linkStr, int currentSiteId)
121121
}
122122

123123
bool siteMediaFolder = "True".Equals(cmsUseMediaLibrariesSiteFolder.OrderBy(x => x.siteId ?? -1).FirstOrDefault(x => x.siteId == linkSiteId || x.siteId == null).value, StringComparison.InvariantCultureIgnoreCase);
124-
if (siteMediaFolder)
124+
if (siteMediaFolder && currentSiteId is not null)
125125
{
126126
if (sites.FirstOrDefault(x => x.siteId == linkSiteId) is var (_, siteName, _))
127127
{
@@ -135,7 +135,7 @@ public MatchMediaLinkResult MatchMediaLink(string? linkStr, int currentSiteId)
135135
}
136136
}
137137

138-
if ("media".Equals(spl[inspectionIndex], StringComparison.InvariantCultureIgnoreCase))
138+
if (spl.Length != 0 && "media".Equals(spl[inspectionIndex], StringComparison.InvariantCultureIgnoreCase))
139139
{
140140
mediaKind = MediaKind.MediaFile;
141141
mediaLinkKind = MediaLinkKind.DirectMediaPath;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using Migration.Tool.Common.Abstractions;
2+
3+
namespace Migration.Tool.Common;
4+
public class InvokedCommands
5+
{
6+
public List<ICommand> Commands { get; } = [];
7+
}

Migration.Tool.Extensions/DefaultMigrations/AssetMigration.cs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public class AssetMigration(
2929
ToolConfiguration configuration,
3030
EntityIdentityFacade entityIdentityFacade,
3131
IAssetFacade assetFacade,
32-
MediaLinkServiceFactory mediaLinkServiceFactory
32+
MediaLinkServiceFactory mediaLinkServiceFactory,
33+
InvokedCommands invokedCommands
3334
) : IFieldMigration
3435
{
3536
public int Rank => 100_000;
@@ -40,25 +41,46 @@ context.SourceDataType is KsFieldDataType.DocAttachments or KsFieldDataType.File
4041
Kx13FormControls.UserControlForText.MediaSelectionControl.Equals(context.SourceFormControl, StringComparison.InvariantCultureIgnoreCase)
4142
) &&
4243
context.SourceObjectContext
43-
// this migration can handle only migration of documents to content items
44-
is DocumentSourceObjectContext
44+
is DocumentSourceObjectContext or CustomTableSourceObjectContext
4545
// this migration also handles empty object context - for example when migrating data class, empty context is supplied
4646
or EmptySourceObjectContext;
4747

4848
public async Task<FieldMigrationResult> MigrateValue(object? sourceValue, FieldMigrationContext context)
4949
{
5050
(string? _, string? sourceFormControl, string? fieldName, var sourceObjectContext) = context;
51-
if (sourceObjectContext is not DocumentSourceObjectContext(_, _, var cmsSite, var oldFormInfo, _, var documentId))
51+
52+
if (!invokedCommands.Commands.Any(x => x is MigrateMediaLibrariesCommand))
53+
{
54+
logger.LogError($"Trying to migrate asset field value {{FieldName}}, but commands {MigrateMediaLibrariesCommand.Moniker} and {MigrateAttachmentsCommand.Moniker} were not invoked", fieldName);
55+
return new(false, null);
56+
}
57+
58+
ICmsSite? cmsSite;
59+
CMS.FormEngine.FormInfo? oldFormInfo;
60+
int? documentId;
61+
if (sourceObjectContext is DocumentSourceObjectContext docContext)
62+
{
63+
cmsSite = docContext.Site;
64+
oldFormInfo = docContext.OldFormInfo;
65+
documentId = docContext.DocumentId;
66+
}
67+
else if (sourceObjectContext is CustomTableSourceObjectContext)
68+
{
69+
cmsSite = null;
70+
oldFormInfo = null;
71+
documentId = null;
72+
}
73+
else
5274
{
5375
throw new ArgumentNullException(nameof(sourceObjectContext));
5476
}
5577

56-
var field = oldFormInfo.GetFormField(fieldName);
78+
var field = oldFormInfo?.GetFormField(fieldName);
5779

5880
List<object> mfis = [];
5981
bool hasMigratedAsset = false;
6082
if (sourceValue is string link &&
61-
mediaLinkServiceFactory.Create().MatchMediaLink(link, cmsSite.SiteID) is (true, var mediaLinkKind, var mediaKind, var path, var mediaGuid, _, _) result)
83+
mediaLinkServiceFactory.Create().MatchMediaLink(link, cmsSite?.SiteID) is (true, var mediaLinkKind, var mediaKind, var path, var mediaGuid, _, _) result)
6284
{
6385
if (mediaLinkKind == MediaLinkKind.Path)
6486
{
@@ -131,7 +153,7 @@ public async Task<FieldMigrationResult> MigrateValue(object? sourceValue, FieldM
131153
{
132154
if (mediaKind == MediaKind.Attachment)
133155
{
134-
switch (await attachmentMigrator.MigrateAttachment(mg, $"__{fieldName}", cmsSite.SiteID))
156+
switch (await attachmentMigrator.MigrateAttachment(mg, $"__{fieldName}", cmsSite!.SiteID))
135157
{
136158
case MigrateAttachmentResultMediaFile(true, var x, _):
137159
{
@@ -159,8 +181,11 @@ public async Task<FieldMigrationResult> MigrateValue(object? sourceValue, FieldM
159181

160182
if (mediaKind == MediaKind.MediaFile)
161183
{
162-
var sourceMediaFile = modelFacade.SelectWhere<IMediaFile>("FileGUID = @mediaFileGuid AND FileSiteID = @fileSiteID", new SqlParameter("mediaFileGuid", mg), new SqlParameter("fileSiteID", cmsSite.SiteID))
163-
.FirstOrDefault();
184+
var sourceMediaFile =
185+
(cmsSite is not null
186+
? modelFacade.SelectWhere<IMediaFile>("FileGUID = @mediaFileGuid AND FileSiteID = @fileSiteID", new SqlParameter("mediaFileGuid", mg), new SqlParameter("fileSiteID", cmsSite.SiteID))
187+
: modelFacade.SelectWhere<IMediaFile>("FileGUID = @mediaFileGuid", new SqlParameter("mediaFileGuid", mg))
188+
).FirstOrDefault();
164189
if (sourceMediaFile != null)
165190
{
166191
if (configuration.MigrateMediaToMediaLibrary)
@@ -193,7 +218,7 @@ public async Task<FieldMigrationResult> MigrateValue(object? sourceValue, FieldM
193218
{
194219
if (sourceValue is Guid attachmentGuid)
195220
{
196-
switch (await attachmentMigrator.MigrateAttachment(attachmentGuid, $"__{fieldName}", cmsSite.SiteID))
221+
switch (await attachmentMigrator.MigrateAttachment(attachmentGuid, $"__{fieldName}", cmsSite!.SiteID))
197222
{
198223
case MigrateAttachmentResultMediaFile(true, var mfi, _):
199224
{
@@ -221,7 +246,7 @@ public async Task<FieldMigrationResult> MigrateValue(object? sourceValue, FieldM
221246
}
222247
else if (sourceValue is string attachmentGuidStr && Guid.TryParse(attachmentGuidStr, out attachmentGuid))
223248
{
224-
switch (await attachmentMigrator.MigrateAttachment(attachmentGuid, $"__{fieldName}", cmsSite.SiteID))
249+
switch (await attachmentMigrator.MigrateAttachment(attachmentGuid, $"__{fieldName}", cmsSite!.SiteID))
225250
{
226251
case MigrateAttachmentResultMediaFile { Success: true, MediaFileInfo: { } x }:
227252
{
@@ -260,7 +285,7 @@ public async Task<FieldMigrationResult> MigrateValue(object? sourceValue, FieldM
260285
if (documentId is { } docId)
261286
{
262287
var mfisl = new List<object>();
263-
await foreach (var migResult in attachmentMigrator.MigrateGroupedAttachments(docId, field.Guid, field.Name))
288+
await foreach (var migResult in attachmentMigrator.MigrateGroupedAttachments(docId, field!.Guid, field.Name))
264289
{
265290
switch (migResult)
266291
{

0 commit comments

Comments
 (0)