using System; using System.ComponentModel; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; using Microsoft.JSInterop; using Microsoft.Extensions.Logging; using CredentialManager.Models; using DataConnection.REST.Interfaces; using DataConnection.REST.Models; using DataConnection.CredentialManagement.Interfaces; using DataConnection.Interfaces; using Data_Coupler.Services; using Data_Coupler.Models; namespace Data_Coupler.Pages; public partial class DataCoupler : ComponentBase { // ===== PROPRIETÀ REST ===== // Credenziali REST protected List restApiCredentials = new(); protected string selectedRestCredential = ""; // Stato connessioni REST protected bool isConnectingRest = false; protected bool isRestConnected = false; protected string restErrorMessage = ""; // REST discovery protected List restEntities = new(); protected RestEntitySummary? selectedRestEntity = null; protected RestEntityInfo? restEntityDetails = null; protected string restSearchTerm = ""; // Proprietà di mapping REST protected string selectedRestProperty = ""; // Servizi REST protected IRestMetadataDiscovery? currentRestDiscovery = null; protected IRestServiceClient? currentRestClient = null; // ===== METODI REST ===== /// /// Carica le credenziali REST API /// protected async Task LoadRestCredentials() { try { restApiCredentials = await CredentialService.GetAllRestApiCredentialsAsync(); } catch (Exception ex) { Logger.LogError(ex, "Errore nel caricamento delle credenziali REST"); throw; } } /// /// Gestisce il cambio di credenziale REST /// 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(); } /// /// Resetta lo stato REST /// protected void ResetRestState() { isRestConnected = false; restEntities.Clear(); selectedRestEntity = null; restEntityDetails = null; restSearchTerm = ""; restErrorMessage = ""; currentRestDiscovery = null; currentRestClient = null; // Clear mappings when resetting REST state - handled by main class // ClearAllMappings(); } /// /// Connette al servizio REST API /// protected 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 con successo per il servizio REST {ServiceType}", credential.ServiceType); // Discovery delle entità disponibili usando il metodo batch ottimizzato Logger.LogInformation("Iniziando discovery batch delle entità REST..."); restEntities = await currentRestDiscovery.DiscoverEntitySummariesAsync(); isRestConnected = true; Logger.LogInformation("Discovery batch completato: trovate {EntityCount} entità REST", restEntities.Count); // Carica anche i dettagli completi delle entità per External ID Relationships try { Logger.LogInformation("Caricamento dettagli entità per External ID Relationships..."); availableRelationshipObjects = await currentRestDiscovery.DiscoverEntitiesAsync(); Logger.LogInformation("Caricati {Count} oggetti disponibili per External ID Relationships", availableRelationshipObjects.Count); } catch (Exception ex) { Logger.LogWarning(ex, "Impossibile caricare i dettagli delle entità per External ID Relationships"); availableRelationshipObjects = new List(); } } catch (Exception ex) { Logger.LogError(ex, "Errore nella connessione al servizio REST {ServiceType}", selectedRestCredential); restErrorMessage = $"Errore nella connessione: {ex.Message}"; } finally { isConnectingRest = false; StateHasChanged(); } } /// /// Seleziona un'entità REST /// protected async Task SelectRestEntity(RestEntitySummary entity) { selectedRestEntity = entity; // Clear mappings when changing entity - handled by main class // 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}"; } } /// /// Filtra le entità REST in base al termine di ricerca /// 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))); } /// /// Applica il filtro alle entità REST /// private async Task FilterRestEntities(ChangeEventArgs e) { restSearchTerm = e.Value?.ToString() ?? ""; await InvokeAsync(StateHasChanged); } /// /// Pulisce la ricerca delle entità REST /// private async Task ClearRestSearch() { restSearchTerm = ""; await InvokeAsync(StateHasChanged); } /// /// Seleziona una proprietà REST per il mapping /// protected void SelectRestProperty(string propertyName) { selectedRestProperty = propertyName; } }