hapi-fhir/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/RuntimeResourceDefinition.java

278 lines
9.2 KiB
Java
Raw Normal View History

2014-02-17 17:58:32 -05:00
package ca.uhn.fhir.context;
/*
* #%L
* HAPI FHIR - Core Library
* %%
2021-01-01 15:50:24 -05:00
* Copyright (C) 2014 - 2021 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%
*/
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;
2014-05-13 18:59:18 -04:00
import java.util.ArrayList;
2014-03-10 12:43:49 -04:00
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
2014-03-10 12:43:49 -04:00
import java.util.List;
import java.util.Map;
2014-12-10 17:40:47 -05:00
public class RuntimeResourceDefinition extends BaseRuntimeElementCompositeDefinition<IBaseResource> {
2014-02-17 17:58:32 -05:00
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>();
2015-02-16 11:33:46 -05:00
private IBaseResource myProfileDef;
private String myResourceProfile;
2014-05-13 18:59:18 -04:00
private List<RuntimeSearchParam> mySearchParams;
2014-12-22 12:03:02 -05:00
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;
2014-03-10 12:43:49 -04:00
myResourceProfile = theResourceAnnotation.profile();
2014-05-28 16:07:53 -04:00
myId = theResourceAnnotation.id();
IBaseResource instance;
2014-12-22 12:03:02 -05:00
try {
2020-12-18 08:49:09 -05:00
instance = theClass.getConstructor().newInstance();
2014-12-22 12:03:02 -05:00
} catch (Exception e) {
throw new ConfigurationException(myContext.getLocalizer().getMessage(getClass(), "nonInstantiableType", theClass.getName(), e.toString()), e);
}
myStructureVersion = instance.getStructureFhirVersionEnum();
if (myStructureVersion != theContext.getVersion().getVersion()) {
throw new ConfigurationException(myContext.getLocalizer().getMessage(getClass(), "typeWrongVersion", theContext.getVersion().getVersion(), theClass.getName(), myStructureVersion));
}
2014-03-10 12:43:49 -04:00
}
public void addSearchParam(RuntimeSearchParam theParam) {
myNameToSearchParam.put(theParam.getName(), theParam);
2014-02-17 17:58:32 -05:00
}
2014-08-23 16:40:26 -04:00
/**
* 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);
}
2014-08-23 16:40:26 -04:00
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("Unable to convert " + getImplementingClass() + " to " + theClass);
}
return (Class<T>) getImplementingClass();
}
@Deprecated
2014-08-23 16:40:26 -04:00
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;
}
2014-08-23 16:40:26 -04:00
public RuntimeSearchParam getSearchParam(String theName) {
validateSealed();
2014-08-23 16:40:26 -04:00
return myNameToSearchParam.get(theName);
}
public List<RuntimeSearchParam> getSearchParams() {
validateSealed();
2014-08-23 16:40:26 -04:00
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")
2014-08-23 16:40:26 -04:00
@Override
public void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
super.sealAndInitialize(theContext, theClassToElementDefinitions);
2014-08-23 16:40:26 -04:00
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
*/
Squashed commit of the following: commit df641506885e09e63f80efc6c658d01a1a3a8142 Author: James Agnew <jamesagnew@gmail.com> Date: Sat Sep 29 13:47:22 2018 -0400 Shrink validation resources commit 4c1550eafa26f4235b4b8804741aecc03276b5c6 Author: James Agnew <jamesagnew@gmail.com> Date: Fri Sep 28 22:45:28 2018 -0400 Updates to get R4 working commit 8332f15291006691ed9b07ead3e3524b0fc85510 Author: jamesagnew <jamesagnew@gmail.com> Date: Fri Sep 28 09:02:13 2018 -0400 Ongoing work on gettign R4 working commit f7146cab7aed937bb625a8aec95744d76db00041 Author: James Agnew <jamesagnew@gmail.com> Date: Fri Sep 28 05:21:01 2018 -0400 More work on sync commit f48de4a10b1bab2584fee813017d9b27b237bfa9 Merge: 9e4f3cc722 aacb78b779 Author: James Agnew <jamesagnew@gmail.com> Date: Thu Sep 27 20:11:29 2018 -0400 Merge branch 'sync_r4' of github.com:jamesagnew/hapi-fhir into sync_r4 commit 9e4f3cc722668aae31a77b19bf9b032af3ae01b0 Author: James Agnew <jamesagnew@gmail.com> Date: Thu Sep 27 20:11:19 2018 -0400 Fix compile error commit 7ec29e0ceda8f9013cbabb95767cbe15b4b81303 Merge: 23f7517325 002c4b3ff7 Author: James Agnew <jamesagnew@gmail.com> Date: Thu Sep 27 20:04:23 2018 -0400 Merge branch 'master' into sync_r4 commit aacb78b7793ab2c71e0a2a774240ee2b5d3f9e7f Author: jamesagnew <jamesagnew@gmail.com> Date: Thu Sep 27 20:01:42 2018 -0400 Keep working on getting R4 building commit 4950de46d99897e74d41dfdb10d3d8a3435cb0c9 Author: jamesagnew <jamesagnew@gmail.com> Date: Tue Sep 25 13:11:27 2018 -0400 Ongoing work to get R4 working commit 819d69c20e112dd3c72569ec50114ddb8263c300 Author: jamesagnew <jamesagnew@gmail.com> Date: Thu Sep 20 08:15:36 2018 -0400 Work on getting build working commit 2c61b6cd1205ced5d7b822cf10942c69272ea078 Merge: 16b5bb06c8 dfb4de86f1 Author: jamesagnew <jamesagnew@gmail.com> Date: Thu Sep 20 05:16:53 2018 -0400 Merge branch 'master' into sync_r4 commit 16b5bb06c8cdaf8d67c3b80f5aa9be9fccd99aef Author: jamesagnew <jamesagnew@gmail.com> Date: Mon Sep 17 05:30:39 2018 -0400 Work on R4 sync commit 23f7517325a14dd2ca1eb5641296ead2776634ae Author: James Agnew <jamesagnew@gmail.com> Date: Sun Sep 16 10:19:00 2018 -0400 Work on sync commit 6cc413c1f1dea538295aa4c16c21a5677494ac4c Author: James Agnew <jamesagnew@gmail.com> Date: Fri Sep 14 17:28:28 2018 -0400 Work on R$ sync commit df6f6ad2ce783b07ccc383134705d874bc5d2cf1 Author: jamesagnew <jamesagnew@gmail.com> Date: Fri Sep 14 08:33:07 2018 -0400 Work on R4
2018-09-29 13:48:35 -04:00
String nextPath = massagePathForCompartmentSimilarity(next.getPath());
for (RuntimeSearchParam nextAlternate : searchParams) {
Squashed commit of the following: commit df641506885e09e63f80efc6c658d01a1a3a8142 Author: James Agnew <jamesagnew@gmail.com> Date: Sat Sep 29 13:47:22 2018 -0400 Shrink validation resources commit 4c1550eafa26f4235b4b8804741aecc03276b5c6 Author: James Agnew <jamesagnew@gmail.com> Date: Fri Sep 28 22:45:28 2018 -0400 Updates to get R4 working commit 8332f15291006691ed9b07ead3e3524b0fc85510 Author: jamesagnew <jamesagnew@gmail.com> Date: Fri Sep 28 09:02:13 2018 -0400 Ongoing work on gettign R4 working commit f7146cab7aed937bb625a8aec95744d76db00041 Author: James Agnew <jamesagnew@gmail.com> Date: Fri Sep 28 05:21:01 2018 -0400 More work on sync commit f48de4a10b1bab2584fee813017d9b27b237bfa9 Merge: 9e4f3cc722 aacb78b779 Author: James Agnew <jamesagnew@gmail.com> Date: Thu Sep 27 20:11:29 2018 -0400 Merge branch 'sync_r4' of github.com:jamesagnew/hapi-fhir into sync_r4 commit 9e4f3cc722668aae31a77b19bf9b032af3ae01b0 Author: James Agnew <jamesagnew@gmail.com> Date: Thu Sep 27 20:11:19 2018 -0400 Fix compile error commit 7ec29e0ceda8f9013cbabb95767cbe15b4b81303 Merge: 23f7517325 002c4b3ff7 Author: James Agnew <jamesagnew@gmail.com> Date: Thu Sep 27 20:04:23 2018 -0400 Merge branch 'master' into sync_r4 commit aacb78b7793ab2c71e0a2a774240ee2b5d3f9e7f Author: jamesagnew <jamesagnew@gmail.com> Date: Thu Sep 27 20:01:42 2018 -0400 Keep working on getting R4 building commit 4950de46d99897e74d41dfdb10d3d8a3435cb0c9 Author: jamesagnew <jamesagnew@gmail.com> Date: Tue Sep 25 13:11:27 2018 -0400 Ongoing work to get R4 working commit 819d69c20e112dd3c72569ec50114ddb8263c300 Author: jamesagnew <jamesagnew@gmail.com> Date: Thu Sep 20 08:15:36 2018 -0400 Work on getting build working commit 2c61b6cd1205ced5d7b822cf10942c69272ea078 Merge: 16b5bb06c8 dfb4de86f1 Author: jamesagnew <jamesagnew@gmail.com> Date: Thu Sep 20 05:16:53 2018 -0400 Merge branch 'master' into sync_r4 commit 16b5bb06c8cdaf8d67c3b80f5aa9be9fccd99aef Author: jamesagnew <jamesagnew@gmail.com> Date: Mon Sep 17 05:30:39 2018 -0400 Work on R4 sync commit 23f7517325a14dd2ca1eb5641296ead2776634ae Author: James Agnew <jamesagnew@gmail.com> Date: Sun Sep 16 10:19:00 2018 -0400 Work on sync commit 6cc413c1f1dea538295aa4c16c21a5677494ac4c Author: James Agnew <jamesagnew@gmail.com> Date: Fri Sep 14 17:28:28 2018 -0400 Work on R$ sync commit df6f6ad2ce783b07ccc383134705d874bc5d2cf1 Author: jamesagnew <jamesagnew@gmail.com> Date: Fri Sep 14 08:33:07 2018 -0400 Work on R4
2018-09-29 13:48:35 -04:00
String nextAlternatePath = massagePathForCompartmentSimilarity(nextAlternate.getPath());
if (nextAlternatePath.equals(nextPath)) {
if (!nextAlternate.getName().equals(next.getName())) {
searchParamsForCompartment.add(nextAlternate);
}
}
}
}
}
}
myCompartmentNameToSearchParams = Collections.unmodifiableMap(compartmentNameToSearchParams);
2014-08-23 16:40:26 -04:00
Class<?> target = getImplementingClass();
myBaseType = (Class<? extends IBaseResource>) target;
2014-08-23 16:40:26 -04:00
do {
target = target.getSuperclass();
2016-03-28 20:51:27 -04:00
if (IBaseResource.class.isAssignableFrom(target) && target.getAnnotation(ResourceDef.class) != null) {
myBaseType = (Class<? extends IBaseResource>) target;
2014-08-23 16:40:26 -04:00
}
} while (target.equals(Object.class) == false);
2017-01-14 07:55:42 -06:00
/*
* See #504:
* Bundle types may not have extensions
*/
if (hasExtensions()) {
if (IAnyResource.class.isAssignableFrom(getImplementingClass())) {
if (!IDomainResource.class.isAssignableFrom(getImplementingClass())) {
throw new ConfigurationException("Class \"" + getImplementingClass() + "\" is invalid. This resource type is not a DomainResource, it must not have extensions");
}
}
}
2014-08-23 16:40:26 -04:00
}
Squashed commit of the following: commit df641506885e09e63f80efc6c658d01a1a3a8142 Author: James Agnew <jamesagnew@gmail.com> Date: Sat Sep 29 13:47:22 2018 -0400 Shrink validation resources commit 4c1550eafa26f4235b4b8804741aecc03276b5c6 Author: James Agnew <jamesagnew@gmail.com> Date: Fri Sep 28 22:45:28 2018 -0400 Updates to get R4 working commit 8332f15291006691ed9b07ead3e3524b0fc85510 Author: jamesagnew <jamesagnew@gmail.com> Date: Fri Sep 28 09:02:13 2018 -0400 Ongoing work on gettign R4 working commit f7146cab7aed937bb625a8aec95744d76db00041 Author: James Agnew <jamesagnew@gmail.com> Date: Fri Sep 28 05:21:01 2018 -0400 More work on sync commit f48de4a10b1bab2584fee813017d9b27b237bfa9 Merge: 9e4f3cc722 aacb78b779 Author: James Agnew <jamesagnew@gmail.com> Date: Thu Sep 27 20:11:29 2018 -0400 Merge branch 'sync_r4' of github.com:jamesagnew/hapi-fhir into sync_r4 commit 9e4f3cc722668aae31a77b19bf9b032af3ae01b0 Author: James Agnew <jamesagnew@gmail.com> Date: Thu Sep 27 20:11:19 2018 -0400 Fix compile error commit 7ec29e0ceda8f9013cbabb95767cbe15b4b81303 Merge: 23f7517325 002c4b3ff7 Author: James Agnew <jamesagnew@gmail.com> Date: Thu Sep 27 20:04:23 2018 -0400 Merge branch 'master' into sync_r4 commit aacb78b7793ab2c71e0a2a774240ee2b5d3f9e7f Author: jamesagnew <jamesagnew@gmail.com> Date: Thu Sep 27 20:01:42 2018 -0400 Keep working on getting R4 building commit 4950de46d99897e74d41dfdb10d3d8a3435cb0c9 Author: jamesagnew <jamesagnew@gmail.com> Date: Tue Sep 25 13:11:27 2018 -0400 Ongoing work to get R4 working commit 819d69c20e112dd3c72569ec50114ddb8263c300 Author: jamesagnew <jamesagnew@gmail.com> Date: Thu Sep 20 08:15:36 2018 -0400 Work on getting build working commit 2c61b6cd1205ced5d7b822cf10942c69272ea078 Merge: 16b5bb06c8 dfb4de86f1 Author: jamesagnew <jamesagnew@gmail.com> Date: Thu Sep 20 05:16:53 2018 -0400 Merge branch 'master' into sync_r4 commit 16b5bb06c8cdaf8d67c3b80f5aa9be9fccd99aef Author: jamesagnew <jamesagnew@gmail.com> Date: Mon Sep 17 05:30:39 2018 -0400 Work on R4 sync commit 23f7517325a14dd2ca1eb5641296ead2776634ae Author: James Agnew <jamesagnew@gmail.com> Date: Sun Sep 16 10:19:00 2018 -0400 Work on sync commit 6cc413c1f1dea538295aa4c16c21a5677494ac4c Author: James Agnew <jamesagnew@gmail.com> Date: Fri Sep 14 17:28:28 2018 -0400 Work on R$ sync commit df6f6ad2ce783b07ccc383134705d874bc5d2cf1 Author: jamesagnew <jamesagnew@gmail.com> Date: Fri Sep 14 08:33:07 2018 -0400 Work on R4
2018-09-29 13:48:35 -04:00
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
2015-02-16 11:33:46 -05:00
public synchronized IBaseResource toProfile() {
validateSealed();
2014-08-23 16:40:26 -04:00
if (myProfileDef != null) {
return myProfileDef;
}
2015-02-16 11:33:46 -05:00
IBaseResource retVal = myContext.getVersion().generateProfile(this, null);
2014-08-23 16:40:26 -04:00
myProfileDef = retVal;
return retVal;
}
2015-02-16 11:33:46 -05:00
public synchronized IBaseResource toProfile(String theServerBase) {
validateSealed();
if (myProfileDef != null) {
return myProfileDef;
}
2014-03-10 12:43:49 -04:00
2015-02-16 11:33:46 -05:00
IBaseResource retVal = myContext.getVersion().generateProfile(this, theServerBase);
myProfileDef = retVal;
2014-03-10 12:43:49 -04:00
return retVal;
}
2014-02-17 17:58:32 -05:00
}