-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathterminology_source.bal
More file actions
1088 lines (939 loc) · 48.3 KB
/
Copy pathterminology_source.bal
File metadata and controls
1088 lines (939 loc) · 48.3 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 terminology_service.store_pg;
import ballerina/http;
import ballerina/lang.regexp;
import ballerina/log;
import ballerina/persist;
import ballerina/regex;
import ballerina/sql;
import ballerinax/health.fhir.r4;
import ballerinax/health.fhir.r4.terminology;
// import ballerina/io;
// Improve after the issue https://github.com/wso2/open-healthcare-prebuilt-services/issues/151 is fixed
final store_pg:Client sClient = check initializeClient();
function initializeClient() returns store_pg:Client|store_h2:Client|error {
if db_type == "postgresql" {
log:printInfo("Initializing PostgreSQL client for terminology service");
return check new store_pg:Client();
} else if db_type == "h2" {
log:printInfo("Initializing H2 client for terminology service");
return check new store_h2:Client();
} else {
log:printError("Unsupported database type provided: " + db_type);
return error("Unsupported database type: " + db_type);
}
}
public isolated class TerminologySource {
*terminology:Terminology;
public isolated function addCodeSystem(r4:CodeSystem codeSystem) returns r4:FHIRError? {
// add the code system to the database
store_h2:CodeSystemInsert dbCodeSystemInsert = {
id: codeSystem.id ?: "",
url: codeSystem.url ?: "",
version: codeSystem.version ?: "",
name: codeSystem.name ?: "",
title: codeSystem.title ?: "",
status: codeSystem.status,
date: codeSystem.date ?: "",
publisher: codeSystem.publisher ?: "",
codeSystem: check codeSystemToByte(codeSystem)
};
int[]|persist:Error response = sClient->/codesystems.post([dbCodeSystemInsert]);
if (response is persist:Error) {
// error while adding code system to the database
return r4:createFHIRError(
"Error while adding CodeSystem, " + response.message(),
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("Error while adding CodeSystem"),
httpStatusCode = http:STATUS_INTERNAL_SERVER_ERROR);
}
// extract the concepts from the codesystem and add them to the database
extractConceptsFromCodeSystem(codeSystem, response[0]);
}
public isolated function addValueSet(r4:ValueSet valueSet) returns r4:FHIRError? {
// add the value set to the database
store_h2:ValueSetInsert dbValueSetInsert = {
id: valueSet.id ?: "",
url: valueSet.url ?: "",
version: valueSet.version ?: "",
name: valueSet.name ?: "",
title: valueSet.title ?: "",
status: valueSet.status,
date: valueSet.date ?: "",
publisher: valueSet.publisher ?: "",
valueSet: check valueSetToByte(valueSet)
};
int[]|persist:Error response = sClient->/valuesets.post([dbValueSetInsert]);
if (response is persist:Error) {
// error while adding value set to the database
return r4:createFHIRError(
"Error while adding ValueSet, " + response.message(),
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("Error while adding ValueSet"),
httpStatusCode = http:STATUS_INTERNAL_SERVER_ERROR);
}
// extract the concepts from the valueset and add them to the database
extractConceptsFromValueSet(valueSet, response[0]);
}
public isolated function findCodeSystem(r4:uri? system, string? id, string? version = ()) returns r4:CodeSystem|r4:FHIRError {
r4:CodeSystem|r4:FHIRError|error? dbCodeSystem = ();
if id != () {
dbCodeSystem = getCodeSystemByID(id, version);
} else if system != () {
dbCodeSystem = getCodeSystemByURL(system, version);
}
if dbCodeSystem is r4:FHIRError {
return dbCodeSystem;
}
if dbCodeSystem is error || dbCodeSystem is () {
return r4:createFHIRError(
dbCodeSystem is error ? dbCodeSystem.message() : "Id or URL for the codesystem is required to find CodeSystem",
r4:ERROR,
r4:PROCESSING_NOT_FOUND,
cause = dbCodeSystem,
httpStatusCode = http:STATUS_NOT_FOUND
);
}
return dbCodeSystem;
}
public isolated function findConcept(r4:uri system, r4:code code, string? version) returns terminology:CodeConceptDetails|r4:FHIRError {
// find the concept in the valueset table
terminology:CodeConceptDetails|r4:FHIRError valuesetConceptDetails = findConceptInValueSet(system, code, version);
if valuesetConceptDetails !is r4:FHIRError {
return valuesetConceptDetails;
}
// find the concept in the codesystem table
terminology:CodeConceptDetails|r4:FHIRError conceptDetails = findConceptInCodeSystem(system, code, version);
if conceptDetails !is r4:FHIRError {
return conceptDetails;
}
// concept not found in both tables
return r4:createFHIRError(
"Concept not found",
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("No matching Concept found"),
httpStatusCode = http:STATUS_NOT_FOUND);
}
public isolated function findValueSet(r4:uri? system, string? id, string? version) returns r4:ValueSet|r4:FHIRError {
r4:ValueSet|r4:FHIRError|error dbValueSet;
if id != () {
dbValueSet = getValueSetByID(id, version);
} else if system != () {
dbValueSet = getValueSetByURL(system, version);
} else {
return r4:createFHIRError(
"Id or URL for the valueset is required to find ValueSet",
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("No matching ValueSet found"),
httpStatusCode = http:STATUS_BAD_REQUEST);
}
if dbValueSet is r4:FHIRError {
return dbValueSet;
}
if dbValueSet is error {
return r4:createFHIRError(
"Cannot find ValueSet, " + dbValueSet.message(),
r4:ERROR,
r4:PROCESSING_NOT_FOUND,
cause = dbValueSet,
httpStatusCode = http:STATUS_NOT_FOUND
);
}
return dbValueSet;
}
public isolated function isCodeSystemExist(r4:uri system, string version) returns boolean {
// TODO: Replace the manual query-based search operation below with the commented logic once the following persist issue is resolved:
// https://github.com/ballerina-platform/ballerina-library/issues/7920
//
// The recommended approach is:
// store_h2:CodeSystem[] codeSystems = check from store_h2:CodeSystem codesystem in sClient->/codesystems(store_h2:CodeSystem)
// where codesystem.url == system && codesystem.version == version
// select codesystem;
// return codeSystems.length() > 0;
sql:ParameterizedQuery sqlQuery = sql:queryConcat(`SELECT 1 FROM `, escapeToQuery("codesystems"), ` WHERE `, escapeToQuery("url"), ` = ${system} AND `, escapeToQuery("version"), ` = ${version} LIMIT 1`);
stream<record {}, persist:Error?> resultStream = sClient->queryNativeSQL(sqlQuery);
record {}[]|error results = from record {} result in resultStream
select result;
return results is error ? false : results.length() > 0;
}
public isolated function isValueSetExist(r4:uri system, string version) returns boolean {
// TODO: Replace the manual query-based search operation below with the commented logic once the following persist issue is resolved:
// https://github.com/ballerina-platform/ballerina-library/issues/7920
//
// store_h2:ValueSet[] valueSets = check from store_h2:ValueSet valueSet in sClient->/valuesets(store_h2:ValueSet)
// where valueSet.url == system && valueSet.version == version
// select valueSet;
// return valueSets.length() > 0;
sql:ParameterizedQuery sqlQuery = sql:queryConcat(`SELECT 1 FROM `, escapeToQuery("valuesets"), ` WHERE `, escapeToQuery("url"), ` = ${system} AND `, escapeToQuery("version"), ` = ${version} LIMIT 1`);
stream<record {}, persist:Error?> resultStream = sClient->queryNativeSQL(sqlQuery);
record {}[]|error results = from record {} result in resultStream
select result;
return results is error ? false : results.length() > 0;
}
public isolated function searchCodeSystem(map<r4:RequestSearchParameter[]> params, int? offset, int? count) returns r4:CodeSystem[]|r4:FHIRError {
sql:ParameterizedQuery whereClause = ``;
boolean isFirst = true;
foreach var [paramName, paramList] in params.entries() {
if terminology:CODESYSTEMS_SEARCH_PARAMS.hasKey(paramName) {
foreach var param in paramList {
sql:ParameterizedQuery fragment = sql:queryConcat(escapeToQuery(paramName == "system" ? "url" : paramName), ` = ${param.value}`);
if fragment.strings.length() > 0 {
if isFirst {
whereClause = fragment;
isFirst = false;
} else {
whereClause = sql:queryConcat(whereClause, ` AND `, fragment);
}
}
}
}
}
if offset is int && count is int {
whereClause = sql:queryConcat(whereClause, ` `, getLimitClause(count, offset));
}
stream<store_h2:CodeSystem, persist:Error?> codeSystemStream = sClient->/codesystems(store_h2:CodeSystem, whereClause);
store_h2:CodeSystem[]|error dbCodeSystems = streamToStoreCodeSystem(codeSystemStream);
if dbCodeSystems is error {
return r4:createFHIRError(
dbCodeSystems.message(),
r4:ERROR,
r4:INVALID_REQUIRED,
cause = dbCodeSystems,
httpStatusCode = http:STATUS_INTERNAL_SERVER_ERROR);
}
r4:CodeSystem[] codeSystemArray = [];
foreach store_h2:CodeSystem dbCodeSystem in dbCodeSystems {
r4:CodeSystem|error parsedCodeSystem = byteToCodeSystem(dbCodeSystem.codeSystem);
if parsedCodeSystem is error {
log:printError("Error while parsing CodeSystem: " + parsedCodeSystem.message());
// Skip this CodeSystem if parsing fails
continue;
}
codeSystemArray.push(parsedCodeSystem);
}
return codeSystemArray;
}
public isolated function searchValueSet(map<r4:RequestSearchParameter[]> params, int? offset, int? count) returns r4:ValueSet[]|r4:FHIRError {
stream<store_h2:ValueSet, persist:Error?> valueSetStream;
if params.length() == 0 {
valueSetStream = sClient->/valuesets(store_h2:ValueSet);
} else {
sql:ParameterizedQuery whereClause = ``;
foreach var [paramName, paramList] in params.entries() {
if terminology:CODESYSTEMS_SEARCH_PARAMS.hasKey(paramName) {
foreach var param in paramList {
sql:ParameterizedQuery fragment = sql:queryConcat(escapeToQuery(paramName == "system" ? "url" : paramName), ` = ${param.value}`);
if fragment.strings.length() > 0 {
whereClause = sql:queryConcat(fragment);
}
}
}
}
if offset is int && count is int {
whereClause = sql:queryConcat(whereClause, ` `, getLimitClause(count, offset));
}
valueSetStream = sClient->/valuesets(store_h2:ValueSet, whereClause);
}
store_h2:ValueSet[]|error dbValueSets = streamToStoreValueSet(valueSetStream);
if dbValueSets is error {
log:printError("Error while streaming ValueSets: " + dbValueSets.message());
return r4:createFHIRError(
dbValueSets.message(),
r4:ERROR,
r4:INVALID_REQUIRED,
cause = dbValueSets,
httpStatusCode = http:STATUS_INTERNAL_SERVER_ERROR);
}
r4:ValueSet[] valueSetArray = [];
foreach store_h2:ValueSet dbValueSet in dbValueSets {
r4:ValueSet|error parsedValueSet = byteToValueSet(dbValueSet.valueSet);
if parsedValueSet is error {
// Skip this ValueSet if parsing fails
continue;
}
valueSetArray.push(parsedValueSet);
}
return valueSetArray;
}
public isolated function expandValueSet(map<r4:RequestSearchParameter[]> searchParameters, r4:ValueSet valueSet, int offset, int count) returns r4:ValueSet|r4:FHIRError {
store_h2:ValueSet|error dbValueSet = getStoreValueSetByURL(valueSet.url.toString(), valueSet.version);
if dbValueSet is error {
return r4:createFHIRError(
"ValueSet not found for expansion",
r4:ERROR,
r4:PROCESSING_NOT_FOUND,
cause = dbValueSet,
httpStatusCode = http:STATUS_NOT_FOUND);
}
int valueSetId = dbValueSet.valueSetId;
// Get all includes for this ValueSet
sql:ParameterizedQuery includeQuery = sql:queryConcat(escapeToQuery("valuesetValueSetId"), ` = ${valueSetId}`);
stream<store_h2:ValueSetComposeInclude, persist:Error?> includeStream = sClient->/valuesetcomposeincludes(store_h2:ValueSetComposeInclude, whereClause = includeQuery);
store_h2:ValueSetComposeInclude[]|error includes = from store_h2:ValueSetComposeInclude inc in includeStream
select inc;
if includes is error {
return r4:createFHIRError(
"Error fetching ValueSet includes: " + includes.message(),
r4:ERROR,
r4:PROCESSING_NOT_FOUND,
cause = includes,
httpStatusCode = http:STATUS_INTERNAL_SERVER_ERROR);
}
r4:ValueSetExpansionContains[] allConcepts = [];
string? filter = searchParameters.hasKey(terminology:FILTER) ? searchParameters.get(terminology:FILTER)[0].value : ();
foreach store_h2:ValueSetComposeInclude include in includes {
// If conceptFlag, get concepts from valueset_compose_include_concepts
if include.conceptFlag {
sql:ParameterizedQuery q = sql:queryConcat(
`SELECT c.* FROM `, escapeToQuery("concepts"), ` c JOIN `, escapeToQuery("valueset_compose_include_concepts"), ` vcic ON c.`, escapeToQuery("conceptId"), ` = vcic.`, escapeToQuery("conceptConceptId"),
` WHERE vcic.`, escapeToQuery("valuesetcomposeValueSetComposeIncludeId"), ` = ${include.valueSetComposeIncludeId}`
);
stream<store_h2:Concept, persist:Error?> conceptStream = sClient->queryNativeSQL(q);
store_h2:Concept[]|error dbConcepts = from store_h2:Concept c in conceptStream
select c;
if dbConcepts is error {
continue;
}
foreach store_h2:Concept c in dbConcepts {
r4:CodeSystemConcept|error concept = byteToConcept(c.concept);
if concept is r4:CodeSystemConcept {
if filter is string {
if concept.display is string && !regexp:isFullMatch(re `.*${filter.toUpperAscii()}.*`, (<string>concept.display).toUpperAscii()) {
continue;
}
}
r4:ValueSetExpansionContains exp = {code: concept.code, display: concept.display, id: concept.id};
allConcepts.push(exp);
}
}
}
// If systemFlag, get all concepts for the code system
else if include.systemFlag && include.codeSystemId is int {
sql:ParameterizedQuery query = sql:queryConcat(escapeToQuery("codesystemCodeSystemId"), ` = ${include.codeSystemId}`);
stream<store_h2:Concept, persist:Error?> conceptStream = sClient->/concepts(store_h2:Concept, query);
store_h2:Concept[]|error dbConcepts = from store_h2:Concept c in conceptStream
select c;
if dbConcepts is error {
continue;
}
foreach store_h2:Concept c in dbConcepts {
r4:CodeSystemConcept|error concept = byteToConcept(c.concept);
if concept is r4:CodeSystemConcept {
if filter is string {
if concept.display is string && !regexp:isFullMatch(re `.*${filter.toUpperAscii()}.*`, (<string>concept.display).toUpperAscii()) {
continue;
}
}
r4:ValueSetExpansionContains exp = {code: concept.code, display: concept.display, id: concept.id};
allConcepts.push(exp);
}
}
}
// If valueSetFlag, get nested value sets and expand recursively
else if include.valueSetFlag {
sql:ParameterizedQuery q = sql:queryConcat(
`SELECT vs.* FROM `, escapeToQuery("valuesets"), ` vs JOIN `, escapeToQuery("valueset_compose_include_value_sets"), ` vcivs ON vs.`, escapeToQuery("valueSetId"), ` = vcivs.`, escapeToQuery("valuesetValueSetId"),
` WHERE vcivs.`, escapeToQuery("valuesetcomposeValueSetComposeIncludeId"), ` = ${include.valueSetComposeIncludeId}`
);
stream<store_h2:ValueSet, persist:Error?> vsStream = sClient->queryNativeSQL(q);
store_h2:ValueSet[]|error nestedVS = from store_h2:ValueSet v in vsStream
select v;
if nestedVS is error {
continue;
}
foreach store_h2:ValueSet v in nestedVS {
r4:ValueSet|error parsedVS = byteToValueSet(v.valueSet);
if parsedVS is r4:ValueSet {
r4:ValueSet|r4:FHIRError expanded = self.expandValueSet(searchParameters, parsedVS, 0, 1000); // Recursively expand, no offset/count for nested
if expanded is r4:ValueSet {
if expanded.expansion is r4:ValueSetExpansion {
r4:ValueSetExpansion expansionVal = <r4:ValueSetExpansion>expanded.expansion;
if expansionVal.contains is r4:ValueSetExpansionContains[] {
r4:ValueSetExpansionContains[] containsArr = <r4:ValueSetExpansionContains[]>expansionVal.contains;
foreach r4:ValueSetExpansionContains c in containsArr {
allConcepts.push(c);
}
}
}
}
}
}
}
}
// Pagination
int totalCount = allConcepts.length();
r4:ValueSetExpansionContains[] pagedConcepts;
if totalCount > offset + count {
pagedConcepts = allConcepts.slice(offset, offset + count);
} else if totalCount >= offset {
pagedConcepts = allConcepts.slice(offset);
} else {
pagedConcepts = [];
}
r4:ValueSetExpansion expansion = createExpandedValueSet(valueSet, pagedConcepts);
expansion.total = totalCount;
expansion.offset = offset;
valueSet.expansion = expansion.clone();
return valueSet;
}
public isolated function subsumes(r4:uri system, r4:code codeA, r4:code codeB, string? version) returns r4:Parameters|r4:FHIRError {
var codeSystem = getStoreCodeSystemByURL(system, version);
if codeSystem !is store_h2:CodeSystem {
return r4:createFHIRError(
"CodeSystem not found",
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("No matching CodeSystem found"),
httpStatusCode = http:STATUS_NOT_FOUND);
}
if codeA == codeB {
return {'parameter: [{name: terminology:OUTCOME, valueCode: terminology:EQUIVALENT}]};
}
ConceptNode conceptA = check getConceptNode(codeA, codeSystem.codeSystemId);
ConceptNode conceptB = check getConceptNode(codeB, codeSystem.codeSystemId);
// Check if A is ancestor of B
boolean aSubsumesB = isInParentChain(conceptA.conceptId, conceptB);
if aSubsumesB {
return {'parameter: [{name: terminology:OUTCOME, valueCode: terminology:SUBSUMED}]};
}
// Check if B is ancestor of A
boolean bSubsumesA = isInParentChain(conceptB.conceptId, conceptA);
if bSubsumesA {
return {'parameter: [{name: terminology:OUTCOME, valueCode: terminology:SUBSUMED_BY}]};
}
return {'parameter: [{name: terminology:OUTCOME, valueCode: terminology:NOT_SUBSUMED}]};
}
public isolated function searchConcept(DISPLAY|DEFINITION property, string filter, string? system, int offset, int count) returns terminology:CodeConceptDetails[]|r4:FHIRError {
sql:ParameterizedQuery whereClause = sql:queryConcat(
escapeToQuery(property), getRegexOperator(), stringToParameterizedQuery("'.*" + filter + ".*'"),
system is () ? `` : sql:queryConcat(` AND c.`, escapeToQuery("url"), ` = ${system}`),
getLimitClause(count, offset)
);
stream<store_h2:ConceptWithRelations, persist:Error?> conceptStream = sClient->/concepts(store_h2:ConceptWithRelations, whereClause);
store_h2:ConceptWithRelations[]|error dbConcepts = from store_h2:ConceptWithRelations concept in conceptStream
select concept;
if dbConcepts is error {
return r4:createFHIRError(
dbConcepts.message(),
r4:ERROR,
r4:INVALID_REQUIRED,
cause = dbConcepts,
httpStatusCode = http:STATUS_INTERNAL_SERVER_ERROR);
}
terminology:CodeConceptDetails[] concepts = [];
foreach store_h2:ConceptWithRelations dbConcept in dbConcepts {
r4:CodeSystemConcept|error codeSystemConcept = byteToConcept(<byte[]>dbConcept.concept);
if codeSystemConcept is error {
// Skip this Concept if parsing fails
continue;
}
terminology:CodeConceptDetails details = {
url: dbConcept.codeSystem?.url ?: "",
concept: codeSystemConcept
};
concepts.push(details);
}
return concepts;
}
public isolated function addConceptMap(r4:ConceptMap conceptMap) returns r4:FHIRError? {
return;
}
public isolated function findConceptMaps(r4:uri sourceValueSetUri, r4:uri? targetValueSetUri) returns r4:ConceptMap[]|r4:FHIRError {
return [];
}
public isolated function getConceptMap(r4:uri conceptMapUrl, string? version) returns r4:ConceptMap|r4:FHIRError {
return r4:createFHIRError(
"ConceptMap operation not yet implemented",
r4:ERROR,
r4:PROCESSING_NOT_FOUND,
cause = error("ConceptMap retrieval not supported"),
httpStatusCode = http:STATUS_NOT_IMPLEMENTED);
}
public isolated function isConceptMapExist(r4:uri system, string version) returns boolean {
return false;
}
public isolated function searchConceptMap(map<r4:RequestSearchParameter[]> params, int? offset, int? count) returns r4:ConceptMap[]|r4:FHIRError {
return [];
}
}
isolated function isInParentChain(int targetAncestorId, ConceptNode currentNode) returns boolean {
int? parentId = currentNode.parentConceptId;
while parentId is int {
if parentId == targetAncestorId {
return true;
}
ConceptNode|error nextNode = sClient->/concepts/[parentId](ConceptNode);
if nextNode is error {
return false;
}
parentId = nextNode.parentConceptId;
}
return false;
}
isolated function findConceptInValueSet(r4:uri system, r4:code code, string? version) returns terminology:CodeConceptDetails|r4:FHIRError {
// check whether the value set exists
var valueset = getStoreValueSetByURL(system, version);
if valueset !is store_h2:ValueSet {
return r4:createFHIRError(
"CodeSystem not found",
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("No matching CodeSystem found"),
httpStatusCode = http:STATUS_NOT_FOUND);
}
// checks for valueset concepts
sql:ParameterizedQuery sqlQuery = sql:queryConcat(
`SELECT c.* FROM `, escapeToQuery("concepts"),
` c JOIN `, escapeToQuery("valueset_compose_include_concepts"), ` vcic ON c.`, escapeToQuery("conceptId"), ` = vcic.`, escapeToQuery("conceptConceptId"),
`JOIN `, escapeToQuery("valueset_compose_includes"), ` vci ON vcic.`, escapeToQuery("valuesetcomposeValueSetComposeIncludeId"), ` = vci.`, escapeToQuery("valueSetComposeIncludeId"),
`JOIN "valuesets" vs ON vci.`, escapeToQuery("valuesetValueSetId"), ` = vs.`, escapeToQuery("valueSetId"),
`WHERE vs.`, escapeToQuery("valueSetId"), ` = ${valueset.valueSetId} AND c.`, escapeToQuery("code"), ` = ${code};`
);
store_h2:Concept|r4:FHIRError dbConcept = getStoreConcept(sqlQuery);
if dbConcept !is r4:FHIRError {
r4:CodeSystemConcept|error valueSetConcept = byteToConcept(dbConcept.concept);
if valueSetConcept !is error {
return {
url: system,
concept: valueSetConcept
};
}
}
// checks for code systems
sqlQuery = sql:queryConcat(
`SELECT c.* FROM `, escapeToQuery("concepts"), ` c JOIN `, escapeToQuery("codesystems"), ` cs ON c.`, escapeToQuery("codesystemCodeSystemId"), ` = cs.`, escapeToQuery("codeSystemId"),
` JOIN `, escapeToQuery("valueset_compose_includes"), ` vci ON cs.`, escapeToQuery("codeSystemId"), ` = vci.`, escapeToQuery("codeSystemId"),
` JOIN `, escapeToQuery("valuesets"), ` vs ON vci.`, escapeToQuery("valuesetValueSetId"), ` = vs.`, escapeToQuery("valueSetId"),
` WHERE vs.`, escapeToQuery("valueSetId"), ` = ${valueset.valueSetId} AND c.`, escapeToQuery("code"), ` = ${code};`
);
dbConcept = getStoreConcept(sqlQuery);
if dbConcept !is r4:FHIRError {
r4:CodeSystemConcept|error valueSetConcept = byteToConcept(dbConcept.concept);
if valueSetConcept !is error {
return {
url: system,
concept: valueSetConcept
};
}
}
// checks for nested valueset references
sqlQuery = sql:queryConcat(
`SELECT vs_included.* FROM `, escapeToQuery("valuesets"), ` vs_parent JOIN `, escapeToQuery("valueset_compose_includes"), ` vci ON vs_parent.`, escapeToQuery("valueSetId"), ` = vci.`, escapeToQuery("valuesetValueSetId"),
` JOIN `, escapeToQuery("valueset_compose_include_value_sets"), ` vcivs ON vci.`, escapeToQuery("valueSetComposeIncludeId"), ` = vcivs.`, escapeToQuery("valuesetcomposeValueSetComposeIncludeId"),
` JOIN `, escapeToQuery("valuesets"), ` vs_included ON vcivs.`, escapeToQuery("valuesetValueSetId"), ` = vs_included.`, escapeToQuery("valueSetId"),
` WHERE vs_parent.`, escapeToQuery("valueSetId"), ` = ${valueset.valueSetId};`
);
stream<store_h2:ValueSet, persist:Error?> valueSetStream = sClient->queryNativeSQL(sqlQuery);
store_h2:ValueSet[]|error nestedValueSets = streamToStoreValueSet(valueSetStream);
if nestedValueSets is error {
return r4:createFHIRError(
"Error while searching for Concept, " + nestedValueSets.message(),
r4:ERROR,
r4:INVALID_REQUIRED,
cause = nestedValueSets,
httpStatusCode = http:STATUS_INTERNAL_SERVER_ERROR);
}
if nestedValueSets.length() > 0 {
foreach store_h2:ValueSet nestedValueSet in nestedValueSets {
var result = findConceptInValueSet(nestedValueSet.url, code, nestedValueSet.version);
if result !is r4:FHIRError {
return result;
}
}
}
// not found in the value set
return r4:createFHIRError(
"Concept not found",
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("No matching Concept found"),
httpStatusCode = http:STATUS_NOT_FOUND);
}
isolated function findConceptInCodeSystem(r4:uri system, r4:code code, string? version) returns terminology:CodeConceptDetails|r4:FHIRError {
// check whether the code system exists
var codeSystem = getStoreCodeSystemByURL(system, version);
if codeSystem !is store_h2:CodeSystem {
return r4:createFHIRError(
"CodeSystem not found",
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("No matching CodeSystem found"),
httpStatusCode = http:STATUS_NOT_FOUND);
}
var dbConcept = getStoreConceptByCode(codeSystem.codeSystemId, code);
if dbConcept is error {
return dbConcept;
}
r4:CodeSystemConcept|error codeSystemConcept = byteToConcept(dbConcept.concept);
if codeSystemConcept is error {
return r4:createFHIRError(
"Error while parsing Concept, " + codeSystemConcept.message(),
r4:ERROR,
r4:INVALID_REQUIRED,
cause = codeSystemConcept,
httpStatusCode = http:STATUS_INTERNAL_SERVER_ERROR);
}
return {
url: system,
concept: codeSystemConcept
};
}
isolated function getCodeSystemByID(string id, string? version = ()) returns r4:CodeSystem|error {
// TODO: Replace the manual query-based search operation below with the commented logic once the following persist issue is resolved:
// https://github.com/ballerina-platform/ballerina-library/issues/7920
//
// store_h2:CodeSystem[] codeSystems;
// if version !is () {
// codeSystems = check from store_h2:CodeSystem codesystem in sClient->/codesystems(store_h2:CodeSystem)
// where codesystem.id == id && codesystem.'version == version
// select codesystem;
// } else {
// codeSystems = check from store_h2:CodeSystem codesystem in sClient->/codesystems(store_h2:CodeSystem)
// where codesystem.id == id
// order by codesystem.version descending
// limit 1
// select codesystem;
// }
sql:ParameterizedQuery sqlQueryWhereClause = version is ()
? sql:queryConcat(escapeToQuery("id"), ` = ${id} ORDER BY `, escapeToQuery("version"), ` DESC LIMIT 1`)
: sql:queryConcat(escapeToQuery("id"), ` = ${id} AND `, escapeToQuery("version"), ` = ${version}`);
stream<store_h2:CodeSystem, persist:Error?> codeSystemStream = sClient->/codesystems(store_h2:CodeSystem, whereClause = sqlQueryWhereClause);
store_h2:CodeSystem[] codeSystems = check streamToStoreCodeSystem(codeSystemStream);
if codeSystems.length() == 0 {
return r4:createFHIRError(
"CodeSystem not found",
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("No matching CodeSystem found"),
httpStatusCode = http:STATUS_NOT_FOUND);
}
return byteToCodeSystem(codeSystems[0].codeSystem);
}
isolated function getCodeSystemByURL(string system, string? version = ()) returns r4:CodeSystem|error {
store_h2:CodeSystem storeCodeSystem = check getStoreCodeSystemByURL(system, version);
return byteToCodeSystem(storeCodeSystem.codeSystem);
}
isolated function getStoreCodeSystemByURL(string system, string? version = ()) returns store_h2:CodeSystem|error {
// TODO: Replace the manual query-based search operation below with the commented logic once the following persist issue is resolved:
// https://github.com/ballerina-platform/ballerina-library/issues/7920
//
// The recommended approach is:
// store_h2:CodeSystem[] codeSystems;
// if version !is () {
// codeSystems = check from store_h2:CodeSystem codesystem in sClient->/codesystems(store_h2:CodeSystem)
// where codesystem.url == system && codesystem.version == version
// select codesystem;
// } else {
// codeSystems = check from store_h2:CodeSystem codesystem in sClient->/codesystems(store_h2:CodeSystem)
// where codesystem.url == system
// order by codesystem.version descending
// limit 1
// select codesystem;
// }
sql:ParameterizedQuery sqlQueryWhereClause = version is ()
? sql:queryConcat(escapeToQuery("url"), ` = ${system} ORDER BY `, escapeToQuery("version"), ` DESC LIMIT 1`)
: sql:queryConcat(escapeToQuery("url"), ` = ${system} AND `, escapeToQuery("version"), ` = ${version}`);
stream<store_h2:CodeSystem, persist:Error?> codeSystemStream = sClient->/codesystems(store_h2:CodeSystem, whereClause = sqlQueryWhereClause);
store_h2:CodeSystem[] codeSystems = check streamToStoreCodeSystem(codeSystemStream);
if codeSystems.length() == 0 {
return r4:createFHIRError(
"CodeSystem not found",
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("No matching CodeSystem found"),
httpStatusCode = http:STATUS_NOT_FOUND);
}
return codeSystems[0];
}
isolated function getValueSetByID(string id, string? version = ()) returns r4:ValueSet|error {
// TODO: Replace the manual query-based search operation below with the commented logic once the following persist issue is resolved:
// https://github.com/ballerina-platform/ballerina-library/issues/7920
//
// store_h2:ValueSet[] valueSets;
// if version !is () {
// valueSets = check from store_h2:ValueSet valueSet in sClient->/valuesets(store_h2:ValueSet)
// where valueSet.id == id && valueSet.version == version
// select valueSet;
// } else {
// valueSets = check from store_h2:ValueSet valueSet in sClient->/valuesets(store_h2:ValueSet)
// where valueSet.id == id
// order by valueSet.version descending
// limit 1
// select valueSet;
// }
sql:ParameterizedQuery sqlQueryWhereClause = version is ()
? sql:queryConcat(escapeToQuery("id"), ` = ${id} ORDER BY `, escapeToQuery("version"), ` DESC LIMIT 1`)
: sql:queryConcat(escapeToQuery("id"), ` = ${id} AND `, escapeToQuery("version"), ` = ${version}`);
stream<store_h2:ValueSet, persist:Error?> valueSetStream = sClient->/valuesets(store_h2:ValueSet, whereClause = sqlQueryWhereClause);
store_h2:ValueSet[] valueSets = check streamToStoreValueSet(valueSetStream);
if valueSets.length() == 0 {
return r4:createFHIRError(
"ValueSet not found",
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("No matching ValueSet found"),
httpStatusCode = http:STATUS_NOT_FOUND);
}
// Assuming byteToValueSet is available similar to byteToCodeSystem
return byteToValueSet(valueSets[0].valueSet);
}
isolated function getValueSetByURL(string system, string? version = ()) returns r4:ValueSet|error {
store_h2:ValueSet storeValueSet = check getStoreValueSetByURL(system, version);
return byteToValueSet(storeValueSet.valueSet);
}
isolated function getStoreValueSetByURL(string system, string? version = ()) returns store_h2:ValueSet|error {
// TODO: Replace the manual query-based search operation below with the commented logic once the following persist issue is resolved:
// https://github.com/ballerina-platform/ballerina-library/issues/7920
//
// store_h2:ValueSet[] valueSets;
// if version !is () {
// valueSets = check from store_h2:ValueSet valueSet in sClient->/valuesets(store_h2:ValueSet)
// where valueSet.url == system && valueSet.version == version
// select valueSet;
// } else {
// valueSets = check from store_h2:ValueSet valueSet in sClient->/valuesets(store_h2:ValueSet)
// where valueSet.url == system
// order by valueSet.version descending
// limit 1
// select valueSet;
// }
sql:ParameterizedQuery sqlQueryWhereClause = version is ()
? sql:queryConcat(escapeToQuery("url"), ` = ${system} ORDER BY `, escapeToQuery("version"), ` DESC LIMIT 1`)
: sql:queryConcat(escapeToQuery("url"), ` = ${system} AND `, escapeToQuery("version"), ` = ${version}`);
stream<store_h2:ValueSet, persist:Error?> valueSetStream = sClient->/valuesets(store_h2:ValueSet, whereClause = sqlQueryWhereClause);
store_h2:ValueSet[] valueSets = check streamToStoreValueSet(valueSetStream);
if valueSets.length() == 0 {
return r4:createFHIRError(
"ValueSet not found",
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("No matching ValueSet found"),
httpStatusCode = http:STATUS_NOT_FOUND);
}
return valueSets[0];
}
isolated function getStoreConceptByCode(int codeSystemId, r4:code code) returns store_h2:Concept|r4:FHIRError {
// TODO: Replace the manual query-based search operation below with the commented logic once the following persist issue is resolved:
// https://github.com/ballerina-platform/ballerina-library/issues/7920
//
// store_h2:Concept[] concepts = check from store_h2:Concept concept in sClient->/concepts(store_h2:Concept)
// where concept.code == code && concept.codesystemCodeSystemId == codeSystemId
// select concept;
//
// if concepts.length() > 0 {
// return concepts[0];
// } else {
// return r4:createFHIRError(
// "Concept not found",
// r4:ERROR,
// r4:INVALID_REQUIRED,
// cause = error("No matching Concept found"),
// httpStatusCode = http:STATUS_NOT_FOUND);
// }
return getStoreConcept(sql:queryConcat(`SELECT * FROM `, escapeToQuery("concepts"), ` WHERE `, escapeToQuery("code"), ` = ${code} AND `, escapeToQuery("codesystemCodeSystemId"), ` = ${codeSystemId}`));
}
isolated function getStoreConcept(sql:ParameterizedQuery sqlQuery) returns store_h2:Concept|r4:FHIRError {
stream<store_h2:Concept, persist:Error?> conceptStream = sClient->queryNativeSQL(sqlQuery);
store_h2:Concept[]|error dbConcepts = streamToStoreConcept(conceptStream);
if dbConcepts is error {
return r4:createFHIRError(
"Error while searching for Concept, " + dbConcepts.message(),
r4:ERROR,
r4:INVALID_REQUIRED,
cause = dbConcepts,
httpStatusCode = http:STATUS_INTERNAL_SERVER_ERROR);
}
if dbConcepts.length() > 0 {
return dbConcepts[0];
}
// concept not found
return r4:createFHIRError(
"Concept not found",
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("No matching Concept found"),
httpStatusCode = http:STATUS_NOT_FOUND);
}
isolated function getConceptNode(string code, int codeSystemId) returns ConceptNode|r4:FHIRError {
// TODO: Replace the manual query-based search operation below with the commented logic once the following persist issue is resolved:
// https://github.com/ballerina-platform/ballerina-library/issues/7920
//
// The recommended approach is:
// ConceptNode[] conceptNodes = check from ConceptNode concept in sClient->/concepts(ConceptNode)
// where concept.code == code && concept.codesystemCodeSystemId == codeSystemId
// select concept;
sql:ParameterizedQuery sqlQuery = sql:queryConcat(escapeToQuery("code"), ` = ${code} AND `, escapeToQuery("codesystemCodeSystemId"), ` = ${codeSystemId}`);
stream<ConceptNode, persist:Error?> conceptStream = sClient->/concepts(ConceptNode, whereClause = sqlQuery);
ConceptNode[]|error dbConcepts = from ConceptNode concept in conceptStream
select concept;
if dbConcepts is error {
return r4:createFHIRError(
"Error while searching for Concept, " + dbConcepts.message(),
r4:ERROR,
r4:INVALID_REQUIRED,
cause = dbConcepts,
httpStatusCode = http:STATUS_INTERNAL_SERVER_ERROR);
}
if dbConcepts.length() > 0 {
return dbConcepts[0];
}
// concept not found
return r4:createFHIRError(
"Concept not found",
r4:ERROR,
r4:INVALID_REQUIRED,
cause = error("No matching Concept found"),
httpStatusCode = http:STATUS_NOT_FOUND);
}
isolated function extractConceptsFromCodeSystem(r4:CodeSystem codeSystem, int codeSystemId) {
if codeSystem.concept !is () {
r4:CodeSystemConcept[]? concepts = codeSystem.concept;
// Flow will not go inside this if condition since the code system is already validated for concepts in a previous point
if concepts !is () {
foreach var concept in concepts {
_ = start extractConceptsFromCodeSystemRecursive(concept.clone(), codeSystemId);
}
}
}
}
isolated function extractConceptsFromCodeSystemRecursive(r4:CodeSystemConcept var_concept, int codeSystemId, int? parentId = ()) {
int|error result = saveCodeSystemConcept(var_concept, codeSystemId, parentId);
if result is error {
log:printError("Error while saving concept: " + result.message());
}
if var_concept.concept !is () {
r4:CodeSystemConcept[]? concepts = var_concept.concept;
if concepts != () && concepts.length() > 0 {
foreach var subConcept in concepts {
_ = start extractConceptsFromCodeSystemRecursive(subConcept.clone(), codeSystemId, (result is int) ? result : ());
}
}
}
}
isolated function saveCodeSystemConcept(r4:CodeSystemConcept concept, int codeSystemId, int? parentId) returns int|error {
store_h2:ConceptInsert dbConceptInsert = {
code: concept.code,
display: concept.display,
definition: concept.definition,
concept: check conceptToByte(concept),
codesystemCodeSystemId: codeSystemId,
parentConceptId: parentId
};
int[] id = check sClient->/concepts.post([dbConceptInsert]);
return id[0];
}
// Extract concepts from a ValueSet and save them recursively
isolated function extractConceptsFromValueSet(r4:ValueSet valueSet, int valueSetId) {
r4:ValueSetCompose? compose = valueSet.compose;
if compose !is () {
foreach r4:ValueSetComposeInclude include in compose.include {
error? result = saveValueSetComposeInclude(include, valueSetId);
if result is error {
log:printError("Error while saving ValueSet concept: " + result.message());
}
}
}
}
// Save a ValueSet concept to the database
isolated function saveValueSetComposeInclude(r4:ValueSetComposeInclude include, int valueSetId) returns error? {
// concept can be a code system or a set of concepts
if include.system is r4:uri {
// find he CodeSystem in the database
store_h2:CodeSystem codesystem = check getStoreCodeSystemByURL(<string>include.system, include.'version);
r4:ValueSetComposeIncludeConcept[]? valueSetComposeIncludeConcept = include.concept;
if valueSetComposeIncludeConcept !is () {
foreach r4:ValueSetComposeIncludeConcept item in valueSetComposeIncludeConcept {
// save valueset concept
_ = start saveValueSetConcept(item.clone(), valueSetId, codesystem.codeSystemId);