Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.OKB.Cockpit.Admin/TableLoadWorker.cs @ 6514

Last change on this file since 6514 was 4311, checked in by swagner, 14 years ago

Integrated OKB clients for HL 3.3 (#1166)

File size: 2.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.BackgroundProcessing;
6using System.Data;
7using HeuristicLab.OKB.Client;
8using HeuristicLab.OKB.Cockpit.Admin.OKBTables;
9
10namespace 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
Note: See TracBrowser for help on using the repository browser.