add RESOURCE_PID to resource UserData before STORAGE_PRECOMMIT_RESOURCE_CREATED hooks are called

This commit is contained in:
Ken Stevens 2019-07-16 15:33:51 -04:00 committed by James Agnew
parent 76384abefb
commit e27685e811
2 changed files with 26 additions and 5 deletions

View File

@ -461,6 +461,11 @@ public abstract class BaseHapiFhirResourceDao<T extends IBaseResource> extends B
// the correct version
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());
}
// Notify JPA interceptors
if (!updatedEntity.isUnchangedInCurrentOperation()) {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {

View File

@ -3,6 +3,7 @@ package ca.uhn.fhir.jpa.provider.r4;
import ca.uhn.fhir.interceptor.api.Pointcut;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.util.TestUtil;
import org.hl7.fhir.instance.model.api.IAnyResource;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.instance.model.api.IIdType;
import org.hl7.fhir.r4.model.Patient;
@ -10,7 +11,9 @@ import org.junit.After;
import org.junit.AfterClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.Assert.*;
public class HookInterceptorR4Test extends BaseResourceProviderR4Test {
@ -26,7 +29,7 @@ public class HookInterceptorR4Test extends BaseResourceProviderR4Test {
@Test
public void testOP_PRESTORAGE_RESOURCE_CREATED_ModifyResource() {
myInterceptorRegistry.registerAnonymousInterceptor(Pointcut.STORAGE_PRESTORAGE_RESOURCE_CREATED, (thePointcut, t)->{
myInterceptorRegistry.registerAnonymousInterceptor(Pointcut.STORAGE_PRESTORAGE_RESOURCE_CREATED, (thePointcut, t) -> {
Patient contents = (Patient) t.get(IBaseResource.class, 0);
contents.getNameFirstRep().setFamily("NEWFAMILY");
});
@ -45,7 +48,7 @@ public class HookInterceptorR4Test extends BaseResourceProviderR4Test {
@Test
public void testOP_PRECOMMIT_RESOURCE_CREATED_ModifyResource() {
myInterceptorRegistry.registerAnonymousInterceptor(Pointcut.STORAGE_PRECOMMIT_RESOURCE_CREATED, (thePointcut, t)->{
myInterceptorRegistry.registerAnonymousInterceptor(Pointcut.STORAGE_PRECOMMIT_RESOURCE_CREATED, (thePointcut, t) -> {
Patient contents = (Patient) t.get(IBaseResource.class, 0);
contents.getNameFirstRep().setFamily("NEWFAMILY");
});
@ -62,13 +65,26 @@ public class HookInterceptorR4Test extends BaseResourceProviderR4Test {
}
@Test
public void testSTORAGE_PRECOMMIT_RESOURCE_CREATED_hasPid() {
AtomicLong pid = new AtomicLong();
myInterceptorRegistry.registerAnonymousInterceptor(Pointcut.STORAGE_PRECOMMIT_RESOURCE_CREATED, (thePointcut, t) -> {
IAnyResource resource = (IAnyResource) t.get(IBaseResource.class, 0);
Long resourcePid = (Long) resource.getUserData("RESOURCE_PID");
assertNotNull("Expecting RESOURCE_PID to be set on resource user data.", resourcePid);
pid.set(resourcePid);
});
ourClient.create().resource(new Patient()).execute();
assertTrue(pid.get() > 0);
}
@Test
public void testOP_PRESTORAGE_RESOURCE_UPDATED_ModifyResource() {
Patient p = new Patient();
p.setActive(true);
IIdType id = ourClient.create().resource(p).execute().getId();
myInterceptorRegistry.registerAnonymousInterceptor(Pointcut.STORAGE_PRESTORAGE_RESOURCE_UPDATED, (thePointcut, t)->{
myInterceptorRegistry.registerAnonymousInterceptor(Pointcut.STORAGE_PRESTORAGE_RESOURCE_UPDATED, (thePointcut, t) -> {
Patient contents = (Patient) t.get(IBaseResource.class, 1);
contents.getNameFirstRep().setFamily("NEWFAMILY");
});
@ -92,7 +108,7 @@ public class HookInterceptorR4Test extends BaseResourceProviderR4Test {
p.setActive(true);
IIdType id = ourClient.create().resource(p).execute().getId();
myInterceptorRegistry.registerAnonymousInterceptor(Pointcut.STORAGE_PRECOMMIT_RESOURCE_UPDATED, (thePointcut, t)->{
myInterceptorRegistry.registerAnonymousInterceptor(Pointcut.STORAGE_PRECOMMIT_RESOURCE_UPDATED, (thePointcut, t) -> {
Patient contents = (Patient) t.get(IBaseResource.class, 1);
contents.getNameFirstRep().setFamily("NEWFAMILY");
});