-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
1047 lines (912 loc) · 37.4 KB
/
Copy pathProgram.cs
File metadata and controls
1047 lines (912 loc) · 37.4 KB
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2020 by Windward Studios, Inc. All rights reserved.
*
* This program can be copied or used in any manner desired.
*/
using System;
using System.Collections.Generic;
using System.Net;
using log4net.Config;
using System;
using System.IO;
using Exception = System.Exception;
using System.Collections;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using log4net;
using Console = System.Console;
using Convert = System.Convert;
using File = System.IO.File;
using WindwardRestApi.src.Api;
using WindwardRestApi.src.Model;
namespace RunReport
{
public class Program
{
private static WindwardClient client;
private static readonly ILog logWriter = LogManager.GetLogger(typeof(Program));
static async Task Main(string[] args)
{
// get connected
string url = ConfigurationManager.AppSettings["url"];
if ((!url.EndsWith("/")) && (!url.EndsWith("\\")))
url += "/";
Console.Out.WriteLine($"Connecting to URL {url}");
client = new WindwardClient(new Uri(url));
VersionInfo version = await client.GetVersion();
string licenseKey = ConfigurationManager.AppSettings["licenseKey"];
if (string.IsNullOrEmpty(licenseKey) || licenseKey.Equals("[LICENSEKEY]"))
{
logWriter.Warn("License key not set in App.config. If the license is not set on your engine server, the engine will respond with 401 errors.");
}
else
{
client.LicenseKey = licenseKey;
}
Console.Out.WriteLine($"REST server version = {version}");
// if no arguments, then we list out the usage.
if (args.Length < 2)
{
DisplayUsage();
return;
}
// the try here is so we can print out an exception if it is thrown. This code does minimal error checking and no other
// exception handling to keep it simple & clear.
try
{
Console.Out.WriteLine("Running in {0}-bit mode", IntPtr.Size * 8);
// parse the arguments passed in. This method makes no calls to Windward, it merely organizes the passed in arguments.
CommandLine cmdLine = CommandLine.Factory(args);
// This turns on log4net logging. You can also call log4net.Config.XmlConfigurator.Configure(); directly.
// If you do not make this call, then there will be no logging (which you may want off).
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("RunReport.config"));
logWriter.Info($"Starting RunReport ({string.Join(", ", args)})");
DateTime startTime = DateTime.Now;
// run one report
if (!cmdLine.IsPerformance)
{
PerfCounters perfCounters = await RunOneReport(cmdLine, args.Length == 2);
PrintPerformanceCounter(startTime, perfCounters, false);
}
else
{
string dirReports = Path.GetDirectoryName(Path.GetFullPath(cmdLine.ReportFilename)) ?? "";
if (!Directory.Exists(dirReports))
{
Console.Error.WriteLine($"The directory {dirReports} does not exist");
return;
}
// drop out threads - twice the number of cores.
int numThreads = cmdLine.NumThreads;
numReportsRemaining = cmdLine.NumReports;
ReportWorker[] workers = new ReportWorker[numThreads];
for (int ind = 0; ind < numThreads; ind++)
workers[ind] = new ReportWorker(ind, new CommandLine(cmdLine));
Task[] threads = new Task[numThreads];
for (int ind = 0; ind < numThreads; ind++)
threads[ind] = workers[ind].DoWork();
Console.Out.WriteLine($"Start time: {startTime.ToLongTimeString()}, {numThreads} threads, {cmdLine.NumReports} reports");
Console.Out.WriteLine();
// for (int ind = 0; ind < numThreads; ind++)
// threads[ind].Start();
// we wait
await Task.WhenAll(threads);
PerfCounters perfCounters = new PerfCounters();
for (int ind = 0; ind < numThreads; ind++)
perfCounters.Add(workers[ind].perfCounters);
Console.Out.WriteLine();
PrintPerformanceCounter(startTime, perfCounters, true);
}
}
catch (Exception ex)
{
logWriter.Error("RunReport", ex);
string indent = " ";
Console.Error.WriteLine();
Console.Error.WriteLine("Error(s) running the report");
while (ex != null)
{
Console.Error.WriteLine($"{indent}Error: {ex.Message} ({ex.GetType().FullName})\n{indent} stack: {ex.StackTrace}\n");
ex = ex.InnerException;
indent += " ";
}
throw;
}
}
private static void PrintPerformanceCounter(DateTime startTime, PerfCounters perfCounters, bool multiThreaded)
{
DateTime endTime = DateTime.Now;
TimeSpan elapsed = endTime - startTime;
Console.Out.WriteLine("End time: {0}", endTime.ToLongTimeString());
Console.Out.WriteLine($"Elapsed time: {elapsed}");
Console.Out.WriteLine("Time per report: {0}", perfCounters.numReports == 0 ? "n/a" : "" + TimeSpan.FromMilliseconds(elapsed.TotalMilliseconds / perfCounters.numReports));
Console.Out.WriteLine("Pages/report: " + (perfCounters.numReports == 0 ? "n/a" : "" + perfCounters.numPages / perfCounters.numReports));
Console.Out.WriteLine("Pages/sec: {0:N2}", perfCounters.numPages / elapsed.TotalSeconds);
Console.Out.WriteLine("Errors: " + perfCounters.numErrors);
if (multiThreaded)
Console.Out.WriteLine("Below values are totaled across all threads (and so add up to more than the elapsed time)");
Console.Out.WriteLine($" Process: {perfCounters.timeProcess}");
}
private static int numReportsRemaining;
private static bool HasNextReport => Interlocked.Decrement(ref numReportsRemaining) >= 0;
private class ReportWorker
{
private readonly int threadNum;
private readonly CommandLine cmdLine;
internal PerfCounters perfCounters;
public ReportWorker(int threadNum, CommandLine cmdLine)
{
this.threadNum = threadNum;
this.cmdLine = cmdLine;
perfCounters = new PerfCounters();
}
public async Task DoWork()
{
while (HasNextReport)
{
Console.Out.Write($"{threadNum}.");
PerfCounters pc = null;
try
{
pc = await RunOneReport(cmdLine, false);
}
catch (Exception e)
{
pc = new PerfCounters();
pc.numErrors = 1;
}
perfCounters.Add(pc);
}
}
}
private static readonly string[] extImages = { "bmp", "eps", "gif", "jpg", "png", "svg" };
private static readonly string[] extHtmls = { "htm", "html", "xhtml" };
private static async Task<PerfCounters> RunOneReport(CommandLine cmdLine, bool preservePodFraming)
{
DateTime startTime = DateTime.Now;
PerfCounters perfCounters = new PerfCounters();
// Create the template object, based on the file extension
Template.OutputFormatEnum formatOutput = Path.GetExtension(cmdLine.ReportFilename).Substring(1).GetEnumFromValue<Template.OutputFormatEnum>();
Template.FormatEnum formatTemplate = Path.GetExtension(cmdLine.TemplateFilename).Substring(1).GetEnumFromValue<Template.FormatEnum>();
Template template = new Template(formatOutput, File.ReadAllBytes(cmdLine.TemplateFilename), formatTemplate);
template.TrackErrors = cmdLine.VerifyFlag;
if (!string.IsNullOrEmpty(cmdLine.Locale))
template.Properties.Add(new Property("report.locale", cmdLine.Locale));
foreach (KeyValuePair<string, object> pair in cmdLine.Parameters)
template.Parameters.Add(new Parameter(pair.Key, pair.Value));
// Now for each datasource, we apply it to the report. This is complex because it handles all datasource types
foreach (CommandLine.DatasourceInfo dsInfo in cmdLine.Datasources)
{
// build the datasource
switch (dsInfo.Type)
{
// An XPATH 2.0 datasource.
case CommandLine.DatasourceInfo.TYPE.XML:
if (!cmdLine.IsPerformance)
{
if (string.IsNullOrEmpty(dsInfo.SchemaFilename))
Console.Out.WriteLine($"XPath 2.0 datasource: {dsInfo.Filename}");
else
Console.Out.WriteLine("XPath 2.0 datasource: {0}, schema {1}", dsInfo.Filename, dsInfo.SchemaFilename);
}
if (string.IsNullOrEmpty(dsInfo.SchemaFilename))
template.Datasources.Add(new Xml_20DataSource(dsInfo.Name, File.ReadAllBytes(dsInfo.ExConnectionString), null));
else
template.Datasources.Add(new Xml_20DataSource(dsInfo.Name, File.ReadAllBytes(dsInfo.ExConnectionString), File.ReadAllBytes(dsInfo.SchemaFilename)));
break;
// An XPATH 1.0 datasource.
case CommandLine.DatasourceInfo.TYPE.XPATH_1:
if (!cmdLine.IsPerformance)
{
if (string.IsNullOrEmpty(dsInfo.SchemaFilename))
Console.Out.WriteLine(string.Format("XPath 1.0 datasource: {0}", dsInfo.Filename));
else
Console.Out.WriteLine(string.Format("XPath 1.0 datasource: {0}, schema {1}", dsInfo.Filename,
dsInfo.SchemaFilename));
}
if (string.IsNullOrEmpty(dsInfo.SchemaFilename))
template.Datasources.Add(new Xml_10DataSource(dsInfo.Name, File.ReadAllBytes(dsInfo.ExConnectionString), null));
else
template.Datasources.Add(new Xml_10DataSource(dsInfo.Name, File.ReadAllBytes(dsInfo.ExConnectionString), File.ReadAllBytes(dsInfo.SchemaFilename)));
break;
case CommandLine.DatasourceInfo.TYPE.JSON:
if (!cmdLine.IsPerformance)
Console.Out.WriteLine($"JSON datasource: {dsInfo.Filename}");
template.Datasources.Add(new JsonDataSource(dsInfo.Name, File.ReadAllBytes(dsInfo.ExConnectionString)));
break;
// An OData datasource.
case CommandLine.DatasourceInfo.TYPE.ODATA:
if (!cmdLine.IsPerformance)
Console.Out.WriteLine(string.Format("OData datasource: {0}", dsInfo.Filename));
template.Datasources.Add(new ODataDataSource(dsInfo.Name, dsInfo.ExConnectionString));
break;
// A SalesForce datasource.
case CommandLine.DatasourceInfo.TYPE.SFORCE:
if (!cmdLine.IsPerformance)
Console.Out.WriteLine(string.Format("SalesForce datasource: {0}", dsInfo.Filename));
template.Datasources.Add(new SalesforceDataSource(dsInfo.Name, dsInfo.ExConnectionString));
break;
case CommandLine.DatasourceInfo.TYPE.SQL:
if (!cmdLine.IsPerformance)
Console.Out.WriteLine(string.Format("{0} datasource: {1}", dsInfo.SqlDriverInfo.Name,
dsInfo.ConnectionString));
template.Datasources.Add(new SqlDataSource(dsInfo.Name, dsInfo.SqlDriverInfo.Classname, dsInfo.ConnectionString));
break;
default:
throw new ArgumentException(string.Format("Unknown datasource type {0}", dsInfo.Type));
}
}
if (!cmdLine.IsPerformance)
Console.Out.WriteLine("Calling REST engine to start generating report");
// template.Properties.Add(new
// Property("pdf.PDF_UA", "true"));
// we have nothing else to do, so we wait till we get the result.
Document document = await client.PostDocument(template);
string guid = document.Guid;
if (!cmdLine.IsPerformance)
Console.Out.WriteLine($"REST Engine has accepted job {guid}");
// wait for it to complete
// instead of this you can use: template.Callback = "http://localhost/alldone/{guid}";
HttpStatusCode status = await client.GetDocumentStatus(guid);
while (status == HttpStatusCode.Created || status == HttpStatusCode.Accepted || status == HttpStatusCode.NotFound)
{
await Task.Delay(1000);
status = await client.GetDocumentStatus(guid);
}
// we have nothing else to do, so we wait till we get the result.
document = await client.GetDocument(guid);
// delete it off the server
await client.DeleteDocument(guid);
if (!cmdLine.IsPerformance)
Console.Out.WriteLine($"REST Engine has completed job {document.Guid}");
perfCounters.timeProcess = DateTime.Now - startTime;
perfCounters.numPages = document.NumberOfPages;
perfCounters.numReports = 1;
PrintVerify(document);
// save
if (document.Data != null)
{
await using var outputStream = cmdLine.GetOutputStream();
await outputStream.WriteAsync(document.Data);
}
else
{
{
string prefix = Path.GetFileNameWithoutExtension(cmdLine.ReportFilename);
string directory = Path.GetDirectoryName(Path.GetFullPath(cmdLine.ReportFilename));
string extension = Path.GetExtension(cmdLine.ReportFilename);
for (int fileNumber = 0; fileNumber < document.Pages.Length; fileNumber++)
{
string filename = Path.Combine(directory, prefix + "_" + fileNumber + extension);
filename = Path.GetFullPath(filename);
File.WriteAllBytes(filename, document.Pages[fileNumber]);
Console.Out.WriteLine(" document page written to " + filename);
}
}
}
if (!cmdLine.IsPerformance)
{
Console.Out.WriteLine($"{cmdLine.ReportFilename} built, {document.NumberOfPages} pages long.");
Console.Out.WriteLine($"Elapsed time: {DateTime.Now - startTime}");
}
if (cmdLine.Launch)
{
Console.Out.WriteLine(string.Format("launching report {0}", cmdLine.ReportFilename));
System.Diagnostics.Process.Start(cmdLine.ReportFilename);
}
return perfCounters;
}
private static void PrintVerify(Document document)
{
if (document.Errors != null)
foreach (Issue issue in document.Errors)
Console.Error.WriteLine(issue.Message);
}
private static void DisplayUsage()
{
Console.Out.WriteLine("Windward Reports REST Engline C# Client API");
Console.Out.WriteLine("usage: RunReport template_file output_file [-basedir path] [-xml xml_file | -sql connection_string | -sforce | -oracle connection_string | -ole oledb_connection_string] [key=value | ...]");
Console.Out.WriteLine(" The template file can be a docx, pptx, or xlsx file.");
Console.Out.WriteLine(" The output file extension determines the report type created:");
Console.Out.WriteLine(" output.csv - SpreadSheet CSV file");
Console.Out.WriteLine(" output.docm - Word DOCM file");
Console.Out.WriteLine(" output.docx - Word DOCX file");
Console.Out.WriteLine(" output.htm - HTML file with no CSS");
Console.Out.WriteLine(" output.html - HTML file with CSS");
Console.Out.WriteLine(" output.pdf - Acrobat PDF file");
Console.Out.WriteLine(" output.pptm - PowerPoint PPTM file");
Console.Out.WriteLine(" output.pptx - PowerPoint PPTX file");
Console.Out.WriteLine(" output.prn - Printer where \"output\" is the printer name");
Console.Out.WriteLine(" output.rtf - Rich Text Format file");
Console.Out.WriteLine(" output.txt - Ascii text file");
Console.Out.WriteLine(" output.xhtml - XHTML file with CSS");
Console.Out.WriteLine(" output.xlsm - Excel XLSM file");
Console.Out.WriteLine(" output.xlsx - Excel XLSX file");
Console.Out.WriteLine(" -launch - will launch the report when complete.");
Console.Out.WriteLine(" -performance:123 - will run the report 123 times.");
Console.Out.WriteLine(" output file is used for directory and extension for reports");
Console.Out.WriteLine(" -threads:4 - will create 4 threads when running -performance.");
Console.Out.WriteLine(" -verify:N - turn on the error handling and verify feature where N is a number: 0 (none) , 1 (track errors), 2 (verify), 3 (all). The list of issues is printed to the standard error.");
Console.Out.WriteLine(" -version=9 - sets the template to the passed version (9 in this example).");
Console.Out.WriteLine(" encoding=UTF-8 (or other) - set BEFORE datasource to specify an encoding.");
Console.Out.WriteLine(" locale=en_US - set the locale passed to the engine.");
Console.Out.WriteLine(" The datasource is identified with a pair of parameters");
Console.Out.WriteLine(" -json filename - passes a JSON file as the datasource");
Console.Out.WriteLine(" filename can be a url/filename or a connection string");
Console.Out.WriteLine(" -odata url - passes a url as the datasource accessing it using the OData protocol");
Console.Out.WriteLine(" -sforce - password should be password + security_token (passwordtoken)");
Console.Out.WriteLine(" -xml filename - XPath 2.0 passes an xml file as the datasource");
Console.Out.WriteLine(" -xml xmlFilename=schema:schemaFilename - passes an xml file and a schema file as the datasource");
Console.Out.WriteLine(" filename can be a url/filename or a connection string");
Console.Out.WriteLine(" -xpath filename - uses the old XPath 1.0 datasource.");
Console.Out.WriteLine(" -xml xmlFilename=schema:schemaFilename - passes an xml file and a schema file as the datasource");
Console.Out.WriteLine(" filename can be a url/filename or a connection string");
foreach (AdoDriverInfo di in AdoDriverInfo.AdoConnectors)
Console.Out.WriteLine(" -" + di.Name + " connection_string ex: " + di.Example);
Console.Out.WriteLine(" if a datasource is named you use the syntax -type:name (ex: -xml:name filename.xml)");
Console.Out.WriteLine(" You can have 0-N key=value pairs that are passed to the datasource Map property");
Console.Out.WriteLine(" If the value starts with I', F', or D' it parses it as an integer, float, or date(yyyy-MM-ddThh:mm:ss)");
Console.Out.WriteLine(" If the value is * it will set a filter of all");
Console.Out.WriteLine(" If the value is \"text,text,...\" it will set a filter of all");
}
}
/// <summary>
/// This class contains everything passed in the command line. It makes no calls to Windward Reports.
/// </summary>
internal class CommandLine
{
private byte[] templateFile;
/// <summary>
/// Create the object.
/// </summary>
/// <param name="templateFilename">The name of the template file.</param>
/// <param name="reportFilename">The name of the report file. null for printer reports.</param>
public CommandLine(string templateFilename, string reportFilename)
{
TemplateFilename = Path.GetFullPath(templateFilename);
if (!reportFilename.ToLower().EndsWith(".prn"))
reportFilename = Path.GetFullPath(reportFilename);
Launch = false;
ReportFilename = reportFilename;
Parameters = new Dictionary<string, object>();
Datasources = new List<DatasourceInfo>();
NumThreads = Environment.ProcessorCount * 2;
VerifyFlag = 0;
}
public CommandLine(CommandLine src)
{
TemplateFilename = src.TemplateFilename;
ReportFilename = src.ReportFilename;
Parameters = src.Parameters == null ? null : new Dictionary<string, object>(src.Parameters);
Datasources = src.Datasources == null ? null : new List<DatasourceInfo>(src.Datasources);
DataProviders = src.DataProviders == null ? null : new Dictionary<string, DataSource>(src.DataProviders);
Locale = src.Locale;
Launch = src.Launch;
TemplateVersion = src.TemplateVersion;
NumReports = src.NumReports;
NumThreads = src.NumThreads;
BaseDirectory = src.BaseDirectory;
VerifyFlag = src.VerifyFlag;
templateFile = src.templateFile?.ToArray();
}
/// <summary>
/// The name of the template file.
/// </summary>
public string TemplateFilename { get; private set; }
public Stream GetTemplateStream()
{
if (TemplateFilename.ToLower().StartsWith("http:") || TemplateFilename.ToLower().StartsWith("https:"))
{
WebRequest request = WebRequest.Create(TemplateFilename);
return request.GetResponse().GetResponseStream();
}
if (templateFile == null)
templateFile = File.ReadAllBytes(TemplateFilename);
return new MemoryStream(templateFile);
}
/// <summary>
/// The name of the report file. null for printer reports.
/// </summary>
public string ReportFilename { get; private set; }
/// <summary>
/// The output stream for the report.
/// </summary>
public Stream GetOutputStream()
{
if (!IsPerformance)
return new FileStream(ReportFilename, FileMode.Create, FileAccess.Write, FileShare.None);
string dirReports = Path.GetDirectoryName(ReportFilename) ?? "";
string extReport = ReportFilename.Substring(ReportFilename.LastIndexOf('.'));
string filename = Path.GetFullPath(Path.Combine(dirReports, "rpt_" + Guid.NewGuid()) + extReport);
return new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
}
/// <summary>
/// If we are caching the data providers, this is them for passes 1 .. N (set on pass 0)
/// </summary>
public IDictionary<string, DataSource> DataProviders { get; set; }
/// <summary>
/// true if launch the app at the end.
/// </summary>
public bool Launch { get; private set; }
/// <summary>
/// The template version number. 0 if not set.
/// </summary>
public int TemplateVersion { get; private set; }
/// <summary>
/// The name/value pairs for variables passed to the datasources.
/// </summary>
public Dictionary<string, object> Parameters { get; private set; }
/// <summary>
/// The parameters passed for each datasource to be created.
/// </summary>
public List<DatasourceInfo> Datasources { get; private set; }
/// <summary>
/// The locale to run under.
/// </summary>
public string Locale { get; private set; }
/// <summary>
/// For performance modeling, how many reports to run.
/// </summary>
public int NumReports { get; private set; }
/// <summary>
/// true if requesting a performance run
/// </summary>
public bool IsPerformance => NumReports != 0;
/// <summary>
/// The number of threads to launch if running a performance test.
/// </summary>
public int NumThreads { get; private set; }
public int VerifyFlag { get; private set; }
/// <summary>
/// Base directory.
/// </summary>
public string BaseDirectory { get; private set; }
/// <summary>
/// The parameters passed for a single datasource. All filenames are expanded to full paths so that if an exception is
/// thrown you know exactly where the file is.
/// </summary>
internal class DatasourceInfo
{
/// <summary>
/// What type of datasource.
/// </summary>
internal enum TYPE
{
/// <summary>
/// Use the REST protocol passing the credentials on the first request.
/// </summary>
REST,
/// <summary>
/// A SQL database.
/// </summary>
SQL,
/// <summary>
/// An XML file using Saxon.
/// </summary>
XML,
/// <summary>
/// An XML file using the .NET XPath library.
/// </summary>
XPATH_1,
/// <summary>
/// An OData url.
/// </summary>
ODATA,
/// <summary>
/// JSON data source.
/// </summary>
JSON,
/// <summary>
/// SalesForce data source
/// </summary>
SFORCE,
/// <summary>
/// A data set
/// </summary>
DATA_SET
}
private readonly TYPE type;
private readonly string name;
private readonly string filename;
private readonly string schemaFilename;
private readonly AdoDriverInfo sqlDriverInfo;
private readonly string connectionString;
private readonly string username;
private readonly string password;
private readonly string securitytoken;//only used for sforce
private readonly string podFilename;
private readonly string encoding;
public bool restful;
/// <summary>
/// Create the object for a PLAYBACK datasource.
/// </summary>
/// <param name="filename">The playback filename.</param>
/// <param name="type">What type of datasource.</param>
public DatasourceInfo(string filename, TYPE type)
{
this.filename = Path.GetFullPath(filename);
this.type = type;
}
/// <summary>
/// Create the object for a XML datasource.
/// </summary>
/// <param name="name">The name for this datasource.</param>
/// <param name="filename">The XML filename.</param>
/// <param name="schemaFilename">The XML schema filename. null if no schema.</param>
/// <param name="username">The username if credentials are needed to access the datasource.</param>
/// <param name="password">The password if credentials are needed to access the datasource.</param>
/// <param name="podFilename">The POD filename if datasets are being passed.</param>
/// <param name="type">What type of datasource.</param>
public DatasourceInfo(string name, string filename, string schemaFilename, string username, string password, string podFilename, TYPE type)
{
this.name = name ?? string.Empty;
this.filename = GetFullPath(filename);
if (!string.IsNullOrEmpty(schemaFilename))
this.schemaFilename = GetFullPath(schemaFilename);
this.username = username;
this.password = password;
if (!string.IsNullOrEmpty(podFilename))
this.podFilename = GetFullPath(podFilename);
this.type = type;
}
/// <summary>
/// Create the object for a JSON datasource.
/// </summary>
/// <param name="name">The name for this datasource.</param>
/// <param name="filename">The XML filename.</param>
/// <param name="schemaFilename">The XML schema filename. null if no schema.</param>
/// <param name="encoding">Theencoding of the JSON file.</param>
/// <param name="type">What type of datasource.</param>
public DatasourceInfo(string name, string filename, string schemaFilename, string encoding, TYPE type)
{
this.name = name ?? string.Empty;
this.filename = GetFullPath(filename);
if (!string.IsNullOrEmpty(schemaFilename))
this.schemaFilename = GetFullPath(schemaFilename);
this.encoding = encoding;
this.type = type;
}
/// <summary>
/// Copy constructor. Does a deep copy.
/// </summary>
/// <param name="src">Initialize with the values in this object.</param>
public DatasourceInfo(DatasourceInfo src)
{
type = src.type;
name = src.name;
filename = src.filename;
schemaFilename = src.schemaFilename;
sqlDriverInfo = src.sqlDriverInfo;
connectionString = src.connectionString;
username = src.username;
password = src.password;
securitytoken = src.securitytoken;
podFilename = src.podFilename;
encoding = src.encoding;
restful = src.restful;
}
private static string GetFullPath(string filename)
{
int pos = filename.IndexOf(':');
if ((pos == -1) || (pos == 1))
return Path.GetFullPath(filename);
return filename;
}
/// <summary>
/// Create the object for a SQL datasource.
/// </summary>
/// <param name="name">The name for this datasource.</param>
/// <param name="sqlDriverInfo">The DriverInfo for the selected SQL vendor.</param>
/// <param name="connectionString">The connection string to connect to the database.</param>
/// <param name="username">The username if credentials are needed to access the datasource.</param>
/// <param name="password">The password if credentials are needed to access the datasource.</param>
/// <param name="podFilename">The POD filename if datasets are being passed.</param>
/// <param name="type">What type of datasource.</param>
public DatasourceInfo(string name, AdoDriverInfo sqlDriverInfo, string connectionString, string username, string password, string podFilename, TYPE type)
{
this.name = name;
this.sqlDriverInfo = sqlDriverInfo;
this.connectionString = connectionString.Trim();
this.username = username;
this.password = password;
if (!string.IsNullOrEmpty(podFilename))
this.podFilename = Path.GetFullPath(podFilename);
this.type = type;
}
/// <summary>
/// What type of datasource.
/// </summary>
public TYPE Type => type;
/// <summary>
/// The name for this datasource.
/// </summary>
public string Name => name;
/// <summary>
/// The XML or playback filename.
/// </summary>
public string Filename => filename;
/// <summary>
/// The XML schema filename. null if no schema.
/// </summary>
public string SchemaFilename => schemaFilename;
/// <summary>
/// The DriverInfo for the selected SQL vendor.
/// </summary>
public AdoDriverInfo SqlDriverInfo => sqlDriverInfo;
/// <summary>
/// The connection string to connect to the database.
/// </summary>
public string ConnectionString => connectionString;
/// <summary>
/// The connection string to connect to the database.
/// </summary>
public string ExConnectionString => filename;
/// <summary>
/// The username if credentials are needed to access the datasource.
/// </summary>
public string Username => username;
/// <summary>
/// The password if credentials are needed to access the datasource.
/// </summary>
public string Password => password;
/// <summary>
/// The POD filename if datasets are being passed.
/// </summary>
public string PodFilename => podFilename;
public string Encoding => encoding;
}
/// <summary>
/// Create a CommandLine object from the command line passed to the program.
/// </summary>
/// <param name="args">The arguments passed to the program.</param>
/// <returns>A CommandLine object populated from the args.</returns>
public static CommandLine Factory(IList<string> args)
{
CommandLine rtn = new CommandLine(args[0], args[1]);
string username = null, password = null, podFilename = null, encoding = null;
for (int ind = 2; ind < args.Count; ind++)
{
string[] sa = args[ind].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
string cmd = sa[0].Trim();
string name = sa.Length < 2 ? "" : sa[1].Trim();
if (cmd == "-performance")
{
rtn.NumReports = int.Parse(name);
continue;
}
if (cmd == "-threads")
{
rtn.NumThreads = int.Parse(name);
continue;
}
if (cmd == "-verify")
{
rtn.VerifyFlag = int.Parse(name);
continue;
}
if (cmd == "-launch")
{
rtn.Launch = true;
continue;
}
if (cmd == "-basedir")
{
rtn.BaseDirectory = args[++ind];
continue;
}
if (cmd == "-rest")
{
if (rtn.Datasources.Count > 0)
rtn.Datasources[rtn.Datasources.Count - 1].restful = true;
continue;
}
if ((cmd == "-xml") || (cmd == "-xpath"))
{
string filename = args[++ind];
string schemaFilename = null;
int split = filename.IndexOf("=schema:");
if (split == -1)
schemaFilename = null;
else
{
schemaFilename = filename.Substring(split + 8).Trim();
filename = filename.Substring(0, split).Trim();
}
DatasourceInfo.TYPE type = (cmd == "-rest" ? DatasourceInfo.TYPE.REST
: (cmd == "-xpath" ? DatasourceInfo.TYPE.XPATH_1 : DatasourceInfo.TYPE.XML));
DatasourceInfo datasourceOn = new DatasourceInfo(name, filename, schemaFilename, username, password, podFilename, type);
rtn.Datasources.Add(datasourceOn);
username = password = podFilename = null;
continue;
}
if (cmd == "-odata")
{
string url = args[++ind];
DatasourceInfo datasourceOn = new DatasourceInfo(name, url, null, username, password, podFilename, DatasourceInfo.TYPE.ODATA);
rtn.Datasources.Add(datasourceOn);
username = password = podFilename = null;
continue;
}
if (cmd == "-json")
{
string url = args[++ind];
DatasourceInfo datasourceOn = new DatasourceInfo(name, url, null, encoding, DatasourceInfo.TYPE.JSON);
rtn.Datasources.Add(datasourceOn);
username = password = podFilename = null;
continue;
}
if (cmd == "-dataset")
{
string dataSetStr = args[++ind];
DatasourceInfo dsInfo = new DatasourceInfo(name, dataSetStr, null, null, DatasourceInfo.TYPE.DATA_SET);
rtn.Datasources.Add(dsInfo);
username = password = podFilename = null;
continue;
}
bool isDb = false;
if (cmd == "-sforce")
{
string url = "https://login.salesforce.com";
DatasourceInfo datasourceOn = new DatasourceInfo(name, url, null, username, password, podFilename, DatasourceInfo.TYPE.SFORCE);
rtn.Datasources.Add(datasourceOn);
isDb = true;
username = password = podFilename = null;
}
foreach (AdoDriverInfo di in AdoDriverInfo.AdoConnectors)
if (cmd == "-" + di.Name)
{
if (((di.Name == "odbc") || (di.Name == "oledb")) && (IntPtr.Size != 4))
Console.Out.WriteLine("Warning - some ODBC & OleDB connectors only work in 32-bit mode.");
DatasourceInfo datasourceOn = new DatasourceInfo(name, di, args[++ind], username, password, podFilename, DatasourceInfo.TYPE.SQL);
rtn.Datasources.Add(datasourceOn);
isDb = true;
username = password = podFilename = null;
break;
}
if (isDb)
continue;
// assume this is a key=value
string[] keyValue = args[ind].Split(new[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
if (keyValue.Length != 2)
if (keyValue.Length == 1 && args[ind].EndsWith("="))
{
// We have a variable with the empty value.
keyValue = new string[2] { keyValue[0], "" };
}
else if (keyValue.Length < 2)
throw new ArgumentException(string.Format("Unknown option {0}", args[ind]));
else
{
// put the rest together.
string val = "";
for (int i = 1; i < keyValue.Length; i++)
{
val += keyValue[i];
if (i < keyValue.Length - 1)
val += '=';
}
keyValue = new string[2] { keyValue[0], val };
}
switch (keyValue[0])
{
case "locale":
rtn.Locale = keyValue[1];
break;
case "version":
rtn.TemplateVersion = int.Parse(keyValue[1]);
break;
case "username":
username = keyValue[1];
break;
case "password":
password = keyValue[1];
break;
case "pod":
podFilename = keyValue[1];
break;
case "encoding":
encoding = keyValue[1];
break;
default:
object value;
// may be a list
if (keyValue[1].IndexOf(',') != -1)
{
string[] tok = keyValue[1].Split(new char[] { ',' });
IList items = new ArrayList();
foreach (string elem in tok)
items.Add(ConvertValue(elem, rtn.Locale));
value = items;
}
else
value = ConvertValue(keyValue[1], rtn.Locale);
rtn.Parameters.Add(keyValue[0], value);
break;
}
}
return rtn;
}
private static object ConvertValue(string keyValue, string locale)
{
if (keyValue.StartsWith("I'"))
return Convert.ToInt64(keyValue.Substring(2));
if (keyValue.StartsWith("F'"))
return Convert.ToDouble(keyValue.Substring(2));
if (keyValue.StartsWith("D'"))
return Convert.ToDateTime(keyValue.Substring(2), (locale == null ? CultureInfo.CurrentCulture : new CultureInfo(locale)));
return keyValue.Replace("\\n", "\n").Replace("\\t", "\t");
}
}
internal class PerfCounters
{
internal TimeSpan timeProcess;
internal int numReports;
internal int numPages;
public int numErrors;
public void Add(PerfCounters pc)
{
timeProcess += pc.timeProcess;
numReports += pc.numReports;
numPages += pc.numPages;
numErrors += pc.numErrors;
}
}
/// <summary>
/// Information on all known ADO.NET connectors.
/// </summary>
internal class AdoDriverInfo
{
private readonly string name;
private readonly string classname;
private readonly string example;
/// <summary>
/// Create the object for a given vendor.
/// </summary>
/// <param name="name">The -vendor part in the command line (ex: -sql).</param>
/// <param name="classname">The classname of the connector.</param>
/// <param name="example">A sample commandline.</param>