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

#290 treenode context for mapping convertor #293

Merged
merged 2 commits into from
Nov 22, 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
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
{
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
17 changes: 15 additions & 2 deletions Migration.Tool.Extensions/ClassMappings/ClassMappingSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,21 @@ 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) =>
{
switch (context)
{
case ConvertorTreeNodeContext treeNodeContext:
// here you can use available treenode context
// (var nodeGuid, int nodeSiteId, int? documentId, bool migratingFromVersionHistory) = treeNodeContext;
break;
default:
// no context is available (in future, mapping feature could be extended and therefore different context will be supplied or no context at all)
break;
}

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
17 changes: 15 additions & 2 deletions Migration.Tool.Extensions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,21 @@ var startDate = m.BuildField("StartDate");
startDate.SetFrom("_ET.Event1", "EventDateStart", true);
// if needed use value conversion to adapt value
startDate.ConvertFrom("_ET.Event2", "EventStartDateAsText", false,
v => v?.ToString() is { } av && !string.IsNullOrWhiteSpace(av) ? DateTime.Parse(av) : null
);
(v, context) =>
{
switch (context)
{
case ConvertorTreeNodeContext treeNodeContext:
// here you can use available treenode context
// (var nodeGuid, int nodeSiteId, int? documentId, bool migratingFromVersionHistory) = treeNodeContext;
break;
default:
// no context is available (possibly when tool is extended with other conversion possibilities)
break;
}

return v?.ToString() is { } av && !string.IsNullOrWhiteSpace(av) ? DateTime.Parse(av) : null;
});
startDate.WithFieldPatch(f => f.Caption = "Event start date");
```

Expand Down