forked from wso2/open-healthcare-prebuilt-services
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_mapping.bal
More file actions
407 lines (361 loc) · 14.8 KB
/
Copy pathdata_mapping.bal
File metadata and controls
407 lines (361 loc) · 14.8 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
// Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
import terminology_service.store_h2;
import ballerina/persist;
import ballerina/sql;
import ballerinax/health.fhir.r4;
import ballerinax/health.fhir.r4.parser;
import ballerina/log;
isolated function codesystemConceptsToParameters(r4:CodeSystemConcept[]|r4:CodeSystemConcept concepts) returns r4:Parameters {
r4:Parameters parameters = {};
if concepts is r4:CodeSystemConcept {
parameters = {
'parameter: [
{name: "name", valueString: concepts.code},
{name: "display", valueString: concepts.display}
]
};
if concepts.definition is string {
(<r4:ParametersParameter[]>parameters.'parameter).push({name: "definition", valueString: concepts.definition});
}
if concepts.property is r4:CodeSystemConceptProperty[] {
foreach var item in <r4:CodeSystemConceptProperty[]>concepts.property {
r4:ParametersParameter result = codeSystemConceptPropertyToParameter(item);
(<r4:ParametersParameter[]>parameters.'parameter).push(result);
}
}
if concepts.designation is r4:CodeSystemConceptDesignation[] {
foreach var item in <r4:CodeSystemConceptDesignation[]>concepts.designation {
r4:ParametersParameter result = designationToParameter(item);
(<r4:ParametersParameter[]>parameters.'parameter).push(result);
}
}
} else {
r4:ParametersParameter[] p = [];
foreach r4:CodeSystemConcept item in concepts {
p.push({name: "name", valueString: item.code},
{name: "display", valueString: item.display});
if item.definition is string {
p.push({name: "definition", valueString: item.definition});
}
if item.property is r4:CodeSystemConceptProperty[] {
foreach var prop in <r4:CodeSystemConceptProperty[]>item.property {
r4:ParametersParameter result = codeSystemConceptPropertyToParameter(prop);
p.push(result);
}
}
if item.designation is r4:CodeSystemConceptDesignation[] {
foreach var desg in <r4:CodeSystemConceptDesignation[]>item.designation {
r4:ParametersParameter result = designationToParameter(desg);
(<r4:ParametersParameter[]>parameters.'parameter).push(result);
}
}
}
parameters = {'parameter: p};
}
return parameters;
}
isolated function designationToParameter(r4:CodeSystemConceptDesignation designation) returns r4:ParametersParameter {
r4:ParametersParameter param = {name: "designation"};
r4:ParametersParameter[] part = [];
if designation.language is string {
part.push({name: "language", valueCode: designation.language});
}
part.push({name: "value", valueString: designation.value});
if designation.use is r4:Coding {
part.push({name: "use", valueCoding: designation.use});
}
param.part = part;
return param;
}
isolated function stringToParameterizedQuery(string queryStr) returns sql:ParameterizedQuery {
sql:ParameterizedQuery query = ``;
query.strings = [queryStr];
return query;
}
isolated function codeSystemToByte(r4:CodeSystem codeSystem) returns byte[]|r4:FHIRError {
log:printDebug("Converting CodeSystem to byte array, codeSystem id: " + (codeSystem.id ?: "unknown"));
// Concepts are stripped here because they are stored separately in the concepts table.
// The incoming CodeSystem is already validated by fhirr4:Listener, so no round-trip
// re-parse is needed.
r4:CodeSystem codeSystemWithoutConcepts = codeSystem.clone();
codeSystemWithoutConcepts.concept = ();
return codeSystemWithoutConcepts.toJsonString().toBytes();
}
isolated function byteToCodeSystem(byte[] byteArray) returns r4:CodeSystem|error {
string codeSystemJsonString = check 'string:fromBytes(byteArray);
r4:CodeSystem parsedCodeSystem = check parser:parse(codeSystemJsonString).ensureType();
return parsedCodeSystem;
}
isolated function conceptToByte(r4:CodeSystemConcept concept) returns byte[]|r4:FHIRError {
r4:CodeSystemConcept conceptWithoutInternlConcept = concept.clone();
conceptWithoutInternlConcept.concept = ();
return conceptWithoutInternlConcept.toJsonString().toBytes();
}
isolated function byteToConcept(byte[] byteArray) returns r4:CodeSystemConcept|error {
string conceptJsonString = check 'string:fromBytes(byteArray);
json conceptJson = check conceptJsonString.fromJsonString();
r4:CodeSystemConcept parsedConcept = check conceptJson.fromJsonWithType(r4:CodeSystemConcept);
return parsedConcept;
}
isolated function valueSetToByte(r4:ValueSet valueSet) returns byte[]|r4:FHIRError {
return valueSet.toJsonString().toBytes();
}
isolated function byteToValueSet(byte[] byteArray) returns r4:ValueSet|error {
string valueSetJsonString = check 'string:fromBytes(byteArray);
r4:ValueSet parsedValueSet = check parser:parse(valueSetJsonString).ensureType();
return parsedValueSet;
}
isolated function streamToStoreCodeSystem(stream<store_h2:CodeSystem, persist:Error?> codeSystemStream) returns store_h2:CodeSystem[]|error {
store_h2:CodeSystem[] dbCodeSystems = check from store_h2:CodeSystem codeSystem in codeSystemStream
select codeSystem;
return dbCodeSystems;
}
isolated function streamToStoreConcept(stream<store_h2:Concept, persist:Error?> conceptStream) returns store_h2:Concept[]|error {
store_h2:Concept[] dbConcepts = check from store_h2:Concept concept in conceptStream
select concept;
return dbConcepts;
}
isolated function streamToStoreValueSet(stream<store_h2:ValueSet, persist:Error?> valueSetStream) returns store_h2:ValueSet[]|error {
store_h2:ValueSet[] dbValueSets = check from store_h2:ValueSet valueSet in valueSetStream
select valueSet;
return dbValueSets;
}
isolated function parseCodeSystemToR4CodeSystem(ParseCodeSystem customCodeSystem) returns r4:CodeSystem => {
resourceType: customCodeSystem.resourceType,
meta: customCodeSystem.meta,
valueSet: customCodeSystem.valueSet,
date: customCodeSystem.date,
purpose: customCodeSystem.purpose,
description: customCodeSystem.description,
experimental: customCodeSystem.experimental,
content: customCodeSystem.content ?: "example",
status: customCodeSystem.status ?: "unknown",
title: customCodeSystem.title,
language: customCodeSystem.language,
id: customCodeSystem.id,
hierarchyMeaning: customCodeSystem.hierarchyMeaning,
extension: customCodeSystem.extension,
copyright: customCodeSystem.copyright,
jurisdiction: customCodeSystem.jurisdiction,
modifierExtension: customCodeSystem.modifierExtension,
contact: customCodeSystem.contact,
property: customCodeSystem.property,
text: customCodeSystem.text,
caseSensitive: customCodeSystem.caseSensitive,
identifier: customCodeSystem.identifier,
publisher: customCodeSystem.publisher,
implicitRules: customCodeSystem.implicitRules,
name: customCodeSystem.name,
compositional: customCodeSystem.compositional,
supplements: customCodeSystem.supplements,
url: customCodeSystem.url,
'version: customCodeSystem.'version,
count: customCodeSystem.count,
versionNeeded: customCodeSystem.versionNeeded,
filter: customCodeSystem.filter,
contained: customCodeSystem.contained,
useContext: customCodeSystem.useContext,
concept: customCodeSystem.concept
};
isolated function parseValueSetToR4ValueSet(ParseValueSet customValueSet) returns r4:ValueSet => {
resourceType: customValueSet.resourceType,
meta: customValueSet.meta,
date: customValueSet.date,
copyright: customValueSet.copyright,
extension: customValueSet.extension,
purpose: customValueSet.purpose,
jurisdiction: customValueSet.jurisdiction,
modifierExtension: customValueSet.modifierExtension,
description: customValueSet.description,
experimental: customValueSet.experimental,
language: customValueSet.language,
title: customValueSet.title,
contact: customValueSet.contact,
id: customValueSet.id,
text: customValueSet.text,
identifier: customValueSet.identifier,
'version: customValueSet.'version,
url: customValueSet.url,
expansion: customValueSet.expansion,
contained: customValueSet.contained,
immutable: customValueSet.immutable,
compose: customValueSet.compose,
name: customValueSet.name,
implicitRules: customValueSet.implicitRules,
publisher: customValueSet.publisher,
useContext: customValueSet.useContext,
status: customValueSet.status ?: "unknown"
};
isolated function xmlCodeSystemToR4CodeSystem(XMLCodeSystem xmlCodeSystem) returns r4:CodeSystem => {
resourceType: xmlCodeSystem.resourceType,
meta: xmlCodeSystem.meta,
valueSet: xmlCodeSystem.valueSet?.value,
date: xmlCodeSystem.date?.value,
purpose: xmlCodeSystem.purpose?.value,
description: xmlCodeSystem.description?.value,
experimental: xmlCodeSystem.experimental?.value,
content: <r4:CodeSystemContent>xmlCodeSystem.content?.value,
status: <r4:CodeSystemStatus>xmlCodeSystem.status?.value,
title: xmlCodeSystem.title?.value,
language: xmlCodeSystem.language?.value,
id: xmlCodeSystem.id?.value,
copyright: xmlCodeSystem.copyright?.value,
caseSensitive: xmlCodeSystem.caseSensitive?.value,
publisher: xmlCodeSystem.publisher?.value,
implicitRules: xmlCodeSystem.implicitRules?.value,
name: xmlCodeSystem.name?.value,
compositional: xmlCodeSystem.compositional?.value,
supplements: xmlCodeSystem.supplements?.value,
url: xmlCodeSystem.url?.value,
'version: xmlCodeSystem.version?.value,
count: xmlCodeSystem.count?.value,
versionNeeded: xmlCodeSystem.versionNeeded?.value,
text: mapText(xmlCodeSystem.text),
identifier: mapIdentifiers(xmlCodeSystem.identifier),
filter: mapFilters(xmlCodeSystem.filter),
property: mapProperties(xmlCodeSystem.property),
contact: mapContacts(xmlCodeSystem.contact),
hierarchyMeaning: mapHierarchyMeaning(xmlCodeSystem.hierarchyMeaning),
concept: mapConcepts(xmlCodeSystem.concept)
};
// Helper function to map filters
isolated function mapFilters(ValueFilter[]? filters) returns r4:CodeSystemFilter[]? {
if filters is () {
return ();
}
r4:CodeSystemFilter[] r4Filters = [];
foreach var filter in filters {
r4Filters.push({
code: filter.code.value,
description: filter.description?.value,
operator: filter.operator.map(op => <r4:CodeSystemFilterOperator>op.value),
value: filter.value.value
});
}
if r4Filters.length() == 0 {
return ();
}
return r4Filters;
}
// Helper function to map properties
isolated function mapProperties(ValueProperty[]? properties) returns r4:CodeSystemProperty[]? {
if properties is () {
return ();
}
r4:CodeSystemProperty[] r4Properties = [];
foreach var property in properties {
r4Properties.push({
code: property.code.value,
uri: property.uri?.value,
description: property.description?.value,
'type: <r4:CodeSystemPropertyType>property.'type.value
});
}
if r4Properties.length() == 0 {
return ();
}
return r4Properties;
}
// Helper function to map Concept[]? to r4:CodeSystemConcept[]?
isolated function mapConcepts(ValueConcept[]? concepts) returns r4:CodeSystemConcept[]? {
if concepts is () {
return ();
}
r4:CodeSystemConcept[] r4Concepts = [];
foreach var concept in concepts {
r4:CodeSystemConcept r4Concept = {code: concept.code.value};
if concept.display is ValueString {
r4Concept.display = concept.display?.value;
}
if concept.definition is ValueString {
r4Concept.definition = concept.definition?.value;
}
r4Concepts.push(r4Concept);
}
if r4Concepts.length() == 0 {
return ();
}
return r4Concepts;
}
// Helper function to map identifiers
isolated function mapIdentifiers(ValueString[]? identifiers) returns r4:Identifier[]? {
if identifiers is () {
return ();
}
r4:Identifier[] r4Identifiers = [];
foreach var identifier in identifiers {
r4Identifiers.push({value: identifier.value});
}
if r4Identifiers.length() == 0 {
return ();
}
return r4Identifiers;
}
// Helper function to map text
isolated function mapText(ValueString? text) returns r4:Narrative? {
if text is () {
return ();
}
return {div: text.value, status: "generated"};
}
// Helper function to map ValueContact[]? to r4:ContactDetail[]?
isolated function mapContacts(ValueContact[]? contacts) returns r4:ContactDetail[]? {
if contacts is () {
return ();
}
r4:ContactDetail[] r4Contacts = [];
foreach var contact in contacts {
r4:ContactPoint[] r4Telecoms = [];
foreach var telecom in contact.telecom {
r4Telecoms.push({
system: <r4:ContactPointSystem>telecom.system.value,
value: telecom.value.value
});
}
r4Contacts.push({
telecom: r4Telecoms
});
}
if r4Contacts.length() == 0 {
return ();
}
return r4Contacts;
}
// Helper function to map hierarchyMeaning
isolated function mapHierarchyMeaning(ValueHierarchyMeaning? hierarchyMeaning) returns r4:CodeSystemHierarchyMeaning? {
if hierarchyMeaning is () {
return ();
}
// Map string value to r4:CodeSystemHierarchyMeaning enum
return <r4:CodeSystemHierarchyMeaning>hierarchyMeaning.value;
}
isolated function codeSystemDetailsIntoBundle(TerminologyConcept[] codeSystemDetails) returns r4:Bundle {
r4:BundleEntry[] entries = [];
foreach var detail in codeSystemDetails {
r4:Coding coding = {
system: detail.url,
code: detail.concept.code,
display: detail.concept.display
};
// Each entry resource is a Coding resource (wrapped as json)
entries.push({
'resource: coding
});
}
return {
'type: r4:BUNDLE_TYPE_SEARCHSET,
total: entries.length(),
entry: entries
};
}