-
Notifications
You must be signed in to change notification settings - Fork 5
/
BaseOclFhirController.java
191 lines (167 loc) · 9.08 KB
/
BaseOclFhirController.java
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
package org.openconceptlab.fhir.controller;
import ca.uhn.fhir.rest.gclient.IQuery;
import ca.uhn.fhir.rest.gclient.StringClientParam;
import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException;
import org.hl7.fhir.r4.model.*;
import org.openconceptlab.fhir.util.OclFhirUtil;
import org.openconceptlab.fhir.provider.CodeSystemResourceProvider;
import org.openconceptlab.fhir.provider.ValueSetResourceProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Optional;
import static org.openconceptlab.fhir.util.OclFhirUtil.*;
import static org.openconceptlab.fhir.util.OclFhirUtil.getIdentifier;
import static org.openconceptlab.fhir.util.OclFhirConstants.*;
import static org.openconceptlab.fhir.util.OclFhirConstants.FW_SLASH;
@Component
public class BaseOclFhirController {
CodeSystemResourceProvider codeSystemResourceProvider;
ValueSetResourceProvider valueSetResourceProvider;
OclFhirUtil oclFhirUtil;
@Autowired
public BaseOclFhirController(CodeSystemResourceProvider codeSystemResourceProvider,
ValueSetResourceProvider valueSetResourceProvider,
OclFhirUtil oclFhirUtil) {
this.codeSystemResourceProvider = codeSystemResourceProvider;
this.valueSetResourceProvider = valueSetResourceProvider;
this.oclFhirUtil = oclFhirUtil;
}
protected ResponseEntity<String> handleSearchResource(final Class<? extends MetadataResource> resourceClass, final String... args) {
try {
String resource = searchResource(resourceClass, args);
return ResponseEntity.ok(resource);
} catch (BaseServerResponseException e) {
return ResponseEntity.status(e.getStatusCode()).body(e.getResponseBody());
} catch (Exception e) {
return badRequest();
}
}
protected ResponseEntity<String> handleFhirOperation(Parameters parameters, Class<? extends Resource> type, String operation) {
try {
return ResponseEntity.ok(oclFhirUtil.getResourceAsString(performFhirOperation(parameters, type, operation)));
} catch (BaseServerResponseException e) {
return ResponseEntity.status(e.getStatusCode()).body(e.getResponseBody());
} catch (Exception e) {
return badRequest();
}
}
protected String searchResource(final Class<? extends MetadataResource> resourceClass, final String... filters) {
IQuery q = oclFhirUtil.getClient().search().forResource(resourceClass);
if (filters.length % 2 == 0) {
for (int i = 0; i < filters.length; i += 2) {
if (i == 0) {
q = q.where(new StringClientParam(filters[i]).matches().value(filters[i + 1]));
} else {
q = q.and(new StringClientParam(filters[i]).matches().value(filters[i + 1]));
}
}
}
Bundle bundle = (Bundle) q.execute();
return oclFhirUtil.getResourceAsString(bundle);
}
protected Parameters performFhirOperation(Parameters parameters, Class<? extends Resource> type, String operation) {
return oclFhirUtil.getClient()
.operation()
.onType(type)
.named(operation)
.withParameters(parameters)
.execute();
}
protected void performCreate(CodeSystem resource, String auth) {
oclFhirUtil.getClient()
.create()
.resource(resource).withAdditionalHeader(AUTHORIZATION, auth)
.execute();
}
protected Parameters generateParameters(String code, String displayLanguage, String owner) {
Parameters parameters = new Parameters();
parameters.addParameter().setName(CODE).setValue(new CodeType(code));
if (isValid(displayLanguage))
parameters.addParameter().setName(DISP_LANG).setValue(new CodeType(displayLanguage));
parameters.addParameter().setName(OWNER).setValue(newStringType(owner));
return parameters;
}
protected Parameters lookupParameters(String system, String code, String version, String displayLanguage, String owner) {
Parameters parameters = generateParameters(code, displayLanguage, owner);
parameters.addParameter().setName(SYSTEM).setValue(new UriType(system));
if (isValid(version))
parameters.addParameter().setName(VERSION).setValue(newStringType(version));
return parameters;
}
protected Parameters codeSystemVCParameters(String url, String code, String version, String display, String displayLanguage,
String owner) {
Parameters parameters = generateParameters(code, displayLanguage, owner);
parameters.addParameter().setName(URL).setValue(new UriType(url));
if (isValid(version))
parameters.addParameter().setName(VERSION).setValue(newStringType(version));
if (isValid(display))
parameters.addParameter().setName(DISPLAY).setValue(newStringType(display));
return parameters;
}
protected Parameters valueSetVCParameters(String url, String valueSetId, String valueSetVersion, String code, String system, String systemVersion,
String display, String displayLanguage, String owner) {
Parameters parameters = generateParameters(code, displayLanguage, owner);
parameters.addParameter().setName(SYSTEM).setValue(new UriType(system));
if (isValid(url))
parameters.addParameter().setName(URL).setValue(new UriType(url));
if (isValid(valueSetId))
parameters.addParameter().setName("valueSetId").setValue(newStringType(valueSetId));
if (isValid(systemVersion))
parameters.addParameter().setName(SYSTEM_VERSION).setValue(newStringType(systemVersion));
if (isValid(valueSetVersion))
parameters.addParameter().setName(VALUESET_VERSION).setValue(newStringType(valueSetVersion));
if (isValid(display))
parameters.addParameter().setName(DISPLAY).setValue(newStringType(display));
return parameters;
}
protected Parameters valueSetExpandParameters(String url, String valueSetVersion, Integer offset, Integer count, Boolean includeDesignations,
Boolean includeDefinition, Boolean activeOnly, String displayLanguage, String filter, String owner) {
Parameters parameters = new Parameters();
if (isValid(url))
parameters.addParameter().setName(URL).setValue(newUri(url));
if (isValid(valueSetVersion))
parameters.addParameter().setName(VALUESET_VERSION).setValue(newStringType(valueSetVersion));
if (isValid(displayLanguage))
parameters.addParameter().setName(DISPLAY_LANGUAGE).setValue(newStringType(displayLanguage));
if (isValid(filter))
parameters.addParameter().setName(FILTER).setValue(newStringType(filter));
parameters.addParameter().setName(OFFSET).setValue(newInteger(offset));
parameters.addParameter().setName(COUNT).setValue(newInteger(count));
parameters.addParameter().setName(INCLUDE_DESIGNATIONS).setValue(newBoolean(includeDesignations));
parameters.addParameter().setName(INCLUDE_DEFINITION).setValue(newBoolean(includeDefinition));
parameters.addParameter().setName(ACTIVE_ONLY).setValue(newBoolean(activeOnly));
parameters.addParameter().setName(OWNER).setValue(newStringType(owner));
return parameters;
}
protected static String formatOrg(String org) {
return ORG_ + org;
}
protected static String formatUser(String user) {
return USER_ + user;
}
protected ResponseEntity<String> validate(String user, CodeSystem system, Optional<Identifier> acsnOpt,
String ownerType, String ownerId) {
ResponseEntity<String> response1 = validateId(system.getId());
if (response1 != null) return response1;
return validateAccessionId(acsnOpt, ownerType, ownerId);
}
protected ResponseEntity<String> validateId(String id) {
if (!isValid(id))
return badRequest("The id can not be empty.");
return null;
}
protected ResponseEntity<String> validateAccessionId(Optional<Identifier> acsnOpt, String ownerType, String ownerId) {
if (acsnOpt.isPresent()) {
String[] values = formatExpression(acsnOpt.get().getValue()).split(FW_SLASH);
if (!(values.length >= 3 && ownerType.equals(values[1]) && ownerId.equals(values[2]))) {
return badRequest("The Accession id does not match with given request.");
}
}
return null;
}
protected void addIdentifier(List<Identifier> identifiers, String ownerType, String ownerId, String resType, String resId) {
identifiers.add(getIdentifier(FW_SLASH + ownerType + FW_SLASH + ownerId + FW_SLASH + resType + FW_SLASH + resId + FW_SLASH).get());
}
}