forked from jtimperley/ReportSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServiceWrapper.cs
550 lines (477 loc) · 11 KB
/
ServiceWrapper.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
namespace ReportSync
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using ReportService;
public class ServiceWrapper
{
private const string _Webservice = "/ReportService2005.asmx";
private string _BasePath;
private ReportingService2005 _Service;
private string _ServiceUrl;
private string _Username;
private string _Password;
public ServiceWrapper()
{
AllItems = new Dictionary<string, ReportItem>();
DataSources = new Dictionary<string, string>();
Schedules = new Dictionary<string, Schedule>();
}
#region Properties
public string BasePath
{
get
{
return _BasePath;
}
set
{
if (value != _BasePath)
{
_BasePath = value;
DisposeOfService();
}
}
}
public string ServiceUrl
{
get
{
return _ServiceUrl;
}
set
{
if (value != _ServiceUrl)
{
var serviceUrl = value;
if (serviceUrl != null
&& !serviceUrl.EndsWith(_Webservice, StringComparison.InvariantCultureIgnoreCase))
{
serviceUrl += _Webservice;
}
_ServiceUrl = serviceUrl;
DisposeOfService();
}
}
}
public string Username
{
get
{
return _Username;
}
set
{
if (value != _Username)
{
_Username = value;
DisposeOfService();
}
}
}
public string Password
{
get
{
return _Password;
}
set
{
if (value != _Password)
{
_Password = value;
DisposeOfService();
}
}
}
public ReportingService2005 Service
{
get
{
if (_Service == null)
{
var service = new ReportingService2005();
service.Url = ServiceUrl;
service.Credentials = BuildCredentials();
_Service = service;
}
return _Service;
}
}
public ReportItem RootItem { get; private set; }
public Dictionary<string, ReportItem> AllItems { get; private set; }
public Dictionary<string, string> DataSources { get; private set; }
public Dictionary<string, Schedule> Schedules { get; private set; }
public ServiceWrapper SourceService { get; set; }
#endregion
#region Setup
public bool LoadReportItems()
{
AllItems.Clear();
DataSources.Clear();
var service = Service;
var rootItem = new ReportItem
{
Name = "/",
Path = "/",
ItemType = ItemTypeEnum.Unknown
};
RootItem = rootItem;
return LoadReportItems(service, rootItem);
}
private bool LoadReportItems(ReportingService2005 service, ReportItem parent)
{
var dataSources = DataSources;
var newItems = GetReportItems(service, parent.Path);
foreach (var item in newItems)
{
switch (item.Type)
{
case ItemTypeEnum.DataSource:
if (!dataSources.ContainsKey(item.Name))
{
dataSources.Add(item.Name, item.Path);
}
break;
case ItemTypeEnum.Folder:
var newParent = AddReportItem(parent, item, true);
LoadReportItems(service, newParent);
break;
case ItemTypeEnum.Model:
break;
default:
AddReportItem(parent, item, false);
break;
}
}
return true;
}
private ReportItem AddReportItem(ReportItem parent, CatalogItem item, bool isDirectory)
{
var child = new ReportItem
{
Key = item.Path.ToLowerInvariant(),
Name = item.Name,
Path = item.Path,
ParentPath = parent.Path,
Item = item,
ItemType = item.Type,
Parent = parent
};
parent.Children.Add(child);
AllItems.Add(child.Key, child);
return child;
}
private CatalogItem[] GetReportItems(ReportingService2005 service, string path)
{
try
{
return service.ListChildren(path, false);
}
catch (Exception exception)
{
// Todo: Display error message
return new CatalogItem[0];
}
}
private NetworkCredential BuildCredentials()
{
var username = Username;
var password = Password;
if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password))
{
return null;
}
var usernameParts = username.Split('\\', '/');
if (usernameParts.Length > 2)
{
throw new InvalidOperationException("Invalid username.");
}
if (usernameParts.Length == 2)
{
return new NetworkCredential(usernameParts[1], password, usernameParts[0]);
}
return new NetworkCredential(username, password);
}
private void DisposeOfService()
{
var service = _Service;
if (service != null)
{
service.Dispose();
_Service = null;
}
}
#endregion
#region Load
public void LoadItem(ReportItem item)
{
switch (item.ItemType)
{
case ItemTypeEnum.Report:
case ItemTypeEnum.LinkedReport:
LoadReport(item);
LoadSubscriptions(item);
break;
case ItemTypeEnum.Resource:
LoadResource(item);
break;
}
}
private void LoadReport(ReportItem item)
{
item.ReportDefinition = _Service.GetReportDefinition(item.Path);
item.DataSources = _Service.GetItemDataSources(item.Path);
}
private void LoadSubscriptions(ReportItem item)
{
var subscriptions = _Service.ListSubscriptions(item.Path, Username);
foreach (var subscription in subscriptions)
{
ExtensionSettings extensionSettings;
string description;
ActiveState active;
string status;
string eventType;
string matchData;
ParameterValue[] values;
var result = _Service.GetSubscriptionProperties(subscription.SubscriptionID, out extensionSettings, out description, out active, out status, out eventType, out matchData, out values);
var subscriptionItem = new SubscriptionItem
{
ExtensionSettings = extensionSettings,
Description = description,
ActiveState = active,
Status = status,
EventType = eventType,
MatchData = matchData,
ParameterValues = values
};
item.Subscriptions.Add(subscriptionItem);
var schedule = Schedules.Select(s => s.Value)
.FirstOrDefault(s => s.ScheduleID == matchData);
if (schedule != null)
{
subscriptionItem.ScheduleName = schedule.Name;
}
}
}
private void LoadResource(ReportItem item)
{
string resourceType;
item.ResourceContents = _Service.GetResourceContents(item.Path, out resourceType);
item.ResourceType = resourceType;
}
#endregion
#region Upload
public void UploadItem(ReportItem item)
{
switch (item.ItemType)
{
case ItemTypeEnum.Folder:
UploadFolder(item);
break;
case ItemTypeEnum.Report:
case ItemTypeEnum.LinkedReport:
UploadReport(item);
UploadSubscriptions(item);
break;
case ItemTypeEnum.Resource:
UploadResource(item);
break;
case ItemTypeEnum.DataSource:
UploadDataSource(item);
break;
}
}
private void UploadFolder(ReportItem item)
{
var exists = HasItem(item);
if (exists)
{
return;
}
var parent = item.Parent;
var parentPath = parent != null ? parent.Path : "/";
_Service.CreateFolder(item.Name, parentPath, null);
}
private void UploadDataSource(ReportItem item)
{
// Todo
////_Service.CreateDataSource(item.Name, item.ParentPath, true, );
}
private void UploadReport(ReportItem item)
{
_Service.CreateReport(item.Name, item.ParentPath, true, item.ReportDefinition, null);
var dataSources = GetDataSources(item.DataSources);
if (dataSources.Length > 0)
{
_Service.SetItemDataSources(item.Path, dataSources);
}
}
private void UploadResource(ReportItem item)
{
_Service.CreateResource(item.Name, item.ParentPath, true, item.ResourceContents, item.ResourceType, null);
}
private void UploadDataSources(ReportItem item)
{
// Todo
}
private void UploadSubscriptions(ReportItem item)
{
foreach (var subscription in item.Subscriptions)
{
UploadSubscription(item, subscription);
}
}
private void UploadSubscription(ReportItem item, SubscriptionItem subscription)
{
try
{
var schedule = Schedules.Select(s => s.Value)
.FirstOrDefault(s => String.Equals(s.Name, subscription.ScheduleName, StringComparison.InvariantCultureIgnoreCase));
if (schedule == null)
{
return;
}
_Service.CreateSubscription(item.Path, subscription.ExtensionSettings, subscription.Description,
subscription.EventType, schedule.ScheduleID, subscription.ParameterValues);
}
catch (Exception)
{
// Suppress
}
}
public bool HasItem(ReportItem item)
{
return AllItems.ContainsKey(item.Key);
}
private DataSource[] GetDataSources(DataSource[] oldDataSources)
{
var dataSources = DataSources;
var results = new List<DataSource>();
if (oldDataSources != null)
{
foreach (var oldDataSource in oldDataSources)
{
if (dataSources.ContainsKey(oldDataSource.Name))
{
var reference = new DataSourceReference
{
Reference = dataSources[oldDataSource.Name]
};
var newDataSource = new DataSource
{
Name = oldDataSource.Name,
Item = reference
};
results.Add(newDataSource);
}
}
}
return results.ToArray();
}
#endregion
#region Save
public void SaveItem(ReportItem item)
{
switch (item.ItemType)
{
case ItemTypeEnum.Folder:
SaveFolder(item);
break;
case ItemTypeEnum.Report:
case ItemTypeEnum.LinkedReport:
SaveReport(item);
SaveSubscriptions(item);
break;
case ItemTypeEnum.Resource:
SaveResource(item);
break;
case ItemTypeEnum.DataSource:
SaveDataSource(item);
break;
}
}
private void SaveFolder(ReportItem item)
{
var path = GetItemPath(item);
if (string.IsNullOrEmpty(path))
{
return;
}
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
private void SaveReport(ReportItem item)
{
var path = GetItemPath(item, ".rdl");
if (string.IsNullOrEmpty(path))
{
return;
}
if (File.Exists(path))
{
File.Delete(path);
}
var content = Encoding.UTF8.GetString(item.ReportDefinition);
using (var file = File.CreateText(path))
{
file.Write(content);
file.Flush();
file.Close();
}
}
private void SaveSubscriptions(ReportItem item)
{
// Todo
}
private void SaveResource(ReportItem item)
{
// Todo
}
private void SaveDataSource(ReportItem item)
{
// Todo
}
private string GetItemPath(ReportItem item, string extension = null)
{
var basePath = BasePath;
if (String.IsNullOrEmpty(basePath))
{
return string.Empty;
}
return String.Concat(BasePath, item.Path, extension);
}
#endregion
#region Schedules
public void LoadSchedules()
{
var allSchedules = Schedules;
allSchedules.Clear();
var schedules = _Service.ListSchedules();
foreach (var schedule in schedules)
{
allSchedules.Add(schedule.Name, schedule);
}
}
public Schedule[] GetSchedules()
{
return _Service.ListSchedules();
}
public void AddSchedule(Schedule schedule)
{
if (Schedules.ContainsKey(schedule.Name))
{
return;
}
_Service.CreateSchedule(schedule.Name, schedule.Definition);
}
#endregion
}
}