@page "/data-coupler" @using CredentialManager.Models @using DataConnection.Interfaces @using DataConnection.CredentialManagement.Interfaces @using DataConnection.REST.Interfaces @using DataConnection.REST.Models @using Data_Coupler.Services @using Microsoft.AspNetCore.Components.Forms @using Microsoft.JSInterop @using System.IO @using System.Text @using System.Data @using ExcelDataReader @inject IDataConnectionCredentialService CredentialService @inject IDataConnectionFactory ConnectionFactory @inject IJSRuntime JSRuntime @inject ILogger Logger Data Coupler

Data Coupler - Coupling Database e REST API

Connetti database e servizi REST per il trasferimento dati

Fonte Dati
@if (selectedSourceType == "database") {
@if (!string.IsNullOrEmpty(selectedDatabaseCredential)) {
@if (isDatabaseConnected) { Connesso }
} @if (!string.IsNullOrEmpty(databaseErrorMessage)) { } @if (databaseTables.Any()) {
Tabelle Database (@databaseTables.Count disponibili):
@if (!string.IsNullOrEmpty(databaseSearchTerm)) { }
@if (!GetFilteredDatabaseTables().Any()) {
Nessuna tabella trovata con il termine di ricerca "@databaseSearchTerm"
}
} } @if (selectedSourceType == "file") {
@if (!string.IsNullOrEmpty(selectedFileName)) { File selezionato: @selectedFileName }
@if (isProcessingFile) {
Elaborazione file in corso...
} @if (!string.IsNullOrEmpty(fileErrorMessage)) { } @if (fileSheets.Any()) {
@{ var fileExtension = Path.GetExtension(selectedFileName).ToLowerInvariant(); var isExcel = fileExtension == ".xlsx" || fileExtension == ".xls"; var fileTypeIcon = isExcel ? "fa-file-excel text-success" : "fa-file-csv text-info"; var fileTypeName = isExcel ? "Excel" : "CSV"; }
Fogli @fileTypeName (@fileSheets.Count):
} else if (selectedSourceType == "file" && !string.IsNullOrEmpty(selectedFileName) && !isProcessingFile && string.IsNullOrEmpty(fileErrorMessage)) { } else if (selectedSourceType == "file" && string.IsNullOrEmpty(selectedFileName)) { } @if (!string.IsNullOrEmpty(selectedSheet) && fileData.ContainsKey(selectedSheet) && fileData[selectedSheet].Any()) {
Preview Dati - @selectedSheet
Record @GetStartRecord()-@GetEndRecord() di @fileData[selectedSheet].Count
@currentPage di @GetTotalPages(selectedSheet)
@if (fileSheets.ContainsKey(selectedSheet)) {
@foreach (var column in fileSheets[selectedSheet]) { } @{ var dataToShow = GetCurrentPageData(); var startRecord = GetStartRecord(); } @for (int i = 0; i < dataToShow.Count; i++) { var row = dataToShow[i]; var absoluteRowIndex = startRecord + i; @foreach (var column in fileSheets[selectedSheet]) { var cellValue = row.ContainsKey(column) ? row[column]?.ToString() : ""; var displayValue = string.IsNullOrEmpty(cellValue) ? "-" : (cellValue.Length > 50 ? cellValue.Substring(0, 50) + "..." : cellValue); } }
#@column
@absoluteRowIndex @displayValue
}
} }
REST API Destination
@if (!string.IsNullOrEmpty(selectedRestCredential)) {
@if (isRestConnected) { Connesso }
} @if (!string.IsNullOrEmpty(restErrorMessage)) { } @if (restEntities.Any()) {
Entità REST (@restEntities.Count disponibili):
@if (!string.IsNullOrEmpty(restSearchTerm)) { }
@if (!GetFilteredRestEntities().Any()) {
Nessuna entità trovata con il termine di ricerca "@restSearchTerm"
}
}
@{ var isSourceReady = (selectedSourceType == "database" && isDatabaseConnected && !string.IsNullOrEmpty(selectedTable)) || (selectedSourceType == "file" && !string.IsNullOrEmpty(selectedSheet)); } @if (isSourceReady && isRestConnected && selectedRestEntity != null) {
Mapping Campi
@{ var sourceDisplayName = selectedSourceType == "database" ? selectedTable : selectedSheet; var sourceTypeName = selectedSourceType == "database" ? "Tabella" : "Foglio"; }
Mapping tra @sourceTypeName @sourceDisplayName e @selectedRestEntity.Name

Configura il mapping tra i campi della fonte dati e le proprietà dell'entità REST

Campi @sourceTypeName (@sourceDisplayName)
@if (selectedSourceType == "database" && databaseTables.ContainsKey(selectedTable)) { @foreach (var column in databaseTables[selectedTable]) {
@column.Name @column.DataType
@if (column.IsPrimaryKey) { PK } @if (fieldMappings.ContainsKey(column.Name)) { Mapped }
} } else if (selectedSourceType == "file" && fileSheets.ContainsKey(selectedSheet)) { @foreach (var column in fileSheets[selectedSheet]) {
@column Colonna File
@if (fieldMappings.ContainsKey(column)) { Mapped }
} }
Proprietà REST (@selectedRestEntity.Name)
@if (restEntityDetails != null) { @foreach (var property in restEntityDetails.Properties) {
@property.Name @property.Type
@if (property.IsRequired) { Required } @if (fieldMappings.ContainsValue(property.Name)) { Mapped }
} }
@if (fieldMappings.Any()) {
Mappature Correnti (@fieldMappings.Count)
@if (keyFields.Any()) { @keyFields.Count campo/i chiave: @string.Join(", ", keyFields) }
@foreach (var mapping in fieldMappings) { DbColumnInfo? dbColumn = null; if (selectedSourceType == "database" && !string.IsNullOrEmpty(selectedTable)) { dbColumn = databaseTables.ContainsKey(selectedTable) ? databaseTables[selectedTable].FirstOrDefault(c => c.Name == mapping.Key) : null; } var restProperty = restEntityDetails?.Properties.FirstOrDefault(p => p.Name == mapping.Value); }
Campo Database Tipo DB Proprietà REST Tipo REST Azioni
@mapping.Key @(dbColumn?.DataType ?? (selectedSourceType == "file" ? "Text" : "Unknown")) @mapping.Value @(restProperty?.Type ?? "Unknown")
} @if (fieldMappings.Any()) {
Configurazione Chiave Sorgente
@if (useRecordAssociations) {
@if (requiresManualKeySelection || selectedSourceType != "database") { Selezione del campo chiave obbligatoria. Scegli un campo che identifichi univocamente ogni record. } else if (!string.IsNullOrEmpty(suggestedPrimaryKey)) { Primary Key rilevata: @suggestedPrimaryKey (consigliato per l'identificazione univoca) }
@if (!string.IsNullOrEmpty(sourceKeyField)) {
Campo chiave selezionato: @sourceKeyField
Questo campo verrà utilizzato per identificare univocamente i record sorgente @if (sourceKeyField == suggestedPrimaryKey) {
Ottima scelta! Stai usando la Primary Key della tabella. }
} else {
Campo chiave richiesto
Seleziona un campo che identifichi univocamente ogni record per abilitare il sistema di associazioni. @if (!string.IsNullOrEmpty(suggestedPrimaryKey)) {
Consiglio: seleziona @suggestedPrimaryKey (Primary Key rilevata) }
}
} else {
Sistema associazioni disabilitato
Tutti i record verranno sempre inseriti come nuovi. Non sarà possibile tracciare aggiornamenti automatici.
}
}
@if (fieldMappings.Any()) { }
@if (fieldMappings.Any()) { @fieldMappings.Count mapping(s) configurati
@if (useRecordAssociations) { Modalità Smart Update @if (!string.IsNullOrEmpty(sourceKeyField)) { (Chiave: @sourceKeyField) } else { (Rilevamento automatico) } } else { Modalità Insert Only }
} else { Configura almeno una mappatura per iniziare }
@if (!string.IsNullOrEmpty(transferMessage)) { } @if (transferResults.Any()) {
Risultati Dettagliati Trasferimento (@transferResults.Count record)
Inseriti: @transferResults.Count(r => r.Status == "success")
Aggiornati: @transferResults.Count(r => r.Status == "updated")
Duplicati: @transferResults.Count(r => r.Status == "duplicate")
Errori: @transferResults.Count(r => r.Status == "error")
@foreach (var result in transferResults) { }
# Stato ID Entità Messaggio
@result.RecordNumber @GetResultStatusText(result.Status) @if (!string.IsNullOrEmpty(result.EntityId)) { @result.EntityId } else { - } @result.Message
}
}
@if (showDatabaseSelectionModal) { } @code { // Classe per i risultati del trasferimento public class TransferResult { public int RecordNumber { get; set; } public string Status { get; set; } = ""; // "success", "error", "updated", "duplicate" public string Message { get; set; } = ""; public string? EntityId { get; set; } public Dictionary RecordData { get; set; } = new(); } // Stato delle credenziali private List databaseCredentials = new(); private List restApiCredentials = new(); // Selezione tipo fonte private string selectedSourceType = ""; // Credenziali selezionate private string selectedDatabaseCredential = ""; private string selectedRestCredential = ""; // Stato connessioni private bool isConnectingDatabase = false; private bool isConnectingRest = false; private bool isDatabaseConnected = false; private bool isRestConnected = false; // Messaggi di errore private string databaseErrorMessage = ""; private string restErrorMessage = ""; // Database discovery private Dictionary> databaseTables = new(); private string selectedTable = ""; private string databaseSearchTerm = ""; // Database selection private List availableDatabases = new(); private string selectedDatabase = ""; private bool showDatabaseSelection = false; private bool showDatabaseSelectionModal = false; private bool isLoadingDatabases = false; // File handling private string selectedFileName = ""; private bool isProcessingFile = false; private string fileErrorMessage = ""; private Dictionary> fileSheets = new(); // SheetName -> Columns private Dictionary>> fileData = new(); // SheetName -> Data rows private string selectedSheet = ""; // File preview pagination private int currentPage = 1; private int pageSize = 20; private int GetTotalPages(string sheetName) => fileData.ContainsKey(sheetName) ? (int)Math.Ceiling((double)fileData[sheetName].Count / pageSize) : 0; // REST discovery private List restEntities = new(); private RestEntitySummary? selectedRestEntity = null; private RestEntityInfo? restEntityDetails = null; private string restSearchTerm = ""; // Mapping campi private Dictionary fieldMappings = new(); // DbColumn -> RestProperty private HashSet keyFields = new(); // REST properties marked as keys private string selectedDbColumn = ""; private string selectedRestProperty = ""; // Gestione chiavi sorgente e associazioni private string sourceKeyField = ""; // Campo che identifica univocamente il record sorgente private string suggestedPrimaryKey = ""; // Campo PK suggerito per database private bool requiresManualKeySelection = false; // Flag per indicare se è richiesta selezione manuale private Dictionary sourceKeyMappings = new(); // Per CSV: mapppatura colonna -> nome campo chiave private bool useRecordAssociations = true; // Se utilizzare il sistema di associazioni // Trasferimento dati private bool isTransferringData = false; private string transferMessage = ""; private string transferMessageType = ""; private List transferResults = new(); private bool showDetailedResults = false; // Servizi private IDatabaseManager? currentDatabaseManager = null; private IRestMetadataDiscovery? currentRestDiscovery = null; private IRestServiceClient? currentRestClient = null; protected override async Task OnInitializedAsync() { await LoadCredentials(); } private async Task LoadCredentials() { try { databaseCredentials = await CredentialService.GetAllDatabaseCredentialsAsync(); restApiCredentials = await CredentialService.GetAllRestApiCredentialsAsync(); } catch (Exception ex) { Logger.LogError(ex, "Errore nel caricamento delle credenziali"); await JSRuntime.InvokeVoidAsync("alert", $"Errore nel caricamento delle credenziali: {ex.Message}"); } } private void OnSourceTypeChanged(ChangeEventArgs e) { selectedSourceType = e.Value?.ToString() ?? ""; // Reset state when changing source type ResetSourceState(); } private void ResetSourceState() { // Reset database state ResetDatabaseState(); // Reset file state selectedFileName = ""; isProcessingFile = false; fileErrorMessage = ""; fileSheets.Clear(); fileData.Clear(); selectedSheet = ""; // Reset pagination currentPage = 1; // Reset mappings ClearAllMappings(); } private async Task OnFileSelected(InputFileChangeEventArgs e) { try { isProcessingFile = true; fileErrorMessage = ""; fileSheets.Clear(); fileData.Clear(); selectedSheet = ""; var file = e.File; selectedFileName = file.Name; // Validate file type var extension = Path.GetExtension(file.Name).ToLowerInvariant(); if (extension != ".xlsx" && extension != ".xls" && extension != ".csv") { fileErrorMessage = "Formato file non supportato. Utilizzare Excel (.xlsx, .xls) o CSV (.csv)"; return; } // Process file based on type if (extension == ".csv") { await ProcessCsvFile(file); } else { await ProcessExcelFile(file); } } catch (Exception ex) { Logger.LogError(ex, "Errore nell'elaborazione del file"); fileErrorMessage = $"Errore nell'elaborazione del file: {ex.Message}"; } finally { isProcessingFile = false; StateHasChanged(); } } private async Task ProcessCsvFile(IBrowserFile file) { using var stream = file.OpenReadStream(maxAllowedSize: 50 * 1024 * 1024); // Aumentato a 50MB using var reader = new StreamReader(stream); var firstLine = await reader.ReadLineAsync(); if (string.IsNullOrEmpty(firstLine)) { fileErrorMessage = "Il file CSV è vuoto"; return; } Logger.LogInformation("CSV first line: {FirstLine}", firstLine); // Detect separator automatically var separator = DetectCsvSeparator(firstLine); Logger.LogInformation("CSV separator detected: '{Separator}'", separator); // Parse headers (first row) - gestisce meglio i separatori var headers = ParseCsvLine(firstLine, separator); Logger.LogInformation("CSV headers parsed: {Headers}", string.Join(" | ", headers)); // For CSV, we create a single "sheet" with the filename var sheetName = Path.GetFileNameWithoutExtension(file.Name); fileSheets[sheetName] = headers; // Read data rows - rimuovo il limite di 1000 righe var dataRows = new List>(); string? line; int rowNumber = 2; // Starting from row 2 (after header) while ((line = await reader.ReadLineAsync()) != null) { if (string.IsNullOrWhiteSpace(line)) continue; var values = ParseCsvLine(line, separator); var row = new Dictionary(); for (int i = 0; i < headers.Count; i++) { var value = i < values.Count ? values[i] : ""; row[headers[i]] = string.IsNullOrEmpty(value) ? "" : value; } dataRows.Add(row); rowNumber++; // Log delle prime 3 righe per debug if (rowNumber <= 5) { Logger.LogInformation("CSV row {RowNumber}: {Values}", rowNumber - 1, string.Join(" | ", values)); } } fileData[sheetName] = dataRows; // Auto-seleziona il foglio per i CSV dato che ce n'è solo uno selectedSheet = sheetName; Logger.LogInformation("CSV file processed: {FileName}, Headers: {HeaderCount} ({Headers}), Rows: {RowCount}, Auto-selected sheet: {SheetName}", file.Name, headers.Count, string.Join(", ", headers), dataRows.Count, selectedSheet); } private List ParseCsvLine(string line, char separator = ',') { var result = new List(); var current = new StringBuilder(); bool inQuotes = false; for (int i = 0; i < line.Length; i++) { char c = line[i]; if (c == '"') { if (inQuotes && i + 1 < line.Length && line[i + 1] == '"') { // Double quote - escaped quote current.Append('"'); i++; // Skip next quote } else { // Toggle quote mode inQuotes = !inQuotes; } } else if (c == separator && !inQuotes) { // End of field result.Add(current.ToString().Trim()); current.Clear(); } else { current.Append(c); } } // Add the last field result.Add(current.ToString().Trim()); return result; }private async Task ProcessExcelFile(IBrowserFile file) { try { using var stream = file.OpenReadStream(maxAllowedSize: 50 * 1024 * 1024); // 50MB max // Crea il reader Excel basato sull'estensione IExcelDataReader reader; var extension = Path.GetExtension(file.Name).ToLowerInvariant(); if (extension == ".xlsx") { reader = ExcelReaderFactory.CreateOpenXmlReader(stream); } else if (extension == ".xls") { reader = ExcelReaderFactory.CreateBinaryReader(stream); } else { fileErrorMessage = "Formato Excel non supportato. Utilizzare .xlsx o .xls"; return; } using (reader) { // Configura per utilizzare la prima riga come header var configuration = new ExcelDataSetConfiguration() { ConfigureDataTable = (_) => new ExcelDataTableConfiguration() { UseHeaderRow = true // Prima riga come header } }; // Converti in DataSet var dataSet = reader.AsDataSet(configuration); Logger.LogInformation("Excel file processed: {FileName}, Sheets: {SheetCount}", file.Name, dataSet.Tables.Count); // Processa ogni foglio foreach (DataTable table in dataSet.Tables) { var sheetName = table.TableName; var headers = new List(); var dataRows = new List>(); // Estrai i nomi delle colonne (headers) foreach (DataColumn column in table.Columns) { headers.Add(column.ColumnName); } Logger.LogInformation("Processing Excel sheet: {SheetName}, Columns: {ColumnCount}, Rows: {RowCount}", sheetName, headers.Count, table.Rows.Count); // Processa le righe di dati for (int i = 0; i < table.Rows.Count; i++) { var row = table.Rows[i]; var rowData = new Dictionary(); for (int j = 0; j < headers.Count; j++) { var cellValue = row[j]?.ToString() ?? ""; rowData[headers[j]] = string.IsNullOrEmpty(cellValue) ? "" : cellValue; } dataRows.Add(rowData); // Log delle prime 3 righe per debug if (i < 3) { Logger.LogInformation("Excel row {RowNumber} in {Sheet}: {Values}", i + 1, sheetName, string.Join(" | ", rowData.Values)); } } // Salva i dati del foglio fileSheets[sheetName] = headers; fileData[sheetName] = dataRows; Logger.LogInformation("Excel sheet completed: {SheetName}, Headers: {Headers}, Rows: {RowCount}", sheetName, string.Join(", ", headers), dataRows.Count); } // Auto-seleziona il primo foglio se non c'è una selezione if (fileSheets.Any() && string.IsNullOrEmpty(selectedSheet)) { selectedSheet = fileSheets.First().Key; Logger.LogInformation("Auto-selected first sheet: {SheetName}", selectedSheet); } Logger.LogInformation("Excel file processing completed: {FileName}, Total sheets: {SheetCount}, Selected: {SelectedSheet}", file.Name, fileSheets.Count, selectedSheet); } } catch (Exception ex) { Logger.LogError(ex, "Errore nell'elaborazione del file Excel: {FileName}", file.Name); fileErrorMessage = $"Errore nell'elaborazione del file Excel: {ex.Message}"; } await Task.CompletedTask; } private void SelectSheet(string sheetName) { selectedSheet = sheetName; // Reset pagination when changing sheet currentPage = 1; // Clear mappings when changing sheet ClearAllMappings(); // For file sources, always require manual key selection sourceKeyField = ""; suggestedPrimaryKey = ""; requiresManualKeySelection = true; StateHasChanged(); } // File preview pagination methods private void GoToPage(int page) { if (string.IsNullOrEmpty(selectedSheet) || !fileData.ContainsKey(selectedSheet)) return; var totalPages = GetTotalPages(selectedSheet); if (page >= 1 && page <= totalPages) { currentPage = page; StateHasChanged(); } } private void FirstPage() => GoToPage(1); private void PreviousPage() => GoToPage(currentPage - 1); private void NextPage() => GoToPage(currentPage + 1); private void LastPage() => GoToPage(GetTotalPages(selectedSheet)); private List> GetCurrentPageData() { if (string.IsNullOrEmpty(selectedSheet) || !fileData.ContainsKey(selectedSheet)) return new List>(); var allData = fileData[selectedSheet]; var skip = (currentPage - 1) * pageSize; return allData.Skip(skip).Take(pageSize).ToList(); } private int GetStartRecord() { if (string.IsNullOrEmpty(selectedSheet) || !fileData.ContainsKey(selectedSheet)) return 0; return (currentPage - 1) * pageSize + 1; } private int GetEndRecord() { if (string.IsNullOrEmpty(selectedSheet) || !fileData.ContainsKey(selectedSheet)) return 0; var totalRecords = fileData[selectedSheet].Count; var endRecord = currentPage * pageSize; return Math.Min(endRecord, totalRecords); } private void OnPageSizeChanged(ChangeEventArgs e) { if (int.TryParse(e.Value?.ToString(), out int newPageSize)) { pageSize = newPageSize; currentPage = 1; // Reset to first page when changing page size StateHasChanged(); } }private void OnDatabaseCredentialChanged(ChangeEventArgs e) { selectedDatabaseCredential = e.Value?.ToString() ?? ""; ResetDatabaseState(); } private void OnRestCredentialChanged(ChangeEventArgs e) { var newCredential = e.Value?.ToString() ?? ""; // Clear the cache if we're switching to a different credential if (!string.IsNullOrEmpty(selectedRestCredential) && selectedRestCredential != newCredential) { ConnectionFactory.ClearRestClientCache(selectedRestCredential); Logger.LogInformation("Cleared REST client cache for credential: {CredentialName}", selectedRestCredential); } selectedRestCredential = newCredential; ResetRestState(); } private void ResetDatabaseState() { isDatabaseConnected = false; databaseTables.Clear(); selectedTable = ""; databaseSearchTerm = ""; databaseErrorMessage = ""; currentDatabaseManager?.Dispose(); currentDatabaseManager = null; // Clear mappings when resetting database state ClearAllMappings(); } private void ResetRestState() { isRestConnected = false; restEntities.Clear(); selectedRestEntity = null; restEntityDetails = null; restSearchTerm = ""; restErrorMessage = ""; currentRestDiscovery = null; currentRestClient = null; // Clear mappings when resetting REST state ClearAllMappings(); }private async Task ConnectToDatabase() { if (string.IsNullOrEmpty(selectedDatabaseCredential)) return; isConnectingDatabase = true; databaseErrorMessage = ""; try { // Trova la credenziale var credential = databaseCredentials.FirstOrDefault(c => c.Name == selectedDatabaseCredential); if (credential == null) { databaseErrorMessage = "Credenziale database non trovata"; return; } // Test della connessione var (success, message) = await CredentialService.TestDatabaseConnectionAsync(credential.Name); if (!success) { databaseErrorMessage = $"Connessione fallita: {message}"; return; } // Crea il database manager usando il factory con le credenziali complete Logger.LogInformation("Creando database manager per credenziale: {CredentialName}", selectedDatabaseCredential); currentDatabaseManager = await ConnectionFactory.CreateDatabaseManagerAsync(selectedDatabaseCredential); Logger.LogInformation("Database manager creato con successo"); Logger.LogInformation("Iniziando discovery dello schema per database {DatabaseType} con credenziale: {CredentialName}", credential.DatabaseType, selectedDatabaseCredential); // Discovery dello schema con try-catch specifico try { var schema = await currentDatabaseManager.GetDatabaseSchemaAsync(); Logger.LogInformation("Schema discovery completato. Tipo restituito: {SchemaType}, Numero elementi: {Count}", schema?.GetType().Name ?? "null", schema?.Count() ?? 0); databaseTables = schema as Dictionary> ?? (schema != null ? new Dictionary>(schema) : new Dictionary>()); Logger.LogInformation("Database tables dopo conversione: {Count} tabelle", databaseTables.Count); if (databaseTables.Count == 0) { // Se non ci sono tabelle, potrebbe essere perché non è stato selezionato un database specifico await HandleDatabaseSelectionRequired(); return; } } catch (Exception schemaEx) { Logger.LogError(schemaEx, "Errore specifico durante lo schema discovery"); databaseErrorMessage = $"Errore nello schema discovery: {schemaEx.Message}"; throw; } isDatabaseConnected = true; } catch (Exception ex) { Logger.LogError(ex, "Errore nella connessione al database"); databaseErrorMessage = $"Errore: {ex.Message}"; } finally { isConnectingDatabase = false; } } private async Task ConnectToRestApi() { if (string.IsNullOrEmpty(selectedRestCredential)) return; isConnectingRest = true; restErrorMessage = ""; try { // Trova la credenziale var credential = restApiCredentials.FirstOrDefault(c => c.Name == selectedRestCredential); if (credential == null) { restErrorMessage = "Credenziale REST API non trovata"; return; } // Test della connessione var (success, message) = await CredentialService.TestRestApiConnectionAsync(credential.Name); if (!success) { restErrorMessage = $"Connessione fallita: {message}"; return; } // Crea i client REST usando il factory con le credenziali complete currentRestClient = await ConnectionFactory.CreateRestServiceClientAsync(selectedRestCredential); currentRestDiscovery = await ConnectionFactory.CreateRestMetadataDiscoveryAsync(selectedRestCredential); Logger.LogInformation("Iniziando autenticazione per il servizio REST {ServiceType} con credenziale: {CredentialName}", credential.ServiceType, selectedRestCredential); // Autenticazione prima del discovery var authResult = await currentRestClient.AuthenticateAsync(); if (!authResult) { Logger.LogWarning("Autenticazione fallita per il servizio REST {ServiceType}", credential.ServiceType); restErrorMessage = "Autenticazione fallita per il servizio REST"; return; } Logger.LogInformation("Autenticazione completata. Iniziando discovery delle entità REST per {ServiceType}", credential.ServiceType); // Discovery delle entità disponibili restEntities = await currentRestDiscovery.DiscoverEntitySummariesAsync(); Logger.LogInformation("Discovery completato. Trovate {Count} entità", restEntities?.Count ?? 0); if (restEntities == null || !restEntities.Any()) { Logger.LogWarning("Nessuna entità trovata dal servizio REST"); restErrorMessage = "Nessuna entità disponibile dal servizio REST"; return; } isRestConnected = true; } catch (Exception ex) { Logger.LogError(ex, "Errore nella connessione al servizio REST"); restErrorMessage = $"Errore: {ex.Message}"; } finally { isConnectingRest = false; } } private async void SelectTable(string tableName) { selectedTable = tableName; // Clear mappings when changing table ClearAllMappings(); // Reset key field logic sourceKeyField = ""; suggestedPrimaryKey = ""; requiresManualKeySelection = false; // If it's a database source, try to detect the primary key if (selectedSourceType == "database" && currentDatabaseManager != null) { try { var primaryKey = await currentDatabaseManager.GetPrimaryKeyFieldAsync(tableName); if (!string.IsNullOrEmpty(primaryKey)) { suggestedPrimaryKey = primaryKey; // Suggest the primary key but don't auto-select it Logger.LogInformation("Primary key detected for table {TableName}: {PrimaryKey}", tableName, primaryKey); } else { // No primary key found, require manual selection requiresManualKeySelection = true; Logger.LogInformation("No primary key found for table {TableName}, manual selection required", tableName); } } catch (Exception ex) { Logger.LogError(ex, "Error detecting primary key for table {TableName}", tableName); requiresManualKeySelection = true; } } else { // For non-database sources, always require manual selection requiresManualKeySelection = true; } StateHasChanged(); } private async Task SelectRestEntity(RestEntitySummary entity) { selectedRestEntity = entity; // Clear mappings when changing entity ClearAllMappings(); try { if (currentRestDiscovery != null) { // Discovery dei dettagli dell'entità restEntityDetails = await currentRestDiscovery.DiscoverEntityDetailsAsync(entity.Name); } else { restErrorMessage = "Servizio di discovery REST non disponibile"; return; } } catch (Exception ex) { Logger.LogError(ex, "Errore nel caricamento dettagli entità {EntityName}", entity.Name); restErrorMessage = $"Errore nel caricamento dettagli entità: {ex.Message}"; } } // Metodi per la ricerca e il filtraggio private IEnumerable GetFilteredDatabaseTables() { if (string.IsNullOrEmpty(databaseSearchTerm)) return databaseTables.Keys; return databaseTables.Keys.Where(table => table.Contains(databaseSearchTerm, StringComparison.OrdinalIgnoreCase)); } private IEnumerable GetFilteredRestEntities() { if (string.IsNullOrEmpty(restSearchTerm)) return restEntities; return restEntities.Where(entity => entity.Name.Contains(restSearchTerm, StringComparison.OrdinalIgnoreCase) || (!string.IsNullOrEmpty(entity.Label) && entity.Label.Contains(restSearchTerm, StringComparison.OrdinalIgnoreCase))); } private async Task FilterDatabaseTables(ChangeEventArgs e) { databaseSearchTerm = e.Value?.ToString() ?? ""; await InvokeAsync(StateHasChanged); } private async Task FilterRestEntities(ChangeEventArgs e) { restSearchTerm = e.Value?.ToString() ?? ""; await InvokeAsync(StateHasChanged); } private async Task ClearDatabaseSearch() { databaseSearchTerm = ""; await InvokeAsync(StateHasChanged); } private async Task ClearRestSearch() { restSearchTerm = ""; await InvokeAsync(StateHasChanged); } // Metodi per il mapping dei campi private void SelectDbColumn(string columnName) { selectedDbColumn = columnName; } private void SelectRestProperty(string propertyName) { selectedRestProperty = propertyName; } private void CreateMapping() { if (string.IsNullOrEmpty(selectedDbColumn) || string.IsNullOrEmpty(selectedRestProperty)) return; // Rimuovi eventuali mapping esistenti per questo campo database if (fieldMappings.ContainsKey(selectedDbColumn)) { fieldMappings.Remove(selectedDbColumn); } // Crea il nuovo mapping fieldMappings[selectedDbColumn] = selectedRestProperty; Logger.LogInformation("Creato mapping: {DbColumn} -> {RestProperty}", selectedDbColumn, selectedRestProperty); // Deseleziona i campi selectedDbColumn = ""; selectedRestProperty = ""; } private void RemoveMapping() { if (string.IsNullOrEmpty(selectedDbColumn) || !fieldMappings.ContainsKey(selectedDbColumn)) return; fieldMappings.Remove(selectedDbColumn); Logger.LogInformation("Rimosso mapping per campo: {DbColumn}", selectedDbColumn); } private void RemoveSpecificMapping(string dbColumn) { if (fieldMappings.ContainsKey(dbColumn)) { fieldMappings.Remove(dbColumn); Logger.LogInformation("Rimosso mapping specifico per campo: {DbColumn}", dbColumn); } } private void ClearAllMappings() { fieldMappings.Clear(); selectedDbColumn = ""; selectedRestProperty = ""; sourceKeyField = ""; transferMessage = ""; transferMessageType = ""; Logger.LogInformation("Tutti i mapping e le configurazioni sono stati cancellati"); } private void AutoMapFields() { if (!databaseTables.ContainsKey(selectedTable) || restEntityDetails == null) return; var dbColumns = databaseTables[selectedTable]; var restProperties = restEntityDetails.Properties; int mappingsCreated = 0; foreach (var dbColumn in dbColumns) { // Trova una proprietà REST con nome simile var matchingProperty = restProperties.FirstOrDefault(p => string.Equals(p.Name, dbColumn.Name, StringComparison.OrdinalIgnoreCase) || string.Equals(p.Name.Replace("_", ""), dbColumn.Name.Replace("_", ""), StringComparison.OrdinalIgnoreCase) || string.Equals(p.Name.Replace("Id", ""), dbColumn.Name.Replace("Id", ""), StringComparison.OrdinalIgnoreCase) ); if (matchingProperty != null && !fieldMappings.ContainsKey(dbColumn.Name)) { fieldMappings[dbColumn.Name] = matchingProperty.Name; mappingsCreated++; } } Logger.LogInformation("Auto-mapping completato. Creati {Count} mapping automatici", mappingsCreated); } private async Task ShowMappingSummary() { var summary = "Riepilogo Configurazione:\n\n"; summary += "=== MAPPING CAMPI ===\n"; foreach (var mapping in fieldMappings) { summary += $"• {mapping.Key} → {mapping.Value}\n"; } summary += "\n=== CONFIGURAZIONE ASSOCIAZIONI ===\n"; summary += $"• Sistema associazioni: {(useRecordAssociations ? "Abilitato" : "Disabilitato")}\n"; if (useRecordAssociations) { summary += $"• Campo chiave sorgente: {(!string.IsNullOrEmpty(sourceKeyField) ? sourceKeyField : "Rilevamento automatico")}\n"; } await JSRuntime.InvokeVoidAsync("alert", summary); } private async Task StartDataTransfer() { if (!fieldMappings.Any() || currentRestClient == null || selectedRestEntity == null) { transferMessage = "Configurazione incompleta. Assicurati di aver selezionato la fonte dati, entità e configurato almeno una mappatura."; transferMessageType = "error"; return; } // Check source-specific requirements if (selectedSourceType == "database" && (currentDatabaseManager == null || string.IsNullOrEmpty(selectedTable))) { transferMessage = "Database non connesso o tabella non selezionata."; transferMessageType = "error"; return; } if (selectedSourceType == "file" && string.IsNullOrEmpty(selectedSheet)) { transferMessage = "File non caricato o foglio non selezionato."; transferMessageType = "error"; return; } // Validate source key field when using record associations if (useRecordAssociations && string.IsNullOrEmpty(sourceKeyField)) { transferMessage = "Campo chiave sorgente richiesto. Seleziona un campo che identifichi univocamente ogni record per utilizzare il sistema di associazioni."; transferMessageType = "error"; return; } isTransferringData = true; transferMessage = ""; transferMessageType = ""; transferResults.Clear(); try { var sourceName = selectedSourceType == "database" ? selectedTable : selectedSheet; Logger.LogInformation("Iniziando trasferimento dati da {SourceType} {Source} a {Entity} con {MappingCount} mappature", selectedSourceType, sourceName, selectedRestEntity.Name, fieldMappings.Count); // 1. Ottieni tutti i record dalla fonte dati var records = await GetAllRecordsFromSource(); Logger.LogInformation("Ottenuti {RecordCount} record da {SourceType} {Source}", records.Count(), selectedSourceType, sourceName); if (!records.Any()) { transferMessage = "Nessun record trovato nella fonte dati selezionata."; transferMessageType = "error"; return; } // 2. Ottieni i campi obbligatori dell'entità REST (se non ci sono campi chiave) var requiredFields = new HashSet(); if (!keyFields.Any() && restEntityDetails != null) { requiredFields = restEntityDetails.Properties .Where(p => p.IsRequired && fieldMappings.ContainsValue(p.Name)) .Select(p => p.Name) .ToHashSet(); Logger.LogInformation("Nessun campo chiave definito. Utilizzo {RequiredFieldsCount} campi obbligatori per controllo duplicati: {RequiredFields}", requiredFields.Count, string.Join(", ", requiredFields)); } // 3. Trasforma e trasferisci ogni record int successCount = 0; int errorCount = 0; int updatedCount = 0; int duplicateCount = 0; var errors = new List(); int recordNumber = 1; foreach (var record in records) { var transferResult = new TransferResult { RecordNumber = recordNumber, RecordData = new Dictionary(record) }; try { // Trasforma il record in base ai mapping var restData = TransformRecordToRestEntity(record); // Genera la chiave sorgente per questo record var sourceKey = GenerateSourceKey(record); var currentSourceName = selectedSourceType == "database" ? selectedTable : selectedSheet; // NUOVA LOGICA: Cerca associazione esistente if (useRecordAssociations && !string.IsNullOrEmpty(sourceKey)) { var existingAssociation = await CredentialService.FindRecordAssociationAsync( currentSourceName, sourceKey, selectedRestEntity.Name); if (existingAssociation != null && existingAssociation.IsActive) { // Prova ad aggiornare il record esistente var updateResult = await currentRestClient.UpdateEntityAsync( selectedRestEntity.Name, existingAssociation.DestinationId, restData); if (updateResult != null) { updatedCount++; transferResult.Status = "updated"; transferResult.Message = $"Record aggiornato con successo tramite associazione (ID: {existingAssociation.DestinationId})"; transferResult.EntityId = existingAssociation.DestinationId; // Aggiorna l'associazione con la data di ultimo aggiornamento existingAssociation.UpdatedAt = DateTime.UtcNow; await CredentialService.UpdateRecordAssociationAsync(existingAssociation); Logger.LogDebug("Record aggiornato tramite associazione: {EntityId} per chiave sorgente {SourceKey}", existingAssociation.DestinationId, sourceKey); } else { // Se l'aggiornamento fallisce, prova a creare un nuovo record Logger.LogWarning("Aggiornamento fallito per associazione {AssociationId}, provo a creare nuovo record", existingAssociation.Id); goto CreateNewRecord; } transferResults.Add(transferResult); recordNumber++; continue; } } CreateNewRecord: // Crea un nuovo record var result = await currentRestClient.CreateEntityAsync(selectedRestEntity.Name, restData); if (result != null) { successCount++; transferResult.Status = "success"; transferResult.Message = "Record inserito con successo"; transferResult.EntityId = result.ContainsKey("id") ? result["id"]?.ToString() : result.ContainsKey("Id") ? result["Id"]?.ToString() : null; // Crea associazione solo se abbiamo una chiave sorgente e un ID destinazione if (useRecordAssociations && !string.IsNullOrEmpty(sourceKey) && !string.IsNullOrEmpty(transferResult.EntityId)) { try { var association = new RecordAssociation { SourceName = currentSourceName, SourceType = selectedSourceType, SourceKey = sourceKey, DestinationEntity = selectedRestEntity.Name, DestinationId = transferResult.EntityId, RestCredentialName = selectedRestCredential, AdditionalInfo = System.Text.Json.JsonSerializer.Serialize(new { TransferDate = DateTime.UtcNow, RecordNumber = recordNumber, MappingCount = fieldMappings.Count }) }; await CredentialService.SaveRecordAssociationAsync(association); Logger.LogDebug("Associazione creata: {SourceKey} -> {DestinationId}", sourceKey, transferResult.EntityId); } catch (Exception assocEx) { Logger.LogWarning(assocEx, "Errore nella creazione dell'associazione per record {RecordNumber}", recordNumber); // Non interrompiamo il trasferimento per errori di associazione } } Logger.LogDebug("Record trasferito con successo: {Data}", string.Join(", ", restData.Select(kvp => $"{kvp.Key}={kvp.Value}"))); } else { errorCount++; transferResult.Status = "error"; transferResult.Message = "Errore nel trasferimento del record (result null)"; errors.Add($"Errore nel trasferimento del record {recordNumber}"); } } catch (Exception ex) { errorCount++; transferResult.Status = "error"; transferResult.Message = $"Errore: {ex.Message}"; errors.Add($"Errore nel trasferimento del record {recordNumber}: {ex.Message}"); Logger.LogError(ex, "Errore nel trasferimento del record {RecordNumber}", recordNumber); } transferResults.Add(transferResult); recordNumber++; } // 4. Mostra risultati if (errorCount == 0) { var message = $"Trasferimento completato con successo! "; var messageParts = new List(); if (successCount > 0) messageParts.Add($"{successCount} record inseriti"); if (updatedCount > 0) messageParts.Add($"{updatedCount} record aggiornati"); if (duplicateCount > 0) messageParts.Add($"{duplicateCount} duplicati rilevati (warning)"); message += string.Join(", ", messageParts) + "."; transferMessage = message; transferMessageType = "success"; } else { var message = $"Trasferimento completato con {(duplicateCount > 0 ? "warning e " : "")}errori. "; var messageParts = new List(); if (successCount > 0) messageParts.Add($"Inserimenti: {successCount}"); if (updatedCount > 0) messageParts.Add($"Aggiornamenti: {updatedCount}"); if (duplicateCount > 0) messageParts.Add($"Duplicati (warning): {duplicateCount}"); messageParts.Add($"Errori: {errorCount}"); message += string.Join(", ", messageParts); if (errors.Any()) { message += $". Primi errori: {string.Join("; ", errors.Take(3))}"; } transferMessage = message; transferMessageType = errorCount > 0 ? "error" : "warning"; } Logger.LogInformation("Trasferimento completato. Inserimenti: {SuccessCount}, Aggiornamenti: {UpdatedCount}, Duplicati: {DuplicateCount}, Errori: {ErrorCount}", successCount, updatedCount, duplicateCount, errorCount); } catch (Exception ex) { Logger.LogError(ex, "Errore generale nel trasferimento dati"); transferMessage = $"Errore nel trasferimento dati: {ex.Message}"; transferMessageType = "error"; } finally { isTransferringData = false; } } private async Task>> GetAllRecordsFromSource() { if (selectedSourceType == "database") { return await GetAllRecordsFromDatabase(); } else if (selectedSourceType == "file") { return await GetAllRecordsFromFile(); } return new List>(); } private async Task>> GetAllRecordsFromDatabase() { if (currentDatabaseManager == null || string.IsNullOrEmpty(selectedTable)) return new List>(); try { // Usa il database manager per eseguire una query che ottiene tutti i record // Questo è un esempio semplificato - potresti voler implementare paginazione per tabelle grandi return await currentDatabaseManager.GetAllRecordsAsync(selectedTable); } catch (Exception ex) { Logger.LogError(ex, "Errore nell'ottenere i record dalla tabella {Table}", selectedTable); throw; } } private async Task>> GetAllRecordsFromFile() { if (string.IsNullOrEmpty(selectedSheet) || !fileData.ContainsKey(selectedSheet)) { return new List>(); } await Task.CompletedTask; return fileData[selectedSheet]; } private Dictionary TransformRecordToRestEntity(Dictionary dbRecord) { var restData = new Dictionary(); foreach (var mapping in fieldMappings) { string dbColumn = mapping.Key; string restProperty = mapping.Value; if (dbRecord.ContainsKey(dbColumn)) { var value = dbRecord[dbColumn]; // Trasforma il valore se necessario (es. date format, null handling, etc.) var transformedValue = TransformValue(value, dbColumn, restProperty); if (transformedValue != null) { restData[restProperty] = transformedValue; } } } Logger.LogDebug("Record trasformato: {DbColumns} → {RestProperties}", string.Join(", ", dbRecord.Keys), string.Join(", ", restData.Keys)); return restData; } private object? TransformValue(object? value, string dbColumn, string restProperty) { if (value == null || value == DBNull.Value) return null; // Ottieni informazioni sui tipi per fare trasformazioni intelligenti var dbColumnInfo = databaseTables.ContainsKey(selectedTable) ? databaseTables[selectedTable].FirstOrDefault(c => c.Name == dbColumn) : null; var restPropertyInfo = restEntityDetails?.Properties.FirstOrDefault(p => p.Name == restProperty); // Trasformazioni specifiche per tipo if (restPropertyInfo != null) { switch (restPropertyInfo.Type.ToLower()) { case "edm.string": return value.ToString(); case "edm.int32": case "edm.int64": if (int.TryParse(value.ToString(), out int intVal)) return intVal; break; case "edm.decimal": case "edm.double": if (decimal.TryParse(value.ToString(), out decimal decVal)) return decVal; break; case "edm.boolean": if (bool.TryParse(value.ToString(), out bool boolVal)) return boolVal; // Gestisci anche valori numerici (0/1) come boolean if (value.ToString() == "1") return true; if (value.ToString() == "0") return false; break; case "edm.datetime": case "edm.datetimeoffset": if (DateTime.TryParse(value.ToString(), out DateTime dateVal)) return dateVal.ToString("yyyy-MM-ddTHH:mm:ss.fffZ"); break; } } // Fallback: restituisci il valore convertito a stringa return value.ToString(); } private string GetPropertyPlaceholder(RestPropertyInfo property) { return property.Type switch { "Edm.String" => $"Inserisci {property.Name}" + (property.MaxLength.HasValue ? $" (max {property.MaxLength})" : ""), "Edm.Int32" => "Numero intero", "Edm.Decimal" => "Numero decimale", "Edm.DateTime" => "Data/Ora (YYYY-MM-DD)", "Edm.Boolean" => "true/false", _ => $"Valore per {property.Name}" }; } public void Dispose() { currentDatabaseManager?.Dispose(); } private char DetectCsvSeparator(string line) { // Common separators to check var separators = new[] { ',', ';', '\t', '|' }; var counts = new Dictionary(); bool inQuotes = false; // Count separators outside of quotes foreach (char c in line) { if (c == '"') { inQuotes = !inQuotes; } else if (!inQuotes && separators.Contains(c)) { counts[c] = counts.GetValueOrDefault(c, 0) + 1; } } // Return the separator with the highest count, default to comma if (counts.Any()) { var mostCommon = counts.OrderByDescending(x => x.Value).First(); // Make sure we have at least one occurrence to avoid single-column files if (mostCommon.Value > 0) { return mostCommon.Key; } } return ','; // Default fallback } /// /// Verifica se il pulsante di trasferimento può essere abilitato /// private bool IsTransferButtonEnabled() { // Base requirements if (!fieldMappings.Any()) return false; // Se il sistema di associazioni è abilitato, il campo chiave sorgente è obbligatorio if (useRecordAssociations && string.IsNullOrEmpty(sourceKeyField)) return false; return true; } // Helper methods per UI risultati private string GetResultRowClass(string status) { return status switch { "success" => "", "updated" => "table-info", "duplicate" => "table-warning", "error" => "table-danger", _ => "" }; } private string GetResultBadgeClass(string status) { return status switch { "success" => "bg-success", "updated" => "bg-info", "duplicate" => "bg-warning text-dark", "error" => "bg-danger", _ => "bg-secondary" }; } private string GetResultIcon(string status) { return status switch { "success" => "fa-check-circle", "updated" => "fa-edit", "duplicate" => "fa-exclamation-triangle", "error" => "fa-times-circle", _ => "fa-question-circle" }; } private string GetResultStatusText(string status) { return status switch { "success" => "Inserito", "updated" => "Aggiornato", "duplicate" => "Duplicato", "error" => "Errore", _ => "Sconosciuto" }; } /// /// Genera una chiave univoca per il record sorgente /// private string GenerateSourceKey(Dictionary record) { try { // Il campo chiave sorgente deve essere sempre specificato if (string.IsNullOrEmpty(sourceKeyField)) { throw new InvalidOperationException("Campo chiave sorgente non specificato. La selezione del campo chiave è obbligatoria."); } if (!record.ContainsKey(sourceKeyField)) { throw new InvalidOperationException($"Il campo chiave '{sourceKeyField}' non è presente nel record sorgente."); } var keyValue = record[sourceKeyField]?.ToString(); if (string.IsNullOrEmpty(keyValue)) { throw new InvalidOperationException($"Il valore del campo chiave '{sourceKeyField}' è vuoto o null per questo record."); } return keyValue; } catch (Exception ex) { Logger.LogError(ex, "Errore nella generazione della chiave sorgente per il campo {SourceKeyField}", sourceKeyField); throw; } } private async Task HandleDatabaseSelectionRequired() { try { if (currentDatabaseManager == null) { databaseErrorMessage = "Database manager non inizializzato"; return; } // Ottieni la lista dei database disponibili availableDatabases = await currentDatabaseManager.GetAvailableDatabasesAsync(); if (availableDatabases != null && availableDatabases.Any()) { // Mostra il modal per la selezione del database showDatabaseSelectionModal = true; StateHasChanged(); } else { databaseErrorMessage = "Nessun database disponibile per la selezione"; } } catch (Exception ex) { Logger.LogError(ex, "Errore nell'ottenere la lista dei database disponibili"); databaseErrorMessage = $"Errore nel recupero dei database: {ex.Message}"; } } private async Task OnDatabaseSelected() { if (string.IsNullOrEmpty(selectedDatabase)) { return; } if (currentDatabaseManager == null) { databaseErrorMessage = "Database manager non inizializzato"; return; } try { // Cambia il database attivo await currentDatabaseManager.ChangeDatabaseAsync(selectedDatabase); // Nasconde il modal showDatabaseSelectionModal = false; // Ritenta il discovery dello schema var schema = await currentDatabaseManager.GetDatabaseSchemaAsync(); databaseTables = schema as Dictionary> ?? (schema != null ? new Dictionary>(schema) : new Dictionary>()); if (databaseTables.Count == 0) { databaseErrorMessage = $"Il database '{selectedDatabase}' non contiene tabelle accessibili"; } else { isDatabaseConnected = true; databaseErrorMessage = ""; } } catch (Exception ex) { Logger.LogError(ex, "Errore nel cambio di database a {Database}", selectedDatabase); databaseErrorMessage = $"Errore nel cambio di database: {ex.Message}"; } finally { StateHasChanged(); } } private void CancelDatabaseSelection() { showDatabaseSelectionModal = false; selectedDatabase = ""; StateHasChanged(); } }