using System.Reflection; using GB5Shared.DTO.Framework.Login; using IceImportDAL.DTO.IceImportPipeline; namespace IceImportBLL.FieldResolvers; // ValueType = 2 (Global System Value). Reflects LoginDTO by property name, modeled on // GB5Shared.GB5Library.Qualifier.DbEnrichExecutor.GetFromData's reflection style — deliberately // NOT a giant if/else chain of string comparisons the way legacy did. // // Design decision: legacy's short field names (OU/Period/Store/PartyBranch/WorkDate/Role) don't // match LoginDTO's actual property names 1:1 (confirmed by reading LoginDTO.cs), so a small alias // map translates the legacy IceMapDetailsSourceFieldName into the real LoginDTO property before // reflecting. Any IceMapDetailsSourceFieldName that already matches a LoginDTO property name // directly (case-insensitive) also works without an alias entry. public class GlobalSystemValueResolver : IIceFieldValueResolver { public int ValueType => 2; private static readonly Dictionary _legacyAliases = new(StringComparer.OrdinalIgnoreCase) { ["OU"] = nameof(LoginDTO.WorkOUId), ["Period"] = nameof(LoginDTO.WorkPeriodId), ["Store"] = nameof(LoginDTO.WorkStoreId), ["PartyBranch"] = nameof(LoginDTO.WorkPartyBranchId), ["WorkDate"] = nameof(LoginDTO.WorkDate), ["Role"] = nameof(LoginDTO.RoleId), }; public Task ResolveAsync(FieldResolutionContext ctx, CancellationToken ct) { var name = ctx.DetailConfig.IceMapDetailsSourceFieldName?.Trim() ?? string.Empty; var propertyName = _legacyAliases.TryGetValue(name, out var mapped) ? mapped : name; var prop = typeof(LoginDTO).GetProperty( propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase); if (prop is null) { var warning = new IceImportFieldErrorDTO { RowNumber = ctx.RowNumber, EntityCode = ctx.DetailConfig.EntityCode ?? string.Empty, FieldName = ctx.DetailConfig.DestinationMemberMemberName ?? name, Message = $"'{name}' does not resolve to any LoginDTO property for a Global System Value field.", Severity = IceImportSeverity.Warning }; return Task.FromResult(FieldResolutionResult.WithWarning(null, warning)); } return Task.FromResult(FieldResolutionResult.Ok(prop.GetValue(ctx.Login))); } }