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
parent 33531f9010
commit fe7d38d0b5
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 {
@ -62,6 +65,19 @@ 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();