Skip to content

Commit

Permalink
#290 treenode context for mapping convertor
Browse files Browse the repository at this point in the history
  • Loading branch information
tkrch committed Nov 12, 2024
1 parent a9cb03a commit 2badcea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
21 changes: 11 additions & 10 deletions KVA/Migration.Tool.Source/Mappers/ContentItemMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ IClassMapping mapping
foreach (string targetColumnName in newColumnNames)
{
string targetFieldName = null!;
Func<object?, object?> valueConvertor = sourceValue => sourceValue;
Func<object?, IConvertorContext, object?> valueConvertor = (sourceValue, _) => sourceValue;
switch (mapping?.GetMapping(targetColumnName, sourceNodeClass.ClassName))
{
case FieldMappingWithConversion fieldMappingWithConversion:
Expand All @@ -534,13 +534,13 @@ IClassMapping mapping
case FieldMapping fieldMapping:
{
targetFieldName = fieldMapping.TargetFieldName;
valueConvertor = sourceValue => sourceValue;
valueConvertor = (sourceValue, _) => sourceValue;
break;
}
case null:
{
targetFieldName = targetColumnName;
valueConvertor = sourceValue => sourceValue;
valueConvertor = (sourceValue, _) => sourceValue;
break;
}

Expand Down Expand Up @@ -587,7 +587,8 @@ IClassMapping mapping
string? controlName = field.Settings[CLASS_FIELD_CONTROL_NAME]?.ToString()?.ToLowerInvariant();

object? sourceValue = getSourceValue(sourceFieldName);
target[targetFieldName] = valueConvertor.Invoke(sourceValue);
var convertorContext = new ConvertorTreeNodeContext(cmsTree.NodeGUID, cmsTree.NodeSiteID, documentId, migratingFromVersionHistory);
target[targetFieldName] = valueConvertor.Invoke(sourceValue, convertorContext);
var fvmc = new FieldMigrationContext(field.DataType, controlName, targetColumnName, new DocumentSourceObjectContext(cmsTree, sourceNodeClass, site, oldFormInfo, newFormInfo, documentId));
var fmb = fieldMigrationService.GetFieldMigration(fvmc);
if (fmb is FieldMigration fieldMigration)
Expand All @@ -600,12 +601,12 @@ IClassMapping mapping
var convertedRelation = relationshipService.GetNodeRelationships(cmsTree.NodeID, sourceNodeClass.ClassName, field.Guid)
.Select(r => new WebPageRelatedItem { WebPageGuid = spoiledGuidContext.EnsureNodeGuid(r.RightNode.NodeGUID, r.RightNode.NodeSiteID, r.RightNode.NodeID) });

target.SetValueAsJson(targetFieldName, valueConvertor.Invoke(convertedRelation));
target.SetValueAsJson(targetFieldName, valueConvertor.Invoke(convertedRelation, convertorContext));
}
else
{
// leave as is
target[targetFieldName] = valueConvertor.Invoke(sourceValue);
target[targetFieldName] = valueConvertor.Invoke(sourceValue, convertorContext);
}

if (fieldMigration.TargetFormComponent == "webpages")
Expand All @@ -622,13 +623,13 @@ IClassMapping mapping
}
}

target[targetFieldName] = valueConvertor.Invoke(parsed.ToString().Replace("\"NodeGuid\"", "\"WebPageGuid\""));
target[targetFieldName] = valueConvertor.Invoke(parsed.ToString().Replace("\"NodeGuid\"", "\"WebPageGuid\""), convertorContext);
}
}
}
else
{
target[targetFieldName] = valueConvertor.Invoke(sourceValue);
target[targetFieldName] = valueConvertor.Invoke(sourceValue, convertorContext);
}
}
else if (fmb != null)
Expand All @@ -637,7 +638,7 @@ IClassMapping mapping
{
case { Success: true } result:
{
target[targetFieldName] = valueConvertor.Invoke(result.MigratedValue);
target[targetFieldName] = valueConvertor.Invoke(result.MigratedValue, convertorContext);
break;
}
case { Success: false }:
Expand All @@ -652,7 +653,7 @@ IClassMapping mapping
}
else
{
target[targetFieldName] = valueConvertor?.Invoke(sourceValue);
target[targetFieldName] = valueConvertor?.Invoke(sourceValue, convertorContext);
}


Expand Down
7 changes: 5 additions & 2 deletions Migration.Tool.Common/Builders/ClassMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface IFieldMapping

public record FieldMapping(string TargetFieldName, string SourceClassName, string SourceFieldName, bool IsTemplate) : IFieldMapping;

public record FieldMappingWithConversion(string TargetFieldName, string SourceClassName, string SourceFieldName, bool IsTemplate, Func<object?, object?> Converter) : IFieldMapping;
public record FieldMappingWithConversion(string TargetFieldName, string SourceClassName, string SourceFieldName, bool IsTemplate, Func<object?, IConvertorContext, object?> Converter) : IFieldMapping;

public class MultiClassMapping(string targetClassName, Action<DataClassInfo> classPatcher) : IClassMapping

Check warning on line 37 in Migration.Tool.Common/Builders/ClassMapper.cs

View workflow job for this annotation

GitHub Actions / Build and Test

Non-nullable property 'PrimaryKey' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
Expand Down Expand Up @@ -93,6 +93,9 @@ public void UseResusableSchema(string reusableSchemaName)
IList<string> IClassMapping.ReusableSchemaNames => reusableSchemaNames;
}

public interface IConvertorContext;
public record ConvertorTreeNodeContext(Guid NodeGuid, int NodeSiteId, int? DocumentId, bool MigratingFromVersionHistory) : IConvertorContext;

public class FieldBuilder(MultiClassMapping multiClassMapping, string targetFieldName)
{
private IFieldMapping? currentFieldMapping;
Expand All @@ -105,7 +108,7 @@ public FieldBuilder SetFrom(string sourceClassName, string sourceFieldName, bool
return this;
}

public FieldBuilder ConvertFrom(string sourceClassName, string sourceFieldName, bool isTemplate, Func<object?, object?> converter)
public FieldBuilder ConvertFrom(string sourceClassName, string sourceFieldName, bool isTemplate, Func<object?, IConvertorContext, object?> converter)
{
currentFieldMapping = new FieldMappingWithConversion(targetFieldName, sourceClassName, sourceFieldName, isTemplate, converter);
multiClassMapping.Mappings.Add(currentFieldMapping);
Expand Down
14 changes: 12 additions & 2 deletions Migration.Tool.Extensions/ClassMappings/ClassMappingSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,18 @@ public static IServiceCollection AddClassMergeExample(this IServiceCollection se
startDate.SetFrom(sourceClassName1, "EventDateStart", true);
// if needed use value conversion to adapt value
startDate.ConvertFrom(sourceClassName2, "EventStartDateAsText", false,
v => v?.ToString() is { } av && !string.IsNullOrWhiteSpace(av) ? DateTime.Parse(av) : null
);
(v, context) =>
{
if (context is ConvertorTreeNodeContext(var nodeGuid, var nodeSiteId, var documentId, var migratingFromVersionHistory))
{
// here you can use available treenode context
}
else
{
// no context is available (possibly when tool is extended with other conversion possibilities)
}
return v?.ToString() is { } av && !string.IsNullOrWhiteSpace(av) ? DateTime.Parse(av) : null;
});
startDate.WithFieldPatch(f => f.Caption = "Event start date");

serviceCollection.AddSingleton<IClassMapping>(m);
Expand Down

0 comments on commit 2badcea

Please sign in to comment.