[Feature] Implementato sistema di valori default per campi mapping
- Creato modello FieldMappingEntry per gestione unificata di field mapping e default values - Aggiunta colonna DefaultValuesJson alla tabella DataCouplerProfile (max 4000 caratteri) - Implementata UI con toggle per selezionare modalità Mapping o Default - Supporto per 9 tipi di dati: string, int, long, decimal, double, float, boolean, datetime, datetimeoffset - Aggiornata logica TransformRecordToRestEntity per applicare valori default dopo field mapping - Implementata serializzazione/deserializzazione DefaultValues in DataCouplerProfileService - Sistema completo di salvataggio/caricamento valori default nei profili - Migrazione database AddDefaultValuesJsonToProfile creata e applicata
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace CredentialManager.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Tipo di mapping field
|
||||
/// </summary>
|
||||
public enum MappingType
|
||||
{
|
||||
/// <summary>
|
||||
/// Mapping da campo sorgente a campo destinazione
|
||||
/// </summary>
|
||||
FieldMapping,
|
||||
|
||||
/// <summary>
|
||||
/// Valore di default per campo destinazione
|
||||
/// </summary>
|
||||
DefaultValue
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rappresenta una voce di mapping che può essere:
|
||||
/// - Un mapping da campo sorgente a campo destinazione
|
||||
/// - Un valore di default per un campo destinazione
|
||||
/// </summary>
|
||||
public class FieldMappingEntry
|
||||
{
|
||||
/// <summary>
|
||||
/// Tipo di mapping
|
||||
/// </summary>
|
||||
[JsonPropertyName("type")]
|
||||
public MappingType Type { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Nome del campo sorgente (solo per FieldMapping)
|
||||
/// </summary>
|
||||
[JsonPropertyName("sourceField")]
|
||||
public string? SourceField { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Nome del campo destinazione
|
||||
/// </summary>
|
||||
[JsonPropertyName("destinationField")]
|
||||
public string DestinationField { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Valore di default (solo per DefaultValue)
|
||||
/// </summary>
|
||||
[JsonPropertyName("defaultValue")]
|
||||
public object? DefaultValue { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Tipo di dato del valore di default (per conversioni corrette)
|
||||
/// Esempi: "string", "int", "decimal", "boolean", "datetime"
|
||||
/// </summary>
|
||||
[JsonPropertyName("defaultValueType")]
|
||||
public string? DefaultValueType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Crea un mapping da campo sorgente a campo destinazione
|
||||
/// </summary>
|
||||
public static FieldMappingEntry CreateFieldMapping(string sourceField, string destinationField)
|
||||
{
|
||||
return new FieldMappingEntry
|
||||
{
|
||||
Type = MappingType.FieldMapping,
|
||||
SourceField = sourceField,
|
||||
DestinationField = destinationField
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Crea un valore di default per un campo destinazione
|
||||
/// </summary>
|
||||
public static FieldMappingEntry CreateDefaultValue(string destinationField, object defaultValue, string? valueType = null)
|
||||
{
|
||||
return new FieldMappingEntry
|
||||
{
|
||||
Type = MappingType.DefaultValue,
|
||||
DestinationField = destinationField,
|
||||
DefaultValue = defaultValue,
|
||||
DefaultValueType = valueType ?? InferValueType(defaultValue)
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determina automaticamente il tipo del valore
|
||||
/// </summary>
|
||||
private static string InferValueType(object? value)
|
||||
{
|
||||
if (value == null) return "string";
|
||||
|
||||
return value switch
|
||||
{
|
||||
string _ => "string",
|
||||
int _ => "int",
|
||||
long _ => "long",
|
||||
decimal _ => "decimal",
|
||||
double _ => "double",
|
||||
float _ => "float",
|
||||
bool _ => "boolean",
|
||||
DateTime _ => "datetime",
|
||||
DateTimeOffset _ => "datetimeoffset",
|
||||
_ => "string"
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ottiene una descrizione user-friendly del mapping
|
||||
/// </summary>
|
||||
public string GetDescription()
|
||||
{
|
||||
return Type switch
|
||||
{
|
||||
MappingType.FieldMapping => $"{SourceField} → {DestinationField}",
|
||||
MappingType.DefaultValue => $"{DestinationField} = {DefaultValue ?? "null"} ({DefaultValueType})",
|
||||
_ => "Unknown"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper per la conversione tra vecchio formato (Dictionary) e nuovo formato (FieldMappingEntry)
|
||||
/// </summary>
|
||||
public static class MappingConverter
|
||||
{
|
||||
/// <summary>
|
||||
/// Converte il vecchio formato Dictionary in lista di FieldMappingEntry
|
||||
/// </summary>
|
||||
public static List<FieldMappingEntry> FromDictionary(Dictionary<string, string> oldMappings)
|
||||
{
|
||||
var entries = new List<FieldMappingEntry>();
|
||||
|
||||
foreach (var mapping in oldMappings)
|
||||
{
|
||||
entries.Add(FieldMappingEntry.CreateFieldMapping(mapping.Key, mapping.Value));
|
||||
}
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converte una lista di FieldMappingEntry nel vecchio formato Dictionary (solo field mappings)
|
||||
/// </summary>
|
||||
public static Dictionary<string, string> ToDictionary(List<FieldMappingEntry> entries)
|
||||
{
|
||||
var dictionary = new Dictionary<string, string>();
|
||||
|
||||
foreach (var entry in entries.Where(e => e.Type == MappingType.FieldMapping && !string.IsNullOrEmpty(e.SourceField)))
|
||||
{
|
||||
dictionary[entry.SourceField!] = entry.DestinationField;
|
||||
}
|
||||
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ottiene solo i valori di default da una lista di entries
|
||||
/// </summary>
|
||||
public static Dictionary<string, (object? Value, string? Type)> GetDefaultValues(List<FieldMappingEntry> entries)
|
||||
{
|
||||
var defaults = new Dictionary<string, (object?, string?)>();
|
||||
|
||||
foreach (var entry in entries.Where(e => e.Type == MappingType.DefaultValue))
|
||||
{
|
||||
defaults[entry.DestinationField] = (entry.DefaultValue, entry.DefaultValueType);
|
||||
}
|
||||
|
||||
return defaults;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user