-
Notifications
You must be signed in to change notification settings - Fork 0
/
FhirPathTerminologies.cs
160 lines (143 loc) · 6.09 KB
/
FhirPathTerminologies.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
using Hl7.Fhir.ElementModel;
using Hl7.Fhir.Model;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Specification;
using System.Collections.Generic;
using System;
namespace FhirPathLab_DotNetEngine
{
/// <summary>
/// http://hl7.org/fhir/fhirpath.html#txapi
/// </summary>
public class FhirPathTerminologies : ITypedElement
{
public string TerminologyServerUrl { get; set; }
public string Name => "terminologes";
public string InstanceType => "TerminologyFhirPathExecutor";
public object Value => this;
public string Location => "%terminologies";
public IElementDefinitionSummary Definition => throw new NotImplementedException();
public IEnumerable<ITypedElement> Children(string name = null)
{
throw new NotImplementedException();
}
// expand(valueSet, params) : ValueSet
public ValueSet Expand(string vsUrl, string parameters)
{
FhirClient fc = new FhirClient(TerminologyServerUrl, new FhirClientSettings() { VerifyFhirVersion = false });
var nvp = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(parameters);
var canUrl = new CanonicalUrl(vsUrl);
var expParams = new Parameters();
expParams.Add("url", new FhirUri(canUrl.Url?.Value));
if (!string.IsNullOrEmpty(canUrl.Version?.Value))
expParams.Add("valueSetVersion", new FhirString(canUrl.Version?.Value));
if (nvp.ContainsKey("filter"))
expParams.Add("filter", new FhirString(nvp["filter"]));
if (nvp.ContainsKey("date"))
expParams.Add("date", new FhirDateTime(nvp["date"]));
return fc.TypeOperation<ValueSet>("expand", expParams) as ValueSet;
}
// lookup(coded, params) : Parameters
public Parameters Lookup(string code, string parameters)
{
FhirClient fc = new FhirClient(TerminologyServerUrl, new FhirClientSettings() { VerifyFhirVersion = false });
Parameters reqParams = ExtractLookupParameters(parameters);
reqParams.Add("code", new Code(code));
return fc.TypeOperation<CodeSystem>("lookup", reqParams) as Parameters;
}
public Parameters Lookup(Coding coding, string parameters)
{
FhirClient fc = new FhirClient(TerminologyServerUrl, new FhirClientSettings() { VerifyFhirVersion = false });
Parameters reqParams = ExtractLookupParameters(parameters);
reqParams.Add("coding", coding);
return fc.TypeOperation<CodeSystem>("lookup", reqParams) as Parameters;
}
private static Parameters ExtractLookupParameters(string parameters)
{
var nvp = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(parameters);
var reqParams = new Parameters();
if (nvp.ContainsKey("system"))
reqParams.Add("system", new FhirUri(nvp["system"]));
if (nvp.ContainsKey("version"))
reqParams.Add("version", new FhirString(nvp["version"]));
if (nvp.ContainsKey("date"))
reqParams.Add("date", new FhirDateTime(nvp["date"]));
if (nvp.ContainsKey("displayLanguage"))
reqParams.Add("displayLanguage", new Code(nvp["displayLanguage"]));
if (nvp.ContainsKey("property"))
{
foreach (var val in nvp["property"])
reqParams.Add("property", new Code(val));
}
return reqParams;
}
// translate(conceptMap, coded, params) : Parameters
public ITypedElement Lookup(ITypedElement a, ITypedElement b = null, ITypedElement c = null)
{
Coding coding = null;
string code = null;
string parameters = null;
if (a.Value == this)
{
if (b.Annotation<IFhirValueProvider>() != null)
{
if (b.Annotation<IFhirValueProvider>().FhirValue is Coding coding2)
{
coding = coding2;
if (c?.Value is string str)
parameters = str;
}
else if (b.Annotation<IFhirValueProvider>().FhirValue is Code code2)
{
code = code2.Value;
if (c?.Value is string str)
parameters = str;
}
else if (b.Value is string str2)
{
code = str2;
if (c?.Value is string str)
parameters = str;
}
}
}
else
{
if (a.Annotation<IFhirValueProvider>() != null)
{
if (a.Annotation<IFhirValueProvider>().FhirValue is Coding coding2)
{
coding = coding2;
if (b?.Value is string str)
parameters = str;
}
else if (a.Annotation<IFhirValueProvider>().FhirValue is Code code2)
{
code = code2.Value;
if (b?.Value is string str)
parameters = str;
}
else if (a.Value is string str2)
{
code = str2;
if (b?.Value is string str)
parameters = str;
}
}
}
if (coding != null)
{
var result = Lookup(coding, parameters);
if (result != null)
return TypedSerialization.ToTypedElement(result);
}
if (code != null)
{
var result = Lookup(code, parameters);
if (result != null)
return TypedSerialization.ToTypedElement(result);
}
return null;
}
}
}