diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/RuntimeResourceDefinition.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/RuntimeResourceDefinition.java index d7ddbaa6d70..96bebd52ac0 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/RuntimeResourceDefinition.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/RuntimeResourceDefinition.java @@ -37,18 +37,14 @@ import ca.uhn.fhir.util.UrlUtil; public class RuntimeResourceDefinition extends BaseRuntimeElementCompositeDefinition { private RuntimeResourceDefinition myBaseDefinition; + private FhirContext myContext; + private String myId; private Map myNameToSearchParam = new LinkedHashMap(); private IBaseResource myProfileDef; private String myResourceProfile; private List mySearchParams; - private FhirContext myContext; - private String myId; private final FhirVersionEnum myStructureVersion; - public FhirVersionEnum getStructureVersion() { - return myStructureVersion; - } - public RuntimeResourceDefinition(FhirContext theContext, String theResourceName, Class theClass, ResourceDef theResourceAnnotation, boolean theStandardType) { super(theResourceName, theClass, theStandardType); myContext= theContext; @@ -65,10 +61,6 @@ public class RuntimeResourceDefinition extends BaseRuntimeElementCompositeDefini } - public String getId() { - return myId; - } - public void addSearchParam(RuntimeSearchParam theParam) { myNameToSearchParam.put(theParam.getName(), theParam); } @@ -91,6 +83,21 @@ public class RuntimeResourceDefinition extends BaseRuntimeElementCompositeDefini return ChildTypeEnum.RESOURCE; } + public String getId() { + return myId; + } + + /** + * Express {@link #getImplementingClass()} as theClass (to prevent casting warnings) + */ + @SuppressWarnings("unchecked") + public Class getImplementingClass(Class theClass) { + if (!theClass.isAssignableFrom(getImplementingClass())) { + throw new ConfigurationException("Unable to convert " + getImplementingClass() + " to " + theClass); + } + return (Class) getImplementingClass(); + } + @Deprecated public String getResourceProfile() { return myResourceProfile; @@ -127,6 +134,14 @@ public class RuntimeResourceDefinition extends BaseRuntimeElementCompositeDefini return mySearchParams; } + public FhirVersionEnum getStructureVersion() { + return myStructureVersion; + } + + public boolean isBundle() { + return "Bundle".equals(getName()); + } + public boolean isStandardProfile() { return myResourceProfile.startsWith("http://hl7.org/fhir/profiles"); } @@ -178,8 +193,4 @@ public class RuntimeResourceDefinition extends BaseRuntimeElementCompositeDefini return retVal; } - - public boolean isBundle() { - return "Bundle".equals(getName()); - } } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java index 90719158109..04f7fcbd25d 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GenericClient.java @@ -1045,7 +1045,7 @@ public class GenericClient extends BaseClient implements IGenericClient { private Integer myCount; private IIdType myId; private Class myReturnType; - private InstantDt mySince; + private IPrimitiveType mySince; private Class myType; @SuppressWarnings("unchecked") @@ -1126,7 +1126,7 @@ public class GenericClient extends BaseClient implements IGenericClient { } @Override - public IHistoryTyped since(InstantDt theCutoff) { + public IHistoryTyped since(IPrimitiveType theCutoff) { mySince = theCutoff; return this; } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IHistoryTyped.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IHistoryTyped.java index accebcc256d..81bcf3f43ab 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IHistoryTyped.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/gclient/IHistoryTyped.java @@ -22,7 +22,7 @@ package ca.uhn.fhir.rest.gclient; import java.util.Date; -import ca.uhn.fhir.model.primitive.InstantDt; +import org.hl7.fhir.instance.model.api.IPrimitiveType; public interface IHistoryTyped extends IClientExecutable, T> { @@ -33,8 +33,12 @@ public interface IHistoryTyped extends IClientExecutable, T> /** * Request that the server return only resource versions that were created at or after the given time (inclusive) + *

+ * Parameter theCutoff can be any priitive type which accepts a date, such as + * a DateTimeDt, InstantType, etc. + *

*/ - IHistoryTyped since(InstantDt theCutoff); + IHistoryTyped since(IPrimitiveType theCutoff); /** * Request that the server return only up to theCount number of resources diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HistoryMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HistoryMethodBinding.java index 509bdcd91b2..d6a963cb9a3 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HistoryMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HistoryMethodBinding.java @@ -24,15 +24,16 @@ import static org.apache.commons.lang3.StringUtils.isNotBlank; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.Date; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.hl7.fhir.instance.model.api.IBaseResource; +import org.hl7.fhir.instance.model.api.IPrimitiveType; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; -import ca.uhn.fhir.model.primitive.BaseDateTimeDt; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.model.valueset.BundleTypeEnum; @@ -154,7 +155,7 @@ public class HistoryMethodBinding extends BaseResourceReturningMethodBinding { } @Override - public IBundleProvider invokeServer(IRestfulServer theServer, RequestDetails theRequest, Object[] theMethodParams) throws InvalidRequestException, InternalErrorException { + public IBundleProvider invokeServer(IRestfulServer theServer, RequestDetails theRequest, Object[] theMethodParams) throws InvalidRequestException, InternalErrorException { if (myIdParamIndex != null) { theMethodParams[myIdParamIndex] = theRequest.getId(); } @@ -205,7 +206,7 @@ public class HistoryMethodBinding extends BaseResourceReturningMethodBinding { }; } - public static HttpGetClientInvocation createHistoryInvocation(String theResourceName, String theId, BaseDateTimeDt theSince, Integer theLimit) { + public static HttpGetClientInvocation createHistoryInvocation(String theResourceName, String theId, IPrimitiveType theSince, Integer theLimit) { StringBuilder b = new StringBuilder(); if (theResourceName != null) { b.append(theResourceName); diff --git a/hapi-fhir-cobertura/pom.xml b/hapi-fhir-cobertura/pom.xml index ab6a18a03c9..11027559a57 100644 --- a/hapi-fhir-cobertura/pom.xml +++ b/hapi-fhir-cobertura/pom.xml @@ -31,6 +31,11 @@ hapi-fhir-structures-dstu2 1.4-SNAPSHOT + + ca.uhn.hapi.fhir + hapi-fhir-structures-dstu2.1 + 1.4-SNAPSHOT + ca.uhn.hapi.fhir hapi-fhir-structures-hl7org-dstu2 @@ -41,6 +46,11 @@ hapi-fhir-validation-resources-dstu2 1.4-SNAPSHOT + + ca.uhn.hapi.fhir + hapi-fhir-validation-resources-dstu2.1 + 1.4-SNAPSHOT + ca.uhn.hapi.fhir hapi-fhir-jpaserver-base @@ -227,7 +237,6 @@ org.eluder.coveralls coveralls-maven-plugin - 3.1.0 @@ -275,6 +284,7 @@ ../hapi-fhir-structures-dstu/src/test/java ../hapi-fhir-structures-dstu2/src/test/java ../hapi-fhir-structures-hl7org-dstu2/src/test/java + ../hapi-fhir-structures-dstu2.1/src/test/java ../hapi-fhir-jpaserver-base/src/test/java @@ -342,6 +352,9 @@ ../hapi-fhir-structures-hl7org-dstu2/src/test/resources + + ../hapi-fhir-structures-dstu2.1/src/test/resources + diff --git a/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/context/RuntimeResourceDefinitionTest.java b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/context/RuntimeResourceDefinitionTest.java new file mode 100644 index 00000000000..a72c78e47d5 --- /dev/null +++ b/hapi-fhir-structures-dstu2.1/src/test/java/ca/uhn/fhir/context/RuntimeResourceDefinitionTest.java @@ -0,0 +1,23 @@ +package ca.uhn.fhir.context; + +import static org.junit.Assert.*; + +import org.hl7.fhir.dstu21.model.Bundle; +import org.hl7.fhir.dstu21.model.Patient; +import org.junit.Test; + +public class RuntimeResourceDefinitionTest { + + private FhirContext ourCtx = FhirContext.forDstu2_1(); + + @Test + public void testAsClass() { + assertEquals(Bundle.class, ourCtx.getResourceDefinition("Bundle").getImplementingClass(Bundle.class)); + } + + @Test(expected=ConfigurationException.class) + public void testAsClassWrong() { + ourCtx.getResourceDefinition("Bundle").getImplementingClass(Patient.class); + } + +} diff --git a/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/Controller.java b/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/Controller.java index f14e5b0dea3..8cf4553db21 100644 --- a/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/Controller.java +++ b/hapi-fhir-testpage-overlay/src/main/java/ca/uhn/fhir/to/Controller.java @@ -22,11 +22,13 @@ import org.hl7.fhir.dstu21.model.Conformance.ConformanceRestComponent; import org.hl7.fhir.dstu21.model.Conformance.ConformanceRestResourceComponent; import org.hl7.fhir.dstu21.model.Conformance.ConformanceRestResourceSearchParamComponent; import org.hl7.fhir.dstu21.model.StringType; +import org.hl7.fhir.instance.model.api.IBaseBundle; import org.hl7.fhir.instance.model.api.IBaseResource; import org.springframework.ui.ModelMap; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RequestMapping; +import ca.uhn.fhir.context.FhirVersionEnum; import ca.uhn.fhir.context.RuntimeResourceDefinition; import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.ExtensionDt; @@ -45,6 +47,9 @@ import ca.uhn.fhir.model.primitive.StringDt; import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.rest.client.GenericClient; import ca.uhn.fhir.rest.gclient.ICreateTyped; +import ca.uhn.fhir.rest.gclient.IHistory; +import ca.uhn.fhir.rest.gclient.IHistoryTyped; +import ca.uhn.fhir.rest.gclient.IHistoryUntyped; import ca.uhn.fhir.rest.gclient.IQuery; import ca.uhn.fhir.rest.gclient.IUntypedQuery; import ca.uhn.fhir.rest.gclient.NumberClientParam.IMatches; @@ -454,6 +459,10 @@ public class Controller extends BaseController { } } + if (client.getFhirContext().getVersion().getVersion() != FhirVersionEnum.DSTU1) { + query.returnBundle(client.getFhirContext().getResourceDefinition("Bundle").getImplementingClass()); + } + long start = System.currentTimeMillis(); ResultType returnsResource; try { @@ -646,7 +655,32 @@ public class Controller extends BaseController { long start = System.currentTimeMillis(); try { ourLog.info(logPrefix(theModel) + "Retrieving history for type {} ID {} since {}", new Object[] { type, id, since }); - client.history(type, id, since, limit); + + IHistory hist0 = client.history(); + IHistoryUntyped hist1; + if (isNotBlank(id)) { + hist1 = hist0.onInstance(new IdDt(theRequest.getResource(), id)); + } else if (type != null) { + hist1 = hist0.onType(type); + } else { + hist1 = hist0.onServer(); + } + + IHistoryTyped hist2; + if (client.getFhirContext().getVersion().getVersion() == FhirVersionEnum.DSTU1) { + hist2 = hist1.andReturnDstu1Bundle(); + } else { + hist2 = hist1.andReturnBundle(client.getFhirContext().getResourceDefinition("Bundle").getImplementingClass(IBaseBundle.class)); + } + + if (since != null) { + hist2.since(since); + } + if (limit != null) { + hist2.count(limit); + } + + hist2.execute(); } catch (Exception e) { returnsResource = handleClientException(client, e, theModel); }