From 61c7fed9b37e439525337f5f62f6febacd4aef99 Mon Sep 17 00:00:00 2001 From: Alessio Dal Santo Date: Mon, 16 Jun 2025 17:45:27 +0200 Subject: [PATCH] =?UTF-8?q?-Rimozione=20dei=20file=20non=20pi=C3=B9=20nece?= =?UTF-8?q?ssari?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Data/DatabaseConnectionService.cs | 150 ---------- Data_Coupler/Pages/DatabaseConnection.razor | 269 ------------------ Data_Coupler/Pages/DatabaseSchema.razor | 266 ----------------- Data_Coupler/Pages/RestDiscovery.razor | 196 ------------- 4 files changed, 881 deletions(-) delete mode 100644 Data_Coupler/Data/DatabaseConnectionService.cs delete mode 100644 Data_Coupler/Pages/DatabaseConnection.razor delete mode 100644 Data_Coupler/Pages/DatabaseSchema.razor delete mode 100644 Data_Coupler/Pages/RestDiscovery.razor diff --git a/Data_Coupler/Data/DatabaseConnectionService.cs b/Data_Coupler/Data/DatabaseConnectionService.cs deleted file mode 100644 index 4552fd4..0000000 --- a/Data_Coupler/Data/DatabaseConnectionService.cs +++ /dev/null @@ -1,150 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using DataConnection.Interfaces; -using DataConnection.EF; -using DataConnection.EF.DatabaseDiscovery; -using DataConnection.Enums; -using Microsoft.EntityFrameworkCore; - -namespace Data_Coupler.Data -{ - public class DatabaseConnectionService - { - private readonly IDatabaseDiscovery _sqlServerDiscovery; - private readonly IDatabaseDiscovery _mySqlDiscovery; - private readonly DbManagerOptions _dbManagerOptions; - private IDatabaseManager _databaseManager; - - public DatabaseType SelectedDatabaseType { get; set; } = DatabaseType.SqlServer; - public string ConnectionString { get; set; } - public List AvailableDatabases { get; private set; } = new List(); - public Dictionary DatabasesInfo { get; private set; } = new Dictionary(); - public string SelectedDatabase { get; set; } - public bool IsConnected { get; private set; } - public string ErrorMessage { get; private set; } - - public DatabaseConnectionService(DbManagerOptions dbManagerOptions) - { - _sqlServerDiscovery = new SqlServerDatabaseDiscovery(); - _dbManagerOptions = dbManagerOptions ?? new DbManagerOptions(); - // Se in futuro verrà implementata la discovery MySQL, potremmo aggiungerla qui - // _mySqlDiscovery = new MySqlDatabaseDiscovery(); - } - - public IDatabaseDiscovery GetDatabaseDiscovery() - { - return SelectedDatabaseType switch - { - DatabaseType.SqlServer => _sqlServerDiscovery, - // DatabaseType.MySql => _mySqlDiscovery, - _ => throw new NotSupportedException($"Tipo di database non supportato: {SelectedDatabaseType}") - }; - } - - public async Task TestConnectionAsync() - { - try - { - var discovery = GetDatabaseDiscovery(); - AvailableDatabases = await discovery.GetAvailableDatabasesAsync(ConnectionString, true); - IsConnected = AvailableDatabases.Any(); - ErrorMessage = IsConnected ? null : "Nessun database trovato sul server."; - return IsConnected; - } - catch (Exception ex) - { - IsConnected = false; - ErrorMessage = $"Errore di connessione: {ex.Message}"; - if (ex.InnerException != null) - { - ErrorMessage += $" - {ex.InnerException.Message}"; - } - return false; - } - } - - public async Task> GetDatabasesInfoAsync() - { - try - { - var discovery = GetDatabaseDiscovery(); - DatabasesInfo = await discovery.GetDatabasesInfoAsync(ConnectionString, false); - return DatabasesInfo; - } - catch (Exception ex) - { - ErrorMessage = $"Errore nel recupero delle informazioni: {ex.Message}"; - return new Dictionary(); - } - } - - public string BuildConnectionStringWithDatabase() - { - if (string.IsNullOrEmpty(SelectedDatabase)) - return ConnectionString; - - // Per SQL Server - if (SelectedDatabaseType == DatabaseType.SqlServer) - { - if (ConnectionString.Contains("Initial Catalog=") || ConnectionString.Contains("Database=")) - { - // Sostituisci il database esistente - var modifiedString = System.Text.RegularExpressions.Regex.Replace( - ConnectionString, - @"(Initial Catalog|Database)=([^;]*)", - $"$1={SelectedDatabase}"); - - return modifiedString; - } - else - { - // Aggiungi il database - return ConnectionString + $";Database={SelectedDatabase}"; - } - } - - // Per altri tipi di database, implementare la logica appropriata - return ConnectionString; - } - - public IDatabaseManager GetDatabaseManager() - { - if (_databaseManager != null) - return _databaseManager; - - // Configura le opzioni del database manager - _dbManagerOptions.ServerConnectionString = ConnectionString; - _dbManagerOptions.DatabaseName = SelectedDatabase; - - // Configura il contesto in base al tipo di database - _dbManagerOptions.DbContextConfigurator = options => - { - switch (SelectedDatabaseType) - { - case DatabaseType.SqlServer: - options.UseSqlServer(BuildConnectionStringWithDatabase(), - sqlOptions => sqlOptions.CommandTimeout(_dbManagerOptions.CommandTimeout)); - break; - // Aggiungi altri tipi di database quando implementati - default: - throw new NotSupportedException($"Tipo di database non supportato: {SelectedDatabaseType}"); - } - }; - - // Crea il database manager - _databaseManager = new EFCoreDatabaseManager(_dbManagerOptions); - return _databaseManager; - } - - public void DisposeDatabaseManager() - { - if (_databaseManager != null) - { - _databaseManager.Dispose(); - _databaseManager = null; - } - } - } -} \ No newline at end of file diff --git a/Data_Coupler/Pages/DatabaseConnection.razor b/Data_Coupler/Pages/DatabaseConnection.razor deleted file mode 100644 index d87ac59..0000000 --- a/Data_Coupler/Pages/DatabaseConnection.razor +++ /dev/null @@ -1,269 +0,0 @@ -@page "/database-connection" -@using DataConnection.Enums -@using DataConnection.Interfaces -@inject Data.DatabaseConnectionService DatabaseService -@inject IJSRuntime JSRuntime - -Connessione al Database - -

Connessione al Database

- -
-
-
-
- Configurazione connessione -
-
-
- - -
- -
- - -
- -
- -
- - @if (DatabaseService.ErrorMessage != null) - { -
- @DatabaseService.ErrorMessage -
- } -
-
-
- - @if (DatabaseService.IsConnected) - { -
-
-
- Database Disponibili -
-
-
- - -
- - @if (!string.IsNullOrEmpty(DatabaseService.SelectedDatabase) && databaseInfo != null) - { -
-
Dettagli Database
- - - - - - - - - - - - - - - - - - - - - - - -
Nome@databaseInfo?.Name
Dimensione@databaseInfo?.SizeMB MB
Data Creazione@databaseInfo?.CreationDate.ToString("dd/MM/yyyy HH:mm")
Stato@databaseInfo?.Status
Proprietario@databaseInfo?.Owner
- -
- -
-
- } -
-
-
- } -
- -@if (showSchemaInfo) -{ -
-
-
-
- Schema del Database: @DatabaseService.SelectedDatabase -
-
- @if (schemaInfo != null && schemaInfo.Count > 0) - { -
- - - - - - - - - @foreach (var table in schemaInfo) - { - - - - - } - -
TabellaColonne
@table.Key -
    - @foreach (var column in table.Value) - { -
  • - @column.Name - (@column.DataType) - @if (column.IsPrimaryKey) - { - PK - } - @if (column.IsForeignKey) - { - FK → @column.ReferencedTable - } -
  • - } -
-
-
- } - else - { -
- Nessuna informazione sullo schema disponibile. -
- } -
-
-
-
-} - -@code { - private bool isLoading = false; - private bool isConnecting = false; - private bool showSchemaInfo = false; - private DatabaseInfo databaseInfo; - private IDictionary> schemaInfo; - - protected override async Task OnInitializedAsync() - { - // Puoi impostare valori predefiniti, magari da configurazione - DatabaseService.ConnectionString = "Server=localhost;User Id=sa;Password=password;TrustServerCertificate=True"; - } - - private async Task TestConnection() - { - try - { - isLoading = true; - showSchemaInfo = false; - databaseInfo = null; - - await DatabaseService.TestConnectionAsync(); - - if (DatabaseService.IsConnected) - { - await DatabaseService.GetDatabasesInfoAsync(); - } - } - catch (Exception ex) - { - await JSRuntime.InvokeVoidAsync("console.error", "Errore connessione:", ex.Message); - } - finally - { - isLoading = false; - } - } - - private async Task ConnectToDatabase() - { - if (string.IsNullOrEmpty(DatabaseService.SelectedDatabase)) - return; - - try - { - isConnecting = true; - showSchemaInfo = false; - - // Ottieni informazioni sul database selezionato - if (DatabaseService.DatabasesInfo.TryGetValue(DatabaseService.SelectedDatabase, out var dbInfo)) - { - databaseInfo = dbInfo; - } - - // Crea il database manager e connettiti al database selezionato - var databaseManager = DatabaseService.GetDatabaseManager(); - - // Verifica la connessione al database specifico - if (!await databaseManager.TestConnectionAsync()) - { - throw new Exception($"Non è possibile connettersi al database {DatabaseService.SelectedDatabase}"); - } - - // Recupera le informazioni sullo schema (tabelle e colonne) - schemaInfo = await databaseManager.GetDatabaseSchemaAsync(); - - showSchemaInfo = true; - } - catch (Exception ex) - { - await JSRuntime.InvokeVoidAsync("console.error", "Errore connessione database:", ex.Message); - //DatabaseService.ErrorMessage = $"Errore nella connessione al database: {ex.Message}"; - showSchemaInfo = false; - } - finally - { - isConnecting = false; - } - } -} \ No newline at end of file diff --git a/Data_Coupler/Pages/DatabaseSchema.razor b/Data_Coupler/Pages/DatabaseSchema.razor deleted file mode 100644 index 8359cfd..0000000 --- a/Data_Coupler/Pages/DatabaseSchema.razor +++ /dev/null @@ -1,266 +0,0 @@ -@page "/database-schema" -@using DataConnection.Interfaces -@using DataConnection.Enums -@inject Data.DatabaseConnectionService DatabaseService -@inject IJSRuntime JSRuntime -@inject NavigationManager NavigationManager - -Schema del Database - -

Schema del Database

- -@if (!DatabaseService.IsConnected || string.IsNullOrEmpty(DatabaseService.SelectedDatabase)) -{ -
-

Non sei connesso a nessun database. Devi prima stabilire una connessione.

- -
-} -else -{ -
-
-
-
- Dettagli del Database: @DatabaseService.SelectedDatabase - -
-
- @if (databaseInfo != null) - { -
-
- Nome: @databaseInfo.Name -
-
- Dimensione: @databaseInfo.SizeMB.ToString("N2") MB -
-
- Creato il: @databaseInfo.CreationDate.ToString("dd/MM/yyyy HH:mm") -
-
- Stato: @databaseInfo.Status -
-
- } -
-
-
-
- -
-
-
-
- Tabelle -
-
-
- - - @if (schemaInfo != null && schemaInfo.Count > 0) - { - @foreach (var table in filteredTables) - { - - } - } - else if (isLoading) - { -
-
- Caricamento... -
-
- } - else - { -
- Nessuna tabella disponibile -
- } -
-
-
-
- -
-
-
- @if (!string.IsNullOrEmpty(selectedTable)) - { - Struttura della tabella: @selectedTable - } - else - { - Seleziona una tabella - } -
-
- @if (!string.IsNullOrEmpty(selectedTable) && schemaInfo != null && schemaInfo.ContainsKey(selectedTable)) - { -
- - - - - - - - - - - - @foreach (var column in schemaInfo[selectedTable]) - { - - - - - - - - } - -
Nome ColonnaTipo DatiNullableChiaveRelazioni
@column.Name@column.DataType - @if (column.IsNullable) - { - NULL - } - else - { - NOT NULL - } - - @if (column.IsPrimaryKey) - { - PK - } - - @if (column.IsForeignKey) - { - - FK → @column.ReferencedTable.@column.ReferencedColumn - - } -
-
- } - else if (string.IsNullOrEmpty(selectedTable)) - { -
- Seleziona una tabella dal menu a sinistra per visualizzarne la struttura. -
- } - else if (isLoading) - { -
-
- Caricamento... -
-
- } - else - { -
- Impossibile trovare informazioni sulla tabella selezionata. -
- } -
-
-
-
-} - -@code { - private bool isLoading = false; - private string selectedTable = ""; - private string tableFilter = ""; - private DatabaseInfo databaseInfo; - private IDictionary> schemaInfo; - private IDictionary> filteredTables; - - protected override async Task OnInitializedAsync() - { - if (DatabaseService.IsConnected && !string.IsNullOrEmpty(DatabaseService.SelectedDatabase)) - { - await LoadDatabaseSchema(); - } - } - - private async Task LoadDatabaseSchema() - { - try - { - isLoading = true; - selectedTable = ""; - - // Ottieni informazioni sul database selezionato - if (DatabaseService.DatabasesInfo.TryGetValue(DatabaseService.SelectedDatabase, out var dbInfo)) - { - databaseInfo = dbInfo; - } - - // Crea il database manager e connettiti al database selezionato - var databaseManager = DatabaseService.GetDatabaseManager(); - - // Verifica la connessione al database specifico - if (await databaseManager.TestConnectionAsync()) - { - // Recupera le informazioni sullo schema (tabelle e colonne) - schemaInfo = await databaseManager.GetDatabaseSchemaAsync(); - filteredTables = schemaInfo; - } - else - { - throw new Exception($"Non è possibile connettersi al database {DatabaseService.SelectedDatabase}"); - } - } - catch (Exception ex) - { - await JSRuntime.InvokeVoidAsync("console.error", "Errore caricamento schema:", ex.Message); - //DatabaseService.ErrorMessage = $"Errore nel caricamento dello schema: {ex.Message}"; - schemaInfo = null; - } - finally - { - isLoading = false; - } - } - - private void SelectTable(string tableName) - { - selectedTable = tableName; - } - - private void FilterTables(ChangeEventArgs e) - { - tableFilter = e.Value?.ToString() ?? ""; - - if (string.IsNullOrWhiteSpace(tableFilter)) - { - filteredTables = schemaInfo; - } - else - { - filteredTables = schemaInfo - .Where(t => t.Key.Contains(tableFilter, StringComparison.OrdinalIgnoreCase)) - .ToDictionary(t => t.Key, t => t.Value); - } - } - - private async Task RefreshSchema() - { - await LoadDatabaseSchema(); - } - - private void NavigateToConnection() - { - NavigationManager.NavigateTo("/database-connection"); - } -} \ No newline at end of file diff --git a/Data_Coupler/Pages/RestDiscovery.razor b/Data_Coupler/Pages/RestDiscovery.razor deleted file mode 100644 index db5fd78..0000000 --- a/Data_Coupler/Pages/RestDiscovery.razor +++ /dev/null @@ -1,196 +0,0 @@ -@page "/rest-discovery" -@using DataConnection.REST.Configuration -@using DataConnection.REST.Implementations -@using DataConnection.REST.Models -@using System.Net.Http -@inject IHttpClientFactory HttpClientFactory - -

REST Service Discovery

- -
- - -
- -@if (SelectedServiceType == "SAPB1") -{ -
-
SAP Business One Connection Details
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
- -
-
-} - -@if (!string.IsNullOrEmpty(StatusMessage)) -{ - -} - -@if (DiscoveredEntities.Any()) -{ -

Discovered Entities

-
-
    - @foreach (var entity in DiscoveredEntities.OrderBy(e => e.Name)) - { -
  • -
    @entity.Name
    -
      - @foreach (var prop in entity.Properties.OrderBy(p => p.Name)) - { -
    • - @prop.Name (@prop.Type) @(prop.IsKey ? "[Key]" : "") -
    • - } -
    -
  • - } -
-
-} - -@code { - private string SelectedServiceType { get; set; } = ""; - private SapB1ConnectionOptions SapB1Options { get; set; } = new SapB1ConnectionOptions(); - private List DiscoveredEntities { get; set; } = new List(); - private bool IsLoading { get; set; } = false; - private string? StatusMessage { get; set; } - private string StatusMessageClass => !string.IsNullOrEmpty(StatusMessage) && StatusMessage.StartsWith("Error") ? "alert-danger" : "alert-success"; - - // Helper class to hold SAP B1 specific inputs - private class SapB1ConnectionOptions - { - public string? BaseUrl { get; set; } - public string? CompanyDb { get; set; } - public string? Username { get; set; } - public string? Password { get; set; } - public bool IgnoreSslErrors { get; set; } - } - - private async Task HandleConnectClick() - { - IsLoading = true; - StatusMessage = null; - DiscoveredEntities.Clear(); - StateHasChanged(); // Update UI to show loading - - if (SelectedServiceType == "SAPB1") - { - if (string.IsNullOrWhiteSpace(SapB1Options.BaseUrl) || - string.IsNullOrWhiteSpace(SapB1Options.CompanyDb) || - string.IsNullOrWhiteSpace(SapB1Options.Username) || - string.IsNullOrWhiteSpace(SapB1Options.Password)) - { - StatusMessage = "Error: Please fill in all SAP Business One connection details."; - IsLoading = false; - StateHasChanged(); - return; - } - - try - { - var restOptions = new RestServiceOptions - { - BaseUrl = SapB1Options.BaseUrl, - IgnoreSslErrors = SapB1Options.IgnoreSslErrors - // Username/Password are not set here as SapB1ServiceClient handles them in LoginAsync - }; - - // SapB1ServiceClient manages its own HttpClient internally for cookie handling - // We don't inject HttpClient directly into it via constructor in this setup. - // The BaseRestServiceClient constructor receives an HttpClient, but SapB1ServiceClient - // creates its own specific one in CreateConfiguredHttpClient. - // We pass the factory-created client to the base, but the SapB1 specific logic uses its own. - // This seems slightly complex, might need refactoring later, but follows current implementation. - - // Let's simplify: Create the client directly here for now. - var client = new SapB1ServiceClient(restOptions); - - StatusMessage = "Attempting login..."; - StateHasChanged(); - - bool loggedIn = await client.LoginAsync(SapB1Options.CompanyDb, SapB1Options.Username, SapB1Options.Password); - - if (loggedIn) - { - StatusMessage = "Login successful. Discovering entities..."; - StateHasChanged(); - - DiscoveredEntities = await client.DiscoverEntitiesAsync(); - - if (DiscoveredEntities.Any()) - { - StatusMessage = $"Discovery complete. Found {DiscoveredEntities.Count} entities."; - } - else - { - StatusMessage = "Login successful, but failed to discover entities or no entities found."; - } - - // Optional: Logout after discovery if desired - // await client.LogoutAsync(); - } - else - { - StatusMessage = "Error: SAP B1 Login failed. Check credentials and Service Layer status."; - } - } - catch (Exception ex) - { - // Log the full exception details somewhere appropriate - Console.WriteLine($"Error during SAP B1 connection/discovery: {ex}"); - StatusMessage = $"Error: An exception occurred: {ex.Message}"; - } - finally - { - IsLoading = false; - StateHasChanged(); // Update UI after completion/error - } - } - else - { - StatusMessage = "Error: Please select a service type."; - IsLoading = false; - StateHasChanged(); - } - } -}