public async Task <ReportDefinition> LoadReportDefinitionAsync(string name) { ReportDefinition reportDefinition = await this.Cache.GetOrCreateAsync($"ReportingService:LoadReportDefinitionAsync:{name}", async (entry) => { entry.AbsoluteExpirationRelativeToNow = this.Options.DefinitionTimeToLive; foreach (var repository in this.Repositories) { reportDefinition = await repository.LoadReportDefinitionAsync(name); if (null != reportDefinition) { this.FixFilterDefinitions(reportDefinition.Filters); this.FixFieldDefinitions(reportDefinition.Output); return(reportDefinition); } } return(null); }); /* * need to return the clone of the definition since multiple clients might be * accessing the cached instance and modifying it state * by cloning we make sure each client gets its own copy of the definition */ return(reportDefinition?.Clone() ?? throw new InvalidOperationException()); }
public static bool TryFindFilterDefinition(this ReportDefinition reportDefinition, string filterName, out FilterDefinition filterDefinition) { filterDefinition = null; if (null == reportDefinition) { return(false); } Queue <IDictionary <string, FilterDefinition> > queue = new Queue <IDictionary <string, FilterDefinition> >(); queue.Enqueue(reportDefinition.Filters); while (0 < queue.Count) { var entries = queue.Dequeue(); if (entries.TryGetValue(filterName, out filterDefinition)) { return(true); } foreach (var entry in entries) { if (null != entry.Value.Children) { queue.Enqueue(entry.Value.Children); } } } return(false); }
public ReportCommandContext(ReportDefinition reportDefinition, string commandName) : base(reportDefinition) { if (!reportDefinition.Commands.TryGetValue(commandName, out CommandDefinition commandDefinition)) { throw new ArgumentException($"Command '{commandName}' could not be found in the report definition."); } this.CommandDefinition = commandDefinition; this.CommandName = commandName; }
public static IEnumerable <FieldDefinition> EnumerateFieldDefinitions(this ReportDefinition reportDefinition) { if (null == reportDefinition?.Output?.Fields) { yield break; } foreach (var fieldDefinitionEntry in reportDefinition.Output.Fields) { yield return(fieldDefinitionEntry.Value); } }
public static bool TryFindDataSourceDefinition(this ReportDefinition reportDefinition, IDataDrivenObject dataDrivenObject, out DataSourceDefinition dataSourceDefinition) { dataSourceDefinition = null; if (null == reportDefinition) { return(false); } if (string.IsNullOrWhiteSpace(dataDrivenObject?.DataSource)) { return(false); } return(reportDefinition.DataSources.TryGetValue(dataDrivenObject.DataSource, out dataSourceDefinition)); }
public async Task <ReportOutput> ExecuteReportOutputAsync(ReportDefinition reportDefinition, IDictionary <string, string> values) { ReportCommandContext context = new ReportCommandContext(reportDefinition, reportDefinition.Output.Command); if (!string.IsNullOrWhiteSpace(reportDefinition.ContextProvider)) { Type contextProviderType = Type.GetType(reportDefinition.ContextProvider, true, true); context.ContextProvider = ActivatorUtilities.CreateInstance(this.ServiceProvider, contextProviderType); } var reportCommand = await this.CommandBuilder.BuildCommandAsync(context, values); using (IServiceScope serviceScope = this.ServiceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope()) { if (!reportDefinition.TryFindDataSourceDefinition(reportDefinition.Output, out DataSourceDefinition dataSourceDefinition)) { throw new InvalidOperationException($"Could not find data source definition '{reportDefinition.Output.DataSource}' in the report '{reportDefinition.Name}'."); } var dataSourceRegistrationOptionsSnapshot = serviceScope.ServiceProvider.GetRequiredService <IOptionsSnapshot <DataSourceRegistrationOptions> >(); var dataSourceRegistrationOptions = dataSourceRegistrationOptionsSnapshot.Get(dataSourceDefinition.Type); if (null == dataSourceRegistrationOptions.TypeHandle || IntPtr.Zero == dataSourceRegistrationOptions.TypeHandle.Value) { throw new InvalidOperationException($"No data source service is registered for data source type '{dataSourceDefinition.Type}'."); } Type dataSourceType = Type.GetTypeFromHandle(dataSourceRegistrationOptions.TypeHandle); IDataSource dataSource = (IDataSource)serviceScope.ServiceProvider.GetRequiredService(dataSourceType); await dataSource.OpenAsync(dataSourceDefinition); try { var dataset = await dataSource.GetDataAsync(reportCommand, reportDefinition.EnumerateFieldDefinitions().ToArray()); return(new ReportOutput(reportCommand, dataset)); } finally { await dataSource.CloseAsync(); } } }
public ReportContext(ReportDefinition reportDefinition) { this.ReportDefinition = reportDefinition ?? throw new ArgumentNullException(nameof(reportDefinition)); }
public static IEnumerable <FilterDefinition> EnumerateFilterDefinitions(this ReportDefinition reportDefinition) { return(ReportDefinitionExtensions.EnumerateFilterDefinitions(reportDefinition?.Filters)); }