1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.BackgroundProcessing;
|
---|
6 | using System.Data;
|
---|
7 | using HeuristicLab.OKB.Client;
|
---|
8 | using HeuristicLab.OKB.Cockpit.Admin.OKBTables;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.OKB.Cockpit.Admin {
|
---|
11 |
|
---|
12 | public class TableLoadWorker : ObservableBackgroundWorker {
|
---|
13 |
|
---|
14 | public string TableName { get; private set; }
|
---|
15 | public DataTable Table { get; private set; }
|
---|
16 | public Dictionary<string, DataTable> ForeignKeys { get; private set; }
|
---|
17 | private bool loadForeignKeys;
|
---|
18 |
|
---|
19 | public TableLoadWorker(string tableName, bool loadForeignKeys)
|
---|
20 | : base("loading " + tableName) {
|
---|
21 | WorkerReportsProgress = true;
|
---|
22 | WorkerSupportsCancellation = true;
|
---|
23 | TableName = tableName;
|
---|
24 | this.loadForeignKeys = loadForeignKeys;
|
---|
25 | DoWork += (s, a) => {
|
---|
26 | Table = loadTable(TableName);
|
---|
27 | if (loadForeignKeys)
|
---|
28 | ForeignKeys = LoadForeignKeys();
|
---|
29 | };
|
---|
30 | }
|
---|
31 |
|
---|
32 | private DataTable loadTable(string tableName) {
|
---|
33 | ReportProgress(0);
|
---|
34 | TableServiceClient client = ClientFactory.Create<TableServiceClient, ITableService>();
|
---|
35 | int nRows = 0;
|
---|
36 | DataTable table = client.PrepareDataTable(out nRows, tableName);
|
---|
37 | int stepSize = Math.Max(1, Math.Min(nRows / 100, 200));
|
---|
38 | if (nRows > 0) {
|
---|
39 | DataTable newRows = new DataTable();
|
---|
40 | do {
|
---|
41 | newRows = client.GetNextRows(stepSize);
|
---|
42 | table.Merge(newRows);
|
---|
43 | ReportProgress(100 * table.Rows.Count / nRows);
|
---|
44 | } while (!CancellationPending && newRows.Rows.Count > 0);
|
---|
45 | }
|
---|
46 | client.FinishFetchingRows();
|
---|
47 | client.Close();
|
---|
48 | return table;
|
---|
49 | }
|
---|
50 |
|
---|
51 | private Dictionary<string, DataTable> LoadForeignKeys() {
|
---|
52 | var fk = new Dictionary<string, DataTable>();
|
---|
53 | Name = "loading referenced tables";
|
---|
54 | var foreignKeyColumns = Table.Columns.Cast<DataColumn>()
|
---|
55 | .Where(c => c.ColumnName.EndsWith("Id") && c.ColumnName.Length > 2);
|
---|
56 | foreach (var column in foreignKeyColumns) {
|
---|
57 | string tableName = column.ColumnName.Substring(0, column.ColumnName.Length - 2);
|
---|
58 | DataTable referencedTable = loadTable(tableName);
|
---|
59 | fk[column.ColumnName] = referencedTable;
|
---|
60 | }
|
---|
61 | return fk;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|