-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
254 lines (218 loc) · 8.08 KB
/
Program.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
using Hl7.Fhir.Model;
using Hl7.Fhir.Specification;
using Hl7.Fhir.Specification.Snapshot;
using Hl7.Fhir.Specification.Source;
using Hl7.Fhir.Utility;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.Office.Interop.Excel;
namespace FhirProfileXRef
{
class Program
{
const bool includeSubDirs = true;
static readonly string TYPENAME_REFERENCE = FHIRAllTypes.Reference.GetLiteral();
static string _targetPath;
static ZipSource _coreSource;
static CachedResolver _cachedCoreSource;
static DirectorySource _dirSource;
static CachedResolver _cachedDirSource;
static SnapshotGenerator _snapshotGenerator;
static List<string> _coreProfiles;
static List<string> _userProfiles;
static Dictionary<string, string> _mappings = new Dictionary<string, string>();
static Application xlApp = new Application();
static Workbook wb;
static Worksheet ws;
static int count = 2;
static void Main(string[] args)
{
ShowIntro();
if (InitArguments(args))
{
Console.WriteLine($"Location: '{_targetPath}'");
InitCoreProfiles();
InitUserProfiles();
CreateExcelSheet();
ValidateXRef();
SaveExcelSheet();
}
else
{
ShowHelp();
}
//#if DEBUG
Console.ReadLine();
//#endif
}
static void CreateExcelSheet()
{
wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
ws = (Worksheet)wb.Worksheets[1];
ws.Columns[1].ColumnWidth = 20;
ws.Columns[2].ColumnWidth = 30;
ws.Columns[3].ColumnWidth = 55;
ws.Columns[4].ColumnWidth = 55;
ws.Rows[1].Font.Bold = true;
ws.Cells[1, 1] = "Resource name";
ws.Cells[1, 2] = "Element path";
ws.Cells[1, 3] = "Reference found";
ws.Cells[1, 4] = "Reference suggestion";
}
static void SaveExcelSheet()
{
if (System.IO.File.Exists(@"C:\Git\KoppeltaalFhirProfileXRef\FhirProfileXRef.xlsx"))
{
System.IO.File.Delete(@"C:\Git\KoppeltaalFhirProfileXRef\FhirProfileXRef.xlsx");
}
wb.SaveAs(@"C:\Git\KoppeltaalFhirProfileXRef\FhirProfileXRef.xlsx");
xlApp.Workbooks.Close();
}
static bool InitArguments(string[] args)
{
if (args.Length == 0)
{
_targetPath = Directory.GetCurrentDirectory();
return true;
}
if (args.Length == 1)
{
var path = args[0];
if (Directory.Exists(path))
{
_targetPath = args[0];
return true;
}
}
return false;
}
static void ShowIntro()
{
var title = GetAppTitle();
Console.WriteLine(title);
Console.WriteLine(new string('=', title.Length));
Console.WriteLine("FHIR profile cross references validator");
Console.WriteLine("(C) Furore 2017");
Console.WriteLine();
}
static void ShowHelp()
{
Console.WriteLine("Usage: ");
Console.WriteLine($"{ExeTitle} [path]");
}
static void InitCoreProfiles()
{
Console.WriteLine("Load FHIR core resource definitions...");
var src = _coreSource = ZipSource.CreateValidationSource();
_cachedCoreSource = new CachedResolver(src);
var profiles = _coreProfiles = src.ListResourceUris(ResourceType.StructureDefinition).ToList();
Console.WriteLine($"Found {profiles.Count} core definitions.");
}
static void InitUserProfiles()
{
Console.WriteLine($"Fetch profiles in target location...");
var src = _dirSource = new DirectorySource(_targetPath, includeSubDirs);
_cachedDirSource = new CachedResolver(src);
var userProfiles = _userProfiles = src.ListResourceUris().ToList();
Console.WriteLine($"Found {userProfiles.Count} profiles.");
Console.WriteLine($"Determine mappings...");
foreach (var profile in userProfiles)
{
var sd = _cachedDirSource.FindStructureDefinition(profile, false);
if (EnsureSnapshot(sd))
{
var key = ModelInfo.CanonicalUriForFhirCoreType(sd.Type);
if (!_mappings.TryGetValue(key, out string existing))
{
Console.WriteLine($"Map references of type '{sd.Type}' to user profile '{sd.Url}'");
_mappings.Add(key, sd.Url);
}
else
{
Console.WriteLine($"Warning! Ignore duplicate user profile '{sd.Url}' for reference target type '{sd.Type}'");
}
}
}
}
static void ValidateXRef()
{
Console.WriteLine($"Validate x-refs...");
foreach (var profile in _userProfiles)
{
Validate(profile);
}
}
static void Validate(string profileUrl)
{
Console.WriteLine($"Validate '{profileUrl}' ...");
var sd = _cachedDirSource.FindStructureDefinition(profileUrl, false);
if (EnsureSnapshot(sd))
{
foreach (var elem in sd.Snapshot.Element)
{
ValidateElement(elem, sd);
}
}
}
static void ValidateElement(ElementDefinition elem, StructureDefinition sd)
{
foreach (var type in elem.Type)
{
if (type.Code == TYPENAME_REFERENCE)
{
var tgt = type.TargetProfile;
// Console.WriteLine($"'{elem.Path}' => '{tgt}'");
if (_mappings.TryGetValue(tgt, out string profile))
{
Console.WriteLine($"Warning! '{elem.Path}' : '{tgt}' => '{profile}'");
ws.Cells[count, 1] = sd.Name;
ws.Cells[count, 2] = elem.Path;
ws.Cells[count, 3] = tgt;
ws.Cells[count, 4] = profile;
count++;
}
}
}
}
static bool EnsureSnapshot(StructureDefinition sd)
{
if (!sd.HasSnapshot)
{
var generator = GetSnapshotGenerator();
Console.WriteLine($"Generate snapshot for profile '{sd.Url}' ...");
generator.Update(sd);
if (generator.Outcome != null)
{
Console.WriteLine("Snapshot generator returned one or more issues:");
foreach (var issue in generator.Outcome.Issue)
{
Console.WriteLine($"{issue.Details}");
}
return false;
}
}
return true;
}
static SnapshotGenerator GetSnapshotGenerator()
{
if (_snapshotGenerator == null)
{
var src = new MultiResolver(_cachedCoreSource, _cachedDirSource);
_snapshotGenerator = new SnapshotGenerator(src);
}
return _snapshotGenerator;
}
static string GetAppTitle()
{
AssemblyTitleAttribute titleAttr = Attribute.GetCustomAttribute(
Assembly.GetExecutingAssembly(),
typeof(AssemblyTitleAttribute),
false) as AssemblyTitleAttribute;
return titleAttr?.Title;
}
static string ExeTitle => Path.GetFileName(Assembly.GetEntryAssembly().CodeBase);
}
}