-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathRuntimeResourceDefinition.java
309 lines (273 loc) · 10.1 KB
/
RuntimeResourceDefinition.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
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
/*
* #%L
* HAPI FHIR - Core Library
* %%
* Copyright (C) 2014 - 2025 Smile CDR, Inc.
* %%
* Licensed 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.
* #L%
*/
package ca.uhn.fhir.context;
import ca.uhn.fhir.i18n.Msg;
import ca.uhn.fhir.model.api.annotation.ResourceDef;
import ca.uhn.fhir.util.UrlUtil;
import org.hl7.fhir.instance.model.api.IAnyResource;
import org.hl7.fhir.instance.model.api.IBase;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IDomainResource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class RuntimeResourceDefinition extends BaseRuntimeElementCompositeDefinition<IBaseResource> {
private Class<? extends IBaseResource> myBaseType;
private Map<String, List<RuntimeSearchParam>> myCompartmentNameToSearchParams;
private FhirContext myContext;
private String myId;
private Map<String, RuntimeSearchParam> myNameToSearchParam = new LinkedHashMap<String, RuntimeSearchParam>();
private IBaseResource myProfileDef;
private String myResourceProfile;
private List<RuntimeSearchParam> mySearchParams;
private final FhirVersionEnum myStructureVersion;
private volatile RuntimeResourceDefinition myBaseDefinition;
public RuntimeResourceDefinition(
FhirContext theContext,
String theResourceName,
Class<? extends IBaseResource> theClass,
ResourceDef theResourceAnnotation,
boolean theStandardType,
Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
super(theResourceName, theClass, theStandardType, theContext, theClassToElementDefinitions);
myContext = theContext;
myResourceProfile = theResourceAnnotation.profile();
myId = theResourceAnnotation.id();
IBaseResource instance;
try {
instance = theClass.getConstructor().newInstance();
} catch (Exception e) {
throw new ConfigurationException(
Msg.code(1730)
+ myContext
.getLocalizer()
.getMessage(getClass(), "nonInstantiableType", theClass.getName(), e.toString()),
e);
}
myStructureVersion = instance.getStructureFhirVersionEnum();
if (myStructureVersion != theContext.getVersion().getVersion()) {
if (myStructureVersion == FhirVersionEnum.R5
&& theContext.getVersion().getVersion() == FhirVersionEnum.R4B) {
// TODO: remove this exception once we've bumped FHIR core to a new version
// TODO: also fix the TODO in ModelScanner
// TODO: also fix the TODO in RestfulServerUtils
// TODO: also fix the TODO in BaseParser
} else {
throw new ConfigurationException(Msg.code(1731)
+ myContext
.getLocalizer()
.getMessage(
getClass(),
"typeWrongVersion",
theContext.getVersion().getVersion(),
theClass.getName(),
myStructureVersion));
}
}
}
public void addSearchParam(RuntimeSearchParam theParam) {
myNameToSearchParam.put(theParam.getName(), theParam);
}
/**
* If this definition refers to a class which extends another resource definition type, this
* method will return the definition of the topmost resource. For example, if this definition
* refers to MyPatient2, which extends MyPatient, which in turn extends Patient, this method
* will return the resource definition for Patient.
* <p>
* If the definition has no parent, returns <code>this</code>
* </p>
*/
public RuntimeResourceDefinition getBaseDefinition() {
validateSealed();
if (myBaseDefinition == null) {
myBaseDefinition = myContext.getResourceDefinition(myBaseType);
}
return myBaseDefinition;
}
@Override
public ca.uhn.fhir.context.BaseRuntimeElementDefinition.ChildTypeEnum getChildType() {
return ChildTypeEnum.RESOURCE;
}
public String getId() {
return myId;
}
/**
* Express {@link #getImplementingClass()} as theClass (to prevent casting warnings)
*/
@SuppressWarnings("unchecked")
public <T> Class<T> getImplementingClass(Class<T> theClass) {
if (!theClass.isAssignableFrom(getImplementingClass())) {
throw new ConfigurationException(
Msg.code(1732) + "Unable to convert " + getImplementingClass() + " to " + theClass);
}
return (Class<T>) getImplementingClass();
}
@Deprecated
public String getResourceProfile() {
return myResourceProfile;
}
public String getResourceProfile(String theServerBase) {
validateSealed();
String profile;
if (!myResourceProfile.isEmpty()) {
profile = myResourceProfile;
} else if (!myId.isEmpty()) {
profile = myId;
} else {
return "";
}
if (!UrlUtil.isValid(profile)) {
String resourceName = "/StructureDefinition/";
String profileWithUrl = theServerBase + resourceName + profile;
if (UrlUtil.isValid(profileWithUrl)) {
return profileWithUrl;
}
}
return profile;
}
public RuntimeSearchParam getSearchParam(String theName) {
validateSealed();
return myNameToSearchParam.get(theName);
}
public List<RuntimeSearchParam> getSearchParams() {
validateSealed();
return mySearchParams;
}
/**
* Will not return null
*/
public List<RuntimeSearchParam> getSearchParamsForCompartmentName(String theCompartmentName) {
validateSealed();
List<RuntimeSearchParam> retVal = myCompartmentNameToSearchParams.get(theCompartmentName);
if (retVal == null) {
return Collections.emptyList();
}
return retVal;
}
public FhirVersionEnum getStructureVersion() {
return myStructureVersion;
}
public boolean isBundle() {
return "Bundle".equals(getName());
}
@SuppressWarnings("unchecked")
@Override
public void sealAndInitialize(
FhirContext theContext,
Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
super.sealAndInitialize(theContext, theClassToElementDefinitions);
myNameToSearchParam = Collections.unmodifiableMap(myNameToSearchParam);
ArrayList<RuntimeSearchParam> searchParams = new ArrayList<RuntimeSearchParam>(myNameToSearchParam.values());
Collections.sort(searchParams, new Comparator<RuntimeSearchParam>() {
@Override
public int compare(RuntimeSearchParam theArg0, RuntimeSearchParam theArg1) {
return theArg0.getName().compareTo(theArg1.getName());
}
});
mySearchParams = Collections.unmodifiableList(searchParams);
Map<String, List<RuntimeSearchParam>> compartmentNameToSearchParams = new HashMap<>();
for (RuntimeSearchParam next : searchParams) {
if (next.getProvidesMembershipInCompartments() != null) {
for (String nextCompartment : next.getProvidesMembershipInCompartments()) {
if (nextCompartment.startsWith("Base FHIR compartment definition for ")) {
nextCompartment = nextCompartment.substring("Base FHIR compartment definition for ".length());
}
if (!compartmentNameToSearchParams.containsKey(nextCompartment)) {
compartmentNameToSearchParams.put(nextCompartment, new ArrayList<>());
}
List<RuntimeSearchParam> searchParamsForCompartment =
compartmentNameToSearchParams.get(nextCompartment);
searchParamsForCompartment.add(next);
/*
* If one search parameter marks an SP as making a resource
* a part of a compartment, let's also denote all other
* SPs with the same path the same way. This behaviour is
* used by AuthorizationInterceptor
*/
String nextPath = massagePathForCompartmentSimilarity(next.getPath());
for (RuntimeSearchParam nextAlternate : searchParams) {
String nextAlternatePath = massagePathForCompartmentSimilarity(nextAlternate.getPath());
if (nextAlternatePath.equals(nextPath)) {
if (!nextAlternate.getName().equals(next.getName())) {
searchParamsForCompartment.add(nextAlternate);
}
}
}
}
}
}
// Make the map of lists completely unmodifiable
for (String nextKey : new ArrayList<>(compartmentNameToSearchParams.keySet())) {
List<RuntimeSearchParam> nextList = compartmentNameToSearchParams.get(nextKey);
compartmentNameToSearchParams.put(nextKey, Collections.unmodifiableList(nextList));
}
myCompartmentNameToSearchParams = Collections.unmodifiableMap(compartmentNameToSearchParams);
Class<?> target = getImplementingClass();
myBaseType = (Class<? extends IBaseResource>) target;
do {
target = target.getSuperclass();
if (IBaseResource.class.isAssignableFrom(target) && target.getAnnotation(ResourceDef.class) != null) {
myBaseType = (Class<? extends IBaseResource>) target;
}
} while (target.equals(Object.class) == false);
/*
* See #504:
* Bundle types may not have extensions
*/
if (hasExtensions()) {
if (IAnyResource.class.isAssignableFrom(getImplementingClass())) {
if (!IDomainResource.class.isAssignableFrom(getImplementingClass())) {
throw new ConfigurationException(Msg.code(1733) + "Class \"" + getImplementingClass()
+ "\" is invalid. This resource type is not a DomainResource, it must not have extensions");
}
}
}
}
private String massagePathForCompartmentSimilarity(String thePath) {
String path = thePath;
if (path.matches(".*\\.where\\(resolve\\(\\) is [a-zA-Z]+\\)")) {
path = path.substring(0, path.indexOf(".where"));
}
return path;
}
@Deprecated
public synchronized IBaseResource toProfile() {
validateSealed();
if (myProfileDef != null) {
return myProfileDef;
}
IBaseResource retVal = myContext.getVersion().generateProfile(this, null);
myProfileDef = retVal;
return retVal;
}
public synchronized IBaseResource toProfile(String theServerBase) {
validateSealed();
if (myProfileDef != null) {
return myProfileDef;
}
IBaseResource retVal = myContext.getVersion().generateProfile(this, theServerBase);
myProfileDef = retVal;
return retVal;
}
}