-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDashboardConfig.cs
128 lines (104 loc) · 6.48 KB
/
DashboardConfig.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using DevExpress.DashboardWeb.Mvc;
using DevExpress.DataAccess.DataFederation;
using DevExpress.DataAccess.Excel;
using DevExpress.DataAccess.Json;
using DevExpress.DataAccess.Sql;
using System;
using System.Web.Hosting;
using System.Web.Routing;
namespace MVC_DataFederationExample {
public static class DashboardConfig {
public static void RegisterService(RouteCollection routes) {
routes.MapDashboardRoute("dashboardControl", "DefaultDashboard");
DashboardFileStorage dashboardFileStorage = new DashboardFileStorage("~/App_Data/Dashboards");
DashboardConfigurator.Default.SetDashboardStorage(dashboardFileStorage);
DashboardConfigurator.PassCredentials = true;
// Uncomment this string to allow end users to create new data sources based on predefined connection strings.
//DashboardConfigurator.Default.SetConnectionStringsProvider(new DevExpress.DataAccess.Web.ConfigFileConnectionStringsProvider());
DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
// Configures an SQL data source.
DashboardSqlDataSource sqlDataSource = new DashboardSqlDataSource("SQL Data Source", "NWindConnectionString");
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 = HostingEnvironment.MapPath(@"~/App_Data/SalesPerson.xlsx");
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(HostingEnvironment.MapPath(@"~/App_Data/Categories.json"), UriKind.RelativeOrAbsolute);
jsonDataSource.JsonSource = new UriJsonSource(fileUri);
// Registers a Federated data source.
dataSourceStorage.RegisterDataSource("federatedDataSource", CreateFederatedDataSource(sqlDataSource,
excelDataSource, objDataSource, jsonDataSource).SaveToXml());
DashboardConfigurator.Default.SetDataSourceStorage(dataSourceStorage);
DashboardConfigurator.Default.DataLoading += DataLoading;
DashboardConfigurator.Default.ConfigureDataConnection += Default_ConfigureDataConnection;
}
private static void Default_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
if (e.ConnectionName == "excelSales") {
(e.ConnectionParameters as ExcelDataSourceConnectionParameters).FileName = HostingEnvironment.MapPath(@"~/App_Data/SalesPerson.xlsx");
}
else if (e.ConnectionName == "jsonCategories") {
UriJsonSource uriSource = (e.ConnectionParameters as JsonSourceConnectionParameters).JsonSource as UriJsonSource;
uriSource.Uri = new Uri(HostingEnvironment.MapPath(@"~/App_Data/Categories.json"), UriKind.RelativeOrAbsolute);
}
}
private static void DataLoading(object sender, DataLoadingWebEventArgs e) {
if (e.DataId == "odsInvoices") {
e.Data = Invoices.CreateData();
}
}
private static DashboardFederationDataSource CreateFederatedDataSource(DashboardSqlDataSource sqlDS,
DashboardExcelDataSource excelDS, DashboardObjectDataSource objDS, DashboardJsonDataSource jsonDS) {
DashboardFederationDataSource federationDataSource = new DashboardFederationDataSource("Federated Data Source");
Source sqlSource = new Source("sqlSource", sqlDS, "SQL Orders");
Source excelSource = new Source("excelSource", excelDS, "");
Source objectSource = new Source("objectSource", objDS, "");
SourceNode jsonSourceNode = new SourceNode(new Source("json", jsonDS, ""));
// Join
SelectNode joinQuery =
sqlSource.From()
.Select("OrderDate", "ShipCity", "ShipCountry")
.Join(excelSource, "[excelSource.OrderID] = [sqlSource.OrderID]")
.Select("CategoryName", "ProductName", "Extended Price")
.Join(objectSource, "[objectSource.Country] = [excelSource.Country]")
.Select("Country", "UnitPrice")
.Build("Join query");
federationDataSource.Queries.Add(joinQuery);
// Union and UnionAll
UnionNode queryUnionAll = sqlSource.From().Select("OrderID", "OrderDate").Build("OrdersSqlite")
.UnionAll(excelSource.From().Select("OrderID", "OrderDate").Build("OrdersExcel"))
.Build("OrdersUnionAll");
queryUnionAll.Alias = "Union query";
UnionNode queryUnion = sqlSource.From().Select("OrderID", "OrderDate").Build("OrdersSqlite")
.Union(excelSource.From().Select("OrderID", "OrderDate").Build("OrdersExcel"))
.Build("OrdersUnion");
queryUnion.Alias = "UnionAll query";
federationDataSource.Queries.Add(queryUnionAll);
federationDataSource.Queries.Add(queryUnion);
// Transformation
TransformationNode unfoldNode = new TransformationNode(jsonSourceNode) {
Alias = "Unfold",
Rules = { new TransformationRule { ColumnName = "Products", Alias = "Product", Unfold = true, Flatten = false } }
};
TransformationNode unfoldFlattenNode = new TransformationNode(jsonSourceNode) {
Alias = "Unfold and Flatten",
Rules = { new TransformationRule { ColumnName = "Products", Unfold = true, Flatten = true } }
};
federationDataSource.Queries.Add(unfoldNode);
federationDataSource.Queries.Add(unfoldFlattenNode);
return federationDataSource;
}
}
}