-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathReportSync.cs
556 lines (463 loc) · 13.2 KB
/
ReportSync.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
namespace ReportSync
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Properties;
using ReportService;
public partial class ReportSync : Form
{
private const string ROOT_FOLDER = "/";
private const string PATH_SEPERATOR = "/";
private const string SOURCE_SELECTION_START = "ReportSyncSource:";
private const string DEST_SELECTION_START = "ReportSyncDest:";
private const string MAPPING_START = "ReportSyncMap:";
private readonly ServiceWrapper _SourceService;
private readonly ServiceWrapper _DestinationService;
private Dictionary<string, string> destDS;
private ReportingService2005 destRS;
private List<string> existingPaths;
private string pathOnDisk;
private int processedNodeCount;
private int selectedNodeCount;
private Dictionary<string, string> sourceDS;
private ReportingService2005 sourceRS;
private string uploadPath = ROOT_FOLDER;
private BackgroundWorker _SyncSchedulesWorker;
public ReportSync()
{
InitializeComponent();
_SourceService = new ServiceWrapper();
_DestinationService = new ServiceWrapper();
bwDownload.DoWork += bwDownload_DoWork;
bwDownload.ProgressChanged += bwDownload_ProgressChanged;
bwDownload.RunWorkerCompleted += bwDownload_RunWorkerCompleted;
bwUpload.DoWork += bwUpload_DoWork;
bwUpload.ProgressChanged += bwUpload_ProgressChanged;
bwUpload.RunWorkerCompleted += bwUpload_RunWorkerCompleted;
bwSync.DoWork += bwSync_DoWork;
bwSync.ProgressChanged += bwSync_ProgressChanged;
bwSync.RunWorkerCompleted += bwSync_RunWorkerCompleted;
_SyncSchedulesWorker = new BackgroundWorker();
_SyncSchedulesWorker.DoWork += bwSyncSchedules_DoWork;
_SyncSchedulesWorker.ProgressChanged += bwSyncSchedules_ProgressChanged;
_SyncSchedulesWorker.RunWorkerCompleted += bwSyncSchedules_RunWorkerCompleted;
}
#region Events
private void btnLoad_Click(object sender, EventArgs e)
{
LoadSourceTree();
}
private void btnDestLoad_Click(object sender, EventArgs e)
{
LoadDestinationTree();
}
private void OnNodeChecked(object sender, TreeViewEventArgs e)
{
if (e.Action == TreeViewAction.Unknown)
{
return;
}
var node = e.Node;
var isChecked = node.Checked;
if (isChecked)
{
var parent = node.Parent;
while (parent != null)
{
parent.Checked = true;
parent = parent.Parent;
}
}
if (node.Nodes.Count > 0)
{
if (!isChecked || !node.IsExpanded)
{
UpdateSelection(node, node.Checked);
}
}
}
private void aboutReportSyncToolStripMenuItem_Click(object sender, EventArgs e)
{
var frmAbout = new AboutReportSync();
frmAbout.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void contentsToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start(@"http://code.google.com/p/reportsync/wiki/");
}
#endregion
#region Utilities
private void UpdateSelection(TreeNode node, bool isChecked)
{
node.Checked = isChecked;
foreach (TreeNode child in node.Nodes)
{
UpdateSelection(child, isChecked);
}
}
private void UpdateSelections(TreeNodeCollection nodes, bool isChecked)
{
foreach (TreeNode node in nodes)
{
UpdateSelection(node, isChecked);
}
}
private void UpdateProgress(BackgroundWorker backgroundWorker, int currentItem, int totalItems)
{
var progress = totalItems > 0 ? (currentItem * 100.0) / totalItems : 0;
backgroundWorker.ReportProgress((int)progress);
}
private void AddSelectedItems(TreeNodeCollection nodes, List<TreeNode> selectedNodes)
{
foreach (TreeNode node in nodes)
{
if (!node.Checked)
{
continue;
}
selectedNodes.Add(node);
var childNodes = node.Nodes;
if (childNodes.Count > 0)
{
AddSelectedItems(childNodes, selectedNodes);
}
}
}
private void ProcessItems(TreeNodeCollection nodes, BackgroundWorker worker, Action<ReportItem> loadAction, Action<ReportItem> saveAction)
{
var selectedNodes = new List<TreeNode>();
AddSelectedItems(nodes, selectedNodes);
var selectedNodeCount = selectedNodes.Count;
if (selectedNodeCount > 0)
{
var nodesProcessed = 0;
foreach (var node in selectedNodes)
{
var item = node.Tag as ReportItem;
if (item != null)
{
loadAction(item);
saveAction(item);
}
nodesProcessed++;
UpdateProgress(worker, nodesProcessed, selectedNodeCount);
}
}
}
#endregion
#region Sync
private void btnSync_Click(object sender, EventArgs e)
{
bwSync.RunWorkerAsync();
}
private void bwSync_DoWork(object sender, DoWorkEventArgs e)
{
UpdateProgress(bwSync, 0, 0);
var destinationService = _DestinationService;
destinationService.BasePath = null;
destinationService.SourceService = _SourceService;
ProcessItems(rptSourceTree.Nodes, bwSync, _SourceService.LoadItem, _DestinationService.UploadItem);
UpdateProgress(bwSync, 1, 1);
}
private void bwSync_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pbSource.Value = e.ProgressPercentage;
pbDest.Value = e.ProgressPercentage;
}
private void bwSync_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
UpdateSelections(rptSourceTree.Nodes, false);
LoadDestinationTree();
MessageBox.Show("Sync completed successfully.", "Sync complete");
}
#endregion
#region Sync Schedules
private void btnSyncSchedules_Click(object sender, EventArgs e)
{
_SyncSchedulesWorker.RunWorkerAsync();
}
private void bwSyncSchedules_DoWork(object sender, DoWorkEventArgs e)
{
UpdateProgress(_SyncSchedulesWorker, 0, 0);
var sourceService = _SourceService;
sourceService.LoadSchedules();
var destinationService = _DestinationService;
destinationService.BasePath = null;
destinationService.SourceService = sourceService;
destinationService.LoadSchedules();
var schedules = sourceService.GetSchedules();
for (var index = 0; index < schedules.Length; index++)
{
var schedule = schedules[index];
destinationService.AddSchedule(schedule);
UpdateProgress(_SyncSchedulesWorker, index, schedules.Length);
}
UpdateProgress(_SyncSchedulesWorker, 1, 1);
}
private void bwSyncSchedules_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pbSource.Value = e.ProgressPercentage;
pbDest.Value = e.ProgressPercentage;
}
private void bwSyncSchedules_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
UpdateSelections(rptSourceTree.Nodes, false);
LoadDestinationTree();
MessageBox.Show("Schedule sync completed successfully.", "Schedule sync complete");
}
#endregion
#region Download
private void btnDownload_Click(object sender, EventArgs e)
{
bwDownload.RunWorkerAsync();
}
private void bwDownload_DoWork(object sender, DoWorkEventArgs e)
{
_DestinationService.BasePath = txtLocalPath.Text;
UpdateProgress(bwDownload, 0, 0);
ProcessItems(rptSourceTree.Nodes, bwDownload, _SourceService.LoadItem, _DestinationService.SaveItem);
UpdateProgress(bwDownload, 1, 1);
}
private void bwDownload_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pbSource.Value = e.ProgressPercentage;
}
private void bwDownload_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
UpdateSelections(rptSourceTree.Nodes, false);
MessageBox.Show("Report files downloaded successfully.", "Download complete");
}
#endregion
#region Upload
private void btnUpload_Click(object sender, EventArgs e)
{
existingPaths = new List<string>();
if (String.IsNullOrEmpty(txtLocalPath.Text))
{
MessageBox.Show("Please select the folder to upload.");
return;
}
bwUpload.RunWorkerAsync();
}
private void bwUpload_DoWork(object sender, DoWorkEventArgs e)
{
// Todo: Load from file system (Should load into tree view so they can select)
// Todo: Format path (remove base path and extension)
// Todo: Upload, re-use framework for Sync and download
bwUpload.ReportProgress(100);
}
private void bwUpload_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pbDest.Value = e.ProgressPercentage;
}
private void bwUpload_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
LoadDestinationTree();
MessageBox.Show("Upload completed successfully.", "Upload complete");
}
#endregion
#region Load Trees
private void LoadSourceTree()
{
var service = _SourceService;
service.ServiceUrl = txtSourceUrl.Text;
service.Username = txtSourceUser.Text;
service.Password = txtSourcePassword.Text;
LoadTreeNodes(service, rptSourceTree);
}
private void LoadDestinationTree()
{
var service = _DestinationService;
service.ServiceUrl = txtDestUrl.Text;
service.Username = txtDestUser.Text;
service.Password = txtDestPassword.Text;
LoadTreeNodes(service, rptDestTree);
}
private void LoadTreeNodes(ServiceWrapper service, TreeView tree)
{
try
{
service.LoadReportItems();
service.LoadSchedules();
var nodes = tree.Nodes;
nodes.Clear();
LoadTreeNodes(nodes, service.RootItem);
tree.AfterCheck -= OnNodeChecked;
tree.AfterCheck += OnNodeChecked;
}
catch (Exception exception)
{
MessageBox.Show("Loading failed." + exception.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void LoadTreeNodes(TreeNodeCollection tree, ReportItem parent)
{
foreach (var child in parent.Children)
{
var parentNode = new TreeNode
{
Name = child.Name,
Text = child.Name,
Tag = child
};
tree.Add(parentNode);
LoadTreeNodes(parentNode.Nodes, child);
}
}
#endregion
#region Cleanup
private void btnDest_Click(object sender, EventArgs e)
{
DialogResult result = dlgDest.ShowDialog();
if (result == DialogResult.OK)
{
txtLocalPath.Text = dlgDest.SelectedPath;
}
}
private void rptDestTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
uploadPath = ROOT_FOLDER + e.Node.FullPath.Replace("\\", PATH_SEPERATOR);
}
private void ReportSync_FormClosed(object sender, FormClosedEventArgs e)
{
if (!chkSaveSource.Checked)
{
Settings.Default.SourcePassword = string.Empty;
}
if (!chkSaveDest.Checked)
{
Settings.Default.DestPassword = string.Empty;
}
Settings.Default.Save();
}
private void mapDataSourcesToolStripMenuItem_Click(object sender, EventArgs e)
{
// Todo
////var frmMapDS = new MapDatasources();
////frmMapDS.sourceDS = sourceDS;
////frmMapDS.destDS = destDS;
////var result = frmMapDS.ShowDialog();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
var res = dlgOpenFile.ShowDialog();
if (res == DialogResult.OK)
{
var data = File.ReadAllLines(dlgOpenFile.FileName);
int stage = 0;
foreach (var line in data)
{
if (line == SOURCE_SELECTION_START)
{
stage = 1;
continue;
}
else if (line == DEST_SELECTION_START)
{
stage = 2;
continue;
}
else if (line == MAPPING_START)
{
stage = 3;
continue;
}
switch (stage)
{
case 1:
checkNodeIfPathExists(rptSourceTree.Nodes, line);
break;
case 2:
checkNodeIfPathExists(rptDestTree.Nodes, line);
break;
case 3:
var entryParts = line.Split('=');
if (entryParts.Length == 2 && destDS.ContainsKey(entryParts[0]))
{
destDS[entryParts[0]] = entryParts[1];
}
break;
}
}
}
}
private void checkNodeIfPathExists(TreeNodeCollection nodes, string path)
{
var parts = path.Split(new char[] { '\\' }, 2);
var key = parts[0];
if (nodes.ContainsKey(key))
{
if (parts.Length == 1)
{
nodes[key].Checked = true;
}
else
{
nodes[key].Expand();
checkNodeIfPathExists(nodes[key].Nodes, parts[1]);
}
}
}
private string saveCheckedNodes(TreeNodeCollection nodes)
{
var data = "";
foreach (TreeNode node in nodes)
{
if (node.Checked)
{
data += node.FullPath + Environment.NewLine;
}
data += saveCheckedNodes(node.Nodes);
}
return data;
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SetPathAndSave();
}
private void SetPathAndSave()
{
var res = dlgSaveFile.ShowDialog();
if (res == DialogResult.OK)
{
pathOnDisk = dlgSaveFile.FileName;
SaveSelectedNodesToDisk();
}
}
// Todo: This is overkill
private void SaveSelectedNodesToDisk()
{
if (!String.IsNullOrEmpty(pathOnDisk))
{
string data = SOURCE_SELECTION_START + Environment.NewLine;
data += saveCheckedNodes(rptSourceTree.Nodes);
data += DEST_SELECTION_START + Environment.NewLine;
data += saveCheckedNodes(rptDestTree.Nodes);
data += MAPPING_START + Environment.NewLine;
//save mapping
foreach (var entry in destDS)
{
data += entry.Key + "=" + entry.Value + Environment.NewLine;
}
File.WriteAllText(pathOnDisk, data);
}
else
{
SetPathAndSave();
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveSelectedNodesToDisk();
}
#endregion
}
}