DataSourceAdapter-Pattern + SiteExportService schlanker + Page-Services Scoped
- IDataSourceAdapter mit 3 Implementierungen (HANA, SAP_GATEWAY, MANUAL_EXCEL) und DataSourceAdapterResolver ersetzen das if/else auf ConnectionKind. - SiteExportService von 338 auf 187 Zeilen reduziert: Pipeline Resolve -> Fetch -> Transform -> Excel -> Central -> SharePoint. - Page-Services auf Scoped (per Blazor-Circuit); Orchestrator bleibt Singleton fuer geteilten Export-Status.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using TrafagSalesExporter.Models;
|
||||
|
||||
namespace TrafagSalesExporter.Services.DataSources;
|
||||
|
||||
public sealed class DataSourceAdapterResolver : IDataSourceAdapterResolver
|
||||
{
|
||||
private readonly Dictionary<string, IDataSourceAdapter> _adapters;
|
||||
|
||||
public DataSourceAdapterResolver(IEnumerable<IDataSourceAdapter> adapters)
|
||||
{
|
||||
_adapters = adapters.ToDictionary(
|
||||
a => a.ConnectionKind,
|
||||
StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
public IDataSourceAdapter Resolve(string connectionKind)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(connectionKind))
|
||||
connectionKind = SourceSystemConnectionKinds.Hana;
|
||||
|
||||
if (_adapters.TryGetValue(connectionKind, out var adapter))
|
||||
return adapter;
|
||||
|
||||
throw new InvalidOperationException(
|
||||
$"Kein DataSourceAdapter fuer ConnectionKind '{connectionKind}' registriert.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using TrafagSalesExporter.Models;
|
||||
|
||||
namespace TrafagSalesExporter.Services.DataSources;
|
||||
|
||||
internal static class DataSourceCredentials
|
||||
{
|
||||
public static (string Username, string Password) Resolve(Site site, SourceSystemDefinition sourceDefinition)
|
||||
=> (FirstNonEmpty(site.UsernameOverride, sourceDefinition.CentralUsername),
|
||||
FirstNonEmpty(site.PasswordOverride, sourceDefinition.CentralPassword));
|
||||
|
||||
public static string ResolveSapServiceUrl(Site site, SourceSystemDefinition sourceDefinition)
|
||||
=> FirstNonEmpty(site.SapServiceUrl, sourceDefinition.CentralServiceUrl);
|
||||
|
||||
public static string FirstNonEmpty(params string[] values)
|
||||
{
|
||||
foreach (var value in values)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
return value.Trim();
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using TrafagSalesExporter.Models;
|
||||
|
||||
namespace TrafagSalesExporter.Services.DataSources;
|
||||
|
||||
public sealed class DataSourceFetchContext
|
||||
{
|
||||
public required Site Site { get; init; }
|
||||
public required SourceSystemDefinition SourceDefinition { get; init; }
|
||||
public required ExportSettings Settings { get; init; }
|
||||
public SharePointConfig? SharePointConfig { get; init; }
|
||||
public Action<string>? UpdateStatus { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using TrafagSalesExporter.Models;
|
||||
|
||||
namespace TrafagSalesExporter.Services.DataSources;
|
||||
|
||||
public sealed class DataSourceFetchResult
|
||||
{
|
||||
public required List<SalesRecord> Records { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// Wenn gesetzt, liefert der Adapter bereits eine Referenz-Datei (z. B. manueller Excel-Import).
|
||||
/// SiteExportService erzeugt dann keine neue Excel-Datei.
|
||||
/// </summary>
|
||||
public string? ReferenceFilePath { get; init; }
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TrafagSalesExporter.Data;
|
||||
using TrafagSalesExporter.Models;
|
||||
|
||||
namespace TrafagSalesExporter.Services.DataSources;
|
||||
|
||||
public sealed class HanaDataSourceAdapter : IDataSourceAdapter
|
||||
{
|
||||
private readonly IDbContextFactory<AppDbContext> _dbFactory;
|
||||
private readonly IHanaQueryService _hanaService;
|
||||
private readonly IAppEventLogService _appEventLogService;
|
||||
|
||||
public HanaDataSourceAdapter(
|
||||
IDbContextFactory<AppDbContext> dbFactory,
|
||||
IHanaQueryService hanaService,
|
||||
IAppEventLogService appEventLogService)
|
||||
{
|
||||
_dbFactory = dbFactory;
|
||||
_hanaService = hanaService;
|
||||
_appEventLogService = appEventLogService;
|
||||
}
|
||||
|
||||
public string ConnectionKind => SourceSystemConnectionKinds.Hana;
|
||||
|
||||
public async Task<DataSourceFetchResult> FetchAsync(DataSourceFetchContext context)
|
||||
{
|
||||
var site = context.Site;
|
||||
var sourceDefinition = context.SourceDefinition;
|
||||
|
||||
using var db = await _dbFactory.CreateDbContextAsync();
|
||||
var exportServer = await BuildEffectiveServerAsync(db, site, sourceDefinition);
|
||||
|
||||
context.UpdateStatus?.Invoke("HANA Abfrage...");
|
||||
await _appEventLogService.WriteAsync("Export", "HANA Abfrage gestartet",
|
||||
siteId: site.Id, land: site.Land,
|
||||
details: exportServer.GetConnectionStringPreview());
|
||||
|
||||
var records = await Task.Run(() => _hanaService.GetSalesRecords(
|
||||
exportServer, site.Schema, site.TSC, site.Land, context.Settings.DateFilter));
|
||||
|
||||
return new DataSourceFetchResult { Records = records };
|
||||
}
|
||||
|
||||
private static async Task<HanaServer> BuildEffectiveServerAsync(
|
||||
AppDbContext db, Site site, SourceSystemDefinition sourceDefinition)
|
||||
{
|
||||
var centralServer = await db.HanaServers
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Id)
|
||||
.FirstOrDefaultAsync(x => x.SourceSystem == sourceDefinition.Code)
|
||||
?? throw new InvalidOperationException(
|
||||
$"Fuer Quellsystem '{sourceDefinition.Code}' ist keine zentrale HANA-Konfiguration vorhanden.");
|
||||
|
||||
var credentials = DataSourceCredentials.Resolve(site, sourceDefinition);
|
||||
|
||||
return new HanaServer
|
||||
{
|
||||
Id = centralServer.Id,
|
||||
SourceSystem = centralServer.SourceSystem,
|
||||
Name = centralServer.Name,
|
||||
Host = centralServer.Host,
|
||||
Port = centralServer.Port,
|
||||
Username = credentials.Username,
|
||||
Password = credentials.Password,
|
||||
DatabaseName = centralServer.DatabaseName,
|
||||
UseSsl = centralServer.UseSsl,
|
||||
ValidateCertificate = centralServer.ValidateCertificate,
|
||||
AdditionalParams = centralServer.AdditionalParams
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace TrafagSalesExporter.Services.DataSources;
|
||||
|
||||
public interface IDataSourceAdapter
|
||||
{
|
||||
/// <summary>
|
||||
/// Der Wert aus <see cref="Models.SourceSystemConnectionKinds"/>, den dieser Adapter behandelt.
|
||||
/// </summary>
|
||||
string ConnectionKind { get; }
|
||||
|
||||
Task<DataSourceFetchResult> FetchAsync(DataSourceFetchContext context);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace TrafagSalesExporter.Services.DataSources;
|
||||
|
||||
public interface IDataSourceAdapterResolver
|
||||
{
|
||||
IDataSourceAdapter Resolve(string connectionKind);
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
using TrafagSalesExporter.Models;
|
||||
|
||||
namespace TrafagSalesExporter.Services.DataSources;
|
||||
|
||||
public sealed class ManualExcelDataSourceAdapter : IDataSourceAdapter
|
||||
{
|
||||
private readonly ISharePointUploadService _sharePointService;
|
||||
private readonly IManualExcelImportService _manualExcelImportService;
|
||||
private readonly IAppEventLogService _appEventLogService;
|
||||
|
||||
public ManualExcelDataSourceAdapter(
|
||||
ISharePointUploadService sharePointService,
|
||||
IManualExcelImportService manualExcelImportService,
|
||||
IAppEventLogService appEventLogService)
|
||||
{
|
||||
_sharePointService = sharePointService;
|
||||
_manualExcelImportService = manualExcelImportService;
|
||||
_appEventLogService = appEventLogService;
|
||||
}
|
||||
|
||||
public string ConnectionKind => SourceSystemConnectionKinds.ManualExcel;
|
||||
|
||||
public async Task<DataSourceFetchResult> FetchAsync(DataSourceFetchContext context)
|
||||
{
|
||||
var site = context.Site;
|
||||
|
||||
if (string.IsNullOrWhiteSpace(site.ManualImportFilePath))
|
||||
throw new InvalidOperationException($"Standort '{site.Land}' hat keine manuelle Excel-Datei.");
|
||||
|
||||
var manualImportPath = site.ManualImportFilePath.Trim();
|
||||
string filePath;
|
||||
string? tempManualImportPath = null;
|
||||
try
|
||||
{
|
||||
if (File.Exists(manualImportPath))
|
||||
{
|
||||
filePath = manualImportPath;
|
||||
}
|
||||
else if (LooksLikeSharePointReference(manualImportPath))
|
||||
{
|
||||
var spConfig = context.SharePointConfig
|
||||
?? throw new InvalidOperationException(
|
||||
"Fuer SharePoint-Manuellimport fehlt eine vollstaendige SharePoint-Konfiguration in Settings.");
|
||||
|
||||
if (string.IsNullOrWhiteSpace(spConfig.TenantId) ||
|
||||
string.IsNullOrWhiteSpace(spConfig.ClientId) ||
|
||||
string.IsNullOrWhiteSpace(spConfig.ClientSecret) ||
|
||||
string.IsNullOrWhiteSpace(spConfig.SiteUrl))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
"Fuer SharePoint-Manuellimport fehlt eine vollstaendige SharePoint-Konfiguration in Settings.");
|
||||
}
|
||||
|
||||
context.UpdateStatus?.Invoke("Manuelle Excel von SharePoint laden...");
|
||||
await _appEventLogService.WriteAsync("Export", "Manuelle Excel von SharePoint laden",
|
||||
siteId: site.Id, land: site.Land, details: manualImportPath);
|
||||
|
||||
tempManualImportPath = await _sharePointService.DownloadToTempFileAsync(
|
||||
spConfig.TenantId, spConfig.ClientId, spConfig.ClientSecret,
|
||||
spConfig.SiteUrl, manualImportPath);
|
||||
filePath = manualImportPath;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Die manuelle Excel-Datei wurde nicht gefunden: {manualImportPath}");
|
||||
}
|
||||
|
||||
var readPath = tempManualImportPath ?? filePath;
|
||||
context.UpdateStatus?.Invoke("Manuelle Excel lesen...");
|
||||
await _appEventLogService.WriteAsync("Export", "Manuelle Excel lesen",
|
||||
siteId: site.Id, land: site.Land, details: filePath);
|
||||
|
||||
var records = await _manualExcelImportService.ReadSalesRecordsAsync(readPath, site);
|
||||
return new DataSourceFetchResult
|
||||
{
|
||||
Records = records,
|
||||
ReferenceFilePath = filePath
|
||||
};
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(tempManualImportPath) && File.Exists(tempManualImportPath))
|
||||
File.Delete(tempManualImportPath);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool LooksLikeSharePointReference(string path)
|
||||
=> path.StartsWith("http://", StringComparison.OrdinalIgnoreCase) ||
|
||||
path.StartsWith("https://", StringComparison.OrdinalIgnoreCase) ||
|
||||
path.StartsWith("/Shared Documents/", StringComparison.OrdinalIgnoreCase) ||
|
||||
path.StartsWith("Shared Documents/", StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TrafagSalesExporter.Data;
|
||||
using TrafagSalesExporter.Models;
|
||||
|
||||
namespace TrafagSalesExporter.Services.DataSources;
|
||||
|
||||
public sealed class SapGatewayDataSourceAdapter : IDataSourceAdapter
|
||||
{
|
||||
private readonly IDbContextFactory<AppDbContext> _dbFactory;
|
||||
private readonly ISapCompositionService _sapCompositionService;
|
||||
private readonly IAppEventLogService _appEventLogService;
|
||||
|
||||
public SapGatewayDataSourceAdapter(
|
||||
IDbContextFactory<AppDbContext> dbFactory,
|
||||
ISapCompositionService sapCompositionService,
|
||||
IAppEventLogService appEventLogService)
|
||||
{
|
||||
_dbFactory = dbFactory;
|
||||
_sapCompositionService = sapCompositionService;
|
||||
_appEventLogService = appEventLogService;
|
||||
}
|
||||
|
||||
public string ConnectionKind => SourceSystemConnectionKinds.SapGateway;
|
||||
|
||||
public async Task<DataSourceFetchResult> FetchAsync(DataSourceFetchContext context)
|
||||
{
|
||||
var site = context.Site;
|
||||
var sourceDefinition = context.SourceDefinition;
|
||||
|
||||
var credentials = DataSourceCredentials.Resolve(site, sourceDefinition);
|
||||
var sapServiceUrl = DataSourceCredentials.ResolveSapServiceUrl(site, sourceDefinition);
|
||||
if (string.IsNullOrWhiteSpace(sapServiceUrl))
|
||||
throw new InvalidOperationException($"Standort '{site.Land}' hat keine SAP Service URL.");
|
||||
|
||||
using var db = await _dbFactory.CreateDbContextAsync();
|
||||
var sapSources = await db.SapSourceDefinitions.Where(s => s.SiteId == site.Id).ToListAsync();
|
||||
var sapJoins = await db.SapJoinDefinitions.Where(j => j.SiteId == site.Id).ToListAsync();
|
||||
var sapMappings = await db.SapFieldMappings.Where(m => m.SiteId == site.Id).ToListAsync();
|
||||
|
||||
if (sapSources.Count == 0)
|
||||
throw new InvalidOperationException($"Standort '{site.Land}' hat keine SAP-Quellen konfiguriert.");
|
||||
if (sapMappings.Count == 0)
|
||||
throw new InvalidOperationException($"Standort '{site.Land}' hat keine SAP-Feldmappings.");
|
||||
|
||||
context.UpdateStatus?.Invoke("SAP Quellen laden...");
|
||||
await _appEventLogService.WriteAsync("Export", "SAP Quellen laden",
|
||||
siteId: site.Id, land: site.Land,
|
||||
details: $"Sources={sapSources.Count} | Mappings={sapMappings.Count}");
|
||||
|
||||
var effectiveSite = CloneSiteWithSapServiceUrl(site, sapServiceUrl);
|
||||
var records = await _sapCompositionService.BuildSalesRecordsAsync(
|
||||
effectiveSite, sapSources, sapJoins, sapMappings,
|
||||
credentials.Username, credentials.Password);
|
||||
|
||||
return new DataSourceFetchResult { Records = records };
|
||||
}
|
||||
|
||||
private static Site CloneSiteWithSapServiceUrl(Site site, string sapServiceUrl)
|
||||
{
|
||||
return new Site
|
||||
{
|
||||
Id = site.Id,
|
||||
HanaServerId = site.HanaServerId,
|
||||
HanaServer = site.HanaServer,
|
||||
Schema = site.Schema,
|
||||
TSC = site.TSC,
|
||||
Land = site.Land,
|
||||
SourceSystem = site.SourceSystem,
|
||||
UsernameOverride = site.UsernameOverride,
|
||||
PasswordOverride = site.PasswordOverride,
|
||||
LocalExportFolderOverride = site.LocalExportFolderOverride,
|
||||
ManualImportFilePath = site.ManualImportFilePath,
|
||||
ManualImportLastUploadedAtUtc = site.ManualImportLastUploadedAtUtc,
|
||||
SapServiceUrl = sapServiceUrl,
|
||||
SapEntitySet = site.SapEntitySet,
|
||||
SapEntitySetsCache = site.SapEntitySetsCache,
|
||||
SapEntitySetsRefreshedAtUtc = site.SapEntitySetsRefreshedAtUtc,
|
||||
IsActive = site.IsActive
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user