forked from wso2/open-healthcare-prebuilt-services
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.bal
More file actions
235 lines (189 loc) · 8.48 KB
/
Copy pathutils.bal
File metadata and controls
235 lines (189 loc) · 8.48 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
// 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 ballerina/file;
import ballerina/io;
import ballerina/regex;
import ballerina/time;
import ballerinax/health.fhir.r4;
import ballerinax/health.fhir.r4.parser;
import ballerinacentral/zip;
// Module-level counter for unique file naming
isolated int fileCount = 0;
isolated function createNewTempDirectory() returns string {
lock {
fileCount = fileCount + 1;
return TEMPORARY_FILES_DIRECTORY_NAME + "/payload_" + fileCount.toString();
}
}
isolated function validationResultToParameters(r4:Parameters|r4:FHIRError concept) returns r4:Parameters|r4:FHIRError {
r4:ParametersParameter[] params = [];
if concept is r4:FHIRError {
if concept.message().matches(re `Can not find any valid concepts for the code:.*`) {
params.push({name: "result", valueBoolean: false});
} else {
return concept;
}
} else {
if (<r4:ParametersParameter[]>concept.'parameter).length() > 0 {
foreach var c in <r4:ParametersParameter[]>concept.'parameter {
_ = c.name == "name" ? params.push({name: "result", valueBoolean: true}) : "";
_ = c.name == "display" ? params.push(c) : "";
_ = c.name == "definition" ? params.push(c) : "";
}
} else {
params.push({name: "result", valueBoolean: false});
}
}
return {
'parameter: params
};
}
isolated function prepareRequestSearchParameter(map<string[]> params) returns map<r4:RequestSearchParameter[]> {
map<r4:RequestSearchParameter[]> searchParams = {};
foreach var 'key in params.keys() {
match 'key {
"_id" => {
searchParams["_id"] = [createRequestSearchParameter("_id", params.get("_id")[0])];
}
"name" => {
searchParams["name"] = [createRequestSearchParameter("name", params.get("name")[0])];
}
"title" => {
searchParams["title"] = [createRequestSearchParameter("title", params.get("title")[0])];
}
"url" => {
searchParams["url"] = [createRequestSearchParameter("url", params.get("url")[0])];
}
"version" => {
r4:RequestSearchParameter[] tempList = [];
foreach var value in params.get("version") {
tempList.push(createRequestSearchParameter("version", value, 'type = r4:STRING));
}
searchParams["version"] = tempList;
}
"description" => {
searchParams["description"] = [createRequestSearchParameter("description", params.get("description")[0])];
}
"publisher" => {
searchParams["publisher"] = [createRequestSearchParameter("publisher", params.get("publisher")[0])];
}
"status" => {
r4:RequestSearchParameter[] tempList = [];
foreach var value in params.get("status") {
tempList.push(createRequestSearchParameter("status", value, 'type = r4:REFERENCE));
}
searchParams["status"] = tempList;
}
"valueSetVersion" => {
searchParams["valueSetVersion"] = [createRequestSearchParameter("valueSetVersion", params.get("valueSetVersion")[0])];
}
"filter" => {
searchParams["filter"] = [createRequestSearchParameter("filter", params.get("filter")[0])];
}
"_count" => {
searchParams["_count"] = [createRequestSearchParameter("_count", params.get("_count")[0], 'type = r4:NUMBER)];
}
"_offset" => {
searchParams["_offset"] = [createRequestSearchParameter("_offset", params.get("_offset")[0], 'type = r4:NUMBER)];
}
}
}
return searchParams;
}
isolated function createRequestSearchParameter(string name, string value, r4:FHIRSearchParameterType? 'type = r4:STRING, r4:FHIRSearchParameterModifier? modifier = r4:MODIFIER_EXACT) returns r4:RequestSearchParameter {
return {name: name, value: value, 'type: r4:STRING, typedValue: {modifier: modifier}};
}
isolated function codeSystemConceptPropertyToParameter(r4:CodeSystemConceptProperty property) returns r4:ParametersParameter {
r4:ParametersParameter param = {name: "property"};
r4:ParametersParameter[] part = [];
if property.valueString is string {
part.push(
{name: "code", valueCode: property.code},
{name: "value", valueString: property.valueString}
);
}
if property.valueCoding is r4:Coding {
part.push(
{name: "code", valueCode: property.code},
{name: "value", valueCoding: property.valueCoding}
);
}
param.part = part;
return param;
}
isolated function extractZipFile(string dirPath) returns error? {
check zip:extract(dirPath + ZIP_FILE_NAME, dirPath + ZIP_FILE_EXTRACTION_PATH);
}
isolated function removeDirectory(string dirPath) returns error? {
if check file:test(dirPath, file:EXISTS) {
check file:remove(dirPath, file:RECURSIVE);
}
}
isolated function saveCompressedPayload(stream<byte[], io:Error?> payloadStream, string dirPath) returns error? {
check removeDirectory(dirPath);
check file:createDir(dirPath, file:RECURSIVE);
check io:fileWriteBlocksFromStream(dirPath + ZIP_FILE_NAME, payloadStream);
}
isolated function readFilesForUpload(string path) returns CodeSystemValueSetJson|error {
file:MetaData[] readDir = check file:readDir(path + FHIR_PACKAGE_PATH);
CodeSystemValueSetJson jsonArrays = {
codeSystems: [],
valueSets: []
};
foreach var item in readDir {
string[] nonEmptyParts = regex:split(item.absPath, "\\\\").filter(s => s != "");
string lastPart = nonEmptyParts[nonEmptyParts.length() - 1];
if lastPart.endsWith(".json") && lastPart.startsWith("CodeSystem-") {
jsonArrays.codeSystems.push(check io:fileReadJson(item.absPath));
} else if lastPart.endsWith(".json") && lastPart.startsWith("ValueSet-") {
jsonArrays.valueSets.push(check io:fileReadJson(item.absPath));
}
}
return jsonArrays;
}
isolated function readFilesAsJsons(string path) returns json[]|error {
file:MetaData[] readDir = check file:readDir(path);
json[] jsonList = [];
foreach var item in readDir {
string[] nonEmptyParts = regex:split(item.absPath, "\\\\").filter(s => s != "");
string lastPart = nonEmptyParts[nonEmptyParts.length() - 1];
if lastPart.endsWith(".json") {
jsonList.push(check io:fileReadJson(item.absPath));
}
}
return jsonList;
}
isolated function readFileJsonAndReturnCodeSystem(string path) returns r4:CodeSystem|error {
string jsonString = check io:fileReadString(path);
return check parser:parse(jsonString).ensureType();
}
function init() returns error? {
check removeDirectory(TEMPORARY_FILES_DIRECTORY_NAME);
}
isolated function createExpandedValueSet(r4:ValueSet vs, r4:ValueSetExpansionContains[] concepts) returns r4:ValueSetExpansion {
r4:ValueSetExpansionContains[] contains = [];
foreach r4:ValueSetExpansionContains concept in concepts {
r4:ValueSetExpansionContains c = {code: concept.code, display: concept.display, id: concept.id};
contains.push(c);
}
r4:ValueSetExpansion expansion = {timestamp: time:utcToString(time:utcNow()), contains: contains};
return expansion;
}
public isolated function getSearchParametersFromFHIRContext(r4:FHIRContext fhirContext) returns map<r4:RequestSearchParameter[]>|error {
map<r4:RequestSearchParameter[]>|error searchParameters = {};
r4:FHIRRequest? fhirRequest = fhirContext.getFHIRRequest();
if fhirRequest !is () {
searchParameters = check fhirRequest.getSearchParameters().cloneWithType();
}
return searchParameters;
}