forked from brianpos/FhirPathTester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomFluentPathFunctions.cs
164 lines (155 loc) · 6.7 KB
/
CustomFluentPathFunctions.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
extern alias dstu2;
extern alias stu3;
using Hl7.ElementModel;
// using Hl7.Fhir.FluentPath;
//using Hl7.Fhir.Model;
using Hl7.FluentPath;
using Hl7.FluentPath.Expressions;
using System;
using System.Collections.Generic;
using System.Linq;
namespace FhirPathTester
{
public static class CustomExtensions
{
public static IEnumerable<dstu2.Hl7.Fhir.Model.Base> ToFhirValues(this IEnumerable<IValueProvider> results)
{
return results.Select(r =>
{
if (r == null)
return null;
if (r is dstu2.Hl7.Fhir.FluentPath.PocoNavigator && (r as dstu2.Hl7.Fhir.FluentPath.PocoNavigator).FhirValue != null)
{
return ((dstu2.Hl7.Fhir.FluentPath.PocoNavigator)r).FhirValue;
}
object result;
if (r.Value is Hl7.FluentPath.ConstantValue)
{
result = (r.Value as Hl7.FluentPath.ConstantValue).Value;
}
else
{
result = r.Value;
}
if (result is bool)
{
return new dstu2.Hl7.Fhir.Model.FhirBoolean((bool)result);
}
if (result is long)
{
return new dstu2.Hl7.Fhir.Model.Integer((int)(long)result);
}
if (result is decimal)
{
return new dstu2.Hl7.Fhir.Model.FhirDecimal((decimal)result);
}
if (result is string)
{
return new dstu2.Hl7.Fhir.Model.FhirString((string)result);
}
if (result is PartialDateTime)
{
var dt = (PartialDateTime)result;
return new dstu2.Hl7.Fhir.Model.FhirDateTime(dt.ToUniversalTime());
}
else
{
// This will throw an exception if the type isn't one of the FHIR types!
return (dstu2.Hl7.Fhir.Model.Base)result;
}
});
}
}
public class CustomFluentPathFunctions
{
static private SymbolTable _st;
static public SymbolTable Scope
{
get
{
if (_st == null)
{
_st = new SymbolTable().AddStandardFP();
// _st.Add("rand", (object f) => { return "slim"; });
// Custom function that returns the name of the property, rather than its value
_st.Add("propname", (object f) =>
{
if (f is IEnumerable<IValueProvider>)
{
object[] bits = (f as IEnumerable<IValueProvider>).Select(i =>
{
if (i is stu3.Hl7.Fhir.FluentPath.PocoNavigator)
{
return (i as stu3.Hl7.Fhir.FluentPath.PocoNavigator).Name;
}
if (i is dstu2.Hl7.Fhir.FluentPath.PocoNavigator)
{
return (i as dstu2.Hl7.Fhir.FluentPath.PocoNavigator).Name;
}
return "?";
}).ToArray();
return FhirValueList.Create(bits);
}
return FhirValueList.Create(new object[] { "?" } );
});
_st.Add("pathname", (object f) =>
{
if (f is IEnumerable<IValueProvider>)
{
object[] bits = (f as IEnumerable<IValueProvider>).Select(i =>
{
if (i is stu3.Hl7.Fhir.FluentPath.PocoNavigator)
{
return (i as stu3.Hl7.Fhir.FluentPath.PocoNavigator).Path;
}
if (i is dstu2.Hl7.Fhir.FluentPath.PocoNavigator)
{
return (i as dstu2.Hl7.Fhir.FluentPath.PocoNavigator).Path;
}
return "?";
}).ToArray();
return FhirValueList.Create(bits);
}
return FhirValueList.Create(new object[] { "?" });
});
//_st.Add("commonpathname", (object f) =>
//{
// if (f is IEnumerable<IValueProvider>)
// {
// object[] bits = (f as IEnumerable<IValueProvider>).Select(i =>
// {
// if (i is PocoNavigator)
// {
// return (i as PocoNavigator).CommonPath;
// }
// return "?";
// }).ToArray();
// return FhirValueList.Create(bits);
// }
// return FhirValueList.Create(new object[] { "?" });
//});
// Custom function for evaluating the date operation (custom Healthconnex)
_st.Add("dateadd", (PartialDateTime f, string field, long amount) =>
{
DateTimeOffset dto = f.ToUniversalTime();
int value = (int)amount;
// Need to convert the amount and field to compensate for partials
//TimeSpan ts = new TimeSpan();
switch (field)
{
case "yy": dto = dto.AddYears(value); break;
case "mm": dto = dto.AddMonths(value); break;
case "dd": dto = dto.AddDays(value); break;
case "hh": dto = dto.AddHours(value); break;
case "mi": dto = dto.AddMinutes(value); break;
case "ss": dto = dto.AddSeconds(value); break;
}
PartialDateTime changedDate = PartialDateTime.Parse(PartialDateTime.FromDateTime(dto).ToString().Substring(0, f.ToString().Length));
return changedDate;
});
}
return _st;
}
}
}
}