// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services .AddResponseCompression() .AddDevExpressControls() .AddMvc(); services.AddScoped <DashboardConfigurator>((IServiceProvider serviceProvider) => { DashboardConfigurator configurator = new DashboardConfigurator(); configurator.SetConnectionStringsProvider(new DashboardConnectionStringsProvider(Configuration)); DashboardFileStorage dashboardFileStorage = new DashboardFileStorage(FileProvider.GetFileInfo("Data/Dashboards").PhysicalPath); configurator.SetDashboardStorage(dashboardFileStorage); DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage(); // Configures an SQL data source. DashboardSqlDataSource sqlDataSource = new DashboardSqlDataSource("SQL Data Source", "NWindConnectionString"); sqlDataSource.DataProcessingMode = DataProcessingMode.Client; SelectQuery query = SelectQueryFluentBuilder .AddTable("Orders") .SelectAllColumnsFromTable() .Build("SQL Orders"); sqlDataSource.Queries.Add(query); // Configures an Object data source. DashboardObjectDataSource objDataSource = new DashboardObjectDataSource("Object Data Source"); objDataSource.DataId = "odsInvoices"; // Configures an Excel data source. DashboardExcelDataSource excelDataSource = new DashboardExcelDataSource("Excel Data Source"); excelDataSource.ConnectionName = "excelSales"; excelDataSource.FileName = FileProvider.GetFileInfo("Data/SalesPerson.xlsx").PhysicalPath; excelDataSource.SourceOptions = new ExcelSourceOptions(new ExcelWorksheetSettings("Data")); // Configures a JSON data source. DashboardJsonDataSource jsonDataSource = new DashboardJsonDataSource("JSON Data Source"); jsonDataSource.ConnectionName = "jsonCategories"; Uri fileUri = new Uri(FileProvider.GetFileInfo("Data/Categories.json").PhysicalPath, UriKind.RelativeOrAbsolute); jsonDataSource.JsonSource = new UriJsonSource(fileUri); // Registers a Federated data source. dataSourceStorage.RegisterDataSource("federatedDataSource", CreateFederatedDataSource(sqlDataSource, excelDataSource, objDataSource, jsonDataSource).SaveToXml()); configurator.SetDataSourceStorage(dataSourceStorage); configurator.DataLoading += (s, e) => { if (e.DataId == "odsInvoices") { e.Data = Invoices.CreateData(); } }; configurator.ConfigureDataConnection += (s, e) => { if (e.ConnectionName == "excelSales") { (e.ConnectionParameters as ExcelDataSourceConnectionParameters).FileName = FileProvider.GetFileInfo("Data/SalesPerson.xlsx").PhysicalPath; } else if (e.ConnectionName == "jsonCategories") { UriJsonSource source = new UriJsonSource(new Uri(FileProvider.GetFileInfo("Data/Categories.json").PhysicalPath, UriKind.RelativeOrAbsolute)); (e.ConnectionParameters as JsonSourceConnectionParameters).JsonSource = source; } }; return(configurator); }); }
// Conditional data loading for other datasource types private static void DashboardConfigurator_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) { var userName = (string)HttpContext.Current.Session["CurrentUser"]; if (e.ConnectionName == "sqlCategories") { var sqlConnectionParameters = e.ConnectionParameters as CustomStringConnectionParameters; if (userName == "Admin") { sqlConnectionParameters.ConnectionString = @"XpoProvider=MSAccess;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\nwind_admin.mdb;"; } else if (userName == "User") { sqlConnectionParameters.ConnectionString = @"XpoProvider=MSAccess;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\nwind_user.mdb;"; } } else if (e.ConnectionName == "jsonCustomers") { if (e.DashboardId == "JSON") { string jsonFileName = ""; if (userName == "Admin") { jsonFileName = "customers_admin.json"; } else if (userName == "User") { jsonFileName = "customers_user.json"; } var fileUri = new Uri(HttpContext.Current.Server.MapPath(@"~/App_Data/" + jsonFileName), UriKind.RelativeOrAbsolute); ((JsonSourceConnectionParameters)e.ConnectionParameters).JsonSource = new UriJsonSource(fileUri); } else if (e.DashboardId == "JSONFilter") { var remoteUri = new Uri(GetBaseUrl() + "Home/GetCustomers"); var jsonSource = new UriJsonSource(remoteUri); if (userName == "User") { jsonSource.QueryParameters.AddRange(new[] { // "CountryPattern" is a dashboard parameter whose value is used for the "CountryStartsWith" query parameter new QueryParameter("CountryStartsWith", typeof(Expression), new Expression("Parameters.CountryPattern")) }); } ((JsonSourceConnectionParameters)e.ConnectionParameters).JsonSource = jsonSource; } } else if (e.ConnectionName == "excelSales") { var excelConnectionParameters = e.ConnectionParameters as ExcelDataSourceConnectionParameters; if (userName == "Admin") { excelConnectionParameters.FileName = HttpContext.Current.Server.MapPath(@"~/App_Data/sales_admin.xlsx"); } else if (userName == "User") { excelConnectionParameters.FileName = HttpContext.Current.Server.MapPath(@"~/App_Data/sales_user.xlsx"); } } else if (e.ConnectionName == "olapAdventureWorks") { if (userName == "Admin") { ((OlapConnectionParameters)e.ConnectionParameters).ConnectionString = @"provider=MSOLAP;data source=http://demos.devexpress.com/Services/OLAP/msmdpump.dll;initial catalog=Adventure Works DW Standard Edition;cube name=Adventure Works;"; } else if (userName == "User") { throw new ApplicationException("You are not authorized to access OLAP data."); } } else if (e.ConnectionName == "extractSalesPerson") { if (userName == "Admin") { ((ExtractDataSourceConnectionParameters)e.ConnectionParameters).FileName = HttpContext.Current.Server.MapPath(@"~/App_Data/SalesPersonExtract.dat"); } else { throw new ApplicationException("You are not authorized to access Extract data."); } } }
// Conditional data loading for other datasource types private void DashboardConfigurator_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) { var userName = contextAccessor.HttpContext.Session.GetString("CurrentUser"); if (e.ConnectionName == "sqlCategories") { var sqlConnectionParameters = e.ConnectionParameters as CustomStringConnectionParameters; if (userName == "Admin") { sqlConnectionParameters.ConnectionString = @"XpoProvider=SQLite; Data Source=App_Data/nwind_admin.db;"; } else if (userName == "User") { sqlConnectionParameters.ConnectionString = @"XpoProvider=SQLite; Data Source=App_Data/nwind_user.db;"; } } else if (e.ConnectionName == "jsonCustomers") { if (e.DashboardId == "JSON") { string jsonFileName = ""; if (userName == "Admin") { jsonFileName = "customers_admin.json"; } else if (userName == "User") { jsonFileName = "customers_user.json"; } var fileUri = new Uri(fileProvider.GetFileInfo("App_Data/").PhysicalPath + jsonFileName, UriKind.RelativeOrAbsolute); ((JsonSourceConnectionParameters)e.ConnectionParameters).JsonSource = new UriJsonSource(fileUri); } else if (e.DashboardId == "JSONFilter") { var remoteUri = new Uri(GetBaseUrl() + "Home/GetCustomers"); var jsonSource = new UriJsonSource(remoteUri); if (userName == "User") { jsonSource.QueryParameters.AddRange(new[] { // "CountryPattern" is a dashboard parameter whose value is used for the "CountryStartsWith" query parameter new QueryParameter("CountryStartsWith", typeof(Expression), new Expression("Parameters.CountryPattern")) }); } else if (userName != "Admin") { throw new ApplicationException("You are not authorized to access JSON data."); } ((JsonSourceConnectionParameters)e.ConnectionParameters).JsonSource = jsonSource; } } else if (e.ConnectionName == "excelSales") { var excelConnectionParameters = e.ConnectionParameters as ExcelDataSourceConnectionParameters; if (userName == "Admin") { excelConnectionParameters.FileName = fileProvider.GetFileInfo("App_Data/sales_admin.xlsx").PhysicalPath; } else if (userName == "User") { excelConnectionParameters.FileName = fileProvider.GetFileInfo("App_Data/sales_user.xlsx").PhysicalPath; } } else if (e.ConnectionName == "extractSalesPerson") { if (userName == "Admin") { ((ExtractDataSourceConnectionParameters)e.ConnectionParameters).FileName = fileProvider.GetFileInfo("App_Data/SalesPersonExtract.dat").PhysicalPath; } else { throw new ApplicationException("You are not authorized to access Extract data."); } } }