From de3fbc9023869c782b57816c7c1a8c487dc647e8 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Fri, 23 May 2014 18:29:32 -0400 Subject: [PATCH] Get transaction working in server --- .../ca/uhn/fhir/model/primitive/IdDt.java | 17 +++ .../ca/uhn/fhir/rest/client/BaseClient.java | 4 +- ...ion.java => BaseHttpClientInvocation.java} | 2 +- .../rest/client/ClientInvocationHandler.java | 2 +- .../uhn/fhir/rest/client/GenericClient.java | 22 +-- .../BaseAddOrDeleteTagsMethodBinding.java | 15 +- ...BaseHttpClientInvocationWithContents.java} | 25 ++- .../fhir/rest/method/BaseMethodBinding.java | 4 +- .../BaseOutcomeReturningMethodBinding.java | 4 +- ...turningMethodBindingWithResourceParam.java | 6 +- .../BaseResourceReturningMethodBinding.java | 75 +++++---- .../rest/method/ConformanceMethodBinding.java | 9 +- .../fhir/rest/method/CreateMethodBinding.java | 11 +- .../fhir/rest/method/DeleteMethodBinding.java | 16 +- .../rest/method/GetTagsMethodBinding.java | 15 +- .../rest/method/HistoryMethodBinding.java | 11 +- .../HttpDeleteClientInvocation.java} | 7 +- .../HttpGetClientInvocation.java} | 11 +- .../HttpPostClientInvocation.java} | 15 +- .../HttpPutClientInvocation.java} | 6 +- .../fhir/rest/method/ReadMethodBinding.java | 13 +- .../fhir/rest/method/SearchMethodBinding.java | 9 +- .../rest/method/TransactionMethodBinding.java | 11 +- .../fhir/rest/method/UpdateMethodBinding.java | 11 +- .../rest/method/ValidateMethodBinding.java | 11 +- .../fhir/rest/param/BaseQueryParameter.java | 4 +- .../uhn/fhir/rest/param/CountParameter.java | 4 +- .../ca/uhn/fhir/rest/param/IParameter.java | 4 +- .../ca/uhn/fhir/rest/param/NullParameter.java | 4 +- .../fhir/rest/param/ResourceParameter.java | 4 +- .../fhir/rest/param/ServerBaseParameter.java | 4 +- .../rest/param/ServletRequestParameter.java | 4 +- .../rest/param/ServletResponseParameter.java | 4 +- .../uhn/fhir/rest/param/SinceParameter.java | 4 +- .../ca/uhn/fhir/rest/param/SortParameter.java | 4 +- .../fhir/rest/param/TransactionParameter.java | 4 +- .../ca/uhn/fhir/parser/XmlParserTest.java | 36 +++++ .../rest/client/TransactionClientTest.java | 108 +++++++++++++ hapi-fhir-jpaserver-base/.classpath | 4 +- .../java/ca/uhn/fhir/jpa/dao/BaseFhirDao.java | 94 +++++++++++- .../ca/uhn/fhir/jpa/dao/FhirResourceDao.java | 142 +++++------------- .../ca/uhn/fhir/jpa/dao/FhirSystemDao.java | 35 ++++- .../uhn/fhir/jpa/entity/BaseHasResource.java | 6 +- .../fhir/jpa/entity/ResourceHistoryTable.java | 7 +- .../jpa/entity/ResourceHistoryTablePk.java | 2 +- .../ca/uhn/fhir/jpa/entity/ResourceTable.java | 13 +- .../uhn/fhir/jpa/dao/FhirResourceDaoTest.java | 9 +- .../uhn/fhir/jpa/dao/FhirSystemDaoTest.java | 82 ++++++++++ .../fhir-jpabase-spring-test-config.xml | 3 + hapi-fhir-jpaserver-test/pom.xml | 2 +- 50 files changed, 615 insertions(+), 304 deletions(-) rename hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/{BaseClientInvocation.java => BaseHttpClientInvocation.java} (98%) rename hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/{client/BaseClientInvocationWithContents.java => method/BaseHttpClientInvocationWithContents.java} (72%) rename hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/{client/DeleteClientInvocation.java => method/HttpDeleteClientInvocation.java} (86%) rename hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/{client/GetClientInvocation.java => method/HttpGetClientInvocation.java} (88%) rename hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/{client/PostClientInvocation.java => method/HttpPostClientInvocation.java} (70%) rename hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/{client/PutClientInvocation.java => method/HttpPutClientInvocation.java} (83%) create mode 100644 hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/client/TransactionClientTest.java create mode 100644 hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirSystemDaoTest.java diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java index 010cb52a46b..126cab4932e 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/model/primitive/IdDt.java @@ -236,4 +236,21 @@ public class IdDt extends BasePrimitive { return myValue; } + /** + * Returns true if the unqualified ID is a valid {@link Long} value (in other + * words, it consists only of digits) + */ + public boolean isValidLong() { + String id = getUnqualifiedId(); + if (StringUtils.isBlank(id)) { + return false; + } + for (int i = 0; i < id.length(); i++) { + if (Character.isDigit(id.charAt(i)) == false) { + return false; + } + } + return true; + } + } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseClient.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseClient.java index 6d3ebb17ff7..5b47c6ff9b5 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseClient.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseClient.java @@ -100,11 +100,11 @@ public abstract class BaseClient { return myUrlBase; } - T invokeClient(IClientResponseHandler binding, BaseClientInvocation clientInvocation) { + T invokeClient(IClientResponseHandler binding, BaseHttpClientInvocation clientInvocation) { return invokeClient(binding, clientInvocation, false); } - T invokeClient(IClientResponseHandler binding, BaseClientInvocation clientInvocation, boolean theLogRequestAndResponse) { + T invokeClient(IClientResponseHandler binding, BaseHttpClientInvocation clientInvocation, boolean theLogRequestAndResponse) { // TODO: handle non 2xx status codes by throwing the correct exception, // and ensure it's passed upwards HttpRequestBase httpRequest; diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseClientInvocation.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseHttpClientInvocation.java similarity index 98% rename from hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseClientInvocation.java rename to hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseHttpClientInvocation.java index 02567d3d5a1..0d31045e091 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseClientInvocation.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseHttpClientInvocation.java @@ -33,7 +33,7 @@ import org.apache.http.message.BasicHeader; import ca.uhn.fhir.rest.server.EncodingEnum; -public abstract class BaseClientInvocation { +public abstract class BaseHttpClientInvocation { private List
myHeaders; diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/ClientInvocationHandler.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/ClientInvocationHandler.java index 6e6f60de042..9c0c9e82832 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/ClientInvocationHandler.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/ClientInvocationHandler.java @@ -72,7 +72,7 @@ public class ClientInvocationHandler extends BaseClient implements InvocationHan BaseMethodBinding binding = myBindings.get(theMethod); if (binding != null) { - BaseClientInvocation clientInvocation = binding.invokeClient(theArgs); + BaseHttpClientInvocation clientInvocation = binding.invokeClient(theArgs); return invokeClient(binding, clientInvocation); } 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 572f40672ed..7e30fa0478d 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 @@ -54,6 +54,8 @@ import ca.uhn.fhir.rest.method.ConformanceMethodBinding; import ca.uhn.fhir.rest.method.CreateMethodBinding; import ca.uhn.fhir.rest.method.DeleteMethodBinding; import ca.uhn.fhir.rest.method.HistoryMethodBinding; +import ca.uhn.fhir.rest.method.HttpDeleteClientInvocation; +import ca.uhn.fhir.rest.method.HttpGetClientInvocation; import ca.uhn.fhir.rest.method.IClientResponseHandler; import ca.uhn.fhir.rest.method.ReadMethodBinding; import ca.uhn.fhir.rest.method.SearchMethodBinding; @@ -79,7 +81,7 @@ public class GenericClient extends BaseClient implements IGenericClient { @Override public Conformance conformance() { - GetClientInvocation invocation = ConformanceMethodBinding.createConformanceInvocation(); + HttpGetClientInvocation invocation = ConformanceMethodBinding.createConformanceInvocation(); if (isKeepResponses()) { myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding()); } @@ -91,7 +93,7 @@ public class GenericClient extends BaseClient implements IGenericClient { @Override public MethodOutcome create(IResource theResource) { - BaseClientInvocation invocation = CreateMethodBinding.createCreateInvocation(theResource, myContext); + BaseHttpClientInvocation invocation = CreateMethodBinding.createCreateInvocation(theResource, myContext); if (isKeepResponses()) { myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding()); } @@ -108,7 +110,7 @@ public class GenericClient extends BaseClient implements IGenericClient { @Override public MethodOutcome delete(final Class theType, IdDt theId) { - DeleteClientInvocation invocation = DeleteMethodBinding.createDeleteInvocation(toResourceName(theType), theId); + HttpDeleteClientInvocation invocation = DeleteMethodBinding.createDeleteInvocation(toResourceName(theType), theId); if (isKeepResponses()) { myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding()); } @@ -130,7 +132,7 @@ public class GenericClient extends BaseClient implements IGenericClient { @Override public Bundle history(final Class theType, IdDt theIdDt) { - GetClientInvocation invocation = HistoryMethodBinding.createHistoryInvocation(toResourceName(theType), theIdDt); + HttpGetClientInvocation invocation = HistoryMethodBinding.createHistoryInvocation(toResourceName(theType), theIdDt); if (isKeepResponses()) { myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding()); } @@ -148,7 +150,7 @@ public class GenericClient extends BaseClient implements IGenericClient { @Override public T read(final Class theType, IdDt theId) { - GetClientInvocation invocation = ReadMethodBinding.createReadInvocation(theId, toResourceName(theType)); + HttpGetClientInvocation invocation = ReadMethodBinding.createReadInvocation(theId, toResourceName(theType)); if (isKeepResponses()) { myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding()); } @@ -182,7 +184,7 @@ public class GenericClient extends BaseClient implements IGenericClient { params.put(nextEntry.getKey()+qualifier, valueList); } - GetClientInvocation invocation = SearchMethodBinding.createSearchInvocation(toResourceName(theType), params); + HttpGetClientInvocation invocation = SearchMethodBinding.createSearchInvocation(toResourceName(theType), params); if (isKeepResponses()) { myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding()); } @@ -201,7 +203,7 @@ public class GenericClient extends BaseClient implements IGenericClient { @Override public MethodOutcome update(IdDt theIdDt, IResource theResource) { - BaseClientInvocation invocation = UpdateMethodBinding.createUpdateInvocation(theResource, theIdDt, null, myContext); + BaseHttpClientInvocation invocation = UpdateMethodBinding.createUpdateInvocation(theResource, theIdDt, null, myContext); if (isKeepResponses()) { myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding()); } @@ -221,7 +223,7 @@ public class GenericClient extends BaseClient implements IGenericClient { @Override public MethodOutcome validate(IResource theResource) { - BaseClientInvocation invocation = ValidateMethodBinding.createValidateInvocation(theResource, null, myContext); + BaseHttpClientInvocation invocation = ValidateMethodBinding.createValidateInvocation(theResource, null, myContext); if (isKeepResponses()) { myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding()); } @@ -236,7 +238,7 @@ public class GenericClient extends BaseClient implements IGenericClient { @Override public T vread(final Class theType, IdDt theId, IdDt theVersionId) { - GetClientInvocation invocation = ReadMethodBinding.createVReadInvocation(theId, theVersionId, toResourceName(theType)); + HttpGetClientInvocation invocation = ReadMethodBinding.createVReadInvocation(theId, theVersionId, toResourceName(theType)); if (isKeepResponses()) { myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding()); } @@ -383,7 +385,7 @@ public class GenericClient extends BaseClient implements IGenericClient { addParam(params, Constants.PARAM_COUNT, Integer.toString(myParamLimit)); } - GetClientInvocation invocation = new GetClientInvocation(params, myResourceName); + HttpGetClientInvocation invocation = new HttpGetClientInvocation(params, myResourceName); if (isKeepResponses()) { myLastRequest = invocation.asHttpRequest(getServerBase(), createExtraParams(), getEncoding()); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseAddOrDeleteTagsMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseAddOrDeleteTagsMethodBinding.java index ad24517cd12..4a1482c4996 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseAddOrDeleteTagsMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseAddOrDeleteTagsMethodBinding.java @@ -39,8 +39,7 @@ import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.rest.annotation.IdParam; -import ca.uhn.fhir.rest.client.BaseClientInvocation; -import ca.uhn.fhir.rest.client.PostClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType; import ca.uhn.fhir.rest.param.IParameter; import ca.uhn.fhir.rest.param.ParameterUtil; @@ -119,8 +118,8 @@ public abstract class BaseAddOrDeleteTagsMethodBinding extends BaseMethodBinding protected abstract boolean isDelete(); @Override - public BaseClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { - PostClientInvocation retVal; + public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { + HttpPostClientInvocation retVal; IdDt id = (IdDt) theArgs[myIdParamIndex]; if (id == null || id.isEmpty()) { @@ -139,15 +138,15 @@ public abstract class BaseAddOrDeleteTagsMethodBinding extends BaseMethodBinding if (isDelete()) { if (versionId != null) { - retVal = new PostClientInvocation(getContext(), tagList, getResourceName(), id.getValue(), Constants.PARAM_HISTORY, versionId.getValue(), Constants.PARAM_TAGS, Constants.PARAM_DELETE); + retVal = new HttpPostClientInvocation(getContext(), tagList, getResourceName(), id.getValue(), Constants.PARAM_HISTORY, versionId.getValue(), Constants.PARAM_TAGS, Constants.PARAM_DELETE); } else { - retVal = new PostClientInvocation(getContext(), tagList, getResourceName(), id.getValue(), Constants.PARAM_TAGS, Constants.PARAM_DELETE); + retVal = new HttpPostClientInvocation(getContext(), tagList, getResourceName(), id.getValue(), Constants.PARAM_TAGS, Constants.PARAM_DELETE); } } else { if (versionId != null) { - retVal = new PostClientInvocation(getContext(), tagList, getResourceName(), id.getValue(), Constants.PARAM_HISTORY, versionId.getValue(), Constants.PARAM_TAGS); + retVal = new HttpPostClientInvocation(getContext(), tagList, getResourceName(), id.getValue(), Constants.PARAM_HISTORY, versionId.getValue(), Constants.PARAM_TAGS); } else { - retVal = new PostClientInvocation(getContext(), tagList, getResourceName(), id.getValue(), Constants.PARAM_TAGS); + retVal = new HttpPostClientInvocation(getContext(), tagList, getResourceName(), id.getValue(), Constants.PARAM_TAGS); } } for (int idx = 0; idx < theArgs.length; idx++) { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseClientInvocationWithContents.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseHttpClientInvocationWithContents.java similarity index 72% rename from hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseClientInvocationWithContents.java rename to hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseHttpClientInvocationWithContents.java index 69c2879d92c..db1b110b0e3 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/BaseClientInvocationWithContents.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseHttpClientInvocationWithContents.java @@ -1,4 +1,4 @@ -package ca.uhn.fhir.rest.client; +package ca.uhn.fhir.rest.method; /* * #%L @@ -29,29 +29,34 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.TagList; import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.parser.IParser; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.EncodingEnum; +import ca.uhn.fhir.rest.server.RestfulServer.NarrativeModeEnum; -public abstract class BaseClientInvocationWithContents extends BaseClientInvocation { +public abstract class BaseHttpClientInvocationWithContents extends BaseHttpClientInvocation { private final FhirContext myContext; private final IResource myResource; private final String myUrlExtension; private final TagList myTagList; + private final List myResources; - public BaseClientInvocationWithContents(FhirContext theContext, IResource theResource, String theUrlExtension) { + public BaseHttpClientInvocationWithContents(FhirContext theContext, IResource theResource, String theUrlExtension) { super(); myContext = theContext; myResource = theResource; myUrlExtension = theUrlExtension; myTagList = null; + myResources=null; } - public BaseClientInvocationWithContents(FhirContext theContext, TagList theTagList, String... theUrlExtension) { + public BaseHttpClientInvocationWithContents(FhirContext theContext, TagList theTagList, String... theUrlExtension) { super(); if (theTagList == null) { throw new NullPointerException("Tag list must not be null"); @@ -60,10 +65,19 @@ public abstract class BaseClientInvocationWithContents extends BaseClientInvocat myResource = null; myContext = theContext; myTagList = theTagList; + myResources=null; myUrlExtension = StringUtils.join(theUrlExtension, '/'); } + public BaseHttpClientInvocationWithContents(FhirContext theContext, List theResources) { + myContext=theContext; + myResource=null; + myTagList=null; + myUrlExtension=null; + myResources = theResources; + } + @Override public HttpRequestBase asHttpRequest(String theUrlBase, Map> theExtraParams, EncodingEnum theEncoding) throws DataFormatException { StringBuilder b = new StringBuilder(); @@ -88,6 +102,9 @@ public abstract class BaseClientInvocationWithContents extends BaseClientInvocat String contents; if (myTagList != null) { contents = parser.encodeTagListToString(myTagList); + } else if (myResources != null) { + Bundle bundle = BaseResourceReturningMethodBinding.createBundleFromResourceList(myContext, "", myResources, theEncoding, theUrlBase, "", false, NarrativeModeEnum.NORMAL); + contents = parser.encodeBundleToString(bundle); } else { contents = parser.encodeResourceToString(myResource); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseMethodBinding.java index bdb66a9f3b4..83ae6eb0482 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseMethodBinding.java @@ -60,7 +60,7 @@ import ca.uhn.fhir.rest.annotation.Transaction; import ca.uhn.fhir.rest.annotation.Update; import ca.uhn.fhir.rest.annotation.Validate; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.client.exceptions.NonFhirResponseException; import ca.uhn.fhir.rest.param.IParameter; import ca.uhn.fhir.rest.param.ParameterUtil; @@ -118,7 +118,7 @@ public abstract class BaseMethodBinding implements IClientResponseHandler public abstract RestfulOperationSystemEnum getSystemOperationType(); - public abstract BaseClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException; + public abstract BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException; public abstract void invokeServer(RestfulServer theServer, Request theRequest, HttpServletResponse theResponse) throws BaseServerResponseException, IOException; diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBinding.java index 8d09b78ed50..cebfc32a719 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBinding.java @@ -47,7 +47,7 @@ import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType; import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.EncodingEnum; @@ -334,7 +334,7 @@ public abstract class BaseOutcomeReturningMethodBinding extends BaseMethodBindin return false; } - protected abstract BaseClientInvocation createClientInvocation(Object[] theArgs, IResource resource); + protected abstract BaseHttpClientInvocation createClientInvocation(Object[] theArgs, IResource resource); /** * For servers, this method will match only incoming requests that match the diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBindingWithResourceParam.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBindingWithResourceParam.java index b4688fb24e2..266b3c8e975 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBindingWithResourceParam.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseOutcomeReturningMethodBindingWithResourceParam.java @@ -31,7 +31,7 @@ import ca.uhn.fhir.model.api.ResourceMetadataKeyEnum; import ca.uhn.fhir.model.api.Tag; import ca.uhn.fhir.model.api.TagList; import ca.uhn.fhir.rest.annotation.ResourceParam; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.param.IParameter; import ca.uhn.fhir.rest.param.ResourceParameter; import ca.uhn.fhir.rest.server.Constants; @@ -88,13 +88,13 @@ abstract class BaseOutcomeReturningMethodBindingWithResourceParam extends BaseOu } @Override - public BaseClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { + public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { IResource resource = (IResource) theArgs[myResourceParameterIndex]; if (resource == null) { throw new NullPointerException("Resource can not be null"); } - BaseClientInvocation retVal = createClientInvocation(theArgs, resource); + BaseHttpClientInvocation retVal = createClientInvocation(theArgs, resource); TagList list = (TagList) resource.getResourceMetadata().get(ResourceMetadataKeyEnum.TAG_LIST); if (list != null) { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseResourceReturningMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseResourceReturningMethodBinding.java index f6548337c08..d863ca4bd8c 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseResourceReturningMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/BaseResourceReturningMethodBinding.java @@ -209,7 +209,7 @@ abstract class BaseResourceReturningMethodBinding extends BaseMethodBinding theResourceMetadata, ResourceMetadataKeyEnum theKey) { + private static InstantDt getInstantFromMetadataOrNullIfNone(Map theResourceMetadata, ResourceMetadataKeyEnum theKey) { Object retValObj = theResourceMetadata.get(theKey); if (retValObj == null) { return null; @@ -280,7 +280,7 @@ abstract class BaseResourceReturningMethodBinding extends BaseMethodBinding theResourceMetadata, ResourceMetadataKeyEnum theKey) { + private static TagList getTagListFromMetadataOrNullIfNone(Map theResourceMetadata, ResourceMetadataKeyEnum theKey) { Object retValObj = theResourceMetadata.get(theKey); if (retValObj == null) { return null; @@ -326,8 +326,26 @@ abstract class BaseResourceReturningMethodBinding extends BaseMethodBinding"); + } + } else { + getNewParser(theResponseEncoding, thePrettyPrint, theNarrativeMode).encodeBundleToWriter(bundle, writer); + } + } finally { + writer.close(); + } + } + + public static Bundle createBundleFromResourceList(FhirContext theContext, String theAuthor, List theResult, EncodingEnum theResponseEncoding, String theServerBase, String theCompleteUrl, boolean thePrettyPrint, NarrativeModeEnum theNarrativeMode) { Bundle bundle = new Bundle(); - bundle.getAuthorName().setValue(getClass().getCanonicalName()); + bundle.getAuthorName().setValue(theAuthor); bundle.getBundleId().setValue(UUID.randomUUID().toString()); bundle.getPublished().setToCurrentTimeInLocalTimeZone(); bundle.getLinkBase().setValue(theServerBase); @@ -347,7 +365,7 @@ abstract class BaseResourceReturningMethodBinding extends BaseMethodBinding"); - } - } else { - getNewParser(theResponseEncoding, thePrettyPrint, theNarrativeMode).encodeBundleToWriter(bundle, writer); - } - } finally { - writer.close(); - } + return bundle; } private void streamResponseAsResource(RestfulServer theServer, HttpServletResponse theHttpResponse, IResource theResource, EncodingEnum theResponseEncoding, boolean thePrettyPrint, boolean theRequestIsBrowser, NarrativeModeEnum theNarrativeMode) throws IOException { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ConformanceMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ConformanceMethodBinding.java index f2e65c11a55..1710e85f143 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ConformanceMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ConformanceMethodBinding.java @@ -30,7 +30,6 @@ import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu.resource.Conformance; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationSystemEnum; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; -import ca.uhn.fhir.rest.client.GetClientInvocation; import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType; import ca.uhn.fhir.rest.param.IParameter; import ca.uhn.fhir.rest.server.exceptions.BaseServerResponseException; @@ -53,8 +52,8 @@ public class ConformanceMethodBinding extends BaseResourceReturningMethodBinding } @Override - public GetClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { - GetClientInvocation retVal = createConformanceInvocation(); + public HttpGetClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { + HttpGetClientInvocation retVal = createConformanceInvocation(); if (theArgs != null) { for (int idx = 0; idx < theArgs.length; idx++) { @@ -66,8 +65,8 @@ public class ConformanceMethodBinding extends BaseResourceReturningMethodBinding return retVal; } - public static GetClientInvocation createConformanceInvocation() { - return new GetClientInvocation("metadata"); + public static HttpGetClientInvocation createConformanceInvocation() { + return new HttpGetClientInvocation("metadata"); } @Override diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/CreateMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/CreateMethodBinding.java index 528c30272e1..3190d42d4a5 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/CreateMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/CreateMethodBinding.java @@ -30,8 +30,7 @@ import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationSystemEnum; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; import ca.uhn.fhir.rest.annotation.Create; -import ca.uhn.fhir.rest.client.BaseClientInvocation; -import ca.uhn.fhir.rest.client.PostClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType; import ca.uhn.fhir.rest.param.IParameter; @@ -57,10 +56,10 @@ public class CreateMethodBinding extends BaseOutcomeReturningMethodBindingWithRe } @Override - protected BaseClientInvocation createClientInvocation(Object[] theArgs, IResource resource) { + protected BaseHttpClientInvocation createClientInvocation(Object[] theArgs, IResource resource) { FhirContext context = getContext(); - BaseClientInvocation retVal = createCreateInvocation(resource, context); + BaseHttpClientInvocation retVal = createCreateInvocation(resource, context); if (theArgs != null) { for (int idx = 0; idx < theArgs.length; idx++) { @@ -72,14 +71,14 @@ public class CreateMethodBinding extends BaseOutcomeReturningMethodBindingWithRe return retVal; } - public static PostClientInvocation createCreateInvocation(IResource resource, FhirContext context) { + public static HttpPostClientInvocation createCreateInvocation(IResource resource, FhirContext context) { RuntimeResourceDefinition def = context.getResourceDefinition(resource); String resourceName = def.getName(); StringBuilder urlExtension = new StringBuilder(); urlExtension.append(resourceName); - return new PostClientInvocation(context, resource, urlExtension.toString()); + return new HttpPostClientInvocation(context, resource, urlExtension.toString()); } @Override diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/DeleteMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/DeleteMethodBinding.java index 17c9c9ce599..0c3c710faa7 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/DeleteMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/DeleteMethodBinding.java @@ -34,9 +34,7 @@ import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.annotation.Delete; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.VersionIdParam; -import ca.uhn.fhir.rest.client.BaseClientInvocation; -import ca.uhn.fhir.rest.client.DeleteClientInvocation; -import ca.uhn.fhir.rest.client.PostClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType; import ca.uhn.fhir.rest.param.IParameter; import ca.uhn.fhir.rest.param.ParameterUtil; @@ -106,11 +104,11 @@ public class DeleteMethodBinding extends BaseOutcomeReturningMethodBinding { } @Override - protected BaseClientInvocation createClientInvocation(Object[] theArgs, IResource theResource) { + protected BaseHttpClientInvocation createClientInvocation(Object[] theArgs, IResource theResource) { StringBuilder urlExtension = new StringBuilder(); urlExtension.append(getContext().getResourceDefinition(theResource).getName()); - return new PostClientInvocation(getContext(), theResource, urlExtension.toString()); + return new HttpPostClientInvocation(getContext(), theResource, urlExtension.toString()); } @Override @@ -119,14 +117,14 @@ public class DeleteMethodBinding extends BaseOutcomeReturningMethodBinding { } @Override - public BaseClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { + public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { IdDt idDt = (IdDt) theArgs[myIdParameterIndex]; if (idDt == null) { throw new NullPointerException("ID can not be null"); } String resourceName = getResourceName(); - DeleteClientInvocation retVal = createDeleteInvocation(resourceName, idDt); + HttpDeleteClientInvocation retVal = createDeleteInvocation(resourceName, idDt); for (int idx = 0; idx < theArgs.length; idx++) { IParameter nextParam = getParameters().get(idx); @@ -136,9 +134,9 @@ public class DeleteMethodBinding extends BaseOutcomeReturningMethodBinding { return retVal; } - public static DeleteClientInvocation createDeleteInvocation(String theResourceName, IdDt idDt) { + public static HttpDeleteClientInvocation createDeleteInvocation(String theResourceName, IdDt idDt) { String id = idDt.getValue(); - DeleteClientInvocation retVal = new DeleteClientInvocation(theResourceName, id); + HttpDeleteClientInvocation retVal = new HttpDeleteClientInvocation(theResourceName, id); return retVal; } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/GetTagsMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/GetTagsMethodBinding.java index 44f74e69d48..96638e1e917 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/GetTagsMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/GetTagsMethodBinding.java @@ -39,8 +39,7 @@ import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.rest.annotation.GetTags; import ca.uhn.fhir.rest.annotation.IdParam; -import ca.uhn.fhir.rest.client.BaseClientInvocation; -import ca.uhn.fhir.rest.client.GetClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType; import ca.uhn.fhir.rest.param.IParameter; import ca.uhn.fhir.rest.param.ParameterUtil; @@ -107,8 +106,8 @@ public class GetTagsMethodBinding extends BaseMethodBinding { } @Override - public BaseClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { - GetClientInvocation retVal; + public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { + HttpGetClientInvocation retVal; IdDt id = null; IdDt versionId = null; @@ -122,15 +121,15 @@ public class GetTagsMethodBinding extends BaseMethodBinding { if (myType != IResource.class) { if (id != null) { if (versionId != null) { - retVal = new GetClientInvocation(getResourceName(), id.getValue(), Constants.PARAM_HISTORY, versionId.getValue(), Constants.PARAM_TAGS); + retVal = new HttpGetClientInvocation(getResourceName(), id.getValue(), Constants.PARAM_HISTORY, versionId.getValue(), Constants.PARAM_TAGS); } else { - retVal = new GetClientInvocation(getResourceName(), id.getValue(), Constants.PARAM_TAGS); + retVal = new HttpGetClientInvocation(getResourceName(), id.getValue(), Constants.PARAM_TAGS); } } else { - retVal = new GetClientInvocation(getResourceName(), Constants.PARAM_TAGS); + retVal = new HttpGetClientInvocation(getResourceName(), Constants.PARAM_TAGS); } } else { - retVal = new GetClientInvocation(Constants.PARAM_TAGS); + retVal = new HttpGetClientInvocation(Constants.PARAM_TAGS); } if (theArgs != null) { 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 f3df8ce5f0f..0c44a432d29 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 @@ -34,8 +34,7 @@ import ca.uhn.fhir.model.dstu.valueset.RestfulOperationSystemEnum; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.annotation.History; -import ca.uhn.fhir.rest.client.BaseClientInvocation; -import ca.uhn.fhir.rest.client.GetClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.param.IParameter; import ca.uhn.fhir.rest.param.ParameterUtil; import ca.uhn.fhir.rest.server.Constants; @@ -103,7 +102,7 @@ public class HistoryMethodBinding extends BaseResourceReturningMethodBinding { } @Override - public BaseClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { + public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { IdDt id = null; String resourceName = myResourceName; if (myIdParamIndex != null) { @@ -113,7 +112,7 @@ public class HistoryMethodBinding extends BaseResourceReturningMethodBinding { } } - GetClientInvocation retVal = createHistoryInvocation(resourceName, id); + HttpGetClientInvocation retVal = createHistoryInvocation(resourceName, id); if (theArgs != null) { for (int idx = 0; idx < theArgs.length; idx++) { @@ -125,7 +124,7 @@ public class HistoryMethodBinding extends BaseResourceReturningMethodBinding { return retVal; } - public static GetClientInvocation createHistoryInvocation(String theResourceName, IdDt theId) { + public static HttpGetClientInvocation createHistoryInvocation(String theResourceName, IdDt theId) { StringBuilder b = new StringBuilder(); if (theResourceName != null) { b.append(theResourceName); @@ -138,7 +137,7 @@ public class HistoryMethodBinding extends BaseResourceReturningMethodBinding { b.append('/'); } b.append(Constants.PARAM_HISTORY); - GetClientInvocation retVal = new GetClientInvocation(b.toString()); + HttpGetClientInvocation retVal = new HttpGetClientInvocation(b.toString()); return retVal; } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/DeleteClientInvocation.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpDeleteClientInvocation.java similarity index 86% rename from hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/DeleteClientInvocation.java rename to hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpDeleteClientInvocation.java index 833e438023f..539727f168c 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/DeleteClientInvocation.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpDeleteClientInvocation.java @@ -1,4 +1,4 @@ -package ca.uhn.fhir.rest.client; +package ca.uhn.fhir.rest.method; /* * #%L @@ -27,13 +27,14 @@ import org.apache.commons.lang3.StringUtils; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpRequestBase; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.server.EncodingEnum; -public class DeleteClientInvocation extends BaseClientInvocation { +public class HttpDeleteClientInvocation extends BaseHttpClientInvocation { private String myUrlPath; - public DeleteClientInvocation(String... theUrlFragments) { + public HttpDeleteClientInvocation(String... theUrlFragments) { super(); myUrlPath = StringUtils.join(theUrlFragments, '/'); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GetClientInvocation.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpGetClientInvocation.java similarity index 88% rename from hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GetClientInvocation.java rename to hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpGetClientInvocation.java index 6055b70f1f6..9df31bdac1d 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/GetClientInvocation.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpGetClientInvocation.java @@ -1,4 +1,4 @@ -package ca.uhn.fhir.rest.client; +package ca.uhn.fhir.rest.method; /* * #%L @@ -32,24 +32,25 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpRequestBase; import ca.uhn.fhir.context.ConfigurationException; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.server.EncodingEnum; -public class GetClientInvocation extends BaseClientInvocation { +public class HttpGetClientInvocation extends BaseHttpClientInvocation { private final Map> myParameters; private final String myUrlPath; - public GetClientInvocation(Map> theParameters, String... theUrlFragments) { + public HttpGetClientInvocation(Map> theParameters, String... theUrlFragments) { myParameters = theParameters; myUrlPath = StringUtils.join(theUrlFragments, '/'); } - public GetClientInvocation(String theUrlPath) { + public HttpGetClientInvocation(String theUrlPath) { myParameters = new HashMap>(); myUrlPath = theUrlPath; } - public GetClientInvocation(String... theUrlFragments) { + public HttpGetClientInvocation(String... theUrlFragments) { myParameters = new HashMap>(); myUrlPath = StringUtils.join(theUrlFragments, '/'); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/PostClientInvocation.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPostClientInvocation.java similarity index 70% rename from hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/PostClientInvocation.java rename to hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPostClientInvocation.java index 0e173e1db1b..85ff147eae1 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/PostClientInvocation.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPostClientInvocation.java @@ -1,4 +1,4 @@ -package ca.uhn.fhir.rest.client; +package ca.uhn.fhir.rest.method; /* * #%L @@ -20,6 +20,8 @@ package ca.uhn.fhir.rest.client; * #L% */ +import java.util.List; + import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; @@ -27,18 +29,23 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.api.TagList; -public class PostClientInvocation extends BaseClientInvocationWithContents { +public class HttpPostClientInvocation extends BaseHttpClientInvocationWithContents { - public PostClientInvocation(FhirContext theContext, IResource theResource, String theUrlExtension) { + public HttpPostClientInvocation(FhirContext theContext, IResource theResource, String theUrlExtension) { super(theContext, theResource, theUrlExtension); } - public PostClientInvocation(FhirContext theContext, TagList theTagList, String... theUrlExtension) { + public HttpPostClientInvocation(FhirContext theContext, TagList theTagList, String... theUrlExtension) { super(theContext, theTagList, theUrlExtension); } + public HttpPostClientInvocation(FhirContext theContext, List theResources) { + super(theContext, theResources); + } + + @Override protected HttpPost createRequest(String url, StringEntity theEntity) { HttpPost retVal = new HttpPost(url); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/PutClientInvocation.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPutClientInvocation.java similarity index 83% rename from hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/PutClientInvocation.java rename to hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPutClientInvocation.java index c1d614f15a5..522c05ded4f 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/client/PutClientInvocation.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/HttpPutClientInvocation.java @@ -1,4 +1,4 @@ -package ca.uhn.fhir.rest.client; +package ca.uhn.fhir.rest.method; /* * #%L @@ -27,9 +27,9 @@ import org.apache.http.entity.StringEntity; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.IResource; -public class PutClientInvocation extends BaseClientInvocationWithContents { +public class HttpPutClientInvocation extends BaseHttpClientInvocationWithContents { - public PutClientInvocation(FhirContext theContext, IResource theResource, String theUrlExtension) { + public HttpPutClientInvocation(FhirContext theContext, IResource theResource, String theUrlExtension) { super(theContext, theResource, theUrlExtension); } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ReadMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ReadMethodBinding.java index af150ef2d4a..7468ee339bc 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ReadMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ReadMethodBinding.java @@ -32,7 +32,6 @@ import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationSystemEnum; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; import ca.uhn.fhir.model.primitive.IdDt; -import ca.uhn.fhir.rest.client.GetClientInvocation; import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType; import ca.uhn.fhir.rest.param.IParameter; import ca.uhn.fhir.rest.param.ParameterUtil; @@ -119,8 +118,8 @@ public class ReadMethodBinding extends BaseResourceReturningMethodBinding { } @Override - public GetClientInvocation invokeClient(Object[] theArgs) { - GetClientInvocation retVal; + public HttpGetClientInvocation invokeClient(Object[] theArgs) { + HttpGetClientInvocation retVal; IdDt id = ((IdDt) theArgs[myIdIndex]); if (myVersionIdIndex == null) { String resourceName = getResourceName(); @@ -139,12 +138,12 @@ public class ReadMethodBinding extends BaseResourceReturningMethodBinding { return retVal; } - public static GetClientInvocation createVReadInvocation(IdDt theId, IdDt vid, String resourceName) { - return new GetClientInvocation(resourceName, theId.getUnqualifiedId(), Constants.URL_TOKEN_HISTORY, vid.getUnqualifiedId()); + public static HttpGetClientInvocation createVReadInvocation(IdDt theId, IdDt vid, String resourceName) { + return new HttpGetClientInvocation(resourceName, theId.getUnqualifiedId(), Constants.URL_TOKEN_HISTORY, vid.getUnqualifiedId()); } - public static GetClientInvocation createReadInvocation(IdDt theId, String resourceName) { - return new GetClientInvocation(resourceName, theId.getUnqualifiedId()); + public static HttpGetClientInvocation createReadInvocation(IdDt theId, String resourceName) { + return new HttpGetClientInvocation(resourceName, theId.getUnqualifiedId()); } @Override diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/SearchMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/SearchMethodBinding.java index a9f1cdf4bf3..65c85430335 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/SearchMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/SearchMethodBinding.java @@ -34,7 +34,6 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationSystemEnum; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; -import ca.uhn.fhir.rest.client.GetClientInvocation; import ca.uhn.fhir.rest.param.BaseQueryParameter; import ca.uhn.fhir.rest.param.IParameter; import ca.uhn.fhir.rest.server.Constants; @@ -76,7 +75,7 @@ public class SearchMethodBinding extends BaseResourceReturningMethodBinding { } @Override - public GetClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { + public HttpGetClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { assert (myQueryName == null || ((theArgs != null ? theArgs.length : 0) == getParameters().size())) : "Wrong number of arguments: " + (theArgs != null ? theArgs.length : "null"); Map> queryStringArgs = new LinkedHashMap>(); @@ -86,7 +85,7 @@ public class SearchMethodBinding extends BaseResourceReturningMethodBinding { } String resourceName = getResourceName(); - GetClientInvocation retVal = createSearchInvocation(resourceName, queryStringArgs); + HttpGetClientInvocation retVal = createSearchInvocation(resourceName, queryStringArgs); if (theArgs != null) { for (int idx = 0; idx < theArgs.length; idx++) { @@ -98,8 +97,8 @@ public class SearchMethodBinding extends BaseResourceReturningMethodBinding { return retVal; } - public static GetClientInvocation createSearchInvocation(String theResourceName, Map> theParameters) { - return new GetClientInvocation(theParameters, theResourceName); + public static HttpGetClientInvocation createSearchInvocation(String theResourceName, Map> theParameters) { + return new HttpGetClientInvocation(theParameters, theResourceName); } @Override diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/TransactionMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/TransactionMethodBinding.java index f54790ba01e..029666e1d0b 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/TransactionMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/TransactionMethodBinding.java @@ -28,12 +28,13 @@ import java.util.List; import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.Bundle; +import ca.uhn.fhir.model.api.BundleEntry; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationSystemEnum; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; import ca.uhn.fhir.parser.IParser; import ca.uhn.fhir.rest.annotation.TransactionParam; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType; import ca.uhn.fhir.rest.param.TransactionParameter; import ca.uhn.fhir.rest.param.IParameter; @@ -108,9 +109,11 @@ public class TransactionMethodBinding extends BaseResourceReturningMethodBinding } @Override - public BaseClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { - // TODO Auto-generated method stub - return null; + public BaseHttpClientInvocation invokeClient(Object[] theArgs) throws InternalErrorException { + List resources = (List) theArgs[myResourceParameterIndex]; + FhirContext context = getContext(); + + return new HttpPostClientInvocation(context, resources); } } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/UpdateMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/UpdateMethodBinding.java index cb348123ad9..744bdb11aaf 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/UpdateMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/UpdateMethodBinding.java @@ -37,8 +37,7 @@ import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.annotation.IdParam; import ca.uhn.fhir.rest.annotation.Update; import ca.uhn.fhir.rest.api.MethodOutcome; -import ca.uhn.fhir.rest.client.BaseClientInvocation; -import ca.uhn.fhir.rest.client.PutClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType; import ca.uhn.fhir.rest.param.IParameter; import ca.uhn.fhir.rest.param.ParameterUtil; @@ -97,7 +96,7 @@ public class UpdateMethodBinding extends BaseOutcomeReturningMethodBindingWithRe } @Override - protected BaseClientInvocation createClientInvocation(Object[] theArgs, IResource theResource) { + protected BaseHttpClientInvocation createClientInvocation(Object[] theArgs, IResource theResource) { IdDt idDt = (IdDt) theArgs[myIdParameterIndex]; if (idDt == null) { throw new NullPointerException("ID can not be null"); @@ -109,7 +108,7 @@ public class UpdateMethodBinding extends BaseOutcomeReturningMethodBindingWithRe } FhirContext context = getContext(); - PutClientInvocation retVal = createUpdateInvocation(theResource, idDt, versionIdDt, context); + HttpPutClientInvocation retVal = createUpdateInvocation(theResource, idDt, versionIdDt, context); for (int idx = 0; idx < theArgs.length; idx++) { IParameter nextParam = getParameters().get(idx); @@ -119,14 +118,14 @@ public class UpdateMethodBinding extends BaseOutcomeReturningMethodBindingWithRe return retVal; } - public static PutClientInvocation createUpdateInvocation(IResource theResource, IdDt idDt, IdDt versionIdDt, FhirContext context) { + public static HttpPutClientInvocation createUpdateInvocation(IResource theResource, IdDt idDt, IdDt versionIdDt, FhirContext context) { String id = idDt.getValue(); String resourceName = context.getResourceDefinition(theResource).getName(); StringBuilder urlExtension = new StringBuilder(); urlExtension.append(resourceName); urlExtension.append('/'); urlExtension.append(id); - PutClientInvocation retVal = new PutClientInvocation(context, theResource, urlExtension.toString()); + HttpPutClientInvocation retVal = new HttpPutClientInvocation(context, theResource, urlExtension.toString()); if (versionIdDt != null) { String versionId = versionIdDt.getValue(); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ValidateMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ValidateMethodBinding.java index f7a71be1f9b..609128a317e 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ValidateMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/ValidateMethodBinding.java @@ -30,8 +30,7 @@ import ca.uhn.fhir.model.dstu.valueset.RestfulOperationSystemEnum; import ca.uhn.fhir.model.dstu.valueset.RestfulOperationTypeEnum; import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.rest.annotation.Validate; -import ca.uhn.fhir.rest.client.BaseClientInvocation; -import ca.uhn.fhir.rest.client.PostClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType; import ca.uhn.fhir.rest.param.IParameter; import ca.uhn.fhir.rest.param.ParameterUtil; @@ -65,7 +64,7 @@ public class ValidateMethodBinding extends BaseOutcomeReturningMethodBindingWith } @Override - protected BaseClientInvocation createClientInvocation(Object[] theArgs, IResource theResource) { + protected BaseHttpClientInvocation createClientInvocation(Object[] theArgs, IResource theResource) { FhirContext context = getContext(); IdDt idDt=null; @@ -73,7 +72,7 @@ public class ValidateMethodBinding extends BaseOutcomeReturningMethodBindingWith idDt = (IdDt) theArgs[myIdParameterIndex]; } - PostClientInvocation retVal = createValidateInvocation(theResource, idDt, context); + HttpPostClientInvocation retVal = createValidateInvocation(theResource, idDt, context); for (int idx = 0; idx < theArgs.length; idx++) { IParameter nextParam = getParameters().get(idx); @@ -83,7 +82,7 @@ public class ValidateMethodBinding extends BaseOutcomeReturningMethodBindingWith return retVal; } - public static PostClientInvocation createValidateInvocation(IResource theResource, IdDt theId, FhirContext theContext) { + public static HttpPostClientInvocation createValidateInvocation(IResource theResource, IdDt theId, FhirContext theContext) { StringBuilder urlExtension = new StringBuilder(); urlExtension.append(theContext.getResourceDefinition(theResource).getName()); urlExtension.append('/'); @@ -95,7 +94,7 @@ public class ValidateMethodBinding extends BaseOutcomeReturningMethodBindingWith urlExtension.append(id); } // TODO: is post correct here? - PostClientInvocation retVal = new PostClientInvocation(theContext, theResource, urlExtension.toString()); + HttpPostClientInvocation retVal = new HttpPostClientInvocation(theContext, theResource, urlExtension.toString()); return retVal; } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/BaseQueryParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/BaseQueryParameter.java index 0f434d81331..7d9c8d0af8c 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/BaseQueryParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/BaseQueryParameter.java @@ -30,7 +30,7 @@ import org.apache.commons.lang3.StringUtils; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.dstu.valueset.SearchParamTypeEnum; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.QualifiedParamList; import ca.uhn.fhir.rest.method.Request; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; @@ -55,7 +55,7 @@ public abstract class BaseQueryParameter implements IParameter { public abstract SearchParamTypeEnum getParamType(); @Override - public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseClientInvocation theClientInvocation) throws InternalErrorException { + public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseHttpClientInvocation theClientInvocation) throws InternalErrorException { if (theSourceClientArgument == null) { if (isRequired()) { throw new NullPointerException("SearchParameter '" + getName() + "' is required and may not be null"); diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/CountParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/CountParameter.java index 4a5c940b115..c55fdc7e4c2 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/CountParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/CountParameter.java @@ -33,7 +33,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.primitive.IntegerDt; import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.rest.annotation.Since; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.Request; import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; @@ -44,7 +44,7 @@ public class CountParameter implements IParameter { private Class myType; @Override - public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseClientInvocation theClientInvocation) throws InternalErrorException { + public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseHttpClientInvocation theClientInvocation) throws InternalErrorException { if (theSourceClientArgument != null) { IntegerDt since = ParameterUtil.toInteger(theSourceClientArgument); if (since.isEmpty() == false) { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/IParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/IParameter.java index ac1409f900c..34d66d9b3eb 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/IParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/IParameter.java @@ -27,7 +27,7 @@ import java.util.Map; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.IResource; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.Request; import ca.uhn.fhir.rest.server.IResourceProvider; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; @@ -35,7 +35,7 @@ import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; public interface IParameter { - void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseClientInvocation theClientInvocation) throws InternalErrorException; + void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseHttpClientInvocation theClientInvocation) throws InternalErrorException; /** * This server method method takes the data received by the server in an incoming request, and translates that data into a single argument for a server method invocation. Note that all diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/NullParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/NullParameter.java index 5a71f0a082f..d3c073c4cae 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/NullParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/NullParameter.java @@ -26,7 +26,7 @@ import java.util.List; import java.util.Map; import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.Request; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; @@ -34,7 +34,7 @@ import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; class NullParameter implements IParameter { @Override - public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseClientInvocation theClientInvocation) throws InternalErrorException { + public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseHttpClientInvocation theClientInvocation) throws InternalErrorException { //nothing } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ResourceParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ResourceParameter.java index 890080f4c3a..a71b2eea9f0 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ResourceParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ResourceParameter.java @@ -27,7 +27,7 @@ import java.util.Map; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.api.IResource; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.Request; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; @@ -41,7 +41,7 @@ public class ResourceParameter implements IParameter { } @Override - public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseClientInvocation theClientInvocation) throws InternalErrorException { + public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseHttpClientInvocation theClientInvocation) throws InternalErrorException { // TODO Auto-generated method stub } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ServerBaseParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ServerBaseParameter.java index d18605d0360..f567b1ac629 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ServerBaseParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ServerBaseParameter.java @@ -26,7 +26,7 @@ import java.util.List; import java.util.Map; import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.Request; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; @@ -35,7 +35,7 @@ class ServerBaseParameter implements IParameter { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ServerBaseParameter.class); @Override - public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseClientInvocation theClientInvocation) throws InternalErrorException { + public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseHttpClientInvocation theClientInvocation) throws InternalErrorException { /* * Does nothing, since we just ignore serverbase arguments */ diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ServletRequestParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ServletRequestParameter.java index a3f12999d19..c4833947217 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ServletRequestParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ServletRequestParameter.java @@ -26,7 +26,7 @@ import java.util.List; import java.util.Map; import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.Request; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; @@ -35,7 +35,7 @@ class ServletRequestParameter implements IParameter { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ServletRequestParameter.class); @Override - public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseClientInvocation theClientInvocation) throws InternalErrorException { + public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseHttpClientInvocation theClientInvocation) throws InternalErrorException { /* * Does nothing, since we just ignore HttpServletRequest arguments */ diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ServletResponseParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ServletResponseParameter.java index 57699c9512d..453cbe86766 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ServletResponseParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/ServletResponseParameter.java @@ -26,7 +26,7 @@ import java.util.List; import java.util.Map; import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.Request; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; @@ -35,7 +35,7 @@ class ServletResponseParameter implements IParameter { private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ServletResponseParameter.class); @Override - public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseClientInvocation theClientInvocation) throws InternalErrorException { + public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseHttpClientInvocation theClientInvocation) throws InternalErrorException { /* * Does nothing, since we just ignore HttpServletResponse arguments */ diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/SinceParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/SinceParameter.java index 49ce13db239..8fe644b9516 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/SinceParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/SinceParameter.java @@ -33,7 +33,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.parser.DataFormatException; import ca.uhn.fhir.rest.annotation.Since; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.Request; import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; @@ -44,7 +44,7 @@ public class SinceParameter implements IParameter { private Class myType; @Override - public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseClientInvocation theClientInvocation) throws InternalErrorException { + public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseHttpClientInvocation theClientInvocation) throws InternalErrorException { if (theSourceClientArgument != null) { InstantDt since = ParameterUtil.toInstant(theSourceClientArgument); if (since.isEmpty() == false) { diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/SortParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/SortParameter.java index 7750c39fcc0..9686dffef94 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/SortParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/SortParameter.java @@ -33,7 +33,7 @@ import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.annotation.Sort; import ca.uhn.fhir.rest.api.SortOrderEnum; import ca.uhn.fhir.rest.api.SortSpec; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.Request; import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; @@ -42,7 +42,7 @@ import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; public class SortParameter implements IParameter { @Override - public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseClientInvocation theClientInvocation) throws InternalErrorException { + public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseHttpClientInvocation theClientInvocation) throws InternalErrorException { SortSpec ss = (SortSpec) theSourceClientArgument; while (ss != null) { String name; diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/TransactionParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/TransactionParameter.java index 0522041282a..1fd95dbf42c 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/TransactionParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/TransactionParameter.java @@ -32,7 +32,7 @@ import ca.uhn.fhir.model.api.Bundle; import ca.uhn.fhir.model.api.BundleEntry; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.rest.annotation.TransactionParam; -import ca.uhn.fhir.rest.client.BaseClientInvocation; +import ca.uhn.fhir.rest.client.BaseHttpClientInvocation; import ca.uhn.fhir.rest.method.Request; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; @@ -43,7 +43,7 @@ public class TransactionParameter implements IParameter { } @Override - public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseClientInvocation theClientInvocation) throws InternalErrorException { + public void translateClientArgumentIntoQueryArgument(FhirContext theContext, Object theSourceClientArgument, Map> theTargetQueryArguments, BaseHttpClientInvocation theClientInvocation) throws InternalErrorException { // TODO Auto-generated method stub } diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java index 4120e1533ef..6b8ecad2e06 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java @@ -323,6 +323,42 @@ public class XmlParserTest { } } + @Test + public void testEncodePrettyPrint() throws DataFormatException { + + Patient patient = new Patient(); + patient.getText().getDiv().setValueAsString("
\n hello \n
"); + patient.addName().addFamily("Family").addGiven("Given"); + + //@formatter:off + String encoded = new FhirContext().newXmlParser().setPrettyPrint(false).encodeResourceToString(patient); + ourLog.info(encoded); + String expected = "
\n" + + " hello \n" + + "
"; + assertEquals(expected, encoded); + + encoded = new FhirContext().newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); + ourLog.info(encoded); + expected = "\n" + + " \n" + + "
\n" + + " hello \n" + + "
\n" + + "
\n" + + " \n" + + " \n" + + " \n" + + " \n" + + "
"; + //@formatter:on + + // Whitespace should be preserved and not reformatted in narrative blocks + assertEquals(expected, encoded); + + } + + @Test public void testEncodeResourceRef() throws DataFormatException { diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/client/TransactionClientTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/client/TransactionClientTest.java new file mode 100644 index 00000000000..09ab5404f05 --- /dev/null +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/client/TransactionClientTest.java @@ -0,0 +1,108 @@ +package ca.uhn.fhir.rest.client; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.InputStreamReader; +import java.io.StringReader; +import java.nio.charset.Charset; +import java.util.Arrays; +import java.util.List; + +import org.apache.commons.io.input.ReaderInputStream; +import org.apache.http.HttpResponse; +import org.apache.http.ProtocolVersion; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.message.BasicHeader; +import org.apache.http.message.BasicStatusLine; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs; + +import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.model.api.Bundle; +import ca.uhn.fhir.model.api.IResource; +import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt; +import ca.uhn.fhir.model.dstu.resource.Conformance; +import ca.uhn.fhir.model.dstu.resource.Observation; +import ca.uhn.fhir.model.dstu.resource.Patient; +import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.annotation.Transaction; +import ca.uhn.fhir.rest.annotation.TransactionParam; +import ca.uhn.fhir.rest.client.api.IBasicClient; +import ca.uhn.fhir.rest.server.Constants; + +public class TransactionClientTest { + + private FhirContext ctx; + private HttpClient httpClient; + private HttpResponse httpResponse; + + // atom-document-large.xml + + @Before + public void before() { + ctx = new FhirContext(Patient.class, Conformance.class); + + httpClient = mock(HttpClient.class, new ReturnsDeepStubs()); + ctx.getRestfulClientFactory().setHttpClient(httpClient); + + httpResponse = mock(HttpResponse.class, new ReturnsDeepStubs()); + } + + + @Test + public void testSimpleTransaction() throws Exception { + Patient patient = new Patient(); + patient.setId(new IdDt("Patient/testPersistWithSimpleLinkP01")); + patient.addIdentifier("urn:system", "testPersistWithSimpleLinkP01"); + patient.addName().addFamily("Tester").addGiven("Joe"); + + Observation obs = new Observation(); + obs.getName().addCoding().setSystem("urn:system").setCode("testPersistWithSimpleLinkO01"); + obs.setSubject(new ResourceReferenceDt("Patient/testPersistWithSimpleLinkP01")); + + List resources = Arrays.asList((IResource)patient, obs); + + IClient client = ctx.newRestfulClient(IClient.class, "http://foo"); + + ArgumentCaptor capt = ArgumentCaptor.forClass(HttpUriRequest.class); + when(httpClient.execute(capt.capture())).thenReturn(httpResponse); + when(httpResponse.getStatusLine()).thenReturn(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), 200, "OK")); + when(httpResponse.getEntity().getContentType()).thenReturn(new BasicHeader("content-type", Constants.CT_ATOM_XML + "; charset=UTF-8")); + when(httpResponse.getEntity().getContent()).thenReturn(new ReaderInputStream(new StringReader(createBundle()), Charset.forName("UTF-8"))); + + client.searchWithParam(resources); + + assertEquals(HttpPost.class, capt.getValue().getClass()); + HttpPost get = (HttpPost) capt.getValue(); + assertEquals("http://foo/", get.getURI().toString()); + + Bundle bundle = ctx.newXmlParser().parseBundle(new InputStreamReader(get.getEntity().getContent())); + ourLog.info(ctx.newXmlParser().setPrettyPrint(true).encodeBundleToString(bundle)); + + assertEquals(2, bundle.size()); + assertEquals("Patient/testPersistWithSimpleLinkP01", bundle.getEntries().get(0).getId().getValue()); + assertEquals("http://foo/Patient/testPersistWithSimpleLinkP01", bundle.getEntries().get(0).getLinkSelf().getValue()); + + assertTrue(bundle.getEntries().get(1).getId().isEmpty()); + + } + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(TransactionClientTest.class); + private String createBundle() { + return ctx.newXmlParser().encodeBundleToString(new Bundle()); + } + + private interface IClient extends IBasicClient { + + @Transaction + public List searchWithParam(@TransactionParam List theResources); + + + } + +} diff --git a/hapi-fhir-jpaserver-base/.classpath b/hapi-fhir-jpaserver-base/.classpath index 15f6337d7f2..520b95df1ee 100644 --- a/hapi-fhir-jpaserver-base/.classpath +++ b/hapi-fhir-jpaserver-base/.classpath @@ -1,7 +1,7 @@ - - + + diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseFhirDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseFhirDao.java index c7746b19b45..741e3c8a939 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseFhirDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseFhirDao.java @@ -4,6 +4,7 @@ import static org.apache.commons.lang3.StringUtils.*; import java.text.Normalizer; import java.util.ArrayList; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -19,6 +20,7 @@ import ca.uhn.fhir.context.ConfigurationException; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.context.RuntimeResourceDefinition; import ca.uhn.fhir.context.RuntimeSearchParam; +import ca.uhn.fhir.jpa.entity.ResourceHistoryTable; import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamDate; import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamNumber; import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamString; @@ -47,7 +49,7 @@ import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException; import ca.uhn.fhir.util.FhirTerser; public abstract class BaseFhirDao { - private FhirContext myContext=new FhirContext(); + private FhirContext myContext = new FhirContext(); @PersistenceContext(name = "FHIR_UT", type = PersistenceContextType.TRANSACTION, unitName = "FHIR_UT") private EntityManager myEntityManager; @Autowired @@ -58,10 +60,87 @@ public abstract class BaseFhirDao { public FhirContext getContext() { return myContext; } + public void setContext(FhirContext theContext) { myContext = theContext; } + protected ResourceTable updateEntity(final IResource theResource, ResourceTable entity, boolean theUpdateHistory) { + if (entity.getPublished() == null) { + entity.setPublished(new Date()); + } + + if (theUpdateHistory) { + final ResourceHistoryTable historyEntry = entity.toHistory(getContext()); + myEntityManager.persist(historyEntry); + } + + entity.setVersion(entity.getVersion()+1); + + final List stringParams = extractSearchParamStrings(entity, theResource); + final List tokenParams = extractSearchParamTokens(entity, theResource); + final List numberParams = extractSearchParamNumber(entity, theResource); + final List dateParams = extractSearchParamDates(entity, theResource); + final List links = extractResourceLinks(entity, theResource); + + populateResourceIntoEntity(theResource, entity); + + entity.setUpdated(new Date()); + + if (entity.getId() == null) { + myEntityManager.persist(entity); + } else { + entity = myEntityManager.merge(entity); + } + + if (entity.isParamsStringPopulated()) { + for (ResourceIndexedSearchParamString next : entity.getParamsString()) { + myEntityManager.remove(next); + } + } + for (ResourceIndexedSearchParamString next : stringParams) { + myEntityManager.persist(next); + } + + if (entity.isParamsTokenPopulated()) { + for (ResourceIndexedSearchParamToken next : entity.getParamsToken()) { + myEntityManager.remove(next); + } + } + for (ResourceIndexedSearchParamToken next : tokenParams) { + myEntityManager.persist(next); + } + + if (entity.isParamsNumberPopulated()) { + for (ResourceIndexedSearchParamNumber next : entity.getParamsNumber()) { + myEntityManager.remove(next); + } + } + for (ResourceIndexedSearchParamNumber next : numberParams) { + myEntityManager.persist(next); + } + + if (entity.isParamsDatePopulated()) { + for (ResourceIndexedSearchParamDate next : entity.getParamsDate()) { + myEntityManager.remove(next); + } + } + for (ResourceIndexedSearchParamDate next : dateParams) { + myEntityManager.persist(next); + } + + if (entity.isHasLinks()) { + for (ResourceLink next : entity.getResourceLinks()) { + myEntityManager.remove(next); + } + } + for (ResourceLink next : links) { + myEntityManager.persist(next); + } + + return entity; + } + protected List extractResourceLinks(ResourceTable theEntity, IResource theResource) { ArrayList retVal = new ArrayList(); @@ -211,7 +290,8 @@ public abstract class BaseFhirDao { if (nextObject instanceof QuantityDt) { QuantityDt nextValue = (QuantityDt) nextObject; - ResourceIndexedSearchParamNumber nextEntity = new ResourceIndexedSearchParamNumber(resourceName, nextValue.getValue().getValue(), nextValue.getSystem().getValueAsString(), nextValue.getUnits().getValue()); + ResourceIndexedSearchParamNumber nextEntity = new ResourceIndexedSearchParamNumber(resourceName, nextValue.getValue().getValue(), nextValue.getSystem().getValueAsString(), + nextValue.getUnits().getValue()); nextEntity.setResource(theEntity); retVal.add(nextEntity); } else { @@ -294,7 +374,8 @@ public abstract class BaseFhirDao { } else if (nextObject instanceof ContactDt) { ContactDt nextContact = (ContactDt) nextObject; if (nextContact.getValue().isEmpty() == false) { - ResourceIndexedSearchParamString nextEntity = new ResourceIndexedSearchParamString(resourceName, normalizeString(nextContact.getValue().getValueAsString()), nextContact.getValue().getValueAsString()); + ResourceIndexedSearchParamString nextEntity = new ResourceIndexedSearchParamString(resourceName, normalizeString(nextContact.getValue().getValueAsString()), nextContact + .getValue().getValueAsString()); nextEntity.setResource(theEntity); retVal.add(nextEntity); } @@ -311,7 +392,7 @@ public abstract class BaseFhirDao { return retVal; } - + protected List extractSearchParamTokens(ResourceTable theEntity, IResource theResource) { ArrayList retVal = new ArrayList(); @@ -377,7 +458,7 @@ public abstract class BaseFhirDao { return retVal; } - protected IFhirResourceDao getDao(Class theType) { + protected IFhirResourceDao getDao(Class theType) { if (myResourceTypeToDao == null) { myResourceTypeToDao = new HashMap<>(); for (IFhirResourceDao next : myResourceDaos) { @@ -403,6 +484,8 @@ public abstract class BaseFhirDao { } protected void populateResourceIntoEntity(IResource theResource, ResourceTable theEntity) { + + theEntity.setResourceType(toResourceName(theResource)); theEntity.setResource(getContext().newJsonParser().encodeResourceToString(theResource)); theEntity.setEncoding(EncodingEnum.JSON); @@ -427,5 +510,4 @@ public abstract class BaseFhirDao { return myContext.getResourceDefinition(theResource).getName(); } - } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDao.java index e1cea234dd0..e4e366ed9b2 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirResourceDao.java @@ -64,6 +64,7 @@ import ca.uhn.fhir.rest.param.DateRangeParam; import ca.uhn.fhir.rest.param.QualifiedDateParam; import ca.uhn.fhir.rest.param.ReferenceParam; import ca.uhn.fhir.rest.param.StringParam; +import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; public class FhirResourceDao extends BaseFhirDao implements IFhirResourceDao { @@ -80,7 +81,7 @@ public class FhirResourceDao extends BaseFhirDao implements @Transactional(propagation = Propagation.REQUIRED, readOnly = true) @Override - public MethodOutcome create(T theResource) { + public MethodOutcome create(final T theResource) { final ResourceTable entity = toEntity(theResource); @@ -88,13 +89,13 @@ public class FhirResourceDao extends BaseFhirDao implements entity.setUpdated(entity.getPublished()); entity.setResourceType(toResourceName(theResource)); - final List stringParams = extractSearchParamStrings(entity, theResource); - final List tokenParams = extractSearchParamTokens(entity, theResource); - final List numberParams = extractSearchParamNumber(entity, theResource); - final List dateParams = extractSearchParamDates(entity, theResource); - final List links = extractResourceLinks(entity, theResource); +// final List stringParams = extractSearchParamStrings(entity, theResource); +// final List tokenParams = extractSearchParamTokens(entity, theResource); +// final List numberParams = extractSearchParamNumber(entity, theResource); +// final List dateParams = extractSearchParamDates(entity, theResource); +// final List links = extractResourceLinks(entity, theResource); - ourLog.info("Saving links: {}", links); +// ourLog.info("Saving links: {}", links); TransactionTemplate template = new TransactionTemplate(myPlatformTransactionManager); template.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW); @@ -102,22 +103,23 @@ public class FhirResourceDao extends BaseFhirDao implements template.execute(new TransactionCallback() { @Override public ResourceTable doInTransaction(TransactionStatus theStatus) { - myEntityManager.persist(entity); - for (ResourceIndexedSearchParamString next : stringParams) { - myEntityManager.persist(next); - } - for (ResourceIndexedSearchParamToken next : tokenParams) { - myEntityManager.persist(next); - } - for (ResourceIndexedSearchParamNumber next : numberParams) { - myEntityManager.persist(next); - } - for (ResourceIndexedSearchParamDate next : dateParams) { - myEntityManager.persist(next); - } - for (ResourceLink next : links) { - myEntityManager.persist(next); - } +// myEntityManager.persist(entity); +// for (ResourceIndexedSearchParamString next : stringParams) { +// myEntityManager.persist(next); +// } +// for (ResourceIndexedSearchParamToken next : tokenParams) { +// myEntityManager.persist(next); +// } +// for (ResourceIndexedSearchParamNumber next : numberParams) { +// myEntityManager.persist(next); +// } +// for (ResourceIndexedSearchParamDate next : dateParams) { +// myEntityManager.persist(next); +// } +// for (ResourceLink next : links) { +// myEntityManager.persist(next); +// } + updateEntity(theResource, entity,false); return entity; } }); @@ -126,7 +128,6 @@ public class FhirResourceDao extends BaseFhirDao implements return outcome; } - public Class getResourceType() { return myResourceType; } @@ -303,84 +304,27 @@ public class FhirResourceDao extends BaseFhirDao implements myResourceType = (Class) theTableType; } - @Transactional(propagation = Propagation.SUPPORTS) + @Transactional(propagation = Propagation.REQUIRED) @Override public MethodOutcome update(final T theResource, final IdDt theId) { - TransactionTemplate template = new TransactionTemplate(myPlatformTransactionManager); - ResourceTable savedEntity = template.execute(new TransactionCallback() { - @Override - public ResourceTable doInTransaction(TransactionStatus theStatus) { +// TransactionTemplate template = new TransactionTemplate(myPlatformTransactionManager); +// ResourceTable savedEntity = template.execute(new TransactionCallback() { +// @Override +// public ResourceTable doInTransaction(TransactionStatus theStatus) { +// final ResourceTable entity = readEntity(theId); +// return updateEntity(theResource, entity,true); +// } +// }); - final ResourceTable entity = readEntity(theId); - entity.setUpdated(entity.getPublished()); - - final ResourceHistoryTable historyEntry = entity.toHistory(getContext()); - - final List stringParams = extractSearchParamStrings(entity, theResource); - final List tokenParams = extractSearchParamTokens(entity, theResource); - final List numberParams = extractSearchParamNumber(entity, theResource); - final List dateParams = extractSearchParamDates(entity, theResource); - final List links = extractResourceLinks(entity, theResource); - - populateResourceIntoEntity(theResource, entity); - myEntityManager.persist(historyEntry); - - entity.setUpdated(new Date()); - myEntityManager.persist(entity); - - if (entity.isParamsStringPopulated()) { - for (ResourceIndexedSearchParamString next : entity.getParamsString()) { - myEntityManager.remove(next); - } - } - for (ResourceIndexedSearchParamString next : stringParams) { - myEntityManager.persist(next); - } - - if (entity.isParamsTokenPopulated()) { - for (ResourceIndexedSearchParamToken next : entity.getParamsToken()) { - myEntityManager.remove(next); - } - } - for (ResourceIndexedSearchParamToken next : tokenParams) { - myEntityManager.persist(next); - } - - if (entity.isParamsNumberPopulated()) { - for (ResourceIndexedSearchParamNumber next : entity.getParamsNumber()) { - myEntityManager.remove(next); - } - } - for (ResourceIndexedSearchParamNumber next : numberParams) { - myEntityManager.persist(next); - } - - if (entity.isParamsDatePopulated()) { - for (ResourceIndexedSearchParamDate next : entity.getParamsDate()) { - myEntityManager.remove(next); - } - } - for (ResourceIndexedSearchParamDate next : dateParams) { - myEntityManager.persist(next); - } - - if (entity.isHasLinks()) { - for (ResourceLink next : entity.getResourceLinks()) { - myEntityManager.remove(next); - } - } - for (ResourceLink next : links) { - myEntityManager.persist(next); - } - - return entity; - } - }); + final ResourceTable entity = readEntity(theId); + ResourceTable savedEntity = updateEntity(theResource, entity,true); return toMethodOutcome(savedEntity); } + + private Set addPredicateDate(Set thePids, List theOrParams) { if (theOrParams == null || theOrParams.isEmpty()) { return thePids; @@ -710,8 +654,6 @@ public class FhirResourceDao extends BaseFhirDao implements return new HashSet(q.getResultList()); } - - private ResourceTable readEntity(IdDt theId) { ResourceTable entity = myEntityManager.find(ResourceTable.class, theId.asLong()); if (entity == null) { @@ -720,11 +662,10 @@ public class FhirResourceDao extends BaseFhirDao implements return entity; } - private MethodOutcome toMethodOutcome(final ResourceTable entity) { MethodOutcome outcome = new MethodOutcome(); - outcome.setId(new IdDt(entity.getResourceType() + '/' + entity.getId())); - outcome.setVersionId(entity.getVersion()); + outcome.setId(new IdDt(entity.getResourceType() + '/' + entity.getId() + '/' + Constants.PARAM_HISTORY + '/' + entity.getVersion())); + outcome.setVersionId(new IdDt(entity.getVersion())); return outcome; } @@ -759,11 +700,10 @@ public class FhirResourceDao extends BaseFhirDao implements protected IFhirResourceDao getDao(Class theType) { if (theType.equals(myResourceType)) { return this; - } + } return super.getDao(theType); } - private T toResource(BaseHasResource theEntity) { String resourceText = theEntity.getResource(); IParser parser = theEntity.getEncoding().newParser(getContext()); diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirSystemDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirSystemDao.java index 0f919799ba7..09872042f68 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirSystemDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/FhirSystemDao.java @@ -15,6 +15,11 @@ import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import ca.uhn.fhir.context.FhirContext; +import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamDate; +import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamNumber; +import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamString; +import ca.uhn.fhir.jpa.entity.ResourceIndexedSearchParamToken; +import ca.uhn.fhir.jpa.entity.ResourceLink; import ca.uhn.fhir.jpa.entity.ResourceTable; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt; @@ -35,13 +40,13 @@ public class FhirSystemDao extends BaseFhirDao implements IFhirSystemDao { ourLog.info("Beginning transaction with {} resources", theResources.size()); FhirTerser terser = myContext.newTerser(); - + Map idConversions = new HashMap<>(); List persistedResources = new ArrayList<>(); for (IResource nextResource : theResources) { IdDt nextId = nextResource.getId(); - if (isBlank(nextId.getUnqualifiedId())) { - continue; + if (nextId == null) { + nextId = new IdDt(); } String resourceName = toResourceName(nextResource); @@ -52,19 +57,28 @@ public class FhirSystemDao extends BaseFhirDao implements IFhirSystemDao { // nextResource.getResourceId().getResourceType()); // } - ResourceTable entity = myEntityManager.find(ResourceTable.class, nextId.asLong()); + ResourceTable entity; + if (nextId.isEmpty()) { + entity = null; + } else if (!nextId.isValidLong()) { + entity = null; + } else { + entity = myEntityManager.find(ResourceTable.class, nextId.asLong()); + } + if (entity == null) { entity = toEntity(nextResource); myEntityManager.persist(entity); myEntityManager.flush(); } + idConversions.put(nextId, new IdDt(resourceName + '/' + entity.getId())); persistedResources.add(entity); - + } for (IResource nextResource : theResources) { - List allRefs = terser.getAllPopulatedChildElementsOfType(nextResource, ResourceReferenceDt.class); + List allRefs = terser.getAllPopulatedChildElementsOfType(nextResource, ResourceReferenceDt.class); for (ResourceReferenceDt nextRef : allRefs) { IdDt nextId = nextRef.getResourceId(); if (idConversions.containsKey(nextId)) { @@ -74,8 +88,13 @@ public class FhirSystemDao extends BaseFhirDao implements IFhirSystemDao { } } } - - + + for (int i = 0; i < theResources.size(); i++) { + IResource resource = theResources.get(i); + ResourceTable table = persistedResources.get(i); + updateEntity(resource, table, table.getId() != null); + } + return null; } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/BaseHasResource.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/BaseHasResource.java index f86c74ea647..41e1b6d7620 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/BaseHasResource.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/BaseHasResource.java @@ -3,16 +3,12 @@ package ca.uhn.fhir.jpa.entity; import java.util.Collection; import java.util.Date; -import javax.persistence.Basic; import javax.persistence.Column; -import javax.persistence.FetchType; import javax.persistence.Lob; import javax.persistence.MappedSuperclass; import javax.persistence.Temporal; import javax.persistence.TemporalType; -import org.hibernate.annotations.Fetch; - import ca.uhn.fhir.model.primitive.IdDt; import ca.uhn.fhir.model.primitive.InstantDt; import ca.uhn.fhir.rest.server.EncodingEnum; @@ -55,7 +51,7 @@ public abstract class BaseHasResource { return new InstantDt(myUpdated); } - public abstract IdDt getVersion(); + public abstract long getVersion(); public void setEncoding(EncodingEnum theEncoding) { myEncoding = theEncoding; diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceHistoryTable.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceHistoryTable.java index e4f8c208ac2..ea337e968e0 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceHistoryTable.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceHistoryTable.java @@ -14,6 +14,7 @@ import javax.persistence.Table; import ca.uhn.fhir.model.api.IResource; import ca.uhn.fhir.model.dstu.resource.Patient; import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.server.Constants; import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; @Entity @@ -32,7 +33,7 @@ public class ResourceHistoryTable extends BaseHasResource implements Serializabl @Override public IdDt getIdDt() { - return new IdDt(myPk.getId()); + return new IdDt(myPk.getResourceType() + '/' + myPk.getId() + '/' + Constants.PARAM_HISTORY + '/' + myPk.getVersion()); } public ResourceHistoryTablePk getPk() { @@ -56,8 +57,8 @@ public class ResourceHistoryTable extends BaseHasResource implements Serializabl } @Override - public IdDt getVersion() { - return new IdDt(myPk.getVersion()); + public long getVersion() { + return myPk.getVersion(); } public void setPk(ResourceHistoryTablePk thePk) { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceHistoryTablePk.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceHistoryTablePk.java index df23b496c0b..fb3bc69d9d6 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceHistoryTablePk.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceHistoryTablePk.java @@ -16,7 +16,7 @@ public class ResourceHistoryTablePk implements Serializable { @Column(name="RES_TYPE", length=30, nullable=false) private String myResourceType; - @Column(name="VERSION") + @Column(name="VERSION", nullable=false) private Long myVersion; public Long getId() { diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceTable.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceTable.java index bfbd06b169f..c5e2e55decd 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceTable.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/entity/ResourceTable.java @@ -19,6 +19,7 @@ import javax.persistence.Version; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.model.primitive.IdDt; +import ca.uhn.fhir.rest.server.Constants; @Entity @Table(name = "HFJ_RESOURCE", uniqueConstraints = {}) @@ -72,7 +73,7 @@ public class ResourceTable extends BaseHasResource implements Serializable { @Version() @Column(name = "RES_VER") - private Long myVersion; + private long myVersion; public void addTag(String theTerm, String theLabel, String theScheme) { for (ResourceTag next : getTags()) { @@ -84,7 +85,7 @@ public class ResourceTable extends BaseHasResource implements Serializable { } public IdDt getIdDt() { - return new IdDt(myId); + return new IdDt(myResourceType + '/' + myId + '/' + Constants.PARAM_HISTORY + '/' + myVersion); } public Long getId() { @@ -137,8 +138,8 @@ public class ResourceTable extends BaseHasResource implements Serializable { return myTags; } - public IdDt getVersion() { - return new IdDt(myVersion); + public long getVersion() { + return myVersion; } public boolean isHasLinks() { @@ -201,8 +202,8 @@ public class ResourceTable extends BaseHasResource implements Serializable { myResourceType = theResourceType; } - public void setVersion(IdDt theVersion) { - myVersion = theVersion.asLong(); + public void setVersion(long theVersion) { + myVersion = theVersion; } public ResourceHistoryTable toHistory(FhirContext theCtx) { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoTest.java index 6aa7ab8aa80..0b1713fac55 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoTest.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirResourceDaoTest.java @@ -454,6 +454,8 @@ public class FhirResourceDaoTest { assertNotNull(outcome.getId()); assertFalse(outcome.getId().isEmpty()); + assertEquals("1", outcome.getId().getUnqualifiedVersionId()); + Date now = new Date(); Patient retrieved = ourPatientDao.read(outcome.getId()); InstantDt published = (InstantDt) retrieved.getResourceMetadata().get(ResourceMetadataKeyEnum.PUBLISHED); @@ -465,12 +467,17 @@ public class FhirResourceDaoTest { retrieved.getIdentifierFirstRep().setValue("002"); MethodOutcome outcome2 = ourPatientDao.update(retrieved, outcome.getId()); - assertEquals(outcome.getId(), outcome2.getId()); + assertEquals(outcome.getId().getUnqualifiedId(), outcome2.getId().getUnqualifiedId()); + assertNotEquals(outcome.getId().getUnqualifiedVersionId(), outcome2.getId().getUnqualifiedVersionId()); assertNotEquals(outcome.getVersionId(), outcome2.getVersionId()); + assertEquals("2", outcome2.getId().getUnqualifiedVersionId()); + Date now2 = new Date(); Patient retrieved2 = ourPatientDao.read(outcome.getId()); + + assertEquals("2", retrieved2.getId().getUnqualifiedVersionId()); assertEquals("002", retrieved2.getIdentifierFirstRep().getValue().getValue()); InstantDt published2 = (InstantDt) retrieved2.getResourceMetadata().get(ResourceMetadataKeyEnum.PUBLISHED); InstantDt updated2 = (InstantDt) retrieved2.getResourceMetadata().get(ResourceMetadataKeyEnum.UPDATED); diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirSystemDaoTest.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirSystemDaoTest.java new file mode 100644 index 00000000000..2d0a2461e5e --- /dev/null +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/dao/FhirSystemDaoTest.java @@ -0,0 +1,82 @@ +package ca.uhn.fhir.jpa.dao; + +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import ca.uhn.fhir.model.api.IResource; +import ca.uhn.fhir.model.dstu.composite.IdentifierDt; +import ca.uhn.fhir.model.dstu.composite.ResourceReferenceDt; +import ca.uhn.fhir.model.dstu.resource.Device; +import ca.uhn.fhir.model.dstu.resource.DiagnosticReport; +import ca.uhn.fhir.model.dstu.resource.Location; +import ca.uhn.fhir.model.dstu.resource.Observation; +import ca.uhn.fhir.model.dstu.resource.Organization; +import ca.uhn.fhir.model.dstu.resource.Patient; +import ca.uhn.fhir.model.primitive.IdDt; + +public class FhirSystemDaoTest { + + private static ClassPathXmlApplicationContext ourCtx; + private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(FhirSystemDaoTest.class); + private static IFhirResourceDao ourObservationDao; + private static IFhirResourceDao ourPatientDao; + private static IFhirResourceDao ourDeviceDao; + private static IFhirResourceDao ourDiagnosticReportDao; + private static IFhirResourceDao ourOrganizationDao; + private static IFhirResourceDao ourLocationDao; + private static Date ourTestStarted; + private static IFhirSystemDao ourSystemDao; + + @Test + public void testPersistWithSimpleLink() { + Patient patient = new Patient(); + patient.setId(new IdDt("Patient/testPersistWithSimpleLinkP01")); + patient.addIdentifier("urn:system", "testPersistWithSimpleLinkP01"); + patient.addName().addFamily("Tester").addGiven("Joe"); + + Observation obs = new Observation(); + obs.getName().addCoding().setSystem("urn:system").setCode("testPersistWithSimpleLinkO01"); + obs.setSubject(new ResourceReferenceDt("Patient/testPersistWithSimpleLinkP01")); + + List response = ourSystemDao.transaction(Arrays.asList((IResource)patient, obs)); + + List obsResults = ourObservationDao.search(Observation.SP_NAME, new IdentifierDt("urn:system","testPersistWithSimpleLinkO01")); + assertEquals(1, obsResults.size()); + + List patResults = ourPatientDao.search(Patient.SP_IDENTIFIER, new IdentifierDt("urn:system","testPersistWithSimpleLinkP01")); + assertEquals(1, obsResults.size()); + + IdDt patientId = patResults.get(0).getId(); + ResourceReferenceDt subject = obs.getSubject(); + assertEquals(patientId.getUnqualifiedId(), subject.getResourceId().getUnqualifiedId()); + } + + + @AfterClass + public static void afterClass() { + ourCtx.close(); + } + + @SuppressWarnings("unchecked") + @BeforeClass + public static void beforeClass() { + ourTestStarted = new Date(); + ourCtx = new ClassPathXmlApplicationContext("fhir-jpabase-spring-test-config.xml"); + ourPatientDao = ourCtx.getBean("myPatientDao", IFhirResourceDao.class); + ourObservationDao = ourCtx.getBean("myObservationDao", IFhirResourceDao.class); + ourDiagnosticReportDao = ourCtx.getBean("myDiagnosticReportDao", IFhirResourceDao.class); + ourDeviceDao = ourCtx.getBean("myDeviceDao", IFhirResourceDao.class); + ourOrganizationDao = ourCtx.getBean("myOrganizationDao", IFhirResourceDao.class); + ourLocationDao = ourCtx.getBean("myLocationDao", IFhirResourceDao.class); + ourSystemDao = ourCtx.getBean("mySystemDao", IFhirSystemDao.class); + } + +} diff --git a/hapi-fhir-jpaserver-base/src/test/resources/fhir-jpabase-spring-test-config.xml b/hapi-fhir-jpaserver-base/src/test/resources/fhir-jpabase-spring-test-config.xml index 2a28e6321c8..ebea02f8889 100644 --- a/hapi-fhir-jpaserver-base/src/test/resources/fhir-jpabase-spring-test-config.xml +++ b/hapi-fhir-jpaserver-base/src/test/resources/fhir-jpabase-spring-test-config.xml @@ -13,6 +13,9 @@ + + + diff --git a/hapi-fhir-jpaserver-test/pom.xml b/hapi-fhir-jpaserver-test/pom.xml index e0eafd5ed2d..9ec0a45a9f5 100644 --- a/hapi-fhir-jpaserver-test/pom.xml +++ b/hapi-fhir-jpaserver-test/pom.xml @@ -12,7 +12,7 @@ hapi-fhir-jpaserver-test jar - HAPI FHIR JPA Server - Tester + HAPI FHIR JPA Server - Test Project