add RESOURCE_PID to resource UserData before STORAGE_PRECOMMIT_RESOURCE_UPDATED hooks are called

This commit is contained in:
Ken Stevens 2019-07-16 16:36:05 -04:00
parent fe7d38d0b5
commit 634718c503
3 changed files with 35 additions and 3 deletions

View File

@ -1216,6 +1216,9 @@ public abstract class BaseHapiFhirDao<T extends IBaseResource> implements IDao,
// Update version/lastUpdated so that interceptors see the correct version // Update version/lastUpdated so that interceptors see the correct version
updateResourceMetadata(savedEntity, theResource); updateResourceMetadata(savedEntity, theResource);
// Populate the PID in the resource so it is available to hooks
addPidToResource(savedEntity, theResource);
// Notify interceptors // Notify interceptors
if (!savedEntity.isUnchangedInCurrentOperation()) { if (!savedEntity.isUnchangedInCurrentOperation()) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() { TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@ -1234,6 +1237,12 @@ public abstract class BaseHapiFhirDao<T extends IBaseResource> implements IDao,
return savedEntity; 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) { protected void doCallHooks(RequestDetails theRequestDetails, Pointcut thePointcut, HookParams theParams) {
JpaInterceptorBroadcaster.doCallHooks(myInterceptorBroadcaster, theRequestDetails, thePointcut, theParams); JpaInterceptorBroadcaster.doCallHooks(myInterceptorBroadcaster, theRequestDetails, thePointcut, theParams);
} }

View File

@ -462,9 +462,7 @@ public abstract class BaseHapiFhirResourceDao<T extends IBaseResource> extends B
updateResourceMetadata(entity, theResource); updateResourceMetadata(entity, theResource);
// Populate the PID in the resource so it is available to hooks // Populate the PID in the resource so it is available to hooks
if (theResource instanceof IAnyResource) { addPidToResource(entity, theResource);
IDao.RESOURCE_PID.put((IAnyResource)theResource, entity.getId());
}
// Notify JPA interceptors // Notify JPA interceptors
if (!updatedEntity.isUnchangedInCurrentOperation()) { if (!updatedEntity.isUnchangedInCurrentOperation()) {

View File

@ -78,6 +78,31 @@ public class HookInterceptorR4Test extends BaseResourceProviderR4Test {
assertTrue(pid.get() > 0); 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 @Test
public void testOP_PRESTORAGE_RESOURCE_UPDATED_ModifyResource() { public void testOP_PRESTORAGE_RESOURCE_UPDATED_ModifyResource() {
Patient p = new Patient(); Patient p = new Patient();