-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBPLCode.cls
202 lines (173 loc) · 6.19 KB
/
BPLCode.cls
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
Class UCDavis.FHIR.RADResultsFHIRBPL Extends Ens.BusinessProcess [ ClassType = persistent ]
{
Property TargetConfigNames As %String(MAXLEN = 1000);
Property FHIRInstance As %String(MAXLEN = 1000);
Parameter SETTINGS = "TargetConfigNames:Basic:selector?multiSelect=0&context={Ens.ContextSearch/ProductionItems?targets=1&productionName=@productionId},FHIRInstance:Basic";
Method OnRequest(pRequest As EnsLib.HL7.Message, Output pResponse As Ens.Response) As %Status
{
#DIM tRequest,tResponse as EnsLib.REST.GenericMessage
do ..PatientSearch(pRequest, .pResponse)
/// If error in response
s tResponseObj=##class(%DynamicObject).%FromJSON(pResponse.Stream.Read())
if ((pResponse.HTTPHeaders.GetAt("StatusLine") '[" 200 ") || ((pResponse.HTTPHeaders.GetAt("StatusLine") [" 200 ") && (tResponseObj.Error'="")) || ($L(pResponse.Stream.Size)=0)) {
// Return the FHIR ID
$$$TRACE("LOCATION="_pResponse.HTTPHeaders.GetAt("LOCATION"))
s tPatFHIRID=$Piece(pResponse.HTTPHeaders.GetAt("LOCATION"), "/",10)
$$$TRACE("Match Found! The FHIR ID for the record is "_tPatFHIRID)
}
else {
// No Patient Found, Create New Patient
if tResponseObj.total=0 {
do ..CreatePatient(pRequest, .pResponse)
if ((pResponse.HTTPHeaders.GetAt("StatusLine") '[" 201 ") || ((pResponse.HTTPHeaders.GetAt("StatusLine") [" 201 ") && (tResponseObj.Error'="")) ){
}
else {
}
$$$TRACE("LOCATION="_pResponse.HTTPHeaders.GetAt("LOCATION"))
s tPatFHIRID=$Piece(pResponse.HTTPHeaders.GetAt("LOCATION"), "/",10)
}
}
Quit $$$OK
}
Method PatientSearch(pRequest As EnsLib.HL7.Message, Output pResponse As EnsLib.REST.GenericMessage)
{
#DIM tRequest,tResponse as EnsLib.REST.GenericMessage
set tRequest=##class(EnsLib.HTTP.GenericMessage).%New()
do ..AddHeaders(tRequest.HTTPHeaders,"GET",..FHIRInstance_"/Patient?family="_(pRequest.GetValueAt("PID:5(1).1.1"))_"&given="_(pRequest.GetValueAt("PID:5(1).2"))_"&birthdate="_(##class(Ens.Util.FunctionSet).ConvertDateTime(pRequest.GetValueAt("PID:7.1"),"%Y%m%d","%Y-%m-%d")))
set tRequest.Stream=##class(%Stream.GlobalCharacter).%New()
set json={
"resourceType":"Patient",
"name": [
{
"given": [(pRequest.GetValueAt("PID:5(1).2"))],
"family": (pRequest.GetValueAt("PID:5(1).1.1"))
}
],
"birthDate": (##class(Ens.Util.FunctionSet).ConvertDateTime(pRequest.GetValueAt("PID:7.1"),"%Y%m%d","%Y-%m-%d")),
"gender": (pRequest.GetValueAt("PID:8")),
"telecom": [
{
"value": (pRequest.GetValueAt("PID:13")),
"use": "home"
}
],
"address": [
{
"line": [
(pRequest.GetValueAt("PID:11(1).1"))
],
"city": (pRequest.GetValueAt("PID:11(1).3")),
"state":(pRequest.GetValueAt("PID:11(1).4")),
"postalCode": (pRequest.GetValueAt("PID:11(1).5")),
"country": "US",
"district": ""
}
]
}
do tRequest.Stream.Write(json.%ToJSON())
do tRequest.HTTPHeaders.SetAt(tRequest.Stream.Size,"Content-Length")
set tStatus = ..SendRequestSync(..TargetConfigNames, tRequest, .tResponse)
s pResponse=tResponse.%ConstructClone()
s tResponseObj=##class(%DynamicObject).%FromJSON(tResponse.Stream.Read())
s tPatFHIRID=tResponseObj.entry."0".resource.id
$$$TRACE("Match Found! The FHIR ID for the record is "_tPatFHIRID)
return
}
Method CreatePatient(pRequest As EnsLib.HL7.Message, Output pResponse As EnsLib.REST.GenericMessage)
{
set tRequest=##class(EnsLib.REST.GenericMessage).%New()
do ..AddHeaders(tRequest.HTTPHeaders,"POST",..FHIRInstance_"/Patient")
//prep & handle gender correctly. can be refactored out
if (pRequest.GetValueAt("PID:8")="M") {
set tGender = "male"
}
else {
if (pRequest.GetValueAt("PID:8")="F") {
set tGender = "female"
}
else {
if (pRequest.GetValueAt("PID:8")="O") {
set tGender = "other"
}
else {
set tGender = "unknown"
}
}
}
set tRequest.Stream=##class(%Stream.GlobalCharacter).%New()
set json={
"resourceType":"Patient",
"name": [
{
"given": [(pRequest.GetValueAt("PID:5(1).2"))],
"family": (pRequest.GetValueAt("PID:5(1).1.1"))
}
],
"birthDate": (##class(Ens.Util.FunctionSet).ConvertDateTime(pRequest.GetValueAt("PID:7.1"),"%Y%m%d","%Y-%m-%d")),
"gender": (tGender),
"telecom": [
{
"value": (pRequest.GetValueAt("PID:13")),
"use": "home"
}
],
"address": [
{
"line": [
(pRequest.GetValueAt("PID:11(1).1"))
],
"city": (pRequest.GetValueAt("PID:11(1).3")),
"state":(pRequest.GetValueAt("PID:11(1).4")),
"postalCode": (pRequest.GetValueAt("PID:11(1).5")),
"country": "US"
}
]
}
do tRequest.Stream.Write(json.%ToJSON())
set tStatus = ..SendRequestSync(..TargetConfigNames, tRequest, .tResponse)
set pResponse=tResponse
return
}
Method AddHeaders(Output headers, reqType As %String, url As %String)
{
do headers.SetAt(url,"URL")
do headers.SetAt(reqType,"HttpRequest")
do headers.SetAt("application/fhir+json","Content-Type")
do headers.SetAt("*/*","Accept")
return
}
/// Add connections from TargetConfigNames
ClassMethod OnGetConnections(Output pArray As %String, pItem As Ens.Config.Item)
{
Do ##super(.pArray,pItem)
If pItem.GetModifiedSetting("TargetConfigNames",.tValue) {
For i=1:1:$L(tValue,",") { Set tOne=$ZStrip($P(tValue,",",i),"<>W") Continue:""=tOne Set pArray(tOne)="" }
}
If pItem.GetModifiedSetting("TargetConfigNamesEmail",.tValue) {
For i=1:1:$L(tValue,",") { Set tOne=$ZStrip($P(tValue,",",i),"<>W") Continue:""=tOne Set pArray(tOne)="" }
}
}
Storage Default
{
<Data name="RADResultsFHIRBPLDefaultData">
<Subscript>"RADResultsFHIRBPL"</Subscript>
<Value name="1">
<Value>TargetConfigNames</Value>
</Value>
<Value name="2">
<Value>FHIRInstance</Value>
</Value>
<Value name="3">
<Value>BasicAuth</Value>
</Value>
<Value name="4">
<Value>EpicClientID</Value>
</Value>
<Value name="5">
<Value>X509Config</Value>
</Value>
</Data>
<DefaultData>RADResultsFHIRBPLDefaultData</DefaultData>
<Type>%Storage.Persistent</Type>
}
}