diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java index b2815845e10..5ef815576be 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirDao.java @@ -1216,6 +1216,9 @@ public abstract class BaseHapiFhirDao implements IDao, // Update version/lastUpdated so that interceptors see the correct version updateResourceMetadata(savedEntity, theResource); + // Populate the PID in the resource so it is available to hooks + addPidToResource(savedEntity, theResource); + // Notify interceptors if (!savedEntity.isUnchangedInCurrentOperation()) { TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { @@ -1234,6 +1237,12 @@ public abstract class BaseHapiFhirDao implements IDao, return savedEntity; } + protected void addPidToResource(ResourceTable theEntity, IBaseResource theResource) { + if (theResource instanceof IAnyResource) { + IDao.RESOURCE_PID.put((IAnyResource) theResource, theEntity.getId()); + } + } + protected void doCallHooks(RequestDetails theRequestDetails, Pointcut thePointcut, HookParams theParams) { JpaInterceptorBroadcaster.doCallHooks(myInterceptorBroadcaster, theRequestDetails, thePointcut, theParams); } diff --git a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java index ad8fecde496..ab880ed8f65 100644 --- a/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java +++ b/hapi-fhir-jpaserver-base/src/main/java/ca/uhn/fhir/jpa/dao/BaseHapiFhirResourceDao.java @@ -462,9 +462,7 @@ public abstract class BaseHapiFhirResourceDao extends B updateResourceMetadata(entity, theResource); // Populate the PID in the resource so it is available to hooks - if (theResource instanceof IAnyResource) { - IDao.RESOURCE_PID.put((IAnyResource)theResource, entity.getId()); - } + addPidToResource(entity, theResource); // Notify JPA interceptors if (!updatedEntity.isUnchangedInCurrentOperation()) { diff --git a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/HookInterceptorR4Test.java b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/HookInterceptorR4Test.java index ced2a1cc690..aa92a4d12de 100644 --- a/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/HookInterceptorR4Test.java +++ b/hapi-fhir-jpaserver-base/src/test/java/ca/uhn/fhir/jpa/provider/r4/HookInterceptorR4Test.java @@ -78,6 +78,31 @@ public class HookInterceptorR4Test extends BaseResourceProviderR4Test { assertTrue(pid.get() > 0); } + @Test + public void testSTORAGE_PRECOMMIT_RESOURCE_UPDATED_hasPid() { + AtomicLong oldPid = new AtomicLong(); + AtomicLong newPid = new AtomicLong(); + myInterceptorRegistry.registerAnonymousInterceptor(Pointcut.STORAGE_PRECOMMIT_RESOURCE_UPDATED, (thePointcut, t) -> { + + IAnyResource oldResource = (IAnyResource) t.get(IBaseResource.class, 0); + Long oldResourcePid = (Long) oldResource.getUserData("RESOURCE_PID"); + assertNotNull("Expecting RESOURCE_PID to be set on resource user data.", oldResourcePid); + oldPid.set(oldResourcePid); + + IAnyResource newResource = (IAnyResource) t.get(IBaseResource.class, 1); + Long newResourcePid = (Long) newResource.getUserData("RESOURCE_PID"); + assertNotNull("Expecting RESOURCE_PID to be set on resource user data.", newResourcePid); + newPid.set(newResourcePid); + }); + Patient patient = new Patient(); + IIdType id = ourClient.create().resource(patient).execute().getId(); + patient.setId(id); + patient.getNameFirstRep().setFamily("SOMECHANGE"); + ourClient.update().resource(patient).execute(); + assertTrue(oldPid.get() > 0); + assertTrue(newPid.get() > 0); + } + @Test public void testOP_PRESTORAGE_RESOURCE_UPDATED_ModifyResource() { Patient p = new Patient();