[Feature] Salesforce: batch describe metadati, discovery parallela e fix scheduler External ID
- Salesforce Composite Batch API per describe SObject: le describe sono ora raggruppate in chunk da 25 e inviate come singole POST a /composite/batch, riducendo le chiamate API da N a ceil(N/25); per 200 SObject: da 201 a 9 chiamate. - Discovery entita' REST in parallelo: DiscoverEntitySummariesAsync e DiscoverEntitiesAsync avviate simultaneamente; la lista entita' diventa interattiva subito dopo le summaries, i dettagli completano in background con StateHasChanged() per aggiornare l'UI istantaneamente. - Fix scheduler - preservazione ExternalIdRelationshipsJson e DefaultValuesJson: in DataCoupler.razor.cs entrambi i blocchi di update profilo esistente (riattivazione profilo inattivo e sovrascrittura profilo attivo) omettevano questi campi nella copia, causandone l'azzeramento silenzioso ad ogni re-salvataggio. Ora entrambi i percorsi propagano correttamente i campi JSON. - Fix scheduler - esclusione campi sorgente External ID dal mapping normale: in ScheduledProfileExecutionService.TransformRecordForRest i campi sorgente usati nelle External ID Relationships venivano inclusi anche nel loop di field mapping standard, generando dati duplicati nell'entita' destinazione. Ora il comportamento e' allineato alla UI manuale (TransformRecordToRestEntity). - Aggiornata documentazione: README.md, AGENTS.md, copilot-instructions.md
This commit was merged in pull request #11.
This commit is contained in:
@@ -171,18 +171,25 @@ public class ScheduledProfileExecutionService : IScheduledProfileExecutionServic
|
||||
_logger.LogInformation("Caricate {Count} External ID Relationships dal profilo", externalIdRelationships.Count);
|
||||
}
|
||||
|
||||
// 4.6. Parse Default Values
|
||||
var defaultValues = ParseDefaultValues(profile.DefaultValuesJson);
|
||||
if (defaultValues.Any())
|
||||
{
|
||||
_logger.LogInformation("Caricati {Count} default values dal profilo", defaultValues.Count);
|
||||
}
|
||||
|
||||
// 5. Determina se utilizzare Salesforce Composite API
|
||||
bool useSalesforceComposite = restClient is DataConnection.REST.Implementations.SalesforceServiceClient;
|
||||
|
||||
if (useSalesforceComposite)
|
||||
{
|
||||
_logger.LogInformation("Utilizzo Salesforce Composite API per il trasferimento");
|
||||
return await ExecuteDataTransferWithCompositeAsync(profile, sourceRecords, restClient, restEntity, restCredential!, fieldMappings, externalIdRelationships, enableDeletionSync);
|
||||
return await ExecuteDataTransferWithCompositeAsync(profile, sourceRecords, restClient, restEntity, restCredential!, fieldMappings, defaultValues, externalIdRelationships, enableDeletionSync);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Utilizzo metodo trasferimento standard per il trasferimento");
|
||||
return await ExecuteDataTransferStandardAsync(profile, sourceRecords, restClient, restEntity, restCredential!, fieldMappings, externalIdRelationships, enableDeletionSync);
|
||||
return await ExecuteDataTransferStandardAsync(profile, sourceRecords, restClient, restEntity, restCredential!, fieldMappings, defaultValues, externalIdRelationships, enableDeletionSync);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -417,6 +424,53 @@ public class ScheduledProfileExecutionService : IScheduledProfileExecutionServic
|
||||
return relationships;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Parse del JSON dei default values
|
||||
/// </summary>
|
||||
private Dictionary<string, (object? Value, string? Type)> ParseDefaultValues(string? defaultValuesJson)
|
||||
{
|
||||
var defaultValues = new Dictionary<string, (object? Value, string? Type)>();
|
||||
|
||||
if (string.IsNullOrEmpty(defaultValuesJson))
|
||||
{
|
||||
_logger.LogDebug("DefaultValues JSON è vuoto o null");
|
||||
return defaultValues;
|
||||
}
|
||||
|
||||
_logger.LogDebug("Parsing DefaultValues JSON: {Json}", defaultValuesJson);
|
||||
|
||||
try
|
||||
{
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
};
|
||||
|
||||
var deserializedDefaults = JsonSerializer.Deserialize<Dictionary<string, DefaultValueDto>>(defaultValuesJson, options);
|
||||
if (deserializedDefaults != null)
|
||||
{
|
||||
foreach (var entry in deserializedDefaults)
|
||||
{
|
||||
defaultValues[entry.Key] = (entry.Value.Value, entry.Value.Type);
|
||||
_logger.LogDebug("Default value: {Field} = {Value} ({Type})",
|
||||
entry.Key, entry.Value.Value, entry.Value.Type);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Trovati {Count} default values nel JSON", defaultValues.Count);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Deserializzazione ritornato null per DefaultValues JSON");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Errore nel parsing dei default values: {Json}", defaultValuesJson);
|
||||
}
|
||||
|
||||
return defaultValues;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ottiene tutti i record dal database
|
||||
/// </summary>
|
||||
@@ -685,6 +739,7 @@ public class ScheduledProfileExecutionService : IScheduledProfileExecutionServic
|
||||
RestEntitySummary restEntity,
|
||||
RestApiCredential restCredential,
|
||||
Dictionary<string, string> fieldMappings,
|
||||
Dictionary<string, (object? Value, string? Type)> defaultValues,
|
||||
List<ExternalIdRelationshipDto> externalIdRelationships,
|
||||
bool enableDeletionSync = false)
|
||||
{
|
||||
@@ -699,8 +754,8 @@ public class ScheduledProfileExecutionService : IScheduledProfileExecutionServic
|
||||
{
|
||||
try
|
||||
{
|
||||
// 1. Trasforma il record utilizzando i field mappings e External ID Relationships
|
||||
var restData = TransformRecordForRest(record, fieldMappings, externalIdRelationships);
|
||||
// 1. Trasforma il record utilizzando i field mappings, default values e External ID Relationships
|
||||
var restData = TransformRecordForRest(record, fieldMappings, defaultValues, externalIdRelationships);
|
||||
|
||||
// 2. Gestione associazioni record se abilitata
|
||||
string? entityId = null;
|
||||
@@ -810,6 +865,7 @@ public class ScheduledProfileExecutionService : IScheduledProfileExecutionServic
|
||||
RestEntitySummary restEntity,
|
||||
RestApiCredential restCredential,
|
||||
Dictionary<string, string> fieldMappings,
|
||||
Dictionary<string, (object? Value, string? Type)> defaultValues,
|
||||
List<ExternalIdRelationshipDto> externalIdRelationships,
|
||||
bool enableDeletionSync = false)
|
||||
{
|
||||
@@ -820,7 +876,7 @@ public class ScheduledProfileExecutionService : IScheduledProfileExecutionServic
|
||||
if (!(restClient is DataConnection.REST.Implementations.SalesforceServiceClient salesforceClient))
|
||||
{
|
||||
_logger.LogWarning("Client REST non è SalesforceServiceClient, fallback al metodo standard");
|
||||
return await ExecuteDataTransferStandardAsync(profile, sourceRecords, restClient, restEntity, restCredential, fieldMappings, externalIdRelationships, enableDeletionSync);
|
||||
return await ExecuteDataTransferStandardAsync(profile, sourceRecords, restClient, restEntity, restCredential, fieldMappings, defaultValues, externalIdRelationships, enableDeletionSync);
|
||||
}
|
||||
|
||||
try
|
||||
@@ -851,7 +907,7 @@ public class ScheduledProfileExecutionService : IScheduledProfileExecutionServic
|
||||
var recordNumber = indexedRecord.RecordNumber;
|
||||
|
||||
// Trasforma il record in base ai mapping e External ID Relationships (operazione locale, thread-safe)
|
||||
var restData = TransformRecordForRest(record, fieldMappings, externalIdRelationships);
|
||||
var restData = TransformRecordForRest(record, fieldMappings, defaultValues, externalIdRelationships);
|
||||
|
||||
// Genera la chiave sorgente e l'hash dei dati per questo record (include MAPPING_SIGNATURE)
|
||||
var sourceKey = GenerateSourceKey(record, profile.SourceKeyField);
|
||||
@@ -1144,12 +1200,30 @@ public class ScheduledProfileExecutionService : IScheduledProfileExecutionServic
|
||||
private Dictionary<string, object> TransformRecordForRest(
|
||||
Dictionary<string, object> sourceRecord,
|
||||
Dictionary<string, string> fieldMappings,
|
||||
Dictionary<string, (object? Value, string? Type)> defaultValues,
|
||||
List<ExternalIdRelationshipDto>? externalIdRelationships = null)
|
||||
{
|
||||
var restData = new Dictionary<string, object>();
|
||||
|
||||
// Costruisce un set dei campi sorgente usati esclusivamente come External ID Relationship:
|
||||
// questi NON devono essere inviati anche come mapping normale (stessa logica della UI manuale).
|
||||
var externalIdSourceFields = (externalIdRelationships != null)
|
||||
? externalIdRelationships
|
||||
.Where(r => !string.IsNullOrWhiteSpace(r.SourceField))
|
||||
.Select(r => r.SourceField)
|
||||
.ToHashSet()
|
||||
: new HashSet<string>();
|
||||
|
||||
// 1. Applica field mappings (escludendo i campi sorgente usati per External ID Relationships)
|
||||
foreach (var mapping in fieldMappings)
|
||||
{
|
||||
// Salta il campo se è usato come sorgente in un External ID Relationship
|
||||
if (externalIdSourceFields.Contains(mapping.Key))
|
||||
{
|
||||
_logger.LogDebug("Campo sorgente '{SourceField}' usato in External ID Relationship, escluso dal mapping normale", mapping.Key);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (sourceRecord.ContainsKey(mapping.Key))
|
||||
{
|
||||
var value = sourceRecord[mapping.Key];
|
||||
@@ -1164,7 +1238,22 @@ public class ScheduledProfileExecutionService : IScheduledProfileExecutionServic
|
||||
}
|
||||
}
|
||||
|
||||
// Aggiungi External ID Relationships (per Salesforce)
|
||||
// 2. Applica default values (solo se il campo non è già stato mappato)
|
||||
foreach (var defaultValue in defaultValues)
|
||||
{
|
||||
if (!restData.ContainsKey(defaultValue.Key))
|
||||
{
|
||||
var (value, type) = defaultValue.Value;
|
||||
if (value != null)
|
||||
{
|
||||
restData[defaultValue.Key] = value;
|
||||
_logger.LogDebug("Applicato default value: {Field} = {Value} ({Type})",
|
||||
defaultValue.Key, value, type);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Aggiungi External ID Relationships (per Salesforce)
|
||||
if (externalIdRelationships != null && externalIdRelationships.Any())
|
||||
{
|
||||
foreach (var relationship in externalIdRelationships)
|
||||
|
||||
Reference in New Issue
Block a user